-
Notifications
You must be signed in to change notification settings - Fork 3
/
batch.go
305 lines (247 loc) · 5.96 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
package nitro
import (
"bytes"
"fmt"
"github.com/t3rm1n4l/nitro/skiplist"
"unsafe"
)
const blockSize = 4096
type itemOp int
const (
itemDeleteOp itemOp = iota
itemInsertop
)
type diskWriter struct {
shard int
w *Writer
rbuf, wbuf []byte
stats BatchOpStats
}
type BatchOpStats struct {
BlocksWritten int64
BlocksRemoved int64
ItemsInserted int64
ItemsWritten int64
ItemsRemoved int64
}
func (b BatchOpStats) String() string {
return fmt.Sprintf(
"blocks_written = %d\n"+
"blocks_removed = %d\n"+
"items_inserted = %d\n"+
"items_written = %d\n"+
"items_removed = %d",
b.BlocksWritten, b.BlocksRemoved, b.ItemsInserted, b.ItemsWritten,
b.ItemsRemoved)
}
func (r *BatchOpStats) ApplyDiff(a, b BatchOpStats) {
r.BlocksWritten += a.BlocksWritten - b.BlocksWritten
r.BlocksRemoved += a.BlocksRemoved - b.BlocksRemoved
r.ItemsInserted += a.ItemsInserted - b.ItemsInserted
r.ItemsWritten += a.ItemsWritten - b.ItemsWritten
r.ItemsRemoved += a.ItemsRemoved - b.ItemsRemoved
}
func (m *Nitro) newDiskWriter(shard int) *diskWriter {
return &diskWriter{
rbuf: make([]byte, blockSize),
wbuf: make([]byte, blockSize),
w: m.NewWriter(),
shard: shard,
}
}
type nodeOpIterator struct {
*Iterator
}
func NewOpIterator(itr *Iterator) BatchOpIterator {
it := &nodeOpIterator{
Iterator: itr,
}
return it
}
func (it *nodeOpIterator) Item() unsafe.Pointer {
return it.Iterator.GetNode().Item()
}
func (it *nodeOpIterator) Next() {
it.Iterator.Next()
}
func (it *nodeOpIterator) Op() itemOp {
itm := (*Item)(it.Iterator.GetNode().Item())
if itm.bornSn != 0 {
return itemInsertop
} else {
return itemDeleteOp
}
}
func (it *nodeOpIterator) Close() {
it.Iterator.Close()
}
type BatchOpIterator interface {
skiplist.BatchOpIterator
Op() itemOp
Close()
}
func (dw *diskWriter) batchModifyCallback(n *skiplist.Node, cmp skiplist.CompareFn,
maxItem unsafe.Pointer, sOpItr skiplist.BatchOpIterator) error {
var err error
var indexItem []byte
var db *dataBlock
opItr := sOpItr.(BatchOpIterator)
if n.Item() != skiplist.MinItem {
dw.w.DeleteNode(n)
dw.stats.BlocksRemoved++
err := dw.w.bm.ReadBlock(blockPtr(n.DataPtr), dw.rbuf)
if err != nil {
return err
}
db = newDataBlock(dw.rbuf)
}
wblock := newDataBlock(dw.wbuf)
flushBlock := func() error {
bptr, err := dw.w.bm.WriteBlock(wblock.Bytes(), dw.shard)
if err == nil {
indexNode := dw.w.Put2(indexItem)
if indexNode == nil {
panic("index node creation should not fail")
}
indexNode.DataPtr = uint64(bptr)
wblock.Reset()
dw.stats.BlocksWritten++
}
return err
}
doWriteItem := func(itm []byte) error {
if indexItem == nil {
indexItem = itm
}
dw.stats.ItemsWritten++
if err := wblock.Write(itm); err == errBlockFull {
if err := flushBlock(); err != nil {
return err
}
indexItem = itm
return wblock.Write(itm)
}
return nil
}
var nItm []byte
for nItm = db.Get(); err == nil && opItr.Valid() &&
skiplist.Compare(cmp, opItr.Item(), maxItem) < 0 && nItm != nil; {
opItm := (*Item)(opItr.Item()).Bytes()
cmpval := bytes.Compare(nItm, opItm)
switch {
case cmpval < 0:
err = doWriteItem(nItm)
nItm = db.Get()
break
case cmpval == 0:
if opItr.Op() == itemInsertop {
err = doWriteItem(opItm)
} else {
dw.stats.ItemsRemoved++
}
opItr.Next()
nItm = db.Get()
break
default:
if opItr.Op() == itemInsertop {
err = doWriteItem(opItm)
dw.stats.ItemsInserted++
opItr.Next()
}
}
}
for ; err == nil && opItr.Valid() &&
skiplist.Compare(cmp, opItr.Item(), maxItem) < 0; opItr.Next() {
if opItr.Op() == itemInsertop {
opItm := (*Item)(opItr.Item()).Bytes()
err = doWriteItem(opItm)
dw.stats.ItemsInserted++
}
}
for ; err == nil && nItm != nil; nItm = db.Get() {
err = doWriteItem(nItm)
}
if err != nil {
return err
}
if !wblock.IsEmpty() {
return flushBlock()
}
return nil
}
type batchOpIterator struct {
db *Nitro
BatchOpIterator
itm unsafe.Pointer
}
func (it *batchOpIterator) fillItem() {
srcItm := (*Item)(it.BatchOpIterator.Item())
l := len(srcItm.Bytes())
dstItm := it.db.allocItem(l, false)
copy(dstItm.Bytes(), srcItm.Bytes())
dstItm.bornSn = it.db.getCurrSn()
it.itm = unsafe.Pointer(dstItm)
}
func (it *batchOpIterator) Next() {
it.BatchOpIterator.Next()
if it.BatchOpIterator.Valid() {
it.fillItem()
}
}
func (it *batchOpIterator) Item() unsafe.Pointer {
return it.itm
}
func isValidNode(n *skiplist.Node) bool {
itm := n.Item()
// TODO: move this check to skiplist module
if itm != skiplist.MaxItem {
return (*Item)(itm).deadSn == 0
}
return true
}
func (m *Nitro) newBatchOpIterator(it *Iterator) BatchOpIterator {
bItr := &batchOpIterator{
db: m,
BatchOpIterator: NewOpIterator(it),
}
if bItr.Valid() {
bItr.fillItem()
}
return bItr
}
func (m *Nitro) ApplyOps(snap *Snapshot, concurr int) (BatchOpStats, error) {
var err error
var stats BatchOpStats
w := m.NewWriter()
currSnap := &Snapshot{db: m, sn: m.getCurrSn(), refCount: 1}
pivots := m.partitionPivots(currSnap, concurr)
beforeStats := make([]BatchOpStats, len(pivots)-1)
errors := make([]chan error, len(pivots)-1)
for i := 0; i < len(pivots)-1; i++ {
errors[i] = make(chan error, 1)
beforeStats[i] = m.shardWrs[i].stats
itr := snap.NewIterator()
itr.Seek(pivots[i].Bytes())
itr.SetEnd(pivots[i+1].Bytes())
opItr := m.newBatchOpIterator(itr)
defer opItr.Close()
head := w.GetNode(pivots[i].Bytes())
tail := w.GetNode(pivots[i+1].Bytes())
if pivots[i] == nil {
head = nil
}
if pivots[i+1] == nil {
tail = nil
}
go func(id int, opItr BatchOpIterator, head, tail *skiplist.Node) {
errors[id] <- m.store.ExecBatchOps(opItr, head, tail, m.shardWrs[id].batchModifyCallback, m.insCmp, isValidNode, &m.store.Stats)
}(i, opItr, head, tail)
}
for i := 0; i < len(pivots)-1; i++ {
if e := <-errors[i]; e != nil {
err = e
}
stats.ApplyDiff(m.shardWrs[i].stats, beforeStats[i])
}
return stats, err
}