-
Notifications
You must be signed in to change notification settings - Fork 182
/
goleveldb_batch.go
67 lines (56 loc) · 1.26 KB
/
goleveldb_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
package db
import (
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
)
type goLevelDBBatch struct {
db *GoLevelDB
batch *leveldb.Batch
}
var _ Batch = (*goLevelDBBatch)(nil)
func newGoLevelDBBatch(db *GoLevelDB) *goLevelDBBatch {
return &goLevelDBBatch{
db: db,
batch: new(leveldb.Batch),
}
}
func (b *goLevelDBBatch) assertOpen() {
if b.batch == nil {
panic("batch has been written or closed")
}
}
// Set implements Batch.
func (b *goLevelDBBatch) Set(key, value []byte) {
b.assertOpen()
b.batch.Put(key, value)
}
// Delete implements Batch.
func (b *goLevelDBBatch) Delete(key []byte) {
b.assertOpen()
b.batch.Delete(key)
}
// Write implements Batch.
func (b *goLevelDBBatch) Write() error {
return b.write(false)
}
// WriteSync implements Batch.
func (b *goLevelDBBatch) WriteSync() error {
return b.write(true)
}
func (b *goLevelDBBatch) write(sync bool) error {
b.assertOpen()
err := b.db.db.Write(b.batch, &opt.WriteOptions{Sync: sync})
if err != nil {
return err
}
// Make sure batch cannot be used afterwards. Callers should still call Close(), for errors.
b.Close()
return nil
}
// Close implements Batch.
func (b *goLevelDBBatch) Close() {
if b.batch != nil {
b.batch.Reset()
b.batch = nil
}
}