-
Notifications
You must be signed in to change notification settings - Fork 1
/
nemo.go
190 lines (164 loc) · 5.57 KB
/
nemo.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
package gonemo
// #include "nemo_c.h"
// #include <stdlib.h>
import "C"
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"unsafe"
slab "github.com/funny/slab"
)
// Options go-nemo instance option
type Options struct {
c *C.nemo_options_t
p *MemPoolOpt
}
// MemPool for go-nemo
var MemPool *slab.SyncPool
// NEMO instance handle
type NEMO struct {
c *C.nemo_t
dbPath string
opts *Options
}
// NewDefaultOptions create default option
func NewDefaultOptions() *Options {
opts := C.nemo_CreateOption()
return &Options{
c: opts,
p: &MemPoolOpt{
MinMemPoolChunkSize: 8, // The smallest chunk size is 64B.
MaxMemPoolChunkSize: 4 * 1024, // The largest chunk size is 64KB.
MemPoolFactor: 2, // Power of 2 growth in chunk size.
},
}
}
// RocksOpt rocksdb options for go nemo
type RocksOpt struct {
// options for nemo rocksdb
CreateIfMissing bool `json:"create_if_missing"`
WriteBufferSize int `json:"write_buffer_size"`
MaxOpenFiles int `json:"max_open_files"`
UseBloomfilter bool `json:"use_bloomfilter"`
WriteThreads int `json:"write_threads"`
TargetFileSizeBase int `json:"target_file_size_base"`
TargetFileSizeMultiplier int `json:"target_file_size_multiplier"`
Compression bool `json:"compression"`
MaxBackgroundFlushes int `json:"max_background_flushes"`
MaxBackgroundCompactions int `json:"max_background_compactions"`
MaxBytesForLevelMultiplier int `json:"max_bytes_for_level_multiplier"`
MaxByteLevelBase int `json:"max_bytes_for_level_base"`
L0SlowWriteTrigger int `json:"level0_slowdown_writes_trigger"`
L0StopWriteTrigger int `json:"level0_stop_writes_trigger"`
MinWriteBufNumMerge int `json:"min_write_buffer_number_to_merge"`
L0FileNumCompactTrigger int `json:"level0_file_num_compaction_trigger"`
DelayWriteRate int `json:"delayed_write_rate"`
MaxWriteBufNum int `json:"max_write_buffer_number"`
DisableWAL bool `json:"disable_wal"`
}
// MemPoolOpt memory pool option for go-nemo
type MemPoolOpt struct {
// options for golang mem pool
MinMemPoolChunkSize int `json:"min_mempool_chunk_size"`
MaxMemPoolChunkSize int `json:"max_mempool_chunk_size"`
MemPoolFactor int `json:"mempool_factor"`
}
// JSONOpt nemo options with json format
type JSONOpt struct {
// options for nemo rocksdb
Db RocksOpt `json:"rocksdb"`
// options for golang mem pool
Pool MemPoolOpt `json:"mem_pool"`
}
// NewOptions Create new options for nemo
func NewOptions(nemoConf string) (*Options, *JSONOpt) {
bytes, err := ioutil.ReadFile(nemoConf)
if err != nil {
fmt.Printf("Read nemo config File[%s] err: %s\n", nemoConf, err.Error())
panic("init error!")
}
var jopt JSONOpt
err = json.Unmarshal(bytes, &jopt)
if err != nil {
fmt.Printf("Parse jsonfile[%s] err: %s\n", nemoConf, err.Error())
panic("init error!")
}
cOpts := C.nemo_CreateOption()
var goOpts = C.GoNemoOpts{
create_if_missing: C.bool(jopt.Db.CreateIfMissing),
write_buffer_size: C.int(jopt.Db.WriteBufferSize * 1024 * 1024),
max_open_files: C.int(jopt.Db.MaxOpenFiles),
use_bloomfilter: C.bool(jopt.Db.UseBloomfilter),
write_threads: C.int(jopt.Db.WriteThreads),
// default target_file_size_base and multiplier is the same as rocksdb
target_file_size_base: C.int(jopt.Db.TargetFileSizeBase * 1024 * 1024),
target_file_size_multiplier: C.int(jopt.Db.TargetFileSizeMultiplier),
compression: C.bool(jopt.Db.Compression),
max_background_flushes: C.int(jopt.Db.MaxBackgroundFlushes),
max_background_compactions: C.int(jopt.Db.MaxBackgroundCompactions),
max_bytes_for_level_multiplier: C.int(jopt.Db.MaxBytesForLevelMultiplier),
max_bytes_for_level_base: C.int(jopt.Db.MaxByteLevelBase * 1024 * 1024),
level0_slowdown_writes_trigger: C.int(jopt.Db.L0SlowWriteTrigger),
level0_stop_writes_trigger: C.int(jopt.Db.L0StopWriteTrigger),
min_write_buffer_number_to_merge: C.int(jopt.Db.MinWriteBufNumMerge),
level0_file_num_compaction_trigger: C.int(jopt.Db.L0FileNumCompactTrigger),
delayed_write_rate: C.int(jopt.Db.DelayWriteRate * 1024 * 1024),
max_write_buffer_number: C.int(jopt.Db.MaxWriteBufNum),
disable_wal: C.bool(jopt.Db.DisableWAL),
}
C.nemo_SetOptions(cOpts, &goOpts)
return &Options{c: cOpts, p: &jopt.Pool}, &jopt
}
// OpenNemo return a nemo handle
func OpenNemo(opts *Options, path string) *NEMO {
var (
cPath = C.CString(path)
)
defer C.free(unsafe.Pointer(cPath))
nemo := C.nemo_Create(cPath, opts.c)
MemPool = slab.NewSyncPool(
opts.p.MinMemPoolChunkSize,
opts.p.MaxMemPoolChunkSize,
opts.p.MemPoolFactor,
)
return &NEMO{
c: nemo,
dbPath: path,
opts: opts,
}
}
// Close nemo instance
func (nemo *NEMO) Close() {
C.nemo_free(nemo.c)
}
// Compact do all compact
func (nemo *NEMO) Compact(dbType DBType, sync bool) error {
var cErr *C.char
C.nemo_Compact(nemo.c, C.int(dbType), C.bool(sync), &cErr)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return res
}
return nil
}
// RunBGTask call background task
func (nemo *NEMO) RunBGTask() error {
var cErr *C.char
C.nemo_RunBGTask(nemo.c, &cErr)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return res
}
return nil
}
// GetCurrentTaskType return current task type
func (nemo *NEMO) GetCurrentTaskType() *string {
cTaskType := C.nemo_GetCurrentTaskType(nemo.c)
res := C.GoString(cTaskType)
C.free(unsafe.Pointer(cTaskType))
return &res
}