-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch.go
162 lines (136 loc) · 3.84 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
package bitcask_go
import (
"bitcask-go/data"
"encoding/binary"
"sync"
"sync/atomic"
)
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 // 暂存用户写入的数据
}
// NewWriteBatch 初始化 WriteBatch
func (db *DB) NewWriteBatch(opts WriteBatchOptions) *WriteBatch {
if db.options.IndexType == BPlusTree && !db.seqNoFileExists && !db.isInitial {
panic("cannot use write batch, seq no file not exists")
}
return &WriteBatch{
options: opts,
mu: new(sync.Mutex),
db: db,
pendingWrites: make(map[string]*data.LogRecord),
}
}
// Put 批量写数据
func (wb *WriteBatch) Put(key []byte, value []byte) error {
if len(key) == 0 {
return ErrKeyIsEmpty
}
// 在写数据的时候,仅仅需要锁住这个WriteBatch,但是不需要锁住DB。
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.LogRecordDeleted}
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)]
var oldPos *data.LogRecordPos
if record.Type == data.LogRecordNormal {
oldPos = wb.db.index.Put(record.Key, pos)
}
if record.Type == data.LogRecordDeleted {
oldPos, _ = wb.db.index.Delete(record.Key)
}
if oldPos != nil {
wb.db.reclaimSize += int64(oldPos.Size)
}
}
// 清空暂存数据
wb.pendingWrites = make(map[string]*data.LogRecord)
return nil
}
// key+Seq Number 编码
func logRecordKeyWithSeq(key []byte, seqNo uint64) []byte {
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
}
// 解析 LogRecord 的 key,获取实际的 key 和事务序列号
func parseLogRecordKey(key []byte) ([]byte, uint64) {
seqNo, n := binary.Uvarint(key)
realKey := key[n:]
return realKey, seqNo
}