-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.go
163 lines (144 loc) · 3.26 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
package godb
import (
"encoding/binary"
"sync"
"sync/atomic"
"github.com/jakub-galecki/godb/common"
)
var batchPool = sync.Pool{New: func() interface{} { return new(Batch) }}
const (
headerLen = 12 // 8 bytes for seqNum and 4 bytes for count
)
type Batch struct {
header []byte
buf []byte
committed atomic.Bool
forceFlush bool
seqNum uint64
size uint32
off uint64
}
func NewBatch() *Batch {
b := batchPool.Get().(*Batch)
b.committed = atomic.Bool{}
clear(b.buf)
clear(b.header)
b.seqNum = 0
b.size = 0
b.off = 0
return b
}
func (b *Batch) release() {
batchPool.Put(b)
}
func (b *Batch) Set(key, value []byte) *Batch {
b.add(common.SET, key, value)
b.size++
return b
}
func (b *Batch) Delete(key []byte) *Batch {
b.add(common.DELETE, key, nil)
b.size++
return b
}
func (b *Batch) add(op common.DbOp, key, value []byte) {
keyLen, valueLen := len(key), len(value)
need := 2*binary.MaxVarintLen64 + keyLen + valueLen + 1
b.grow(need)
copy(b.buf[b.off:], []byte{op})
b.off += 1
written := binary.PutUvarint(b.buf[b.off:], uint64(keyLen))
b.off += uint64(written)
copy(b.buf[b.off:], key)
b.off += uint64(keyLen)
if value == nil {
return
}
written = binary.PutUvarint(b.buf[b.off:], uint64(valueLen))
b.off += uint64(written)
copy(b.buf[b.off:], value)
b.off += uint64(valueLen)
}
func (b *Batch) grow(n int) {
nSize := n + len(b.buf)
if nSize > cap(b.buf) {
newSlice := make([]byte, (n+len(b.buf))*2)
copy(newSlice, b.buf)
b.buf = newSlice
}
b.buf = b.buf[:nSize]
}
func (b *Batch) encode() []byte {
if b.header == nil {
b.header = make([]byte, headerLen)
}
n := binary.PutUvarint(b.header, b.seqNum)
binary.LittleEndian.PutUint32(b.header[n:], b.size)
return append(b.header, b.buf[:b.off]...)
}
func (b *Batch) decodeHeader(raw []byte) {
var read int
if len(raw) < headerLen {
panic("cannot decode as slice is smaller than header length")
}
header := raw[:headerLen]
b.seqNum, read = binary.Uvarint(header)
b.size = binary.LittleEndian.Uint32(header[read:])
}
func (b *Batch) Size() int {
return int(b.size)
}
func (b *Batch) decode(raw []byte) {
b.decodeHeader(raw)
b.buf = raw[headerLen:]
b.off = 0
}
func DecodeBatch(raw []byte) *Batch {
b := NewBatch()
b.decode(raw)
b.off = uint64(len(b.buf))
return b
}
type batchIter struct {
off uint64
total uint64
buf []byte
seqNum uint64
}
func (b *Batch) Iter() *batchIter {
return &batchIter{
off: 0,
total: b.off,
buf: b.buf[:b.off],
seqNum: b.seqNum,
}
}
func (b *batchIter) Next() (common.DbOp, uint64, []byte, []byte) {
if b.off >= uint64(len(b.buf)) || b.off >= b.total {
return 0, 0, nil, nil
}
op := b.buf[b.off]
seq := b.seqNum
b.seqNum++
if op == common.DELETE {
b.off += 1
keyLen, read := binary.Uvarint(b.buf[b.off:])
b.off += uint64(read)
key := make([]byte, keyLen)
copy(key, b.buf[b.off:b.off+keyLen])
b.off += keyLen
return op, seq, key, nil
}
b.off += 1
keyLen, read := binary.Uvarint(b.buf[b.off:])
b.off += uint64(read)
key := make([]byte, keyLen)
copy(key, b.buf[b.off:b.off+keyLen])
b.off += keyLen
valueLen, read := binary.Uvarint(b.buf[b.off:])
b.off += uint64(read)
value := make([]byte, valueLen)
copy(value, b.buf[b.off:b.off+valueLen])
b.off += valueLen
return op, seq, key, value
}