-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.go
62 lines (48 loc) · 1.04 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
package storager
import (
"context"
"sync"
"github.com/weedge/xdis-storager/openkv"
)
// Batch write batch commit
type Batch struct {
store *Storager
*openkv.WriteBatch
sync.Locker
}
func NewBatch(store *Storager, wb *openkv.WriteBatch, locker sync.Locker) *Batch {
return &Batch{store: store, WriteBatch: wb, Locker: locker}
}
func (b *Batch) Commit(ctx context.Context) error {
if b.store != nil && b.store.committer != nil {
return b.store.committer.Commit(ctx, b.WriteBatch)
}
b.store.commitLock.Lock()
defer b.store.commitLock.Unlock()
return b.WriteBatch.Commit()
}
func (b *Batch) Lock() {
b.Locker.Lock()
}
func (b *Batch) Unlock() {
b.WriteBatch.Rollback()
b.Locker.Unlock()
}
func (b *Batch) Put(key []byte, value []byte) {
b.WriteBatch.Put(key, value)
}
func (b *Batch) Delete(key []byte) {
b.WriteBatch.Delete(key)
}
type dbBatchLocker struct {
l *sync.Mutex
wrLock *sync.RWMutex
}
func (l *dbBatchLocker) Lock() {
l.wrLock.RLock()
l.l.Lock()
}
func (l *dbBatchLocker) Unlock() {
l.l.Unlock()
l.wrLock.RUnlock()
}