-
Notifications
You must be signed in to change notification settings - Fork 201
/
batch.go
66 lines (54 loc) · 1.34 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
package leveldb
import (
"sync"
"github.com/ElrondNetwork/elrond-go/storage"
"github.com/syndtr/goleveldb/leveldb"
)
var _ storage.Batcher = (*batch)(nil)
const removed = "removed"
type batch struct {
batch *leveldb.Batch
cachedData map[string][]byte
mutBatch sync.RWMutex
}
// NewBatch creates a batch
func NewBatch() *batch {
return &batch{
batch: &leveldb.Batch{},
cachedData: make(map[string][]byte),
mutBatch: sync.RWMutex{},
}
}
// Put inserts one entry - key, value pair - into the batch
func (b *batch) Put(key []byte, val []byte) error {
b.mutBatch.Lock()
b.batch.Put(key, val)
b.cachedData[string(key)] = val
b.mutBatch.Unlock()
return nil
}
// Delete deletes the entry for the provided key from the batch
func (b *batch) Delete(key []byte) error {
b.mutBatch.Lock()
b.batch.Delete(key)
b.cachedData[string(key)] = []byte(removed)
b.mutBatch.Unlock()
return nil
}
// Reset clears the contents of the batch
func (b *batch) Reset() {
b.mutBatch.Lock()
b.batch.Reset()
b.cachedData = make(map[string][]byte)
b.mutBatch.Unlock()
}
// Get returns the value
func (b *batch) Get(key []byte) []byte {
b.mutBatch.RLock()
defer b.mutBatch.RUnlock()
return b.cachedData[string(key)]
}
// IsInterfaceNil returns true if there is no value under the interface
func (b *batch) IsInterfaceNil() bool {
return b == nil
}