Skip to content

Commit d4417ca

Browse files
authored
Merge pull request #134 from MichaelMure/querysize
add a Size field to Query's Result
2 parents 146d375 + 68a7796 commit d4417ca

File tree

8 files changed

+23
-9
lines changed

8 files changed

+23
-9
lines changed

autobatch/autobatch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
dsq "github.com/ipfs/go-datastore/query"
99
)
1010

11-
// Datastore implements a go-datatsore.
11+
// Datastore implements a go-datastore.
1212
type Datastore struct {
1313
child ds.Batching
1414

basic_ds.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (d *MapDatastore) Delete(key Key) (err error) {
6161
func (d *MapDatastore) Query(q dsq.Query) (dsq.Results, error) {
6262
re := make([]dsq.Entry, 0, len(d.values))
6363
for k, v := range d.values {
64-
e := dsq.Entry{Key: k.String()}
64+
e := dsq.Entry{Key: k.String(), Size: len(v)}
6565
if !q.KeysOnly {
6666
e.Value = v
6767
}

mount/mount.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ func (d *Datastore) Query(master query.Query) (query.Results, error) {
227227
Orders: master.Orders,
228228
KeysOnly: master.KeysOnly,
229229
ReturnExpirations: master.ReturnExpirations,
230+
ReturnsSizes: master.ReturnsSizes,
230231
}
231232

232233
prefix := ds.NewKey(childQuery.Prefix)

namespace/namespace_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ func (ks *DSSuite) TestQuery(c *C) {
101101
c.Check(err, Equals, nil)
102102

103103
expect := []dsq.Entry{
104-
{Key: "/bar", Value: []byte("/foo/bar")},
105-
{Key: "/bar/baz", Value: []byte("/foo/bar/baz")},
106-
{Key: "/baz/abc", Value: []byte("/foo/baz/abc")},
104+
{Key: "/bar", Size: len([]byte("/foo/bar")), Value: []byte("/foo/bar")},
105+
{Key: "/bar/baz", Size: len([]byte("/foo/bar/baz")), Value: []byte("/foo/bar/baz")},
106+
{Key: "/baz/abc", Size: len([]byte("/foo/baz/abc")), Value: []byte("/foo/baz/abc")},
107107
}
108108

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

124124
expect = []dsq.Entry{
125-
{Key: "/bar", Value: []byte("/foo/bar")},
126-
{Key: "/bar/baz", Value: []byte("/foo/bar/baz")},
125+
{Key: "/bar", Size: len([]byte("/foo/bar")), Value: []byte("/foo/bar")},
126+
{Key: "/bar/baz", Size: len([]byte("/foo/bar/baz")), Value: []byte("/foo/bar/baz")},
127127
}
128128

129129
results, err = qres.Rest()

query/query.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ type Query struct {
6666
Offset int // skip given number of results
6767
KeysOnly bool // return only keys.
6868
ReturnExpirations bool // return expirations (see TTLDatastore)
69+
ReturnsSizes bool // always return sizes. If not set, datastore impl can return
70+
// // it anyway if it doesn't involve a performance cost. If KeysOnly
71+
// // is not set, Size should always be set.
6972
}
7073

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

122127
// Result is a special entry that includes an error, so that the client

query/query_impl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func NaiveQueryApply(q Query, qr Results) Results {
136136
func ResultEntriesFrom(keys []string, vals [][]byte) []Entry {
137137
re := make([]Entry, len(keys))
138138
for i, k := range keys {
139-
re[i] = Entry{Key: k, Value: vals[i]}
139+
re[i] = Entry{Key: k, Size: len(vals[i]), Value: vals[i]}
140140
}
141141
return re
142142
}

test/basic_tests.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,10 @@ func SubtestFilter(t *testing.T, ds dstore.Datastore) {
301301
test(new(testFilter))
302302
}
303303

304+
func SubtestReturnSizes(t *testing.T, ds dstore.Datastore) {
305+
subtestQuery(t, ds, dsq.Query{ReturnsSizes: true}, 100)
306+
}
307+
304308
func randValue() []byte {
305309
value := make([]byte, 64)
306310
rand.Read(value)
@@ -315,6 +319,7 @@ func subtestQuery(t *testing.T, ds dstore.Datastore, q dsq.Query, count int) {
315319
value := randValue()
316320
input = append(input, dsq.Entry{
317321
Key: key,
322+
Size: len(value),
318323
Value: value,
319324
})
320325
}
@@ -375,7 +380,9 @@ func subtestQuery(t *testing.T, ds dstore.Datastore, q dsq.Query, count int) {
375380
if !q.KeysOnly && !bytes.Equal(actual[i].Value, expected[i].Value) {
376381
t.Errorf("value mismatch for result %d (key=%q)", i, expected[i].Key)
377382
}
378-
383+
if q.ReturnsSizes && actual[i].Size <= 0 {
384+
t.Errorf("for result %d, expected size > 0 with ReturnsSizes", i)
385+
}
379386
}
380387

381388
t.Log("deleting all keys")

test/suite.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var BasicSubtests = []func(t *testing.T, ds dstore.Datastore){
1818
SubtestLimit,
1919
SubtestFilter,
2020
SubtestManyKeysAndQuery,
21+
SubtestReturnSizes,
2122
}
2223

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

0 commit comments

Comments
 (0)