-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.go
166 lines (128 loc) · 3.42 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
package bitcask
import (
"encoding/binary"
"sync"
"sync/atomic"
"github.com/ysoding/bitcask/data"
)
const nonTransactionSeqNo uint64 = 0
var txnFinishedKey = []byte("txn-finished")
type WriteBatch struct {
writeBatchOption
mu *sync.Mutex
db *DB
pendingWrites map[string]*data.LogRecord
}
func (db *DB) NewWriteBatch(opts ...WriteBatchOption) *WriteBatch {
b := &WriteBatch{
writeBatchOption: DefaultWriteBatchOption,
db: db,
mu: new(sync.Mutex),
pendingWrites: make(map[string]*data.LogRecord),
}
for _, opt := range opts {
opt(&b.writeBatchOption)
}
return b
}
func (wb *WriteBatch) Put(key []byte, value []byte) error {
if len(key) == 0 {
return ErrKeyIsEmpty
}
wb.mu.Lock()
defer wb.mu.Unlock()
logRecord := &data.LogRecord{Key: key, Value: value, Type: data.LogRecordNormal}
wb.pendingWrites[string(key)] = logRecord
return nil
}
func (wb *WriteBatch) Delete(key []byte) error {
if len(key) == 0 {
return ErrKeyIsEmpty
}
wb.mu.Lock()
defer wb.mu.Unlock()
logRecordPos := wb.db.indexer.Get(key)
if logRecordPos == nil {
tmp := string(key)
if wb.pendingWrites[tmp] != nil {
delete(wb.pendingWrites, tmp)
}
return nil
}
// 数据存在,已经被保存,则需要append delete log
logRecord := &data.LogRecord{Key: key, Type: data.LogRecordDeleted}
wb.pendingWrites[string(key)] = logRecord
return nil
}
func (wb *WriteBatch) Commit() error {
wb.mu.Lock()
defer wb.mu.Unlock()
if len(wb.pendingWrites) == 0 {
return nil
}
if len(wb.pendingWrites) > wb.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: logRecordKeyWithSeqNo(record.Key, seqNo),
Value: record.Value,
Type: record.Type,
})
if err != nil {
return err
}
positions[string(record.Key)] = logRecordPos
}
// 写一条标识事务完成的数据
finishedRecord := &data.LogRecord{
Key: logRecordKeyWithSeqNo(txnFinishedKey, seqNo),
Type: data.LogRecordTxnFinished,
}
if _, err := wb.db.appendLogRecord(finishedRecord); err != nil {
return err
}
// 根据配置决定是否持久化
if wb.syncWrite && 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.indexer.Put(record.Key, pos)
}
if record.Type == data.LogRecordDeleted {
oldPos, _ = wb.db.indexer.Delete(record.Key)
}
if oldPos != nil {
wb.db.reclaimSize += int64(oldPos.Size)
}
}
// 清空暂存数据
wb.pendingWrites = make(map[string]*data.LogRecord)
return nil
}
func logRecordKeyWithSeqNo(key []byte, seqNo uint64) []byte {
buf := make([]byte, binary.MaxVarintLen64)
n := binary.PutUvarint(buf, seqNo)
encKey := make([]byte, n+len(key))
copy(encKey[:n], buf[: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
}