-
Notifications
You must be signed in to change notification settings - Fork 3
/
batch.go
208 lines (165 loc) · 3.66 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
package bond
import (
"fmt"
"io"
"github.com/cockroachdb/pebble"
)
var sequenceId = NumberSequence{}
type Committer interface {
Commit(opt WriteOptions) error
OnCommit(func(b Batch) error)
OnCommitted(func(b Batch))
OnError(func(b Batch, err error))
OnClose(func(b Batch))
}
type BatchType int
const (
BatchTypeWriteOnly BatchType = iota
BatchTypeReadWrite
)
type Batch interface {
ID() uint64
Len() int
Empty() bool
Reset()
Type() BatchType
Getter
Setter
Deleter
DeleterWithRange
Iterationer
Applier
Committer
Closer
}
type _batch struct {
*pebble.Batch
id uint64
onCommitCallbacks []func(b Batch) error
onCommittedCallbacks []func(b Batch)
onErrorCallbacks []func(b Batch, err error)
onClose []func(b Batch)
}
func newBatch(db *_db, indexed bool) Batch {
id, _ := sequenceId.Next()
if indexed {
return &_batch{
Batch: db.pebble.NewIndexedBatch(),
id: id,
}
}
return &_batch{
Batch: db.pebble.NewBatch(),
id: id,
}
}
func (b *_batch) ID() uint64 {
return b.id
}
func (b *_batch) Type() BatchType {
if b.Batch.Indexed() {
return BatchTypeReadWrite
}
return BatchTypeWriteOnly
}
func (b *_batch) Reset() {
b.Batch.Reset()
b.id, _ = sequenceId.Next()
b.onCommitCallbacks = nil
b.onCommittedCallbacks = nil
b.onErrorCallbacks = nil
b.onClose = nil
}
func (b *_batch) Get(key []byte, _ ...Batch) (data []byte, closer io.Closer, err error) {
return b.Batch.Get(key)
}
func (b *_batch) Set(key []byte, value []byte, opt WriteOptions, _ ...Batch) error {
return b.Batch.Set(key, value, pebbleWriteOptions(opt))
}
func (b *_batch) Delete(key []byte, opts WriteOptions, _ ...Batch) error {
return b.Batch.Delete(key, pebbleWriteOptions(opts))
}
func (b *_batch) DeleteRange(start []byte, end []byte, opt WriteOptions, _ ...Batch) error {
return b.Batch.DeleteRange(start, end, pebbleWriteOptions(opt))
}
func (b *_batch) Iter(opt *IterOptions, _ ...Batch) Iterator {
return newIterator(&_bondIterConstructor{pebbleConstructor: b.Batch}, opt)
}
func (b *_batch) Apply(batch Batch, opt WriteOptions) error {
innerBatch, ok := batch.(*_batch)
if !ok {
return fmt.Errorf("incorrect batch param")
}
err := innerBatch.notifyOnCommit()
if err != nil {
return err
}
defer innerBatch.notifyOnCommitted()
err = b.Batch.Apply(innerBatch.Batch, pebbleWriteOptions(opt))
if err != nil {
innerBatch.notifyOnError(err)
return err
}
return nil
}
func (b *_batch) Commit(opt WriteOptions) error {
if b.Empty() {
return nil
}
err := b.notifyOnCommit()
if err != nil {
return err
}
err = b.Batch.Commit(pebbleWriteOptions(opt))
if err != nil {
b.notifyOnError(err)
return err
}
b.notifyOnCommitted()
return nil
}
func (b *_batch) Close() error {
b.notifyOnClose()
err := b.Batch.Close()
if err != nil {
b.notifyOnError(err)
return err
}
return nil
}
func (b *_batch) OnCommit(f func(b Batch) error) {
b.onCommitCallbacks = append(b.onCommitCallbacks, f)
}
func (b *_batch) OnCommitted(f func(b Batch)) {
b.onCommittedCallbacks = append(b.onCommittedCallbacks, f)
}
func (b *_batch) OnError(f func(b Batch, err error)) {
b.onErrorCallbacks = append(b.onErrorCallbacks, f)
}
func (b *_batch) OnClose(f func(b Batch)) {
b.onClose = append(b.onClose, f)
}
func (b *_batch) notifyOnCommit() error {
for _, f := range b.onCommitCallbacks {
err := f(b)
if err != nil {
return err
}
}
return nil
}
func (b *_batch) notifyOnCommitted() {
for _, f := range b.onCommittedCallbacks {
f(b)
}
}
func (b *_batch) notifyOnError(err error) {
for _, f := range b.onErrorCallbacks {
f(b, err)
}
}
func (b *_batch) notifyOnClose() {
for _, f := range b.onCommittedCallbacks {
f(b)
}
}