-
Notifications
You must be signed in to change notification settings - Fork 246
/
tx.go
40 lines (33 loc) · 1.01 KB
/
tx.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
package db
import (
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/iterator"
"github.com/syndtr/goleveldb/leveldb/util"
)
// LevelDBTx doesn't provide any read isolation. It allows committing all writes atomically (put/delete).
type LevelDBTx struct {
batch *leveldb.Batch
db LevelDBStorage
}
// Put adds key/value to associated batch.
func (tx LevelDBTx) Put(key, buf []byte) error {
tx.batch.Put(key, buf)
return nil
}
// Delete adds delete operation to associated batch.
func (tx LevelDBTx) Delete(key []byte) error {
tx.batch.Delete(key)
return nil
}
// Get reads from currently committed state.
func (tx LevelDBTx) Get(key []byte) ([]byte, error) {
return tx.db.Get(key)
}
// NewIterator returns iterator.Iterator that will read from currently committed state.
func (tx LevelDBTx) NewIterator(slice *util.Range) iterator.Iterator {
return tx.db.NewIterator(slice)
}
// Commit writes batch atomically.
func (tx LevelDBTx) Commit() error {
return tx.db.db.Write(tx.batch, nil)
}