@@ -166,10 +166,7 @@ func testSideloadingSideloadedStorage(t *testing.T, eng storage.Engine) {
166166 },
167167 {
168168 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 ) },
173170 },
174171 {
175172 err : nil ,
@@ -221,19 +218,17 @@ func testSideloadingSideloadedStorage(t *testing.T, eng storage.Engine) {
221218
222219 for n := range payloads {
223220 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 })
225222 require .NoError (t , err )
226- prefixBytes , err := ss .BytesIfTruncatedFromTo (ctx , kvpb.RaftSpan {Last : index })
223+ _ , prefixBytes , err := ss .Stats (ctx , kvpb.RaftSpan {Last : index })
227224 require .NoError (t , err )
228- suffixBytes , err := ss .BytesIfTruncatedFromTo (ctx , kvpb.RaftSpan {
225+ _ , suffixBytes , err := ss .Stats (ctx , kvpb.RaftSpan {
229226 After : index , Last : math .MaxUint64 ,
230227 })
231228 require .NoError (t , err )
232229 require .Equal (t , total , prefixBytes + suffixBytes )
233230 // 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 ))
237232 // Indexes > payloads[n] are still there at both terms.
238233 for _ , term := range []kvpb.RaftTerm {lowTerm , highTerm } {
239234 for _ , i := range payloads [n + 1 :] {
@@ -260,56 +255,64 @@ func testSideloadingSideloadedStorage(t *testing.T, eng storage.Engine) {
260255 // we will be prevented from removing it below.
261256 require .NoError (t , f .Close ())
262257
263- _ , err = ss .TruncateTo (ctx , math .MaxUint64 )
258+ require . NoError ( t , ss .TruncateTo (ctx , math .MaxUint64 ) )
264259 // The sideloaded storage should not error out here; removing files
265260 // is optional. But the file should still be there!
266- require .NoError (t , err )
267261 _ , err = eng .Env ().Stat (nonRemovableFile )
268262 require .NoError (t , err )
269263
270264 // Now remove extra file and let truncation proceed to remove directory.
271265 require .NoError (t , eng .Env ().Remove (nonRemovableFile ))
272266
273267 // 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 ))
276269 // Ensure directory is removed, now that all files should be gone.
277270 _ , err = eng .Env ().Stat (ss .Dir ())
278271 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 })
281274 require .NoError (t , err )
282- require .False (t , found )
275+ require .Zero (t , count )
276+ require .Zero (t , size )
283277
284278 // Repopulate with some random indexes to test deletion when there are a
285279 // non-zero number of filepath.Glob matches.
286280 payloads := []kvpb.RaftIndex {3 , 5 , 7 , 9 , 10 }
281+ sizes := make ([]int64 , len (payloads )+ 1 )
287282 for n := range rand .Perm (len (payloads )) {
288283 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 ]
290290 }
291291 assertExists (true )
292- // Verify the HasAnyEntry semantics .
292+ // Verify the Stats computation .
293293 for _ , check := range []struct {
294- from , to kvpb.RaftIndex
295- want bool
294+ from , to kvpb.RaftIndex
295+ wantCount uint64
296+ wantSize int64
296297 }{
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 )},
303309 } {
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 })
305311 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 )
307314 }
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 ))
313316 // Ensure directory is removed when all records are removed.
314317 _ , err = eng .Env ().Stat (ss .Dir ())
315318 require .True (t , oserror .IsNotExist (err ), "%v" , err )
@@ -319,14 +322,12 @@ func testSideloadingSideloadedStorage(t *testing.T, eng storage.Engine) {
319322
320323 assertExists (false )
321324
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 })
325327 require .NoError (t , err )
328+ require .Zero (t , count )
326329 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 ))
330331
331332 assertExists (false )
332333
0 commit comments