This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
forked from linxGnu/grocksdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sst_file_writer.go
139 lines (120 loc) · 4.25 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
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
package grocksdb
// #include <stdlib.h>
// #include "rocksdb/c.h"
import "C"
import (
"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}
}
// NewSSTFileWriterWithComparator creates an SSTFileWriter object with comparator.
func NewSSTFileWriterWithComparator(opts *EnvOptions, dbOpts *Options, cmp *Comparator) *SSTFileWriter {
cmp_ := unsafe.Pointer(cmp.c)
return NewSSTFileWriterWithNativeComparator(opts, dbOpts, cmp_)
}
// NewSSTFileWriterWithNativeComparator creates an SSTFileWriter object with native comparator.
func NewSSTFileWriterWithNativeComparator(opts *EnvOptions, dbOpts *Options, cmp unsafe.Pointer) *SSTFileWriter {
cmp_ := (*C.rocksdb_comparator_t)(cmp)
c := C.rocksdb_sstfilewriter_create_with_comparator(opts.c, dbOpts.c, cmp_)
return &SSTFileWriter{c: c}
}
// Open prepares SstFileWriter to write into file located at "path".
func (w *SSTFileWriter) Open(path string) (err error) {
var (
cErr *C.char
cPath = C.CString(path)
)
C.rocksdb_sstfilewriter_open(w.c, cPath, &cErr)
err = fromCError(cErr)
C.free(unsafe.Pointer(cPath))
return
}
// 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) (err 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)
err = fromCError(cErr)
return
}
// Put key, value to currently opened file.
func (w *SSTFileWriter) Put(key, value []byte) (err error) {
cKey := byteToChar(key)
cValue := byteToChar(value)
var cErr *C.char
C.rocksdb_sstfilewriter_put(w.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)), &cErr)
err = fromCError(cErr)
return
}
// Put key with timestamp, value to the currently opened file
func (w *SSTFileWriter) PutWithTS(key, ts, value []byte) (err error) {
cKey := byteToChar(key)
cValue := byteToChar(value)
cTs := byteToChar(ts)
var cErr *C.char
C.rocksdb_sstfilewriter_put_with_ts(w.c, cKey, C.size_t(len(key)), cTs, C.size_t(len(ts)), cValue, C.size_t(len(value)), &cErr)
err = fromCError(cErr)
return
}
// Merge key, value to currently opened file.
func (w *SSTFileWriter) Merge(key, value []byte) (err error) {
cKey := byteToChar(key)
cValue := byteToChar(value)
var cErr *C.char
C.rocksdb_sstfilewriter_merge(w.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)), &cErr)
err = fromCError(cErr)
return
}
// Delete key from currently opened file.
func (w *SSTFileWriter) Delete(key []byte) (err error) {
cKey := byteToChar(key)
var cErr *C.char
C.rocksdb_sstfilewriter_delete(w.c, cKey, C.size_t(len(key)), &cErr)
err = fromCError(cErr)
return
}
// DeleteWithTS deletes key with timestamp to the currently opened file
func (w *SSTFileWriter) DeleteWithTS(key, ts []byte) (err error) {
cKey := byteToChar(key)
cTs := byteToChar(ts)
var cErr *C.char
C.rocksdb_sstfilewriter_delete_with_ts(w.c, cKey, C.size_t(len(key)), cTs, C.size_t(len(ts)), &cErr)
err = fromCError(cErr)
return
}
// DeleteRange deletes keys that are between [startKey, endKey)
func (w *SSTFileWriter) DeleteRange(startKey, endKey []byte) (err error) {
cStartKey := byteToChar(startKey)
cEndKey := byteToChar(endKey)
var cErr *C.char
C.rocksdb_sstfilewriter_delete_range(w.c, cStartKey, C.size_t(len(startKey)), cEndKey, C.size_t(len(endKey)), &cErr)
err = fromCError(cErr)
return
}
// FileSize returns size of currently opened file.
func (w *SSTFileWriter) FileSize() (size uint64) {
C.rocksdb_sstfilewriter_file_size(w.c, (*C.uint64_t)(&size))
return
}
// Finish finishes writing to sst file and close file.
func (w *SSTFileWriter) Finish() (err error) {
var cErr *C.char
C.rocksdb_sstfilewriter_finish(w.c, &cErr)
err = fromCError(cErr)
return
}
// Destroy destroys the SSTFileWriter object.
func (w *SSTFileWriter) Destroy() {
C.rocksdb_sstfilewriter_destroy(w.c)
w.c = nil
}