-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.go
239 lines (208 loc) · 5.14 KB
/
wrapper.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package dbwrapper
import (
"github.com/dgraph-io/badger"
"github.com/dgraph-io/badger/options"
)
// helpers
func handle(err error) {
if err != nil {
errorHandleHelper(err)
}
}
func errorHandleHelper(err error) error {
return err
}
var opened *badger.DB
var dbhelper int
// SimpleDBW database struct for use in view, update etc methods
type SimpleDBW struct {
Badger *badger.DB
}
// NewSimpleDBW passing the database to android
func NewSimpleDBW() *SimpleDBW {
db := &opened
ret := &SimpleDBW{*db}
return ret
}
// OpenDB package function for initialising the database
func OpenDB(dir string) {
if dbhelper == 0 {
opts := badger.DefaultOptions
opts.Dir = dir
opts.ValueDir = dir
// badger memory usage optimisation
opts.ValueLogLoadingMode = options.FileIO
opts.TableLoadingMode = options.FileIO
opts.NumLevelZeroTables = 3
opts.NumLevelZeroTablesStall = 6
opts.NumMemtables = 3
opening, err := badger.Open(opts)
handle(err)
//db.Badger = opening
opened = opening
dbhelper = 1
}
}
// CloseDB package function for closing the database
func CloseDB() {
// check if database is open
if dbhelper == 1 {
db := &opened
dbw := *db
err := dbw.Close()
handle(err)
dbhelper = 0
}
}
// Update method for updating a key
func (db *SimpleDBW) Update(key, value []byte) {
err := db.Badger.Update(func(txn *badger.Txn) error {
err := txn.Set(key, value)
return err
})
handle(err)
}
// PrePopulate database function
func (db *SimpleDBW) PrePopulate(key, value []byte) {
init := db.View(key)
inittoString := string(init)
if inittoString == "key does not exist" {
db.Update(key, value)
}
}
// View method for viewing a key
func (db *SimpleDBW) View(key []byte) []byte {
var varDBentry []byte
// badger view entry function
db.Badger.View(func(txn *badger.Txn) error {
item, errGET := txn.Get([]byte(key))
if errGET != nil {
varDBentry = []byte("key does not exist")
return errGET
}
errValue := item.Value(func(val []byte) error {
varDBentry = append([]byte{}, val...)
return nil
})
handle(errValue)
return nil
})
return varDBentry
}
// Delete method for deleting a key
func (db *SimpleDBW) Delete(key []byte) {
db.Badger.Update(func(txn *badger.Txn) error {
err := txn.Delete(key)
handle(err)
return nil
})
}
// DropPrefix method for droping all keys related to specific prefix
func (db *SimpleDBW) DropPrefix(prefix []byte) {
err := db.Badger.DropPrefix(prefix)
handle(err)
}
// DropAll method for droping the whole database
func (db *SimpleDBW) DropAll() {
err := db.Badger.DropAll()
handle(err)
}
var store = make(map[int][]byte)
func (db *SimpleDBW) KeyOnlyIterator() int {
var i int
err := db.Badger.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false
it := txn.NewIterator(opts)
defer it.Close()
for it.Rewind(); it.Valid(); it.Next() {
item := it.Item()
k := item.Key()
store[i] = append([]byte{}, k...)
i++
}
return nil
})
handle(err)
return i
}
func (db *SimpleDBW) KeyOnlyIteratorPrefix(prefix []byte) int {
var i int
err := db.Badger.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false
it := txn.NewIterator(opts)
defer it.Close()
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
item := it.Item()
k := item.Key()
store[i] = append([]byte{}, k...)
i++
}
return nil
})
handle(err)
return i
}
func Dump(i int) []byte {
key := store[i]
return key
}
// IncInt method for monotonically increasing integers - ie. notifications id
func (db *SimpleDBW) IncInt(key []byte) int {
seq, errGetSequence := db.Badger.GetSequence([]byte(key), 1000)
defer seq.Release()
handle(errGetSequence)
num, errNext := seq.Next()
handle(errNext)
return int(num)
}
/*
// TTL Time To Live database keys with specified lifetime
func (db *SimpleDBW) Ttl(key, value, expiration string) {
expi, errParse := time.ParseDuration(expiration)
handle(errParse)
// Start a writable transaction.
txn := db.Badger.NewTransaction(true)
defer txn.Discard()
// Use the transaction...
errTTL := txn.SetWithTTL([]byte(key), []byte(value), expi)
handle(errTTL)
// Commit the transaction and check for error.
errCommit := txn.Commit()
handle(errCommit)
}
// Backup database
func (db *SimpleDBW) Backup(dir string) {
var since uint64
timestamp := db.View("LATEST_BACKUP_TIMESTAMP")
//f, errCreate := os.Create(path.Join(dir, "database.bak"))
f, errCreate := os.Create(dir)
defer f.Close()
handle(errCreate)
w := bufio.NewWriter(f)
if timestamp == "key does not exist" {
now := time.Now()
since = uint64(now.Unix())
} else {
u, errParse := strconv.ParseUint(timestamp, 10, 64)
handle(errParse)
since = u
}
newTimestamp, errTimeStamp := db.Badger.Backup(w, since)
handle(errTimeStamp)
s := strconv.FormatUint(newTimestamp, 10)
db.Update("LATEST_BACKUP_TIMESTAMP", s)
}
// Restore database
func (db *SimpleDBW) Restore(dir string) {
//f, erroOpen := os.Open(path.Join(dir, "database.bak"))
f, erroOpen := os.Open(dir)
defer f.Close()
handle(erroOpen)
r := bufio.NewReader(f)
errLoad := db.Badger.Load(r)
handle(errLoad)
}
*/
// TODO for v1.1 Stream implementation