Commit cd593edd authored by Łukasz Magiera's avatar Łukasz Magiera

Fixed mount lookup depending on order

parent 18063741
......@@ -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}
}
......
......@@ -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)
}
}
......@@ -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}
}
......
......@@ -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)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment