forked from rosedblabs/rosedb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.go
317 lines (277 loc) · 6.75 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package rosedb
import (
"fmt"
"sync"
"github.com/bwmarrin/snowflake"
"github.com/rosedblabs/wal"
)
// 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 // save the data to be written
options BatchOptions
mu sync.RWMutex
committed bool // whether the batch has been committed
rollbacked bool // whether the batch has been rollbacked
batchId *snowflake.Node
}
// NewBatch creates a new Batch instance.
func (db *DB) NewBatch(options BatchOptions) *Batch {
batch := &Batch{
db: db,
options: options,
committed: false,
rollbacked: 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, db *DB) *Batch {
b.options.ReadOnly = rdonly
b.options.Sync = sync
b.db = db
b.lock()
return b
}
func (b *Batch) withPendingWrites() *Batch {
b.pendingWrites = make(map[string]*LogRecord)
return b
}
func (b *Batch) reset() {
b.db = nil
b.pendingWrites = nil
b.committed = false
b.rollbacked = 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 data file
chunkPosition := b.db.index.Get(key)
if chunkPosition == nil {
return nil, ErrKeyNotFound
}
chunk, err := b.db.dataFiles.Read(chunkPosition)
if err != nil {
return nil, err
}
record := decodeLogRecord(chunk)
if record.Type == LogRecordDeleted {
panic("Deleted data cannot exist in the index")
}
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()
if position := b.db.index.Get(key); position != nil {
// write to pendingWrites if the key exists
b.pendingWrites[string(key)] = &LogRecord{
Key: key,
Type: LogRecordDeleted,
}
} else {
delete(b.pendingWrites, string(key))
}
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()
}
// check if the key exists in data file
position := b.db.index.Get(key)
return position != 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 or rollbacked
if b.committed {
return ErrBatchCommitted
}
if b.rollbacked {
return ErrBatchRollbacked
}
batchId := b.batchId.Generate()
positions := make(map[string]*wal.ChunkPosition)
// write to wal
for _, record := range b.pendingWrites {
record.BatchId = uint64(batchId)
encRecord := encodeLogRecord(record)
pos, err := b.db.dataFiles.Write(encRecord)
if err != nil {
return err
}
positions[string(record.Key)] = pos
}
// write a record to indicate the end of the batch
endRecord := encodeLogRecord(&LogRecord{
Key: batchId.Bytes(),
Type: LogRecordBatchFinished,
})
if _, err := b.db.dataFiles.Write(endRecord); err != nil {
return err
}
// flush wal if necessary
if b.options.Sync && !b.db.options.Sync {
if err := b.db.dataFiles.Sync(); err != nil {
return err
}
}
// write to index
for key, record := range b.pendingWrites {
if record.Type == LogRecordDeleted {
b.db.index.Delete(record.Key)
} else {
b.db.index.Put(record.Key, positions[key])
}
if b.db.options.WatchQueueSize > 0 {
e := &Event{Key: record.Key, Value: record.Value, BatchId: record.BatchId}
if record.Type == LogRecordDeleted {
e.Action = WatchActionDelete
} else {
e.Action = WatchActionPut
}
b.db.watcher.putEvent(e)
}
}
b.committed = true
return nil
}
// Rollback discards a uncommitted batch instance.
// the discard operation will clear the buffered data and release the lock.
func (b *Batch) Rollback() error {
defer b.unlock()
if b.db.closed {
return ErrDBClosed
}
if b.committed {
return ErrBatchCommitted
}
if b.rollbacked {
return ErrBatchRollbacked
}
if !b.options.ReadOnly {
// clear pendingWrites
b.pendingWrites = nil
}
b.rollbacked = true
return nil
}