aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/gitea.com/lunny/nodb/store/driver/batch.go
blob: 6b79c21c48d1fedeff3bd87e5be4fafa9fbeb594 (plain)
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
package driver

type BatchPuter interface {
	BatchPut([]Write) error
}

type Write struct {
	Key   []byte
	Value []byte
}

type WriteBatch struct {
	batch BatchPuter
	wb    []Write
}

func (w *WriteBatch) Put(key, value []byte) {
	if value == nil {
		value = []byte{}
	}
	w.wb = append(w.wb, Write{key, value})
}

func (w *WriteBatch) Delete(key []byte) {
	w.wb = append(w.wb, Write{key, nil})
}

func (w *WriteBatch) Commit() error {
	return w.batch.BatchPut(w.wb)
}

func (w *WriteBatch) Rollback() error {
	w.wb = w.wb[0:0]
	return nil
}

func NewWriteBatch(puter BatchPuter) IWriteBatch {
	return &WriteBatch{puter, []Write{}}
}