-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch_test.go
146 lines (130 loc) · 3.99 KB
/
batch_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package bitcask
import (
"bitcask/utils"
"github.com/stretchr/testify/assert"
"os"
"testing"
)
// TestDB_NewWriteBatch is a unit test for the NewWriteBatch function.
func TestDB_NewWriteBatch(t *testing.T) {
// Create a default configuration for the Bitcask database.
cfg := DefaultConfig
// Create a temporary directory for the database.
dir, err := os.MkdirTemp("", "bitcask-test-batch-new")
assert.Nil(t, err)
cfg.DirPath = dir
// Open the Bitcask database.
db, err := Open(cfg)
defer destroyDB(db)
assert.Nil(t, err)
assert.NotNil(t, db)
// Create a new write batch.
batch := db.NewWriteBatch(DefaultWriteBatchConfig)
assert.NotNil(t, batch)
}
// TestWriteBatch_1 is a unit test for the WriteBatch function.
func TestWriteBatch_1(t *testing.T) {
// Create a default configuration for the Bitcask database.
cfg := DefaultConfig
// Create a temporary directory for the database.
dir, err := os.MkdirTemp("", "bitcask-test-batch-write1")
assert.Nil(t, err)
cfg.DirPath = dir
// Open the Bitcask database.
db, err := Open(cfg)
defer destroyDB(db)
assert.Nil(t, err)
assert.NotNil(t, db)
// Create a new write batch without committing any data.
batch1 := db.NewWriteBatch(DefaultWriteBatchConfig)
assert.NotNil(t, batch1)
// Put a key-value pair into the write batch.
err = batch1.Put(utils.GetTestKey(10), utils.GetTestValue(10))
assert.Nil(t, err)
// Delete a key from the write batch.
err = batch1.Delete(utils.GetTestKey(11))
assert.Nil(t, err)
// Retrieve the value of a key that was not committed.
get1, err := db.Get(utils.GetTestKey(10))
assert.Equal(t, err, ErrKeyNotFound)
assert.Equal(t, len(get1), 0)
// Commit the write batch, making the changes permanent.
err = batch1.Commit()
assert.Nil(t, err)
// Retrieve the value of the key after committing the changes.
get2, err := db.Get(utils.GetTestKey(10))
assert.NotNil(t, get2)
assert.Nil(t, err)
// Create a new write batch and delete the key.
batch2 := db.NewWriteBatch(DefaultWriteBatchConfig)
err = batch2.Delete(utils.GetTestKey(10))
assert.Nil(t, err)
// Commit the write batch, making the deletion permanent.
err = batch2.Commit()
assert.Nil(t, err)
// Retrieve the value of the key after committing the deletion.
get3, err := db.Get(utils.GetTestKey(10))
assert.Equal(t, err, ErrKeyNotFound)
assert.Equal(t, len(get3), 0)
}
// TestWriteBatch_2 is a unit test for the WriteBatch function.
func TestWriteBatch_2(t *testing.T) {
cfg := DefaultConfig
dir, err := os.MkdirTemp("", "bitcask-test-batch-write2")
assert.Nil(t, err)
cfg.DirPath = dir
db, err := Open(cfg)
defer destroyDB(db)
assert.Nil(t, err)
assert.NotNil(t, db)
// Create a new WriteBatch
batch1 := db.NewWriteBatch(DefaultWriteBatchConfig)
assert.NotNil(t, batch1)
// Put an existing data
err = batch1.Put(utils.GetTestKey(11), utils.GetTestValue(10))
assert.Nil(t, err)
err = batch1.Put(utils.GetTestKey(12), utils.GetTestValue(10))
assert.Nil(t, err)
err = batch1.Delete(utils.GetTestKey(11))
assert.Nil(t, err)
// Commit the batch
err = batch1.Commit()
assert.Nil(t, err)
// Put another data
err = batch1.Put(utils.GetTestKey(12), utils.GetTestValue(10))
assert.Nil(t, err)
// Commit the batch again
err = batch1.Commit()
assert.Nil(t, err)
// Restart the database
err = db.Close()
assert.Nil(t, err)
db, err = Open(cfg)
assert.Nil(t, err)
assert.NotNil(t, db)
// Check if the data is retrieved correctly
get1, err := db.Get(utils.GetTestKey(11))
assert.Equal(t, err, ErrKeyNotFound)
assert.Equal(t, len(get1), 0)
assert.Equal(t, uint64(2), db.seqNo)
}
//func TestWriteBatch_3(t *testing.T) {
// cfg := DefaultConfig
// dir := "/temp"
// cfg.DirPath = dir
// t.Log(cfg.DirPath)
// db, err := Open(cfg)
// assert.Nil(t, err)
// assert.NotNil(t, db)
// batch := db.NewWriteBatch(WriteBatchConfig{
// MaxBatchNum: 5000000,
// SyncWrites: true,
// })
// //for i := 0; i < 500000; i++ {
// // err := batch.Put(utils.GetTestKey(i), utils.GetTestValue(1024))
// // assert.Nil(t, err)
// //}
// err = batch.Commit()
// assert.Nil(t, err)
//
//}