-
Notifications
You must be signed in to change notification settings - Fork 1
/
tidesdb.go
280 lines (234 loc) · 7.45 KB
/
tidesdb.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Package tidesdb_go
// Copyright (C) TidesDB
//
// Original Author: Alex Gaetano Padula
//
// Licensed under the Mozilla Public License, v. 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
//
// https://www.mozilla.org/en-US/MPL/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.
package tidesdb_go
/*
#include <tidesdb.h>
*/
import "C"
import (
"errors"
"unsafe"
)
// TidesDB represents a TidesDB instance.
type TidesDB struct {
tdb *C.tidesdb_t
}
// Cursor represents a TidesDB cursor.
type Cursor struct {
cursor *C.tidesdb_cursor_t
}
// Transaction represents a TidesDB transaction.
type Transaction struct {
txn *C.tidesdb_txn_t
}
// Open opens a TidesDB instance.
func Open(directory string) (*TidesDB, error) {
cDir := C.CString(directory)
defer C.free(unsafe.Pointer(cDir))
var tdb *C.tidesdb_t
err := C.tidesdb_open(cDir, &tdb)
if err != nil {
return nil, errors.New(C.GoString(err.message))
}
return &TidesDB{tdb: tdb}, nil
}
// Close closes a TidesDB instance.
func (db *TidesDB) Close() error {
err := C.tidesdb_close(db.tdb)
if err != nil {
return errors.New(C.GoString(err.message))
}
return nil
}
// CreateColumnFamily creates a new column family.
func (db *TidesDB) CreateColumnFamily(name string, flushThreshold, maxLevel int, probability float32, compressed bool, compressAlgo int, bloomFilter bool) error {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
err := C.tidesdb_create_column_family(db.tdb, cName, C.int(flushThreshold), C.int(maxLevel), C.float(probability), C.bool(compressed), C.tidesdb_compression_algo_t(compressAlgo), C.bool(bloomFilter))
if err != nil {
return errors.New(C.GoString(err.message))
}
return nil
}
// DropColumnFamily drops a column family and all associated data.
func (db *TidesDB) DropColumnFamily(name string) error {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
err := C.tidesdb_drop_column_family(db.tdb, cName)
if err != nil {
return errors.New(C.GoString(err.message))
}
return nil
}
// CompactSSTables pairs and merges SSTables in a column family.
func (db *TidesDB) CompactSSTables(columnFamilyName string, maxThreads int) error {
cName := C.CString(columnFamilyName)
defer C.free(unsafe.Pointer(cName))
err := C.tidesdb_compact_sstables(db.tdb, cName, C.int(maxThreads))
if err != nil {
return errors.New(C.GoString(err.message))
}
return nil
}
// Put puts a key-value pair into TidesDB.
func (db *TidesDB) Put(columnFamilyName string, key, value []byte, ttl int64) error {
cfName := C.CString(columnFamilyName)
defer C.free(unsafe.Pointer(cfName))
cKey := (*C.uint8_t)(unsafe.Pointer(&key[0]))
cValue := (*C.uint8_t)(unsafe.Pointer(&value[0]))
err := C.tidesdb_put(db.tdb, cfName, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)), C.time_t(ttl))
if err != nil {
return errors.New(C.GoString(err.message))
}
return nil
}
// Get gets a value from TidesDB.
func (db *TidesDB) Get(columnFamilyName string, key []byte) ([]byte, error) {
cfName := C.CString(columnFamilyName)
defer C.free(unsafe.Pointer(cfName))
cKey := (*C.uint8_t)(unsafe.Pointer(&key[0]))
var cValue *C.uint8_t
var cValueSize C.size_t
err := C.tidesdb_get(db.tdb, cfName, cKey, C.size_t(len(key)), &cValue, &cValueSize)
if err != nil {
return nil, errors.New(C.GoString(err.message))
}
value := C.GoBytes(unsafe.Pointer(cValue), C.int(cValueSize))
return value, nil
}
// Delete deletes a key-value pair from TidesDB.
func (db *TidesDB) Delete(columnFamilyName string, key []byte) error {
cfName := C.CString(columnFamilyName)
defer C.free(unsafe.Pointer(cfName))
cKey := (*C.uint8_t)(unsafe.Pointer(&key[0]))
err := C.tidesdb_delete(db.tdb, cfName, cKey, C.size_t(len(key)))
if err != nil {
return errors.New(C.GoString(err.message))
}
return nil
}
// ListColumnFamilies lists the column families in TidesDB.
func (db *TidesDB) ListColumnFamilies() (string, error) {
cfList := C.tidesdb_list_column_families(db.tdb)
if cfList == nil {
return "", errors.New("failed to list column families")
}
return C.GoString(cfList), nil
}
// CursorInit initializes a new TidesDB cursor.
func (db *TidesDB) CursorInit(columnFamily string) (*Cursor, error) {
cfName := C.CString(columnFamily)
defer C.free(unsafe.Pointer(cfName))
var cursor *C.tidesdb_cursor_t
err := C.tidesdb_cursor_init(db.tdb, cfName, &cursor)
if err != nil {
return nil, errors.New(C.GoString(err.message))
}
return &Cursor{cursor: cursor}, nil
}
// Next moves the cursor to the next key-value pair.
func (c *Cursor) Next() error {
err := C.tidesdb_cursor_next(c.cursor)
if err != nil {
return errors.New(C.GoString(err.message))
}
return nil
}
// Prev moves the cursor to the previous key-value pair.
func (c *Cursor) Prev() error {
err := C.tidesdb_cursor_prev(c.cursor)
if err != nil {
return errors.New(C.GoString(err.message))
}
return nil
}
// Get gets the current key-value pair from the cursor.
func (c *Cursor) Get() ([]byte, []byte, error) {
var cKey *C.uint8_t
var cKeySize C.size_t
var cValue *C.uint8_t
var cValueSize C.size_t
err := C.tidesdb_cursor_get(c.cursor, &cKey, &cKeySize, &cValue, &cValueSize)
if err != nil {
return nil, nil, errors.New(C.GoString(err.message))
}
key := C.GoBytes(unsafe.Pointer(cKey), C.int(cKeySize))
value := C.GoBytes(unsafe.Pointer(cValue), C.int(cValueSize))
return key, value, nil
}
// Free frees the memory for the cursor.
func (c *Cursor) Free() error {
err := C.tidesdb_cursor_free(c.cursor)
if err != nil {
return errors.New(C.GoString(err.message))
}
return nil
}
// BeginTxn begins a transaction.
func (db *TidesDB) BeginTxn(columnFamily string) (*Transaction, error) {
cfName := C.CString(columnFamily)
defer C.free(unsafe.Pointer(cfName))
var txn *C.tidesdb_txn_t
err := C.tidesdb_txn_begin(db.tdb, &txn, cfName)
if err != nil {
return nil, errors.New(C.GoString(err.message))
}
return &Transaction{txn: txn}, nil
}
// Put adds a key-value pair to the transaction.
func (txn *Transaction) Put(key, value []byte, ttl int64) error {
cKey := (*C.uint8_t)(unsafe.Pointer(&key[0]))
cValue := (*C.uint8_t)(unsafe.Pointer(&value[0]))
err := C.tidesdb_txn_put(txn.txn, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)), C.time_t(ttl))
if err != nil {
return errors.New(C.GoString(err.message))
}
return nil
}
// Delete removes a key-value pair from the transaction.
func (txn *Transaction) Delete(key []byte) error {
cKey := (*C.uint8_t)(unsafe.Pointer(&key[0]))
err := C.tidesdb_txn_delete(txn.txn, cKey, C.size_t(len(key)))
if err != nil {
return errors.New(C.GoString(err.message))
}
return nil
}
// Commit commits the transaction.
func (txn *Transaction) Commit() error {
err := C.tidesdb_txn_commit(txn.txn)
if err != nil {
return errors.New(C.GoString(err.message))
}
return nil
}
// Rollback rolls back the transaction.
func (txn *Transaction) Rollback() error {
err := C.tidesdb_txn_rollback(txn.txn)
if err != nil {
return errors.New(C.GoString(err.message))
}
return nil
}
// Free frees the transaction and its operations.
func (txn *Transaction) Free() error {
err := C.tidesdb_txn_free(txn.txn)
if err != nil {
return errors.New(C.GoString(err.message))
}
return nil
}