forked from deepkaran/goforestdb
-
Notifications
You must be signed in to change notification settings - Fork 6
/
file.go
257 lines (220 loc) · 7.16 KB
/
file.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package forestdb
// Copyright (c) 2014 Couchbase, Inc.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
//#include <stdlib.h>
//#include <libforestdb/forestdb.h>
import "C"
import (
"reflect"
"unsafe"
)
// Database handle
type File struct {
dbfile *C.fdb_file_handle
}
// Init initializes forestdb library
func Init(config *Config) error {
if config == nil {
config = DefaultConfig()
}
errNo := C.fdb_init(config.config)
if errNo != RESULT_SUCCESS {
return Error(errNo)
}
return nil
}
// Open opens the database with a given file name
func Open(filename string, config *Config) (*File, error) {
if config == nil {
config = DefaultConfig()
}
dbname := C.CString(filename)
defer C.free(unsafe.Pointer(dbname))
rv := File{}
Log.Tracef("fdb_open call rv:%p dbname:%v conf:%v", &rv, dbname, config.config)
errNo := C.fdb_open(&rv.dbfile, dbname, config.config)
Log.Tracef("fdb_open ret rv:%p errNo:%v rv:%v", &rv, errNo, rv)
if errNo != RESULT_SUCCESS {
return nil, Error(errNo)
}
return &rv, nil
}
// Options to be passed to Commit()
type CommitOpt uint8
const (
// Perform commit without any options.
COMMIT_NORMAL CommitOpt = 0x00
// Manually flush WAL entries even though it doesn't reach the configured threshol
COMMIT_MANUAL_WAL_FLUSH CommitOpt = 0x01
)
// Commit all pending changes into disk.
func (f *File) Commit(opt CommitOpt) error {
Log.Tracef("fdb_commit call f:%p dbfile:%p opt:%v", f, f.dbfile, opt)
errNo := C.fdb_commit(f.dbfile, C.fdb_commit_opt_t(opt))
Log.Tracef("fdb_commit retn f:%p errNo:%v", f, errNo)
if errNo != RESULT_SUCCESS {
return Error(errNo)
}
return nil
}
// Compact the current database file and create a new compacted file
func (f *File) Compact(newfilename string) error {
fn := C.CString(newfilename)
defer C.free(unsafe.Pointer(fn))
Log.Tracef("fdb_compact call f:%p dbfile:%p fn:%v", f, f.dbfile, fn)
errNo := C.fdb_compact(f.dbfile, fn)
Log.Tracef("fdb_compact retn f:%p errNo:%v", f, errNo)
if errNo != RESULT_SUCCESS {
return Error(errNo)
}
return nil
}
// CompactUpto compacts the current database file upto given snapshot marker
//and creates a new compacted file
func (f *File) CompactUpto(newfilename string, sm *SnapMarker) error {
fn := C.CString(newfilename)
defer C.free(unsafe.Pointer(fn))
Log.Tracef("fdb_compact_upto call f:%p dbfile:%p fn:%v marker:%v", f, f.dbfile, fn, sm.marker)
errNo := C.fdb_compact_upto(f.dbfile, fn, sm.marker)
Log.Tracef("fdb_compact_upto retn f:%p errNo:%v", f, errNo)
if errNo != RESULT_SUCCESS {
return Error(errNo)
}
return nil
}
// EstimateSpaceUsed returns the overall disk space actively used by the current database file
func (f *File) EstimateSpaceUsed() int {
Log.Tracef("fdb_estimate_space_used call f:%p dbfile:%p", f, f.dbfile)
rv := int(C.fdb_estimate_space_used(f.dbfile))
Log.Tracef("fdb_estimate_space_used retn f:%p rv:%v", f, rv)
return rv
}
// DbInfo returns the information about a given database handle
func (f *File) Info() (*FileInfo, error) {
rv := FileInfo{}
Log.Tracef("fdb_get_file_info call f:%p dbfile:%p", f, f.dbfile)
errNo := C.fdb_get_file_info(f.dbfile, &rv.info)
Log.Tracef("fdb_get_file_info retn f:%p errNo:%v, info:%v", f, errNo, rv.info)
if errNo != RESULT_SUCCESS {
return nil, Error(errNo)
}
return &rv, nil
}
// SwitchCompactionMode changes the compaction mode of a ForestDB file
// if the mode is changed to COMPACT_AUTO the compaction
// threshold is set to the threshold passed to this API
func (f *File) SwitchCompactionMode(mode CompactOpt, threshold int) error {
errNo := C.fdb_switch_compaction_mode(f.dbfile, C.fdb_compaction_mode_t(mode), C.size_t(threshold))
if errNo != RESULT_SUCCESS {
return Error(errNo)
}
return nil
}
// Close the database file
func (f *File) Close() error {
Log.Tracef("fdb_close call f:%p dbfile:%p", f, f.dbfile)
errNo := C.fdb_close(f.dbfile)
Log.Tracef("fdb_close retn f:%p errNo:%v", f, errNo)
if errNo != RESULT_SUCCESS {
return Error(errNo)
}
return nil
}
// OpenKVStore opens the named KVStore within the File
// using the provided KVStoreConfig. If config is
// nil the DefaultKVStoreConfig() will be used.
func (f *File) OpenKVStore(name string, config *KVStoreConfig) (*KVStore, error) {
if config == nil {
config = DefaultKVStoreConfig()
}
rv := KVStore{
f: f,
name: name,
}
kvsname := C.CString(name)
defer C.free(unsafe.Pointer(kvsname))
Log.Tracef("fdb_kvs_open call f:%p dbfile:%p kvsname:%v config:%v", f, f.dbfile, kvsname, config.config)
errNo := C.fdb_kvs_open(f.dbfile, &rv.db, kvsname, config.config)
Log.Tracef("fdb_kvs_open retn f:%p errNo:%v db:%p rv:%p", f, errNo, rv.db, &rv)
if errNo != RESULT_SUCCESS {
return nil, Error(errNo)
}
return &rv, nil
}
// OpenKVStore opens the default KVStore within the File
// using the provided KVStoreConfig. If config is
// nil the DefaultKVStoreConfig() will be used.
func (f *File) OpenKVStoreDefault(config *KVStoreConfig) (*KVStore, error) {
return f.OpenKVStore("default", config)
}
func (f *File) GetKVStoreNames() ([]string, error) {
var ninfo C.fdb_kvs_name_list
errNo := C.fdb_get_kvs_name_list(f.dbfile, &ninfo)
if errNo != RESULT_SUCCESS {
return nil, Error(errNo)
}
size := int(ninfo.num_kvs_names)
hdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(ninfo.kvs_names)),
Len: size,
Cap: size,
}
all := *(*[]*C.char)(unsafe.Pointer(&hdr))
rv := make([]string, size)
for i := 0; i < size; i++ {
rv[i] = C.GoString(all[i])
}
C.fdb_free_kvs_name_list(&ninfo)
if errNo != RESULT_SUCCESS {
return nil, Error(errNo)
}
return rv, nil
}
// Destroy destroys all resources associated with a ForestDB file permanently
func Destroy(filename string, config *Config) error {
if config == nil {
config = DefaultConfig()
}
dbname := C.CString(filename)
defer C.free(unsafe.Pointer(dbname))
Log.Tracef("fdb_destroy call dbname:%v config:%v", dbname, config.config)
errNo := C.fdb_destroy(dbname, config.config)
Log.Tracef("fdb_destroy retn dbname:%v errNo:%v", dbname, errNo)
if errNo != RESULT_SUCCESS {
return Error(errNo)
}
return nil
}
// CloseFileKVStore closes the KVStore and its associated forestdb file db.
func CloseFileKVStore(kvs *KVStore) error {
f := kvs.File()
err0 := kvs.Close()
err1 := f.Close()
if err0 != nil {
return err0
}
return err1
}
// OpenFileKVStore opens up a forestdb file db and a single KVStore
// inside that file/db.
func OpenFileKVStore(fileName string, config *Config,
kvstoreName string, kvstoreConfig *KVStoreConfig) (
*KVStore, error) {
db, err := Open(fileName, config)
if err != nil {
return nil, err
}
kvs, err := db.OpenKVStore(kvstoreName, kvstoreConfig)
if err != nil {
// close the db file we just opened
db.Close()
return nil, err
}
return kvs, nil
}