Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-datastore
Commits
cd593edd
Commit
cd593edd
authored
Jun 27, 2017
by
Łukasz Magiera
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed mount lookup depending on order
parent
18063741
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
107 additions
and
1 deletion
+107
-1
mount/mount.go
mount/mount.go
+16
-0
mount/mount_test.go
mount/mount_test.go
+37
-0
syncmount/mount.go
syncmount/mount.go
+16
-0
syncmount/mount_test.go
syncmount/mount_test.go
+38
-1
No files found.
mount/mount.go
View file @
cd593edd
...
...
@@ -5,6 +5,7 @@ package mount
import
(
"errors"
"io"
"sort"
"strings"
"github.com/ipfs/go-datastore"
...
...
@@ -21,12 +22,27 @@ type Mount struct {
Datastore
datastore
.
Datastore
}
type
MountSlice
[]
Mount
func
(
m
MountSlice
)
Len
()
int
{
return
len
(
m
)
}
func
(
m
MountSlice
)
Less
(
i
,
j
int
)
bool
{
return
m
[
i
]
.
Prefix
.
String
()
>
m
[
j
]
.
Prefix
.
String
()
}
func
(
m
MountSlice
)
Swap
(
i
,
j
int
)
{
m
[
i
],
m
[
j
]
=
m
[
j
],
m
[
i
]
}
func
New
(
mounts
[]
Mount
)
*
Datastore
{
// make a copy so we're sure it doesn't mutate
m
:=
make
([]
Mount
,
len
(
mounts
))
for
i
,
v
:=
range
mounts
{
m
[
i
]
=
v
}
sort
.
Sort
(
MountSlice
(
m
))
return
&
Datastore
{
mounts
:
m
}
}
...
...
mount/mount_test.go
View file @
cd593edd
...
...
@@ -239,3 +239,40 @@ func TestQuerySimple(t *testing.T) {
t
.
Errorf
(
"did not see wanted key %q in %+v"
,
myKey
,
entries
)
}
}
func
TestLookupPrio
(
t
*
testing
.
T
)
{
mapds0
:=
datastore
.
NewMapDatastore
()
mapds1
:=
datastore
.
NewMapDatastore
()
m
:=
mount
.
New
([]
mount
.
Mount
{
{
Prefix
:
datastore
.
NewKey
(
"/"
),
Datastore
:
mapds0
},
{
Prefix
:
datastore
.
NewKey
(
"/foo"
),
Datastore
:
mapds1
},
})
m
.
Put
(
datastore
.
NewKey
(
"/foo/bar"
),
"123"
)
m
.
Put
(
datastore
.
NewKey
(
"/baz"
),
"234"
)
found
,
err
:=
mapds0
.
Has
(
datastore
.
NewKey
(
"/baz"
))
if
err
!=
nil
{
t
.
Fatalf
(
"Has error: %v"
,
err
)
}
if
g
,
e
:=
found
,
true
;
g
!=
e
{
t
.
Fatalf
(
"wrong value: %v != %v"
,
g
,
e
)
}
found
,
err
=
mapds0
.
Has
(
datastore
.
NewKey
(
"/foo/bar"
))
if
err
!=
nil
{
t
.
Fatalf
(
"Has error: %v"
,
err
)
}
if
g
,
e
:=
found
,
false
;
g
!=
e
{
t
.
Fatalf
(
"wrong value: %v != %v"
,
g
,
e
)
}
found
,
err
=
mapds1
.
Has
(
datastore
.
NewKey
(
"/bar"
))
if
err
!=
nil
{
t
.
Fatalf
(
"Has error: %v"
,
err
)
}
if
g
,
e
:=
found
,
true
;
g
!=
e
{
t
.
Fatalf
(
"wrong value: %v != %v"
,
g
,
e
)
}
}
syncmount/mount.go
View file @
cd593edd
...
...
@@ -5,6 +5,7 @@ package syncmount
import
(
"errors"
"io"
"sort"
"strings"
"sync"
...
...
@@ -22,12 +23,27 @@ type Mount struct {
Datastore
ds
.
Datastore
}
type
MountSlice
[]
Mount
func
(
m
MountSlice
)
Len
()
int
{
return
len
(
m
)
}
func
(
m
MountSlice
)
Less
(
i
,
j
int
)
bool
{
return
m
[
i
]
.
Prefix
.
String
()
>
m
[
j
]
.
Prefix
.
String
()
}
func
(
m
MountSlice
)
Swap
(
i
,
j
int
)
{
m
[
i
],
m
[
j
]
=
m
[
j
],
m
[
i
]
}
func
New
(
mounts
[]
Mount
)
*
Datastore
{
// make a copy so we're sure it doesn't mutate
m
:=
make
([]
Mount
,
len
(
mounts
))
for
i
,
v
:=
range
mounts
{
m
[
i
]
=
v
}
sort
.
Sort
(
MountSlice
(
m
))
return
&
Datastore
{
mounts
:
m
}
}
...
...
syncmount/mount_test.go
View file @
cd593edd
...
...
@@ -4,8 +4,8 @@ import (
"testing"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/mount"
"github.com/ipfs/go-datastore/query"
mount
"github.com/ipfs/go-datastore/syncmount"
)
func
TestPutBadNothing
(
t
*
testing
.
T
)
{
...
...
@@ -239,3 +239,40 @@ func TestQuerySimple(t *testing.T) {
t
.
Errorf
(
"did not see wanted key %q in %+v"
,
myKey
,
entries
)
}
}
func
TestLookupPrio
(
t
*
testing
.
T
)
{
mapds0
:=
datastore
.
NewMapDatastore
()
mapds1
:=
datastore
.
NewMapDatastore
()
m
:=
mount
.
New
([]
mount
.
Mount
{
{
Prefix
:
datastore
.
NewKey
(
"/"
),
Datastore
:
mapds0
},
{
Prefix
:
datastore
.
NewKey
(
"/foo"
),
Datastore
:
mapds1
},
})
m
.
Put
(
datastore
.
NewKey
(
"/foo/bar"
),
"123"
)
m
.
Put
(
datastore
.
NewKey
(
"/baz"
),
"234"
)
found
,
err
:=
mapds0
.
Has
(
datastore
.
NewKey
(
"/baz"
))
if
err
!=
nil
{
t
.
Fatalf
(
"Has error: %v"
,
err
)
}
if
g
,
e
:=
found
,
true
;
g
!=
e
{
t
.
Fatalf
(
"wrong value: %v != %v"
,
g
,
e
)
}
found
,
err
=
mapds0
.
Has
(
datastore
.
NewKey
(
"/foo/bar"
))
if
err
!=
nil
{
t
.
Fatalf
(
"Has error: %v"
,
err
)
}
if
g
,
e
:=
found
,
false
;
g
!=
e
{
t
.
Fatalf
(
"wrong value: %v != %v"
,
g
,
e
)
}
found
,
err
=
mapds1
.
Has
(
datastore
.
NewKey
(
"/bar"
))
if
err
!=
nil
{
t
.
Fatalf
(
"Has error: %v"
,
err
)
}
if
g
,
e
:=
found
,
true
;
g
!=
e
{
t
.
Fatalf
(
"wrong value: %v != %v"
,
g
,
e
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment