Skip to content

Commit f860a4a

Browse files
committed
add a Size field to Query's Result
Rational is that some datastore can easilty get the size of a value when listing only keys, so we might as well have a way to pass that. One way this could be useful is when implementing a cache for Has/GetSize.
1 parent 507733c commit f860a4a

File tree

5 files changed

+9
-7
lines changed

5 files changed

+9
-7
lines changed

basic_ds.go

+1-1
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
}

namespace/namespace_test.go

+5-5
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

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ type Query struct {
7070
// Entry is a query result entry.
7171
type Entry struct {
7272
Key string // cant be ds.Key because circular imports ...!!!
73+
Size int // Might be zero if the datastore doesn't support listing the size with KeysOnly
7374
Value []byte // Will be nil if KeysOnly has been passed.
7475
Expiration time.Time // Entry expiration timestamp if requested and supported (see TTLDatastore).
7576
}

query/query_impl.go

+1-1
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

+1
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ func subtestQuery(t *testing.T, ds dstore.Datastore, q dsq.Query, check func(t *
267267
value := randValue()
268268
input = append(input, dsq.Entry{
269269
Key: key,
270+
Size: len(value),
270271
Value: value,
271272
})
272273
}

0 commit comments

Comments
 (0)