-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch.go
155 lines (131 loc) · 3.58 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
package gobitcask
import (
"encoding/binary"
"go-bitcask/data"
"sync"
"sync/atomic"
)
// nonTransactionSeqNo 非事务操作序列号
const nonTransactionSeqNo uint64 = 0
var txnFinKey = []byte("txn-fin")
// WriteBatch 原子批量写入数据,保证原子性
type WriteBatch struct {
options WriteBatchOptions
mu *sync.Mutex
db *DB
pendingWrites map[string]*data.LogRecord // 暂存用户写入的数据
}
// NewWriteBtach 初始化 WriteBatch
func (db *DB) NewWriteBtach(opts WriteBatchOptions) *WriteBatch {
return &WriteBatch{
options: opts,
mu: new(sync.Mutex),
db: db,
pendingWrites: make(map[string]*data.LogRecord),
}
}
// Put 批量写数据
func (wb *WriteBatch) Put(key, value []byte) error {
if len(key) == 0 {
return ErrKeyIsEmpty
}
wb.mu.Lock()
defer wb.mu.Unlock()
// 暂存LogRecord
logRecord := &data.LogRecord{Key: key, Value: value}
wb.pendingWrites[string(key)] = logRecord
return nil
}
// Delete 删除数据
func (wb *WriteBatch) Delete(key []byte) error {
if len(key) == 0 {
return ErrKeyIsEmpty
}
wb.mu.Lock()
defer wb.mu.Unlock()
// 数据不存在则直接返回
logRecordPos := wb.db.index.Get(key)
if logRecordPos == nil {
if wb.pendingWrites[string(key)] != nil {
delete(wb.pendingWrites, string(key))
}
return nil
}
// 暂存 LogRecord
logRecord := &data.LogRecord{Key: key, Type: data.LogRecordDelete}
wb.pendingWrites[string(key)] = logRecord
return nil
}
// Commit 提交事务,将暂存的数据写到数据文件,并更新内存索引
func (wb *WriteBatch) Commit() error {
wb.mu.Lock()
defer wb.mu.Unlock()
if len(wb.pendingWrites) == 0 {
return nil
}
if uint(len(wb.pendingWrites)) > wb.options.MaxBatchNum {
return ErrExceedMaxBatchNum
}
// 加锁保证事务串行化
wb.db.mu.Lock()
defer wb.db.mu.Unlock()
// 获取当前最新的事务序列号
seqNo := atomic.AddUint64(&wb.db.seqNo, 1)
// 开始写数据到数据文件中
positions := make(map[string]*data.LogRecordPos)
for _, record := range wb.pendingWrites {
logRecordPos, err := wb.db.appendLogRecord(&data.LogRecord{
Key: logRecordKeyWithSeq(record.Key, seqNo),
Value: record.Value,
Type: record.Type,
})
if err != nil {
return err
}
positions[string(record.Key)] = logRecordPos
}
// 写一条标识事务完成的数据
finishedRecord := &data.LogRecord{
Key: logRecordKeyWithSeq(txnFinKey, seqNo),
Type: data.LogRecordTxnFinished,
}
if _, err := wb.db.appendLogRecord(finishedRecord); err != nil {
return err
}
// 根据配置决定是否持久化
if wb.options.SyncWrites && wb.db.activeFile != nil {
if err := wb.db.activeFile.Sync(); err != nil {
return err
}
}
// 更新内存索引
for _, record := range wb.pendingWrites {
pos := positions[string(record.Key)]
if record.Type == data.LogRecordNormal {
wb.db.index.Put(record.Key, pos)
}
if record.Type == data.LogRecordDelete {
wb.db.index.Delete(record.Key)
}
}
// 清空暂存数据
wb.pendingWrites = make(map[string]*data.LogRecord)
return nil
}
// logRecordKeyWithSeq 将 key + seq 编码
func logRecordKeyWithSeq(key []byte, seqNo uint64) []byte {
// 对 seqNo进行编码
seq := make([]byte, binary.MaxVarintLen64)
n := binary.PutUvarint(seq[:], seqNo)
encKey := make([]byte, n+len(key))
copy(encKey[:n], seq[:n])
copy(encKey[n:], key)
return encKey
}
// parseLogRecordKey 解析 LogRecord 的 key
// 返回实际的 key 和 事务序列号
func parseLogRecordKey(key []byte) ([]byte, uint64) {
seqNo, n := binary.Uvarint(key)
realKey := key[n:]
return realKey, seqNo
}