diff --git a/.circleci/config.yml b/.circleci/config.yml index 7c51cb6..50a977c 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,6 +1,6 @@ version: 2.1 orbs: - ci-go: ipfs/ci-go@0.1 + ci-go: ipfs/ci-go@0.2.0 workflows: version: 2 diff --git a/basic_ds.go b/basic_ds.go index 46567cc..4c85187 100644 --- a/basic_ds.go +++ b/basic_ds.go @@ -171,6 +171,12 @@ func (d *LogDatastore) Put(key Key, value []byte) (err error) { return d.child.Put(key, value) } +// Sync implements Datastore.Sync +func (d *LogDatastore) Sync(prefix Key) error { + log.Printf("%s: Sync %s\n", d.Name, prefix) + return d.child.Sync(prefix) +} + // Get implements Datastore.Get func (d *LogDatastore) Get(key Key) (value []byte, err error) { log.Printf("%s: Get %s\n", d.Name, key) diff --git a/basic_ds_test.go b/basic_ds_test.go index 6246a4d..4353bb1 100644 --- a/basic_ds_test.go +++ b/basic_ds_test.go @@ -1,6 +1,8 @@ package datastore_test import ( + "io/ioutil" + "log" "testing" dstore "github.com/ipfs/go-datastore" @@ -17,3 +19,10 @@ func TestNullDatastore(t *testing.T) { // The only test that passes. Nothing should be found. dstest.SubtestNotFounds(t, ds) } + +func TestLogDatastore(t *testing.T) { + defer log.SetOutput(log.Writer()) + log.SetOutput(ioutil.Discard) + ds := dstore.NewLogDatastore(dstore.NewMapDatastore(), "") + dstest.SubtestAll(t, ds) +} diff --git a/key.go b/key.go index 161fedf..1c740a2 100644 --- a/key.go +++ b/key.go @@ -125,7 +125,7 @@ func (k Key) List() []string { // NewKey("/Actor:JohnCleese/MontyPython/Comedy") func (k Key) Reverse() Key { l := k.List() - r := make([]string, len(l), len(l)) + r := make([]string, len(l)) for i, e := range l { r[len(l)-i-1] = e } diff --git a/key_test.go b/key_test.go index aeeb284..cbf44ae 100644 --- a/key_test.go +++ b/key_test.go @@ -29,7 +29,7 @@ func (ks *KeySuite) SubtestKey(s string, c *C) { kname := lnparts[len(lnparts)-1] kchild := path.Clean(fixed + "/cchildd") - kparent := "/" + strings.Join(append(namespaces[:len(namespaces)-1]), "/") + kparent := "/" + strings.Join(namespaces[:len(namespaces)-1], "/") kpath := path.Clean(kparent + "/" + ktype) kinstance := fixed + ":" + "inst" diff --git a/keytransform/keytransform_test.go b/keytransform/keytransform_test.go index 77a8d15..c7c7768 100644 --- a/keytransform/keytransform_test.go +++ b/keytransform/keytransform_test.go @@ -20,9 +20,6 @@ type DSSuite struct{} var _ = Suite(&DSSuite{}) -func testDatastore() { -} - var pair = &kt.Pair{ Convert: func(k ds.Key) ds.Key { return ds.NewKey("/abc").Child(k) diff --git a/mount/mount.go b/mount/mount.go index d2556b2..c3c8474 100644 --- a/mount/mount.go +++ b/mount/mount.go @@ -26,9 +26,7 @@ type Mount struct { 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 - } + copy(m, mounts) sort.Slice(m, func(i, j int) bool { return m[i].Prefix.String() > m[j].Prefix.String() }) return &Datastore{mounts: m} } diff --git a/mount/mount_test.go b/mount/mount_test.go index 497ba2d..6db61d4 100644 --- a/mount/mount_test.go +++ b/mount/mount_test.go @@ -252,11 +252,21 @@ func TestQueryAcrossMounts(t *testing.T) { {Prefix: datastore.NewKey("/"), Datastore: mapds0}, }) - m.Put(datastore.NewKey("/foo/lorem"), []byte("123")) - m.Put(datastore.NewKey("/bar/ipsum"), []byte("234")) - m.Put(datastore.NewKey("/bar/dolor"), []byte("345")) - m.Put(datastore.NewKey("/baz/sit"), []byte("456")) - m.Put(datastore.NewKey("/banana"), []byte("567")) + if err := m.Put(datastore.NewKey("/foo/lorem"), []byte("123")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/bar/ipsum"), []byte("234")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/bar/dolor"), []byte("345")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/baz/sit"), []byte("456")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/banana"), []byte("567")); err != nil { + t.Fatal(err) + } res, err := m.Query(query.Query{Prefix: "/ba"}) if err != nil { @@ -313,11 +323,21 @@ func TestQueryAcrossMountsWithSort(t *testing.T) { {Prefix: datastore.NewKey("/boo"), Datastore: mapds0}, }) - m.Put(datastore.NewKey("/zoo/0"), []byte("123")) - m.Put(datastore.NewKey("/zoo/1"), []byte("234")) - m.Put(datastore.NewKey("/boo/9"), []byte("345")) - m.Put(datastore.NewKey("/boo/3"), []byte("456")) - m.Put(datastore.NewKey("/boo/5/hello"), []byte("789")) + if err := m.Put(datastore.NewKey("/zoo/0"), []byte("123")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/zoo/1"), []byte("234")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/boo/9"), []byte("345")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/boo/3"), []byte("456")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/boo/5/hello"), []byte("789")); err != nil { + t.Fatal(err) + } res, err := m.Query(query.Query{Orders: []query.Order{query.OrderByKey{}}}) if err != nil { @@ -362,13 +382,27 @@ func TestQueryLimitAcrossMountsWithSort(t *testing.T) { {Prefix: datastore.NewKey("/noop"), Datastore: mapds3}, }) - m.Put(datastore.NewKey("/rok/0"), []byte("ghi")) - m.Put(datastore.NewKey("/zoo/0"), []byte("123")) - m.Put(datastore.NewKey("/rok/1"), []byte("def")) - m.Put(datastore.NewKey("/zoo/1"), []byte("167")) - m.Put(datastore.NewKey("/zoo/2"), []byte("345")) - m.Put(datastore.NewKey("/rok/3"), []byte("abc")) - m.Put(datastore.NewKey("/zoo/3"), []byte("456")) + if err := m.Put(datastore.NewKey("/rok/0"), []byte("ghi")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/zoo/0"), []byte("123")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/rok/1"), []byte("def")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/zoo/1"), []byte("167")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/zoo/2"), []byte("345")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/rok/3"), []byte("abc")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/zoo/3"), []byte("456")); err != nil { + t.Fatal(err) + } q := query.Query{Limit: 2, Orders: []query.Order{query.OrderByKeyDescending{}}} res, err := m.Query(q) @@ -412,13 +446,27 @@ func TestQueryLimitAndOffsetAcrossMountsWithSort(t *testing.T) { {Prefix: datastore.NewKey("/noop"), Datastore: mapds3}, }) - m.Put(datastore.NewKey("/rok/0"), []byte("ghi")) - m.Put(datastore.NewKey("/zoo/0"), []byte("123")) - m.Put(datastore.NewKey("/rok/1"), []byte("def")) - m.Put(datastore.NewKey("/zoo/1"), []byte("167")) - m.Put(datastore.NewKey("/zoo/2"), []byte("345")) - m.Put(datastore.NewKey("/rok/3"), []byte("abc")) - m.Put(datastore.NewKey("/zoo/3"), []byte("456")) + if err := m.Put(datastore.NewKey("/rok/0"), []byte("ghi")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/zoo/0"), []byte("123")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/rok/1"), []byte("def")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/zoo/1"), []byte("167")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/zoo/2"), []byte("345")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/rok/3"), []byte("abc")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/zoo/3"), []byte("456")); err != nil { + t.Fatal(err) + } q := query.Query{Limit: 3, Offset: 2, Orders: []query.Order{query.OrderByKey{}}} res, err := m.Query(q) @@ -463,13 +511,27 @@ func TestQueryFilterAcrossMountsWithSort(t *testing.T) { {Prefix: datastore.NewKey("/noop"), Datastore: mapds3}, }) - m.Put(datastore.NewKey("/rok/0"), []byte("ghi")) - m.Put(datastore.NewKey("/zoo/0"), []byte("123")) - m.Put(datastore.NewKey("/rok/1"), []byte("def")) - m.Put(datastore.NewKey("/zoo/1"), []byte("167")) - m.Put(datastore.NewKey("/zoo/2"), []byte("345")) - m.Put(datastore.NewKey("/rok/3"), []byte("abc")) - m.Put(datastore.NewKey("/zoo/3"), []byte("456")) + if err := m.Put(datastore.NewKey("/rok/0"), []byte("ghi")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/zoo/0"), []byte("123")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/rok/1"), []byte("def")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/zoo/1"), []byte("167")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/zoo/2"), []byte("345")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/rok/3"), []byte("abc")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/zoo/3"), []byte("456")); err != nil { + t.Fatal(err) + } f := &query.FilterKeyCompare{Op: query.Equal, Key: "/rok/3"} q := query.Query{Filters: []query.Filter{f}} @@ -542,8 +604,12 @@ func TestQueryLimitWithNotEnoughData(t *testing.T) { {Prefix: datastore.NewKey("/zoo"), Datastore: mapds2}, }) - m.Put(datastore.NewKey("/zoo/0"), []byte("123")) - m.Put(datastore.NewKey("/rok/1"), []byte("167")) + if err := m.Put(datastore.NewKey("/zoo/0"), []byte("123")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/rok/1"), []byte("167")); err != nil { + t.Fatal(err) + } q := query.Query{Limit: 4} res, err := m.Query(q) @@ -579,8 +645,12 @@ func TestQueryOffsetWithNotEnoughData(t *testing.T) { {Prefix: datastore.NewKey("/zoo"), Datastore: mapds2}, }) - m.Put(datastore.NewKey("/zoo/0"), []byte("123")) - m.Put(datastore.NewKey("/rok/1"), []byte("167")) + if err := m.Put(datastore.NewKey("/zoo/0"), []byte("123")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/rok/1"), []byte("167")); err != nil { + t.Fatal(err) + } q := query.Query{Offset: 4} res, err := m.Query(q) @@ -614,8 +684,12 @@ func TestLookupPrio(t *testing.T) { {Prefix: datastore.NewKey("/foo"), Datastore: mapds1}, }) - m.Put(datastore.NewKey("/foo/bar"), []byte("123")) - m.Put(datastore.NewKey("/baz"), []byte("234")) + if err := m.Put(datastore.NewKey("/foo/bar"), []byte("123")); err != nil { + t.Fatal(err) + } + if err := m.Put(datastore.NewKey("/baz"), []byte("234")); err != nil { + t.Fatal(err) + } found, err := mapds0.Has(datastore.NewKey("/baz")) if err != nil { @@ -723,7 +797,9 @@ func TestErrQueryClose(t *testing.T) { {Prefix: datastore.NewKey("/foo"), Datastore: eqds}, }) - m.Put(datastore.NewKey("/baz"), []byte("123")) + if err := m.Put(datastore.NewKey("/baz"), []byte("123")); err != nil { + t.Fatal(err) + } _, err := m.Query(query.Query{}) if err == nil { diff --git a/namespace/example_test.go b/namespace/example_test.go index 2acfd1c..2fb234a 100644 --- a/namespace/example_test.go +++ b/namespace/example_test.go @@ -14,7 +14,9 @@ func Example() { k := ds.NewKey("/beep") v := "boop" - ns.Put(k, []byte(v)) + if err := ns.Put(k, []byte(v)); err != nil { + panic(err) + } fmt.Printf("ns.Put %s %s\n", k, v) v2, _ := ns.Get(k) diff --git a/query/query.go b/query/query.go index b6bfa45..d14fb42 100644 --- a/query/query.go +++ b/query/query.go @@ -289,7 +289,7 @@ func ResultsWithProcess(q Query, proc func(goprocess.Process, chan<- Result)) Re proc(worker, b.Output) }) - go b.Process.CloseAfterChildren() + go b.Process.CloseAfterChildren() //nolint return b.Results() } @@ -427,10 +427,9 @@ func (r *resultsIter) useLegacyResults() { return } } - return }) - go b.Process.CloseAfterChildren() + go b.Process.CloseAfterChildren() //nolint r.legacyResults = b.Results().(*results) } diff --git a/query/query_test.go b/query/query_test.go index 7f08cb0..d82d817 100644 --- a/query/query_test.go +++ b/query/query_test.go @@ -15,11 +15,6 @@ var sampleKeys = []string{ "/ab", } -type testCase struct { - keys []string - expect []string -} - func testResults(t *testing.T, res Results, expect []string) { actualE, err := res.Rest() if err != nil {