@@ -166,10 +166,7 @@ func testSideloadingSideloadedStorage(t *testing.T, eng storage.Engine) {
166
166
},
167
167
{
168
168
err : nil ,
169
- fun : func () error {
170
- _ , err := ss .TruncateTo (ctx , 122 )
171
- return err
172
- },
169
+ fun : func () error { return ss .TruncateTo (ctx , 122 ) },
173
170
},
174
171
{
175
172
err : nil ,
@@ -221,19 +218,17 @@ func testSideloadingSideloadedStorage(t *testing.T, eng storage.Engine) {
221
218
222
219
for n := range payloads {
223
220
index := payloads [n ] // (0, index] + (index, ...]
224
- total , err := ss .BytesIfTruncatedFromTo (ctx , kvpb.RaftSpan {Last : math .MaxUint64 })
221
+ _ , total , err := ss .Stats (ctx , kvpb.RaftSpan {Last : math .MaxUint64 })
225
222
require .NoError (t , err )
226
- prefixBytes , err := ss .BytesIfTruncatedFromTo (ctx , kvpb.RaftSpan {Last : index })
223
+ _ , prefixBytes , err := ss .Stats (ctx , kvpb.RaftSpan {Last : index })
227
224
require .NoError (t , err )
228
- suffixBytes , err := ss .BytesIfTruncatedFromTo (ctx , kvpb.RaftSpan {
225
+ _ , suffixBytes , err := ss .Stats (ctx , kvpb.RaftSpan {
229
226
After : index , Last : math .MaxUint64 ,
230
227
})
231
228
require .NoError (t , err )
232
229
require .Equal (t , total , prefixBytes + suffixBytes )
233
230
// Truncate indexes <= payloads[n] (payloads is sorted in increasing order).
234
- freed , err := ss .TruncateTo (ctx , index )
235
- require .NoError (t , err )
236
- require .Equal (t , prefixBytes , freed )
231
+ require .NoError (t , ss .TruncateTo (ctx , index ))
237
232
// Indexes > payloads[n] are still there at both terms.
238
233
for _ , term := range []kvpb.RaftTerm {lowTerm , highTerm } {
239
234
for _ , i := range payloads [n + 1 :] {
@@ -260,56 +255,64 @@ func testSideloadingSideloadedStorage(t *testing.T, eng storage.Engine) {
260
255
// we will be prevented from removing it below.
261
256
require .NoError (t , f .Close ())
262
257
263
- _ , err = ss .TruncateTo (ctx , math .MaxUint64 )
258
+ require . NoError ( t , ss .TruncateTo (ctx , math .MaxUint64 ) )
264
259
// The sideloaded storage should not error out here; removing files
265
260
// is optional. But the file should still be there!
266
- require .NoError (t , err )
267
261
_ , err = eng .Env ().Stat (nonRemovableFile )
268
262
require .NoError (t , err )
269
263
270
264
// Now remove extra file and let truncation proceed to remove directory.
271
265
require .NoError (t , eng .Env ().Remove (nonRemovableFile ))
272
266
273
267
// Test that directory is removed when filepath.Glob returns 0 matches.
274
- _ , err = ss .TruncateTo (ctx , math .MaxUint64 )
275
- require .NoError (t , err )
268
+ require .NoError (t , ss .TruncateTo (ctx , math .MaxUint64 ))
276
269
// Ensure directory is removed, now that all files should be gone.
277
270
_ , err = eng .Env ().Stat (ss .Dir ())
278
271
require .True (t , oserror .IsNotExist (err ), "%v" , err )
279
- // Ensure HasAnyEntry doesn 't find anything.
280
- found , err := ss .HasAnyEntry (ctx , kvpb.RaftSpan {Last : 10000 })
272
+ // Ensure that Stats don 't find anything.
273
+ count , size , err := ss .Stats (ctx , kvpb.RaftSpan {Last : 10000 })
281
274
require .NoError (t , err )
282
- require .False (t , found )
275
+ require .Zero (t , count )
276
+ require .Zero (t , size )
283
277
284
278
// Repopulate with some random indexes to test deletion when there are a
285
279
// non-zero number of filepath.Glob matches.
286
280
payloads := []kvpb.RaftIndex {3 , 5 , 7 , 9 , 10 }
281
+ sizes := make ([]int64 , len (payloads )+ 1 )
287
282
for n := range rand .Perm (len (payloads )) {
288
283
i := payloads [n ]
289
- require .NoError (t , ss .Put (ctx , i , highTerm , file (i , highTerm )))
284
+ content := file (i , highTerm )
285
+ sizes [n + 1 ] = sizes [n ] + int64 (len (content ))
286
+ require .NoError (t , ss .Put (ctx , i , highTerm , content ))
287
+ }
288
+ spanSize := func (from , to int ) int64 {
289
+ return sizes [to ] - sizes [from ]
290
290
}
291
291
assertExists (true )
292
- // Verify the HasAnyEntry semantics .
292
+ // Verify the Stats computation .
293
293
for _ , check := range []struct {
294
- from , to kvpb.RaftIndex
295
- want bool
294
+ from , to kvpb.RaftIndex
295
+ wantCount uint64
296
+ wantSize int64
296
297
}{
297
- {from : 0 , to : 2 , want : false }, // 2 is included
298
- {from : 0 , to : 3 , want : true }, // but included if to == 3
299
- {from : 2 , to : 4 , want : true }, // 2 is excluded
300
- {from : 3 , to : 4 , want : false },
301
- {from : 49 , to : 59 , want : false },
302
- {from : 0 , to : 9 , want : true },
298
+ // Not found cases.
299
+ {from : 0 , to : 2 }, // 2 is included
300
+ {from : 3 , to : 4 },
301
+ {from : 49 , to : 59 },
302
+ // Found cases.
303
+ {from : 0 , to : 3 , wantCount : 1 , wantSize : spanSize (0 , 1 )}, // 3 is included
304
+ {from : 2 , to : 4 , wantCount : 1 , wantSize : spanSize (0 , 1 )}, // 2 is excluded
305
+ {from : 0 , to : 9 , wantCount : 4 , wantSize : spanSize (0 , 4 )},
306
+ {from : 0 , to : 10 , wantCount : 5 , wantSize : spanSize (0 , 5 )},
307
+ {from : 0 , to : 100 , wantCount : 5 , wantSize : spanSize (0 , 5 )},
308
+ {from : 3 , to : 10 , wantCount : 4 , wantSize : spanSize (1 , 5 )},
303
309
} {
304
- found , err := ss .HasAnyEntry (ctx , kvpb.RaftSpan {After : check .from , Last : check .to })
310
+ count , size , err := ss .Stats (ctx , kvpb.RaftSpan {After : check .from , Last : check .to })
305
311
require .NoError (t , err )
306
- require .Equal (t , check .want , found )
312
+ require .Equal (t , check .wantCount , count )
313
+ require .Equal (t , check .wantSize , size )
307
314
}
308
- prefixBytes , err := ss .BytesIfTruncatedFromTo (ctx , kvpb.RaftSpan {Last : math .MaxUint64 })
309
- require .NoError (t , err )
310
- freed , err := ss .TruncateTo (ctx , math .MaxUint64 )
311
- require .NoError (t , err )
312
- require .Equal (t , prefixBytes , freed )
315
+ require .NoError (t , ss .TruncateTo (ctx , math .MaxUint64 ))
313
316
// Ensure directory is removed when all records are removed.
314
317
_ , err = eng .Env ().Stat (ss .Dir ())
315
318
require .True (t , oserror .IsNotExist (err ), "%v" , err )
@@ -319,14 +322,12 @@ func testSideloadingSideloadedStorage(t *testing.T, eng storage.Engine) {
319
322
320
323
assertExists (false )
321
324
322
- // Sanity check that we can call BytesIfTruncatedFromTo and TruncateTo
323
- // without the directory existing.
324
- size , err := ss .BytesIfTruncatedFromTo (ctx , kvpb.RaftSpan {Last : math .MaxUint64 })
325
+ // Sanity check that Stats returns zeroes when the directory does not exist.
326
+ count , size , err := ss .Stats (ctx , kvpb.RaftSpan {Last : math .MaxUint64 })
325
327
require .NoError (t , err )
328
+ require .Zero (t , count )
326
329
require .Zero (t , size )
327
- freed , err := ss .TruncateTo (ctx , 0 )
328
- require .NoError (t , err )
329
- require .Zero (t , freed )
330
+ require .NoError (t , ss .TruncateTo (ctx , 0 ))
330
331
331
332
assertExists (false )
332
333
0 commit comments