@@ -177,27 +177,27 @@ func SubtestManyKeysAndQuery(t *testing.T, ds dstore.Datastore) {
177
177
}
178
178
179
179
func SubtestBasicSync (t * testing.T , ds dstore.Datastore ) {
180
- if err := ds .Sync (dstore .NewKey ("foo " )); err != nil {
180
+ if err := ds .Sync (dstore .NewKey ("prefix " )); err != nil {
181
181
t .Fatal (err )
182
182
}
183
183
184
- if err := ds .Put (dstore .NewKey ("/foo " ), []byte ("foo" )); err != nil {
184
+ if err := ds .Put (dstore .NewKey ("/prefix " ), []byte ("foo" )); err != nil {
185
185
t .Fatal (err )
186
186
}
187
187
188
- if err := ds .Sync (dstore .NewKey ("/foo " )); err != nil {
188
+ if err := ds .Sync (dstore .NewKey ("/prefix " )); err != nil {
189
189
t .Fatal (err )
190
190
}
191
191
192
- if err := ds .Put (dstore .NewKey ("/foo/bar " ), []byte ("bar" )); err != nil {
192
+ if err := ds .Put (dstore .NewKey ("/prefix/sub " ), []byte ("bar" )); err != nil {
193
193
t .Fatal (err )
194
194
}
195
195
196
- if err := ds .Sync (dstore .NewKey ("/foo " )); err != nil {
196
+ if err := ds .Sync (dstore .NewKey ("/prefix " )); err != nil {
197
197
t .Fatal (err )
198
198
}
199
199
200
- if err := ds .Sync (dstore .NewKey ("/foo/bar " )); err != nil {
200
+ if err := ds .Sync (dstore .NewKey ("/prefix/sub " )); err != nil {
201
201
t .Fatal (err )
202
202
}
203
203
@@ -237,6 +237,11 @@ func SubtestCombinations(t *testing.T, ds dstore.Datastore) {
237
237
Key : "/2" ,
238
238
}},
239
239
}
240
+ prefixes := []string {
241
+ "" ,
242
+ "/prefix" ,
243
+ "/0" , // keys exist under this prefix but they shouldn't match.
244
+ }
240
245
orders := [][]dsq.Order {
241
246
{dsq.OrderByKey {}},
242
247
{dsq.OrderByKeyDescending {}},
@@ -255,8 +260,9 @@ func SubtestCombinations(t *testing.T, ds dstore.Datastore) {
255
260
Limit : limits [perm [1 ]],
256
261
Filters : filters [perm [2 ]],
257
262
Orders : orders [perm [3 ]],
263
+ Prefix : prefixes [perm [4 ]],
258
264
}
259
- length := lengths [perm [4 ]]
265
+ length := lengths [perm [5 ]]
260
266
261
267
t .Run (strings .ReplaceAll (fmt .Sprintf ("%d/{%s}" , length , q ), " " , "·" ), func (t * testing.T ) {
262
268
subtestQuery (t , ds , q , length )
@@ -266,6 +272,7 @@ func SubtestCombinations(t *testing.T, ds dstore.Datastore) {
266
272
len (limits ),
267
273
len (filters ),
268
274
len (orders ),
275
+ len (prefixes ),
269
276
len (lengths ),
270
277
)
271
278
}
@@ -335,6 +342,28 @@ func SubtestReturnSizes(t *testing.T, ds dstore.Datastore) {
335
342
subtestQuery (t , ds , dsq.Query {ReturnsSizes : true }, 100 )
336
343
}
337
344
345
+ func SubtestPrefix (t * testing.T , ds dstore.Datastore ) {
346
+ test := func (prefix string ) {
347
+ t .Run (prefix , func (t * testing.T ) {
348
+ subtestQuery (t , ds , dsq.Query {
349
+ Prefix : prefix ,
350
+ }, 100 )
351
+ })
352
+ }
353
+ test ("" )
354
+ test ("/" )
355
+ test ("/./" )
356
+ test ("/.././/" )
357
+ test ("/prefix/../" )
358
+
359
+ test ("/prefix" )
360
+ test ("/prefix/" )
361
+ test ("/prefix/sub/" )
362
+
363
+ test ("/0/" )
364
+ test ("/bad/" )
365
+ }
366
+
338
367
func randValue () []byte {
339
368
value := make ([]byte , 64 )
340
369
rand .Read (value )
@@ -354,6 +383,28 @@ func subtestQuery(t *testing.T, ds dstore.Datastore, q dsq.Query, count int) {
354
383
})
355
384
}
356
385
386
+ for i := 0 ; i < count ; i ++ {
387
+ s := fmt .Sprintf ("/prefix/%dkey%d" , i , i )
388
+ key := dstore .NewKey (s ).String ()
389
+ value := randValue ()
390
+ input = append (input , dsq.Entry {
391
+ Key : key ,
392
+ Size : len (value ),
393
+ Value : value ,
394
+ })
395
+ }
396
+
397
+ for i := 0 ; i < count ; i ++ {
398
+ s := fmt .Sprintf ("/prefix/sub/%dkey%d" , i , i )
399
+ key := dstore .NewKey (s ).String ()
400
+ value := randValue ()
401
+ input = append (input , dsq.Entry {
402
+ Key : key ,
403
+ Size : len (value ),
404
+ Value : value ,
405
+ })
406
+ }
407
+
357
408
t .Logf ("putting %d values" , count )
358
409
for i , e := range input {
359
410
err := ds .Put (dstore .RawKey (e .Key ), e .Value )
0 commit comments