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

Function TestWriteBatch

batch_test.go:27–89  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

25)
26
27func TestWriteBatch(t *testing.T) {
28 key := func(i int) []byte {
29 return []byte(fmt.Sprintf("%10d", i))
30 }
31 val := func(i int) []byte {
32 return []byte(fmt.Sprintf("%128d", i))
33 }
34
35 test := func(t *testing.T, db *DB) {
36 wb := db.NewWriteBatch()
37 defer wb.Cancel()
38
39 // Sanity check for SetEntryAt.
40 require.Error(t, wb.SetEntryAt(&Entry{}, 12))
41
42 N, M := 50000, 1000
43 start := time.Now()
44
45 for i := 0; i < N; i++ {
46 require.NoError(t, wb.Set(key(i), val(i)))
47 }
48 for i := 0; i < M; i++ {
49 require.NoError(t, wb.Delete(key(i)))
50 }
51 require.NoError(t, wb.Flush())
52 t.Logf("Time taken for %d writes (w/ test options): %s\n", N+M, time.Since(start))
53
54 err := db.View(func(txn *Txn) error {
55 itr := txn.NewIterator(DefaultIteratorOptions)
56 defer itr.Close()
57
58 i := M
59 for itr.Rewind(); itr.Valid(); itr.Next() {
60 item := itr.Item()
61 require.Equal(t, string(key(i)), string(item.Key()))
62 valcopy, err := item.ValueCopy(nil)
63 require.NoError(t, err)
64 require.Equal(t, val(i), valcopy)
65 i++
66 }
67 require.Equal(t, N, i)
68 return nil
69 })
70 require.NoError(t, err)
71 }
72 t.Run("disk mode", func(t *testing.T) {
73 opt := getTestOptions("")
74 // Set value threshold to 32 bytes otherwise write batch will generate
75 // too many files and we will crash with too many files open error.
76 opt.ValueThreshold = 32
77 runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
78 test(t, db)
79 })
80 })
81 t.Run("InMemory mode", func(t *testing.T) {
82 opt := getTestOptions("")
83 opt.InMemory = true
84 db, err := Open(opt)

Callers

nothing calls this directly

Calls 15

CancelMethod · 0.95
SetEntryAtMethod · 0.95
SetMethod · 0.95
DeleteMethod · 0.95
FlushMethod · 0.95
CloseMethod · 0.95
RewindMethod · 0.95
ValidMethod · 0.95
NextMethod · 0.95
ItemMethod · 0.95
valFunction · 0.85
getTestOptionsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…