forked from tendermint/tm-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleveldb_batch.go
82 lines (73 loc) · 1.52 KB
/
cleveldb_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
//go:build cleveldb
// +build cleveldb
package db
import "github.com/jmhodges/levigo"
// cLevelDBBatch is a LevelDB batch.
type cLevelDBBatch struct {
db *CLevelDB
batch *levigo.WriteBatch
}
func newCLevelDBBatch(db *CLevelDB) *cLevelDBBatch {
return &cLevelDBBatch{
db: db,
batch: levigo.NewWriteBatch(),
}
}
// Set implements Batch.
func (b *cLevelDBBatch) Set(key, value []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
if value == nil {
return errValueNil
}
if b.batch == nil {
return errBatchClosed
}
b.batch.Put(key, value)
return nil
}
// Delete implements Batch.
func (b *cLevelDBBatch) Delete(key []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
if b.batch == nil {
return errBatchClosed
}
b.batch.Delete(key)
return nil
}
// Write implements Batch.
func (b *cLevelDBBatch) Write() error {
if b.batch == nil {
return errBatchClosed
}
err := b.db.db.Write(b.db.wo, b.batch)
if err != nil {
return err
}
// Make sure batch cannot be used afterwards. Callers should still call Close(), for errors.
return b.Close()
}
// WriteSync implements Batch.
func (b *cLevelDBBatch) WriteSync() error {
if b.batch == nil {
return errBatchClosed
}
err := b.db.db.Write(b.db.woSync, b.batch)
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 *cLevelDBBatch) Close() error {
if b.batch != nil {
b.batch.Close()
b.batch = nil
}
return nil
}