File: batch.go

package info (click to toggle)
golang-github-lunny-nodb 0.0~git20160621.0.fc1ef06-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 424 kB
  • sloc: python: 259; sh: 50; makefile: 3
file content (39 lines) | stat: -rw-r--r-- 633 bytes parent folder | download | duplicates (2)
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{}}
}