forked from tecbot/gorocksdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
800 lines (711 loc) · 20.8 KB
/
db.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
package gorocksdb
// #include <stdlib.h>
// #include "rocksdb/c.h"
import "C"
import (
"errors"
"fmt"
"unsafe"
)
// Range is a range of keys in the database. GetApproximateSizes calls with it
// begin at the key Start and end right before the key Limit.
type Range struct {
Start []byte
Limit []byte
}
// DB is a reusable handle to a RocksDB database on disk, created by Open.
type DB struct {
c *C.rocksdb_t
name string
opts *Options
}
// OpenDb opens a database with the specified options.
func OpenDb(opts *Options, name string) (*DB, error) {
var (
cErr *C.char
cName = C.CString(name)
)
defer C.free(unsafe.Pointer(cName))
db := C.rocksdb_open(opts.c, cName, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return nil, errors.New(C.GoString(cErr))
}
return &DB{
name: name,
c: db,
opts: opts,
}, nil
}
// OpenDbForReadOnly opens a database with the specified options for readonly usage.
func OpenDbForReadOnly(opts *Options, name string, errorIfLogFileExist bool) (*DB, error) {
var (
cErr *C.char
cName = C.CString(name)
)
defer C.free(unsafe.Pointer(cName))
db := C.rocksdb_open_for_read_only(opts.c, cName, boolToChar(errorIfLogFileExist), &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return nil, errors.New(C.GoString(cErr))
}
return &DB{
name: name,
c: db,
opts: opts,
}, nil
}
// OpenDbColumnFamilies opens a database with the specified column families.
func OpenDbColumnFamilies(
opts *Options,
name string,
cfNames []string,
cfOpts []*Options,
) (*DB, []*ColumnFamilyHandle, error) {
numColumnFamilies := len(cfNames)
if numColumnFamilies != len(cfOpts) {
return nil, nil, errors.New("must provide the same number of column family names and options")
}
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
cNames := make([]*C.char, numColumnFamilies)
for i, s := range cfNames {
cNames[i] = C.CString(s)
}
defer func() {
for _, s := range cNames {
C.free(unsafe.Pointer(s))
}
}()
cOpts := make([]*C.rocksdb_options_t, numColumnFamilies)
for i, o := range cfOpts {
cOpts[i] = o.c
}
cHandles := make([]*C.rocksdb_column_family_handle_t, numColumnFamilies)
var cErr *C.char
db := C.rocksdb_open_column_families(
opts.c,
cName,
C.int(numColumnFamilies),
&cNames[0],
&cOpts[0],
&cHandles[0],
&cErr,
)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return nil, nil, errors.New(C.GoString(cErr))
}
cfHandles := make([]*ColumnFamilyHandle, numColumnFamilies)
for i, c := range cHandles {
cfHandles[i] = NewNativeColumnFamilyHandle(c)
}
return &DB{
name: name,
c: db,
opts: opts,
}, cfHandles, nil
}
// OpenDbForReadOnlyColumnFamilies opens a database with the specified column
// families in read only mode.
func OpenDbForReadOnlyColumnFamilies(
opts *Options,
name string,
cfNames []string,
cfOpts []*Options,
errorIfLogFileExist bool,
) (*DB, []*ColumnFamilyHandle, error) {
numColumnFamilies := len(cfNames)
if numColumnFamilies != len(cfOpts) {
return nil, nil, errors.New("must provide the same number of column family names and options")
}
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
cNames := make([]*C.char, numColumnFamilies)
for i, s := range cfNames {
cNames[i] = C.CString(s)
}
defer func() {
for _, s := range cNames {
C.free(unsafe.Pointer(s))
}
}()
cOpts := make([]*C.rocksdb_options_t, numColumnFamilies)
for i, o := range cfOpts {
cOpts[i] = o.c
}
cHandles := make([]*C.rocksdb_column_family_handle_t, numColumnFamilies)
var cErr *C.char
db := C.rocksdb_open_for_read_only_column_families(
opts.c,
cName,
C.int(numColumnFamilies),
&cNames[0],
&cOpts[0],
&cHandles[0],
boolToChar(errorIfLogFileExist),
&cErr,
)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return nil, nil, errors.New(C.GoString(cErr))
}
cfHandles := make([]*ColumnFamilyHandle, numColumnFamilies)
for i, c := range cHandles {
cfHandles[i] = NewNativeColumnFamilyHandle(c)
}
return &DB{
name: name,
c: db,
opts: opts,
}, cfHandles, nil
}
// ListColumnFamilies lists the names of the column families in the DB.
func ListColumnFamilies(opts *Options, name string) ([]string, error) {
var (
cErr *C.char
cLen C.size_t
cName = C.CString(name)
)
defer C.free(unsafe.Pointer(cName))
cNames := C.rocksdb_list_column_families(opts.c, cName, &cLen, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return nil, errors.New(C.GoString(cErr))
}
namesLen := int(cLen)
names := make([]string, namesLen)
cNamesArr := (*[1 << 30]*C.char)(unsafe.Pointer(cNames))[:namesLen:namesLen]
for i, n := range cNamesArr {
names[i] = C.GoString(n)
}
C.rocksdb_list_column_families_destroy(cNames, cLen)
return names, nil
}
// UnsafeGetDB returns the underlying c rocksdb instance.
func (db *DB) UnsafeGetDB() unsafe.Pointer {
return unsafe.Pointer(db.c)
}
// Name returns the name of the database.
func (db *DB) Name() string {
return db.name
}
// Get returns the data associated with the key from the database.
func (db *DB) Get(opts *ReadOptions, key []byte) (*Slice, error) {
var (
cErr *C.char
cValLen C.size_t
cKey = byteToChar(key)
)
cValue := C.rocksdb_get(db.c, opts.c, cKey, C.size_t(len(key)), &cValLen, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return nil, errors.New(C.GoString(cErr))
}
return NewSlice(cValue, cValLen), nil
}
// GetBytes is like Get but returns a copy of the data.
func (db *DB) GetBytes(opts *ReadOptions, key []byte) ([]byte, error) {
var (
cErr *C.char
cValLen C.size_t
cKey = byteToChar(key)
)
cValue := C.rocksdb_get(db.c, opts.c, cKey, C.size_t(len(key)), &cValLen, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return nil, errors.New(C.GoString(cErr))
}
if cValue == nil {
return nil, nil
}
defer C.free(unsafe.Pointer(cValue))
return C.GoBytes(unsafe.Pointer(cValue), C.int(cValLen)), nil
}
// GetCF returns the data associated with the key from the database and column family.
func (db *DB) GetCF(opts *ReadOptions, cf *ColumnFamilyHandle, key []byte) (*Slice, error) {
var (
cErr *C.char
cValLen C.size_t
cKey = byteToChar(key)
)
cValue := C.rocksdb_get_cf(db.c, opts.c, cf.c, cKey, C.size_t(len(key)), &cValLen, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return nil, errors.New(C.GoString(cErr))
}
return NewSlice(cValue, cValLen), nil
}
// MultiGet returns the data associated with the passed keys from the database
func (db *DB) MultiGet(opts *ReadOptions, keys ...[]byte) (Slices, error) {
cKeys, cKeySizes := byteSlicesToCSlices(keys)
defer cKeys.Destroy()
vals := make(charsSlice, len(keys))
valSizes := make(sizeTSlice, len(keys))
rocksErrs := make(charsSlice, len(keys))
C.rocksdb_multi_get(
db.c,
opts.c,
C.size_t(len(keys)),
cKeys.c(),
cKeySizes.c(),
vals.c(),
valSizes.c(),
rocksErrs.c(),
)
var errs []error
for i, rocksErr := range rocksErrs {
if rocksErr != nil {
defer C.free(unsafe.Pointer(rocksErr))
err := fmt.Errorf("getting %q failed: %v", string(keys[i]), C.GoString(rocksErr))
errs = append(errs, err)
}
}
if len(errs) > 0 {
return nil, fmt.Errorf("failed to get %d keys, first error: %v", len(errs), errs[0])
}
slices := make(Slices, len(keys))
for i, val := range vals {
slices[i] = NewSlice(val, valSizes[i])
}
return slices, nil
}
// MultiGetCF returns the data associated with the passed keys from the column family
func (db *DB) MultiGetCF(opts *ReadOptions, cf *ColumnFamilyHandle, keys ...[]byte) (Slices, error) {
cfs := make(ColumnFamilyHandles, len(keys))
for i := 0; i < len(keys); i++ {
cfs[i] = cf
}
return db.MultiGetCFMultiCF(opts, cfs, keys)
}
// MultiGetCFMultiCF returns the data associated with the passed keys and
// column families.
func (db *DB) MultiGetCFMultiCF(opts *ReadOptions, cfs ColumnFamilyHandles, keys [][]byte) (Slices, error) {
cKeys, cKeySizes := byteSlicesToCSlices(keys)
defer cKeys.Destroy()
vals := make(charsSlice, len(keys))
valSizes := make(sizeTSlice, len(keys))
rocksErrs := make(charsSlice, len(keys))
C.rocksdb_multi_get_cf(
db.c,
opts.c,
cfs.toCSlice().c(),
C.size_t(len(keys)),
cKeys.c(),
cKeySizes.c(),
vals.c(),
valSizes.c(),
rocksErrs.c(),
)
var errs []error
for i, rocksErr := range rocksErrs {
if rocksErr != nil {
defer C.free(unsafe.Pointer(rocksErr))
err := fmt.Errorf("getting %q failed: %v", string(keys[i]), C.GoString(rocksErr))
errs = append(errs, err)
}
}
if len(errs) > 0 {
return nil, fmt.Errorf("failed to get %d keys, first error: %v", len(errs), errs[0])
}
slices := make(Slices, len(keys))
for i, val := range vals {
slices[i] = NewSlice(val, valSizes[i])
}
return slices, nil
}
// Put writes data associated with a key to the database.
func (db *DB) Put(opts *WriteOptions, key, value []byte) error {
var (
cErr *C.char
cKey = byteToChar(key)
cValue = byteToChar(value)
)
C.rocksdb_put(db.c, opts.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)), &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// PutCF writes data associated with a key to the database and column family.
func (db *DB) PutCF(opts *WriteOptions, cf *ColumnFamilyHandle, key, value []byte) error {
var (
cErr *C.char
cKey = byteToChar(key)
cValue = byteToChar(value)
)
C.rocksdb_put_cf(db.c, opts.c, cf.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)), &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// Delete removes the data associated with the key from the database.
func (db *DB) Delete(opts *WriteOptions, key []byte) error {
var (
cErr *C.char
cKey = byteToChar(key)
)
C.rocksdb_delete(db.c, opts.c, cKey, C.size_t(len(key)), &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// DeleteCF removes the data associated with the key from the database and column family.
func (db *DB) DeleteCF(opts *WriteOptions, cf *ColumnFamilyHandle, key []byte) error {
var (
cErr *C.char
cKey = byteToChar(key)
)
C.rocksdb_delete_cf(db.c, opts.c, cf.c, cKey, C.size_t(len(key)), &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// Merge merges the data associated with the key with the actual data in the database.
func (db *DB) Merge(opts *WriteOptions, key []byte, value []byte) error {
var (
cErr *C.char
cKey = byteToChar(key)
cValue = byteToChar(value)
)
C.rocksdb_merge(db.c, opts.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)), &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// MergeCF merges the data associated with the key with the actual data in the
// database and column family.
func (db *DB) MergeCF(opts *WriteOptions, cf *ColumnFamilyHandle, key []byte, value []byte) error {
var (
cErr *C.char
cKey = byteToChar(key)
cValue = byteToChar(value)
)
C.rocksdb_merge_cf(db.c, opts.c, cf.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)), &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// Write writes a WriteBatch to the database
func (db *DB) Write(opts *WriteOptions, batch *WriteBatch) error {
var cErr *C.char
C.rocksdb_write(db.c, opts.c, batch.c, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// NewIterator returns an Iterator over the the database that uses the
// ReadOptions given.
func (db *DB) NewIterator(opts *ReadOptions) *Iterator {
cIter := C.rocksdb_create_iterator(db.c, opts.c)
return NewNativeIterator(unsafe.Pointer(cIter))
}
// NewIteratorCF returns an Iterator over the the database and column family
// that uses the ReadOptions given.
func (db *DB) NewIteratorCF(opts *ReadOptions, cf *ColumnFamilyHandle) *Iterator {
cIter := C.rocksdb_create_iterator_cf(db.c, opts.c, cf.c)
return NewNativeIterator(unsafe.Pointer(cIter))
}
// NewSnapshot creates a new snapshot of the database.
func (db *DB) NewSnapshot() *Snapshot {
cSnap := C.rocksdb_create_snapshot(db.c)
return NewNativeSnapshot(cSnap)
}
// ReleaseSnapshot releases the snapshot and its resources.
func (db *DB) ReleaseSnapshot(snapshot *Snapshot) {
C.rocksdb_release_snapshot(db.c, snapshot.c)
snapshot.c = nil
}
// GetProperty returns the value of a database property.
func (db *DB) GetProperty(propName string) string {
cprop := C.CString(propName)
defer C.free(unsafe.Pointer(cprop))
cValue := C.rocksdb_property_value(db.c, cprop)
defer C.free(unsafe.Pointer(cValue))
return C.GoString(cValue)
}
// GetPropertyCF returns the value of a database property.
func (db *DB) GetPropertyCF(propName string, cf *ColumnFamilyHandle) string {
cProp := C.CString(propName)
defer C.free(unsafe.Pointer(cProp))
cValue := C.rocksdb_property_value_cf(db.c, cf.c, cProp)
defer C.free(unsafe.Pointer(cValue))
return C.GoString(cValue)
}
// CreateColumnFamily create a new column family.
func (db *DB) CreateColumnFamily(opts *Options, name string) (*ColumnFamilyHandle, error) {
var (
cErr *C.char
cName = C.CString(name)
)
defer C.free(unsafe.Pointer(cName))
cHandle := C.rocksdb_create_column_family(db.c, opts.c, cName, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return nil, errors.New(C.GoString(cErr))
}
return NewNativeColumnFamilyHandle(cHandle), nil
}
// DropColumnFamily drops a column family.
func (db *DB) DropColumnFamily(c *ColumnFamilyHandle) error {
var cErr *C.char
C.rocksdb_drop_column_family(db.c, c.c, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// GetApproximateSizes returns the approximate number of bytes of file system
// space used by one or more key ranges.
//
// The keys counted will begin at Range.Start and end on the key before
// Range.Limit.
func (db *DB) GetApproximateSizes(ranges []Range) []uint64 {
sizes := make([]uint64, len(ranges))
if len(ranges) == 0 {
return sizes
}
cStarts := make([]*C.char, len(ranges))
cLimits := make([]*C.char, len(ranges))
cStartLens := make([]C.size_t, len(ranges))
cLimitLens := make([]C.size_t, len(ranges))
for i, r := range ranges {
cStarts[i] = byteToChar(r.Start)
cStartLens[i] = C.size_t(len(r.Start))
cLimits[i] = byteToChar(r.Limit)
cLimitLens[i] = C.size_t(len(r.Limit))
}
C.rocksdb_approximate_sizes(
db.c,
C.int(len(ranges)),
&cStarts[0],
&cStartLens[0],
&cLimits[0],
&cLimitLens[0],
(*C.uint64_t)(&sizes[0]))
return sizes
}
// GetApproximateSizesCF returns the approximate number of bytes of file system
// space used by one or more key ranges in the column family.
//
// The keys counted will begin at Range.Start and end on the key before
// Range.Limit.
func (db *DB) GetApproximateSizesCF(cf *ColumnFamilyHandle, ranges []Range) []uint64 {
sizes := make([]uint64, len(ranges))
if len(ranges) == 0 {
return sizes
}
cStarts := make([]*C.char, len(ranges))
cLimits := make([]*C.char, len(ranges))
cStartLens := make([]C.size_t, len(ranges))
cLimitLens := make([]C.size_t, len(ranges))
for i, r := range ranges {
cStarts[i] = byteToChar(r.Start)
cStartLens[i] = C.size_t(len(r.Start))
cLimits[i] = byteToChar(r.Limit)
cLimitLens[i] = C.size_t(len(r.Limit))
}
C.rocksdb_approximate_sizes_cf(
db.c,
cf.c,
C.int(len(ranges)),
&cStarts[0],
&cStartLens[0],
&cLimits[0],
&cLimitLens[0],
(*C.uint64_t)(&sizes[0]))
return sizes
}
// LiveFileMetadata is a metadata which is associated with each SST file.
type LiveFileMetadata struct {
Name string
Level int
Size int64
SmallestKey []byte
LargestKey []byte
}
// GetLiveFilesMetaData returns a list of all table files with their
// level, start key and end key.
func (db *DB) GetLiveFilesMetaData() []LiveFileMetadata {
lf := C.rocksdb_livefiles(db.c)
defer C.rocksdb_livefiles_destroy(lf)
count := C.rocksdb_livefiles_count(lf)
liveFiles := make([]LiveFileMetadata, int(count))
for i := C.int(0); i < count; i++ {
var liveFile LiveFileMetadata
liveFile.Name = C.GoString(C.rocksdb_livefiles_name(lf, i))
liveFile.Level = int(C.rocksdb_livefiles_level(lf, i))
liveFile.Size = int64(C.rocksdb_livefiles_size(lf, i))
var cSize C.size_t
key := C.rocksdb_livefiles_smallestkey(lf, i, &cSize)
liveFile.SmallestKey = C.GoBytes(unsafe.Pointer(key), C.int(cSize))
key = C.rocksdb_livefiles_largestkey(lf, i, &cSize)
liveFile.LargestKey = C.GoBytes(unsafe.Pointer(key), C.int(cSize))
liveFiles[int(i)] = liveFile
}
return liveFiles
}
// CompactRange runs a manual compaction on the Range of keys given. This is
// not likely to be needed for typical usage.
func (db *DB) CompactRange(r Range) {
cStart := byteToChar(r.Start)
cLimit := byteToChar(r.Limit)
C.rocksdb_compact_range(db.c, cStart, C.size_t(len(r.Start)), cLimit, C.size_t(len(r.Limit)))
}
// CompactRangeCF runs a manual compaction on the Range of keys given on the
// given column family. This is not likely to be needed for typical usage.
func (db *DB) CompactRangeCF(cf *ColumnFamilyHandle, r Range) {
cStart := byteToChar(r.Start)
cLimit := byteToChar(r.Limit)
C.rocksdb_compact_range_cf(db.c, cf.c, cStart, C.size_t(len(r.Start)), cLimit, C.size_t(len(r.Limit)))
}
// Flush triggers a manuel flush for the database.
func (db *DB) Flush(opts *FlushOptions) error {
var cErr *C.char
C.rocksdb_flush(db.c, opts.c, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// DisableFileDeletions disables file deletions and should be used when backup the database.
func (db *DB) DisableFileDeletions() error {
var cErr *C.char
C.rocksdb_disable_file_deletions(db.c, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// EnableFileDeletions enables file deletions for the database.
func (db *DB) EnableFileDeletions(force bool) error {
var cErr *C.char
C.rocksdb_enable_file_deletions(db.c, boolToChar(force), &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// DeleteFile deletes the file name from the db directory and update the internal state to
// reflect that. Supports deletion of sst and log files only. 'name' must be
// path relative to the db directory. eg. 000001.sst, /archive/000003.log.
func (db *DB) DeleteFile(name string) {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
C.rocksdb_delete_file(db.c, cName)
}
// IngestExternalFile loads a list of external SST files.
func (db *DB) IngestExternalFile(filePaths []string, opts *IngestExternalFileOptions) error {
cFilePaths := make([]*C.char, len(filePaths))
for i, s := range filePaths {
cFilePaths[i] = C.CString(s)
}
defer func() {
for _, s := range cFilePaths {
C.free(unsafe.Pointer(s))
}
}()
var cErr *C.char
C.rocksdb_ingest_external_file(
db.c,
&cFilePaths[0],
C.size_t(len(filePaths)),
opts.c,
&cErr,
)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// IngestExternalFileCF loads a list of external SST files for a column family.
func (db *DB) IngestExternalFileCF(handle *ColumnFamilyHandle, filePaths []string, opts *IngestExternalFileOptions) error {
cFilePaths := make([]*C.char, len(filePaths))
for i, s := range filePaths {
cFilePaths[i] = C.CString(s)
}
defer func() {
for _, s := range cFilePaths {
C.free(unsafe.Pointer(s))
}
}()
var cErr *C.char
C.rocksdb_ingest_external_file_cf(
db.c,
handle.c,
&cFilePaths[0],
C.size_t(len(filePaths)),
opts.c,
&cErr,
)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// NewCheckpoint creates a new Checkpoint for this db.
func (db *DB) NewCheckpoint() (*Checkpoint, error) {
var (
cErr *C.char
)
cCheckpoint := C.rocksdb_checkpoint_object_create(
db.c, &cErr,
)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return nil, errors.New(C.GoString(cErr))
}
return NewNativeCheckpoint(cCheckpoint), nil
}
// Close closes the database.
func (db *DB) Close() {
C.rocksdb_close(db.c)
}
// DestroyDb removes a database entirely, removing everything from the
// filesystem.
func DestroyDb(name string, opts *Options) error {
var (
cErr *C.char
cName = C.CString(name)
)
defer C.free(unsafe.Pointer(cName))
C.rocksdb_destroy_db(opts.c, cName, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// RepairDb repairs a database.
func RepairDb(name string, opts *Options) error {
var (
cErr *C.char
cName = C.CString(name)
)
defer C.free(unsafe.Pointer(cName))
C.rocksdb_repair_db(opts.c, cName, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}