5
5
package rocksdb
6
6
7
7
// #include <stdlib.h>
8
+ // #include <string.h>
8
9
// #include "rocksdb/c.h"
9
10
import "C"
10
11
@@ -37,8 +38,8 @@ type RDBIterator struct {
37
38
it * C.rocksdb_iterator_t
38
39
opts * C.rocksdb_readoptions_t
39
40
first bool
40
- lowerBound [] byte
41
- upperBound [] byte
41
+ lowerBound * C. char
42
+ upperBound * C. char
42
43
}
43
44
44
45
func cerror (cerr * C.char ) error {
@@ -159,16 +160,29 @@ func (db *RDBDatabase) Delete(key []byte) error {
159
160
return nil
160
161
}
161
162
162
- func (db * RDBDatabase ) NewIterator (prefix , start []byte ) ethdb.Iterator {
163
- begin := prefix
164
- end := incrBytes (begin )
165
- begin = append (begin , start ... )
163
+ func memdup (b []byte ) * C.char {
164
+ x := (* C .char )(C .malloc (C .size_t (len (b ))))
165
+ C .memcpy (unsafe .Pointer (x ), unsafe .Pointer (b2c (b )), C .size_t (len (b )))
166
+ return x
167
+ }
166
168
169
+ func (db * RDBDatabase ) NewIterator (prefix , start []byte ) ethdb.Iterator {
170
+ var begin , end []byte
171
+ begin = prefix
172
+ if len (start ) > 0 {
173
+ begin = append (begin , start ... )
174
+ }
175
+ if len (prefix ) > 0 {
176
+ end = incrBytes (prefix )
177
+ }
178
+ var lowerBound , upperBound * C.char
167
179
opts := C .rocksdb_readoptions_create ()
168
180
if len (begin ) > 0 {
169
- lowerBound := b2c (begin )
181
+ lowerBound = memdup (begin )
170
182
C .rocksdb_readoptions_set_iterate_lower_bound (opts , lowerBound , C .size_t (len (begin )))
171
- upperBound := b2c (end )
183
+ }
184
+ if len (end ) > 0 {
185
+ upperBound = memdup (end )
172
186
C .rocksdb_readoptions_set_iterate_upper_bound (opts , upperBound , C .size_t (len (end )))
173
187
}
174
188
it := C .rocksdb_create_iterator (db .db , opts )
@@ -179,23 +193,23 @@ func (db *RDBDatabase) NewIterator(prefix, start []byte) ethdb.Iterator {
179
193
first : true ,
180
194
}
181
195
if len (begin ) > 0 {
182
- rit .lowerBound = begin
183
- rit .upperBound = end
196
+ rit .lowerBound = lowerBound
197
+ rit .upperBound = upperBound
184
198
}
185
199
return rit
186
200
}
187
201
188
202
func (db * RDBDatabase ) NewIteratorWithStart (start []byte ) ethdb.Iterator {
189
203
opts := C .rocksdb_readoptions_create ()
190
- lowerBound := b2c (start )
204
+ lowerBound := memdup (start )
191
205
C .rocksdb_readoptions_set_iterate_lower_bound (opts , lowerBound , C .size_t (len (start )))
192
206
it := C .rocksdb_create_iterator (db .db , opts )
193
207
C .rocksdb_iter_seek_to_first (it )
194
208
return & RDBIterator {
195
209
it : it ,
196
210
opts : opts ,
197
211
first : true ,
198
- lowerBound : start ,
212
+ lowerBound : lowerBound ,
199
213
}
200
214
}
201
215
@@ -204,18 +218,18 @@ func (db *RDBDatabase) NewIteratorWithPrefix(prefix []byte) ethdb.Iterator {
204
218
end := incrBytes (start )
205
219
206
220
opts := C .rocksdb_readoptions_create ()
207
- lowerBound := b2c (start )
221
+ lowerBound := memdup (start )
208
222
C .rocksdb_readoptions_set_iterate_lower_bound (opts , lowerBound , C .size_t (len (start )))
209
- upperBound := b2c (end )
223
+ upperBound := memdup (end )
210
224
C .rocksdb_readoptions_set_iterate_upper_bound (opts , upperBound , C .size_t (len (end )))
211
225
it := C .rocksdb_create_iterator (db .db , opts )
212
226
C .rocksdb_iter_seek_to_first (it )
213
227
return & RDBIterator {
214
228
it : it ,
215
229
opts : opts ,
216
230
first : true ,
217
- lowerBound : start ,
218
- upperBound : end ,
231
+ lowerBound : lowerBound ,
232
+ upperBound : upperBound ,
219
233
}
220
234
}
221
235
@@ -284,6 +298,12 @@ func (it *RDBIterator) Release() {
284
298
if it .opts != nil {
285
299
C .rocksdb_readoptions_destroy (it .opts )
286
300
}
301
+ if it .lowerBound != nil {
302
+ C .free (unsafe .Pointer (it .lowerBound ))
303
+ }
304
+ if it .upperBound != nil {
305
+ C .free (unsafe .Pointer (it .upperBound ))
306
+ }
287
307
it .it , it .opts , it .lowerBound , it .upperBound = nil , nil , nil , nil
288
308
}
289
309
@@ -312,10 +332,10 @@ func (db *RDBDatabase) Meter(prefix string) {
312
332
func (db * RDBDatabase ) NewBatch () ethdb.Batch {
313
333
b := C .rocksdb_writebatch_create ()
314
334
bb := & rdbBatch {db : db .db , b : b , wopts : db .wopts , data : nil }
315
- runtime .SetFinalizer (bb , func (bb * rdbBatch ) {
316
- if bb .b != nil {
317
- C .rocksdb_writebatch_destroy (bb .b )
318
- bb .b = nil
335
+ runtime .SetFinalizer (bb , func (bbb * rdbBatch ) {
336
+ if bbb .b != nil {
337
+ C .rocksdb_writebatch_destroy (bbb .b )
338
+ bbb .b = nil
319
339
}
320
340
})
321
341
return bb
@@ -368,7 +388,8 @@ func (b *rdbBatch) ValueSize() int {
368
388
}
369
389
370
390
func (b * rdbBatch ) Reset () {
371
- C .rocksdb_writebatch_clear (b .b )
391
+ C .rocksdb_writebatch_destroy (b .b )
392
+ b .b = C .rocksdb_writebatch_create ()
372
393
b .data = nil
373
394
b .size = 0
374
395
}
0 commit comments