-
Notifications
You must be signed in to change notification settings - Fork 182
/
batch.go
288 lines (256 loc) · 5.95 KB
/
batch.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package lotusdb
import (
"fmt"
"sync"
"github.com/bwmarrin/snowflake"
"github.com/rosedblabs/diskhash"
)
// Batch is a batch operations of the database.
// If readonly is true, you can only get data from the batch by Get method.
// An error will be returned if you try to use Put or Delete method.
//
// If readonly is false, you can use Put and Delete method to write data to the batch.
// The data will be written to the database when you call Commit method.
//
// Batch is not a transaction, it does not guarantee isolation.
// But it can guarantee atomicity, consistency and durability(if the Sync options is true).
//
// You must call Commit method to commit the batch, otherwise the DB will be locked.
type Batch struct {
db *DB
pendingWrites map[string]*LogRecord
options BatchOptions
mu sync.RWMutex
committed bool
batchID *snowflake.Node
}
// NewBatch creates a new Batch instance.
func (db *DB) NewBatch(options BatchOptions) *Batch {
batch := &Batch{
db: db,
options: options,
committed: false,
}
if !options.ReadOnly {
batch.pendingWrites = make(map[string]*LogRecord)
node, err := snowflake.NewNode(1)
if err != nil {
panic(fmt.Sprintf("snowflake.NewNode(1) failed: %v", err))
}
batch.batchID = node
}
batch.lock()
return batch
}
func makeBatch() interface{} {
node, err := snowflake.NewNode(1)
if err != nil {
panic(fmt.Sprintf("snowflake.NewNode(1) failed: %v", err))
}
return &Batch{
options: DefaultBatchOptions,
batchID: node,
}
}
func (b *Batch) init(rdonly, sync bool, disableWal bool, db *DB) *Batch {
b.options.ReadOnly = rdonly
b.options.Sync = sync
b.options.DisableWal = disableWal
b.db = db
b.lock()
return b
}
func (b *Batch) withPendingWrites() {
b.pendingWrites = make(map[string]*LogRecord)
}
func (b *Batch) reset() {
b.db = nil
b.pendingWrites = nil
b.committed = false
}
func (b *Batch) lock() {
if b.options.ReadOnly {
b.db.mu.RLock()
} else {
b.db.mu.Lock()
}
}
func (b *Batch) unlock() {
if b.options.ReadOnly {
b.db.mu.RUnlock()
} else {
b.db.mu.Unlock()
}
}
// Put adds a key-value pair to the batch for writing.
func (b *Batch) Put(key []byte, value []byte) error {
if len(key) == 0 {
return ErrKeyIsEmpty
}
if b.db.closed {
return ErrDBClosed
}
if b.options.ReadOnly {
return ErrReadOnlyBatch
}
b.mu.Lock()
// write to pendingWrites
b.pendingWrites[string(key)] = &LogRecord{
Key: key,
Value: value,
Type: LogRecordNormal,
}
b.mu.Unlock()
return nil
}
// Get retrieves the value associated with a given key from the batch.
func (b *Batch) Get(key []byte) ([]byte, error) {
if len(key) == 0 {
return nil, ErrKeyIsEmpty
}
if b.db.closed {
return nil, ErrDBClosed
}
// get from pendingWrites
if b.pendingWrites != nil {
b.mu.RLock()
if record := b.pendingWrites[string(key)]; record != nil {
if record.Type == LogRecordDeleted {
b.mu.RUnlock()
return nil, ErrKeyNotFound
}
b.mu.RUnlock()
return record.Value, nil
}
b.mu.RUnlock()
}
// get from memtables
tables := b.db.getMemTables()
for _, table := range tables {
deleted, value := table.get(key)
if deleted {
return nil, ErrKeyNotFound
}
if len(value) != 0 {
return value, nil
}
}
// get from index
var value []byte
var matchKey func(diskhash.Slot) (bool, error)
if b.db.options.IndexType == Hash {
matchKey = MatchKeyFunc(b.db, key, nil, &value)
}
position, err := b.db.index.Get(key, matchKey)
if err != nil {
return nil, err
}
if b.db.options.IndexType == Hash {
if value == nil {
return nil, ErrKeyNotFound
}
return value, nil
}
if position == nil {
return nil, ErrKeyNotFound
}
record, err := b.db.vlog.read(position)
if err != nil {
return nil, err
}
return record.value, nil
}
// Delete marks a key for deletion in the batch.
func (b *Batch) Delete(key []byte) error {
if len(key) == 0 {
return ErrKeyIsEmpty
}
if b.db.closed {
return ErrDBClosed
}
if b.options.ReadOnly {
return ErrReadOnlyBatch
}
b.mu.Lock()
b.pendingWrites[string(key)] = &LogRecord{
Key: key,
Type: LogRecordDeleted,
}
b.mu.Unlock()
return nil
}
// Exist checks if the key exists in the database.
func (b *Batch) Exist(key []byte) (bool, error) {
if len(key) == 0 {
return false, ErrKeyIsEmpty
}
if b.db.closed {
return false, ErrDBClosed
}
// check if the key exists in pendingWrites
if b.pendingWrites != nil {
b.mu.RLock()
if record := b.pendingWrites[string(key)]; record != nil {
b.mu.RUnlock()
return record.Type != LogRecordDeleted, nil
}
b.mu.RUnlock()
}
// get from memtables
tables := b.db.getMemTables()
for _, table := range tables {
deleted, value := table.get(key)
if deleted {
return false, nil
}
if len(value) != 0 {
return true, nil
}
}
// check if the key exists in index
var value []byte
var matchKeyFunc func(diskhash.Slot) (bool, error)
if b.db.options.IndexType == Hash {
matchKeyFunc = MatchKeyFunc(b.db, key, nil, &value)
}
pos, err := b.db.index.Get(key, matchKeyFunc)
if err != nil {
return false, err
}
if b.db.options.IndexType == Hash {
return value != nil, nil
}
return pos != nil, nil
}
// Commit commits the batch, if the batch is readonly or empty, it will return directly.
//
// It will iterate the pendingWrites and write the data to the database,
// then write a record to indicate the end of the batch to guarantee atomicity.
// Finally, it will write the index.
func (b *Batch) Commit() error {
defer b.unlock()
if b.db.closed {
return ErrDBClosed
}
if b.options.ReadOnly || len(b.pendingWrites) == 0 {
return nil
}
b.mu.Lock()
defer b.mu.Unlock()
// check if committed
if b.committed {
return ErrBatchCommitted
}
// wait for memtable space
if err := b.db.waitMemtableSpace(); err != nil {
return err
}
batchID := b.batchID.Generate()
// call memtable put batch
err := b.db.activeMem.putBatch(b.pendingWrites, batchID, b.options.WriteOptions)
if err != nil {
return err
}
b.committed = true
return nil
}