forked from tecbot/gorocksdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sst_file_writer.go
70 lines (62 loc) · 1.79 KB
/
sst_file_writer.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
package gorocksdb
// #include <stdlib.h>
// #include "rocksdb/c.h"
import "C"
import (
"errors"
"runtime"
"unsafe"
)
// SSTFileWriter is used to create sst files that can be added to database later.
// All keys in files generated by SstFileWriter will have sequence number = 0.
type SSTFileWriter struct {
c *C.rocksdb_sstfilewriter_t
}
// NewSSTFileWriter creates an SSTFileWriter object.
func NewSSTFileWriter(opts *EnvOptions, dbOpts *Options) *SSTFileWriter {
c := C.rocksdb_sstfilewriter_create(opts.c, dbOpts.c)
return &SSTFileWriter{c: c}
}
// Open prepares SstFileWriter to write into file located at "path".
func (w *SSTFileWriter) Open(path string) error {
var (
cErr *C.char
cPath = C.CString(path)
)
defer C.free(unsafe.Pointer(cPath))
C.rocksdb_sstfilewriter_open(w.c, cPath, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// Add adds key, value to currently opened file.
// REQUIRES: key is after any previously added key according to comparator.
func (w *SSTFileWriter) Add(key, value []byte) error {
cKey := byteToChar(key)
cValue := byteToChar(value)
var cErr *C.char
C.rocksdb_sstfilewriter_add(w.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)), &cErr)
runtime.KeepAlive(key)
runtime.KeepAlive(value)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// Finish finishes writing to sst file and close file.
func (w *SSTFileWriter) Finish() error {
var cErr *C.char
C.rocksdb_sstfilewriter_finish(w.c, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// Destroy destroys the SSTFileWriter object.
func (w *SSTFileWriter) Destroy() {
C.rocksdb_sstfilewriter_destroy(w.c)
}