forked from tecbot/gorocksdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterator.go
More file actions
322 lines (284 loc) · 9.93 KB
/
Copy pathiterator.go
File metadata and controls
322 lines (284 loc) · 9.93 KB
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
package gorocksdb
// #include <stdlib.h>
// #include "rocksdb/c.h"
// #include "gorocksdb.h"
import "C"
import (
"bytes"
"errors"
"reflect"
"unsafe"
)
// Iterator provides a way to seek to specific keys and iterate through
// the keyspace from that point, as well as access the values of those keys.
//
// For example:
//
// it := db.NewIterator(readOpts)
// defer it.Close()
//
// it.Seek([]byte("foo"))
// for ; it.Valid(); it.Next() {
// fmt.Printf("Key: %v Value: %v\n", it.Key().Data(), it.Value().Data())
// }
//
// if err := it.Err(); err != nil {
// return err
// }
//
type Iterator struct {
c *C.rocksdb_iterator_t
}
// NewNativeIterator creates a Iterator object.
func NewNativeIterator(c unsafe.Pointer) *Iterator {
return &Iterator{(*C.rocksdb_iterator_t)(c)}
}
// Valid returns false only when an Iterator has iterated past either the
// first or the last key in the database.
func (iter *Iterator) Valid() bool {
return C.rocksdb_iter_valid(iter.c) != 0
}
// ValidForPrefix returns false only when an Iterator has iterated past the
// first or the last key in the database or the specified prefix.
func (iter *Iterator) ValidForPrefix(prefix []byte) bool {
if C.rocksdb_iter_valid(iter.c) == 0 {
return false
}
key := iter.Key()
result := bytes.HasPrefix(key.Data(), prefix)
key.Free()
return result
}
// Key returns the key the iterator currently holds.
func (iter *Iterator) Key() *Slice {
var cLen C.size_t
cKey := C.rocksdb_iter_key(iter.c, &cLen)
if cKey == nil {
return nil
}
return &Slice{cKey, cLen, true}
}
// Value returns the value in the database the iterator currently holds.
func (iter *Iterator) Value() *Slice {
var cLen C.size_t
cVal := C.rocksdb_iter_value(iter.c, &cLen)
if cVal == nil {
return nil
}
return &Slice{cVal, cLen, true}
}
// Next moves the iterator to the next sequential key in the database.
func (iter *Iterator) Next() {
C.rocksdb_iter_next(iter.c)
}
// Prev moves the iterator to the previous sequential key in the database.
func (iter *Iterator) Prev() {
C.rocksdb_iter_prev(iter.c)
}
// SeekToFirst moves the iterator to the first key in the database.
func (iter *Iterator) SeekToFirst() {
C.rocksdb_iter_seek_to_first(iter.c)
}
// SeekToLast moves the iterator to the last key in the database.
func (iter *Iterator) SeekToLast() {
C.rocksdb_iter_seek_to_last(iter.c)
}
// Seek moves the iterator to the position greater than or equal to the key.
func (iter *Iterator) Seek(key []byte) {
cKey := byteToChar(key)
C.rocksdb_iter_seek(iter.c, cKey, C.size_t(len(key)))
}
// SeekForPrev moves the iterator to the last key that less than or equal
// to the target key, in contrast with Seek.
func (iter *Iterator) SeekForPrev(key []byte) {
cKey := byteToChar(key)
C.rocksdb_iter_seek_for_prev(iter.c, cKey, C.size_t(len(key)))
}
// Err returns nil if no errors happened during iteration, or the actual
// error otherwise.
func (iter *Iterator) Err() error {
var cErr *C.char
C.rocksdb_iter_get_error(iter.c, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))
return errors.New(C.GoString(cErr))
}
return nil
}
// Close closes the iterator.
func (iter *Iterator) Close() {
C.rocksdb_iter_destroy(iter.c)
iter.c = nil
}
var ManyKeysPageAllocSize int = 512
func (iter *Iterator) fetchNextManyKeys(reverse bool, limit int, keyPrefix, keyEnd []byte) *ManyKeys {
cKeyFilter := C.gorocksdb_many_keys_filter_t{}
if len(keyPrefix) > 0 {
cKeyPrefix := C.CString(string(keyPrefix))
defer C.free(unsafe.Pointer(cKeyPrefix))
cKeyFilter.key_prefix = cKeyPrefix
cKeyFilter.key_prefix_s = C.size_t(len(keyPrefix))
}
if len(keyEnd) > 0 {
cKeyEnd := C.CString(string(keyEnd))
defer C.free(unsafe.Pointer(cKeyEnd))
cKeyFilter.key_end = cKeyEnd
cKeyFilter.key_end_s = C.size_t(len(keyEnd))
}
return &ManyKeys{c: C.gorocksdb_iter_many_keys(iter.c, C.int(limit), C.bool(btoi(reverse)), &cKeyFilter, C.int(ManyKeysPageAllocSize))}
}
// NextManyKeys...
func (iter *Iterator) NextManyKeys(limit int, keyPrefix, keyEnd []byte) *ManyKeys {
return iter.fetchNextManyKeys(false, limit, keyPrefix, keyEnd)
}
// NextManyKeysF... (compat)
func (iter *Iterator) NextManyKeysF(limit int, keyPrefix, keyEnd []byte) *ManyKeys {
return iter.NextManyKeys(limit, keyPrefix, keyEnd)
}
// PrevManyKeys...
func (iter *Iterator) PrevManyKeys(limit int, keyPrefix, keyEnd []byte) *ManyKeys {
return iter.fetchNextManyKeys(true, limit, keyPrefix, keyEnd)
}
type KeysSearch struct {
KeyFrom,
KeyPrefix,
KeyEnd []byte
Limit int
Reverse bool
ExcludeKeyFrom bool
}
func (iter *Iterator) ManySearchKeys(searches []KeysSearch) *ManyManyKeys {
nbSearches := len(searches)
cManyKeysSearches := make([]C.gorocksdb_keys_search_t, nbSearches)
for i := range searches {
cKSearch := C.gorocksdb_keys_search_t{
limit: C.int(searches[i].Limit),
reverse: C.bool(btoi(searches[i].Reverse)),
exclude_key_from: C.bool(btoi(searches[i].ExcludeKeyFrom)),
}
cKSearch.key_from = C.CString(string(searches[i].KeyFrom))
cKSearch.key_from_s = C.size_t(len(searches[i].KeyFrom))
if len(searches[i].KeyPrefix) > 0 {
cKSearch.key_prefix = C.CString(string(searches[i].KeyPrefix))
cKSearch.key_prefix_s = C.size_t(len(searches[i].KeyPrefix))
}
if len(searches[i].KeyEnd) > 0 {
cKSearch.key_end = C.CString(string(searches[i].KeyEnd))
cKSearch.key_end_s = C.size_t(len(searches[i].KeyEnd))
}
cManyKeysSearches[i] = cKSearch
}
cManyManyKeys := C.gorocksdb_many_search_keys(iter.c,
(*C.gorocksdb_keys_search_t)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&cManyKeysSearches)).Data)),
C.int(nbSearches),
C.int(ManyKeysPageAllocSize),
)
// free
for i := range searches {
C.free(unsafe.Pointer(cManyKeysSearches[i].key_from))
if len(searches[i].KeyPrefix) > 0 {
C.free(unsafe.Pointer(cManyKeysSearches[i].key_prefix))
}
if len(searches[i].KeyEnd) > 0 {
C.free(unsafe.Pointer(cManyKeysSearches[i].key_end))
}
}
return &ManyManyKeys{c: cManyManyKeys, size: nbSearches}
}
//func (iter *Iterator) ManySearchKeysExp(searches []KeysSearch) *ManyManyKeys {
// nbSearches := len(searches)
//
// cKeyFroms := C.malloc(C.size_t(nbSearches) * C.size_t(unsafe.Sizeof(uintptr(0))))
// defer C.free(cKeyFroms)
// cKeyPrefixes := C.malloc(C.size_t(nbSearches) * C.size_t(unsafe.Sizeof(uintptr(0))))
// defer C.free(cKeyPrefixes)
// cKeyEnds := C.malloc(C.size_t(nbSearches) * C.size_t(unsafe.Sizeof(uintptr(0))))
// defer C.free(cKeyEnds)
// cKeyFromSizes := make([]C.size_t, nbSearches)
// cKeyPrefixSizes := make([]C.size_t, nbSearches)
// cKeyEndSizes := make([]C.size_t, nbSearches)
// cLimits := make([]C.int, nbSearches)
//
// for i := uintptr(0); i < uintptr(nbSearches); i++ {
// search := searches[i]
// *(**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(cKeyFroms)) + i*unsafe.Sizeof(cKeyFroms))) = byteToChar(search.KeyFrom)
// *(**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(cKeyPrefixes)) + i*unsafe.Sizeof(cKeyPrefixes))) = byteToChar(search.KeyPrefix)
// *(**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(cKeyEnds)) + i*unsafe.Sizeof(cKeyEnds))) = byteToChar(search.KeyEnd)
// cKeyFromSizes[i] = C.size_t(len(searches[i].KeyFrom))
// cKeyPrefixSizes[i] = C.size_t(len(searches[i].KeyPrefix))
// cKeyEndSizes[i] = C.size_t(len(searches[i].KeyEnd))
// cLimits[i] = C.int(searches[i].Limit)
// }
// cManyManyKeys := C.gorocksdb_many_search_keys_raw(
// iter.c,
// (**C.char)(unsafe.Pointer(cKeyFroms)),
// (*C.size_t)(unsafe.Pointer(&cKeyFromSizes[0])),
// (**C.char)(unsafe.Pointer(cKeyPrefixes)),
// (*C.size_t)(unsafe.Pointer(&cKeyPrefixSizes[0])),
// (**C.char)(unsafe.Pointer(cKeyEnds)),
// (*C.size_t)(unsafe.Pointer(&cKeyEndSizes[0])),
// (*C.int)(unsafe.Pointer(&cLimits[0])),
// C.int(nbSearches),
// C.int(ManyKeysPageAllocSize),
// )
// return &ManyManyKeys{c: cManyManyKeys, size: nbSearches}
//}
type ManyKeys struct {
c *C.gorocksdb_many_keys_t
}
func (m *ManyKeys) Destroy() {
C.gorocksdb_destroy_many_keys(m.c)
}
func (m *ManyKeys) Found() int {
return int(m.c.found)
}
func (m *ManyKeys) Keys() [][]byte {
found := m.Found()
keys := make([][]byte, found)
for i := uintptr(0); i < uintptr(found); i++ {
chars := *(**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(m.c.keys)) + i*unsafe.Sizeof(m.c.keys)))
size := *(*C.size_t)(unsafe.Pointer(uintptr(unsafe.Pointer(m.c.key_sizes)) + i*unsafe.Sizeof(m.c.key_sizes)))
keys[i] = charToByte(chars, size)
}
return keys
}
func (m *ManyKeys) Values() [][]byte {
found := m.Found()
values := make([][]byte, found)
for i := uintptr(0); i < uintptr(found); i++ {
chars := *(**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(m.c.values)) + i*unsafe.Sizeof(m.c.values)))
size := *(*C.size_t)(unsafe.Pointer(uintptr(unsafe.Pointer(m.c.value_sizes)) + i*unsafe.Sizeof(m.c.value_sizes)))
values[i] = charToByte(chars, size)
}
return values
}
func (m *ManyKeys) Each(each func(i int, key []byte, value []byte) bool) bool {
found := m.Found()
for i := uintptr(0); i < uintptr(found); i++ {
chars := *(**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(m.c.keys)) + i*unsafe.Sizeof(m.c.keys)))
size := *(*C.size_t)(unsafe.Pointer(uintptr(unsafe.Pointer(m.c.key_sizes)) + i*unsafe.Sizeof(m.c.key_sizes)))
key := charToByte(chars, size)
chars = *(**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(m.c.values)) + i*unsafe.Sizeof(m.c.values)))
size = *(*C.size_t)(unsafe.Pointer(uintptr(unsafe.Pointer(m.c.value_sizes)) + i*unsafe.Sizeof(m.c.value_sizes)))
value := charToByte(chars, size)
if !each(int(i), key, value) {
return false
}
}
return true
}
type ManyManyKeys struct {
c **C.gorocksdb_many_keys_t
size int
}
func (m ManyManyKeys) Result() []*ManyKeys {
result := make([]*ManyKeys, m.size)
for i := uintptr(0); i < uintptr(m.size); i++ {
manyKeys := *(**C.gorocksdb_many_keys_t)(unsafe.Pointer(uintptr(unsafe.Pointer(m.c)) + i*unsafe.Sizeof(m.c)))
result[i] = &ManyKeys{c: manyKeys}
}
return result
}
func (m ManyManyKeys) Destroy() {
C.gorocksdb_destroy_many_many_keys(m.c, C.int(m.size))
}