Skip to content

Commit

Permalink
Merge pull request #134 from MichaelMure/querysize
Browse files Browse the repository at this point in the history
add a Size field to Query's Result
  • Loading branch information
Stebalien authored Dec 2, 2019
2 parents 146d375 + 68a7796 commit d4417ca
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion autobatch/autobatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
dsq "github.com/ipfs/go-datastore/query"
)

// Datastore implements a go-datatsore.
// Datastore implements a go-datastore.
type Datastore struct {
child ds.Batching

Expand Down
2 changes: 1 addition & 1 deletion basic_ds.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (d *MapDatastore) Delete(key Key) (err error) {
func (d *MapDatastore) Query(q dsq.Query) (dsq.Results, error) {
re := make([]dsq.Entry, 0, len(d.values))
for k, v := range d.values {
e := dsq.Entry{Key: k.String()}
e := dsq.Entry{Key: k.String(), Size: len(v)}
if !q.KeysOnly {
e.Value = v
}
Expand Down
1 change: 1 addition & 0 deletions mount/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ func (d *Datastore) Query(master query.Query) (query.Results, error) {
Orders: master.Orders,
KeysOnly: master.KeysOnly,
ReturnExpirations: master.ReturnExpirations,
ReturnsSizes: master.ReturnsSizes,
}

prefix := ds.NewKey(childQuery.Prefix)
Expand Down
10 changes: 5 additions & 5 deletions namespace/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ func (ks *DSSuite) TestQuery(c *C) {
c.Check(err, Equals, nil)

expect := []dsq.Entry{
{Key: "/bar", Value: []byte("/foo/bar")},
{Key: "/bar/baz", Value: []byte("/foo/bar/baz")},
{Key: "/baz/abc", Value: []byte("/foo/baz/abc")},
{Key: "/bar", Size: len([]byte("/foo/bar")), Value: []byte("/foo/bar")},
{Key: "/bar/baz", Size: len([]byte("/foo/bar/baz")), Value: []byte("/foo/bar/baz")},
{Key: "/baz/abc", Size: len([]byte("/foo/baz/abc")), Value: []byte("/foo/baz/abc")},
}

results, err := qres.Rest()
Expand All @@ -122,8 +122,8 @@ func (ks *DSSuite) TestQuery(c *C) {
c.Check(err, Equals, nil)

expect = []dsq.Entry{
{Key: "/bar", Value: []byte("/foo/bar")},
{Key: "/bar/baz", Value: []byte("/foo/bar/baz")},
{Key: "/bar", Size: len([]byte("/foo/bar")), Value: []byte("/foo/bar")},
{Key: "/bar/baz", Size: len([]byte("/foo/bar/baz")), Value: []byte("/foo/bar/baz")},
}

results, err = qres.Rest()
Expand Down
5 changes: 5 additions & 0 deletions query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ type Query struct {
Offset int // skip given number of results
KeysOnly bool // return only keys.
ReturnExpirations bool // return expirations (see TTLDatastore)
ReturnsSizes bool // always return sizes. If not set, datastore impl can return
// // it anyway if it doesn't involve a performance cost. If KeysOnly
// // is not set, Size should always be set.
}

// String returns a string represenation of the Query for debugging/validation
Expand Down Expand Up @@ -117,6 +120,8 @@ type Entry struct {
Key string // cant be ds.Key because circular imports ...!!!
Value []byte // Will be nil if KeysOnly has been passed.
Expiration time.Time // Entry expiration timestamp if requested and supported (see TTLDatastore).
Size int // Might be -1 if the datastore doesn't support listing the size with KeysOnly
// // or if ReturnsSizes is not set
}

// Result is a special entry that includes an error, so that the client
Expand Down
2 changes: 1 addition & 1 deletion query/query_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func NaiveQueryApply(q Query, qr Results) Results {
func ResultEntriesFrom(keys []string, vals [][]byte) []Entry {
re := make([]Entry, len(keys))
for i, k := range keys {
re[i] = Entry{Key: k, Value: vals[i]}
re[i] = Entry{Key: k, Size: len(vals[i]), Value: vals[i]}
}
return re
}
9 changes: 8 additions & 1 deletion test/basic_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ func SubtestFilter(t *testing.T, ds dstore.Datastore) {
test(new(testFilter))
}

func SubtestReturnSizes(t *testing.T, ds dstore.Datastore) {
subtestQuery(t, ds, dsq.Query{ReturnsSizes: true}, 100)
}

func randValue() []byte {
value := make([]byte, 64)
rand.Read(value)
Expand All @@ -315,6 +319,7 @@ func subtestQuery(t *testing.T, ds dstore.Datastore, q dsq.Query, count int) {
value := randValue()
input = append(input, dsq.Entry{
Key: key,
Size: len(value),
Value: value,
})
}
Expand Down Expand Up @@ -375,7 +380,9 @@ func subtestQuery(t *testing.T, ds dstore.Datastore, q dsq.Query, count int) {
if !q.KeysOnly && !bytes.Equal(actual[i].Value, expected[i].Value) {
t.Errorf("value mismatch for result %d (key=%q)", i, expected[i].Key)
}

if q.ReturnsSizes && actual[i].Size <= 0 {
t.Errorf("for result %d, expected size > 0 with ReturnsSizes", i)
}
}

t.Log("deleting all keys")
Expand Down
1 change: 1 addition & 0 deletions test/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var BasicSubtests = []func(t *testing.T, ds dstore.Datastore){
SubtestLimit,
SubtestFilter,
SubtestManyKeysAndQuery,
SubtestReturnSizes,
}

// BatchSubtests is a list of all basic batching datastore tests.
Expand Down

0 comments on commit d4417ca

Please sign in to comment.