MCPcopy
hub / github.com/dgraph-io/badger / TestCompactionFilePicking

Function TestCompactionFilePicking

db2_test.go:457–518  ·  view source on GitHub ↗

This test is for compaction file picking testing. We are creating db with two levels. We have 10 tables on level 3 and 3 tables on level 2. Tables on level 2 have overlap with 2, 4, 3 tables on level 3.

(t *testing.T)

Source from the content-addressed store, hash-verified

455// tables on level 3 and 3 tables on level 2. Tables on level 2 have overlap with 2, 4, 3 tables on
456// level 3.
457func TestCompactionFilePicking(t *testing.T) {
458 dir, err := ioutil.TempDir("", "badger-test")
459 require.NoError(t, err)
460 defer removeDir(dir)
461
462 db, err := Open(DefaultOptions(dir).WithTableLoadingMode(options.LoadToRAM))
463 require.NoError(t, err, "error while opening db")
464 defer func() {
465 require.NoError(t, db.Close())
466 }()
467
468 l3 := db.lc.levels[3]
469 for i := 1; i <= 10; i++ {
470 // Each table has difference of 1 between smallest and largest key.
471 tab := createTableWithRange(t, db, 2*i-1, 2*i)
472 addToManifest(t, db, tab, 3)
473 require.NoError(t, l3.replaceTables([]*table.Table{}, []*table.Table{tab}))
474 }
475
476 l2 := db.lc.levels[2]
477 // First table has keys 1 and 4.
478 tab := createTableWithRange(t, db, 1, 4)
479 addToManifest(t, db, tab, 2)
480 require.NoError(t, l2.replaceTables([]*table.Table{}, []*table.Table{tab}))
481
482 // Second table has keys 5 and 12.
483 tab = createTableWithRange(t, db, 5, 12)
484 addToManifest(t, db, tab, 2)
485 require.NoError(t, l2.replaceTables([]*table.Table{}, []*table.Table{tab}))
486
487 // Third table has keys 13 and 18.
488 tab = createTableWithRange(t, db, 13, 18)
489 addToManifest(t, db, tab, 2)
490 require.NoError(t, l2.replaceTables([]*table.Table{}, []*table.Table{tab}))
491
492 cdef := &compactDef{
493 thisLevel: db.lc.levels[2],
494 nextLevel: db.lc.levels[3],
495 }
496
497 tables := db.lc.levels[2].tables
498 db.lc.sortByOverlap(tables, cdef)
499
500 var expKey [8]byte
501 // First table should be with smallest and biggest keys as 1 and 4.
502 binary.BigEndian.PutUint64(expKey[:], uint64(1))
503 require.Equal(t, expKey[:], y.ParseKey(tables[0].Smallest()))
504 binary.BigEndian.PutUint64(expKey[:], uint64(4))
505 require.Equal(t, expKey[:], y.ParseKey(tables[0].Biggest()))
506
507 // Second table should be with smallest and biggest keys as 13 and 18.
508 binary.BigEndian.PutUint64(expKey[:], uint64(13))
509 require.Equal(t, expKey[:], y.ParseKey(tables[1].Smallest()))
510 binary.BigEndian.PutUint64(expKey[:], uint64(18))
511 require.Equal(t, expKey[:], y.ParseKey(tables[1].Biggest()))
512
513 // Third table should be with smallest and biggest keys as 5 and 12.
514 binary.BigEndian.PutUint64(expKey[:], uint64(5))

Callers

nothing calls this directly

Calls 12

ParseKeyFunction · 0.92
removeDirFunction · 0.85
OpenFunction · 0.85
DefaultOptionsFunction · 0.85
createTableWithRangeFunction · 0.85
addToManifestFunction · 0.85
WithTableLoadingModeMethod · 0.80
replaceTablesMethod · 0.80
sortByOverlapMethod · 0.80
CloseMethod · 0.65
SmallestMethod · 0.65
BiggestMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…