@@ -192,7 +192,7 @@ func TestConcurrentRemove(t *testing.T) {
192192 })
193193}
194194
195- func TestRemoveRange (t * testing.T ) {
195+ func TestRemoveByPrefix (t * testing.T ) {
196196 dbtest .RunWithStorages (t , func (t * testing.T , r storage.Reader , withWriter dbtest.WithWriter ) {
197197
198198 // Define the prefix
@@ -244,6 +244,59 @@ func TestRemoveRange(t *testing.T) {
244244 })
245245}
246246
247+ func TestRemoveByRange (t * testing.T ) {
248+ dbtest .RunWithStorages (t , func (t * testing.T , r storage.Reader , withWriter dbtest.WithWriter ) {
249+
250+ startPrefix , endPrefix := []byte {0x10 }, []byte {0x12 }
251+ // Create a range of keys around the boundaries of the prefix
252+ keys := [][]byte {
253+ {0x09 , 0xff },
254+ // within the range
255+ {0x10 , 0x00 },
256+ {0x10 , 0x50 },
257+ {0x10 , 0xff },
258+ {0x11 },
259+ {0x12 },
260+ {0x12 , 0x00 },
261+ {0x12 , 0xff },
262+ // after end -> not included in range
263+ {0x13 },
264+ {0x1A , 0xff },
265+ }
266+
267+ // Keys expected to be in the prefix range
268+ includeStart , includeEnd := 1 , 7
269+
270+ // Insert the keys into the storage
271+ require .NoError (t , withWriter (func (writer storage.Writer ) error {
272+ for _ , key := range keys {
273+ value := []byte {0x00 } // value are skipped, doesn't matter
274+ err := operation .Upsert (key , value )(writer )
275+ if err != nil {
276+ return err
277+ }
278+ }
279+ return nil
280+ }))
281+
282+ // Remove the keys in the prefix range
283+ require .NoError (t , withWriter (operation .RemoveByRange (r , startPrefix , endPrefix )))
284+
285+ // Verify that the keys in the prefix range have been removed
286+ for i , key := range keys {
287+ var exists bool
288+ require .NoError (t , operation .Exists (key , & exists )(r ))
289+ t .Logf ("key %x exists: %t" , key , exists )
290+
291+ deleted := includeStart <= i && i <= includeEnd
292+
293+ // An item that was not deleted must exist
294+ require .Equal (t , ! deleted , exists ,
295+ "expected key %x to be %s" , key , map [bool ]string {true : "deleted" , false : "not deleted" })
296+ }
297+ })
298+ }
299+
247300func TestRemoveFrom (t * testing.T ) {
248301 dbtest .RunWithStorages (t , func (t * testing.T , r storage.Reader , withWriter dbtest.WithWriter ) {
249302
0 commit comments