-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_leveldb.go
69 lines (55 loc) · 1.27 KB
/
store_leveldb.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
67
68
69
package main
import (
"fmt"
"github.com/syndtr/goleveldb/leveldb"
)
var _ Store = &LevelDB{}
type LevelDB struct {
db *leveldb.DB
}
func NewLevelDB(path string) (*LevelDB, error) {
db, err := leveldb.OpenFile(path, nil)
return &LevelDB{db}, err
}
func (store *LevelDB) Close() error {
return store.db.Close()
}
func (store *LevelDB) Get(txhash Hash) ([]byte, error) {
data, err := store.db.Get(txhash[:], nil)
if err == leveldb.ErrNotFound {
err = ErrNotFound
}
return data, err
}
func (store *LevelDB) List(fn func(Hash, []byte) error) error {
iter := store.db.NewIterator(nil, nil)
defer iter.Release()
for iter.Next() {
key, value := iter.Key(), iter.Value()
var txhash Hash
if n := copy(txhash[:], key); n != HashLength {
return ErrInvalidHash
}
if err := fn(txhash, value); err != nil {
return err
}
}
return iter.Error()
}
func (store *LevelDB) Put(txhash Hash, data []byte) error {
tx, err := store.db.OpenTransaction()
if err != nil {
return fmt.Errorf("Problem starting DB transaction: %v", err)
}
defer tx.Discard()
_, err = tx.Get(txhash[:], nil)
if err == nil {
return ErrExists
} else if err != leveldb.ErrNotFound {
return err
}
if err := tx.Put(txhash[:], data, nil); err != nil {
return err
}
return tx.Commit()
}