-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldb.go
152 lines (116 loc) · 2.61 KB
/
ldb.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package ldb
import (
"bytes"
"encoding/gob"
"log"
"github.com/spf13/cast"
"github.com/syndtr/goleveldb/leveldb"
leveldb_errors "github.com/syndtr/goleveldb/leveldb/errors"
)
var ldb_conn *DB
type Config struct {
Datadir string
ErrorOnNotFound bool
}
type DB struct {
conn *leveldb.DB
config *Config
}
func NewLdb(config *Config) (*DB, error) {
if ldb_conn != nil {
return ldb_conn, nil
}
conn, err := leveldb.OpenFile(config.Datadir, nil)
if err != nil {
return nil, err
}
db := &DB{
conn: conn,
config: config,
}
ldb_conn = db
return db, nil
}
func (this *DB) Close() {
if this.conn != nil {
if err := this.conn.Close(); err != nil {
log.Println(err)
}
this.conn = nil
}
}
func (this *DB) Delete(key string) error {
return this.conn.Delete([]byte(key), nil)
}
// TODO too many get/sets's... consolidate
func (this *DB) GetObj(key string, value interface{}) error {
v, err := this.conn.Get([]byte(key), nil)
if err != nil {
if err == leveldb_errors.ErrNotFound && !this.config.ErrorOnNotFound {
return nil
}
return err
}
if v != nil {
d := gob.NewDecoder(bytes.NewReader(v))
return d.Decode(value)
}
return nil
}
func (this *DB) SetObj(key string, value interface{}) error {
if value == nil {
return leveldb_errors.ErrNotFound
}
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(value); err != nil {
return err
}
return this.conn.Put([]byte(key), buf.Bytes(), nil)
}
func (this *DB) Get(key string) (string, error) {
value, err := this.conn.Get([]byte(key), nil)
if err != nil {
if err == leveldb_errors.ErrNotFound && !this.config.ErrorOnNotFound {
return "", nil
}
return "", err
}
if value != nil {
return string(value), nil
}
return "", nil
}
func (this *DB) GetInt(key string) (int, error) {
value, err := this.Get(key)
if err != nil {
return 0, err
}
return cast.ToInt(value), nil
}
func (this *DB) GetInt64(key string) (int64, error) {
value, err := this.Get(key)
if err != nil {
return 0, err
}
return cast.ToInt64(value), nil
}
func (this *DB) Set(key string, value string) error {
err := this.conn.Put([]byte(key), []byte(value), nil)
return err
}
func (this *DB) SetInt64(key string, value int64) error {
return this.Set(key, cast.ToString(value))
}
func (this *DB) SetInt(key string, value int) error {
return this.Set(key, cast.ToString(value))
}
func (this *DB) List() (map[string]string, error) {
list := make(map[string]string)
iter := this.conn.NewIterator(nil, nil)
for iter.Next() {
list[string(iter.Key())] = string(iter.Value())
}
iter.Release()
err := iter.Error()
return list, err
}