Skip to content

Use a malloc allocated bytes for set_iterate_upper_bound #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions options_read.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gorocksdb

// #include <stdlib.h>
// #include "rocksdb/c.h"
import "C"
import "unsafe"
Expand All @@ -23,7 +24,8 @@ const (
// ReadOptions represent all of the available options when reading from a
// database.
type ReadOptions struct {
c *C.rocksdb_readoptions_t
c *C.rocksdb_readoptions_t
cIterateUpperBound *C.char
}

// NewDefaultReadOptions creates a default ReadOptions object.
Expand All @@ -33,7 +35,7 @@ func NewDefaultReadOptions() *ReadOptions {

// NewNativeReadOptions creates a ReadOptions object.
func NewNativeReadOptions(c *C.rocksdb_readoptions_t) *ReadOptions {
return &ReadOptions{c}
return &ReadOptions{c: c}
}

// UnsafeGetReadOptions returns the underlying c read options object.
Expand Down Expand Up @@ -104,9 +106,15 @@ func (opts *ReadOptions) SetTailing(value bool) {
// implemented.
// Default: nullptr
func (opts *ReadOptions) SetIterateUpperBound(key []byte) {
cKey := byteToChar(key)
C.free(unsafe.Pointer(opts.cIterateUpperBound))
if key == nil {
opts.cIterateUpperBound = nil
} else {
opts.cIterateUpperBound = cByteSlice(key)
}

cKeyLen := C.size_t(len(key))
C.rocksdb_readoptions_set_iterate_upper_bound(opts.c, cKey, cKeyLen)
C.rocksdb_readoptions_set_iterate_upper_bound(opts.c, opts.cIterateUpperBound, cKeyLen)
}

// SetPinData specifies the value of "pin_data". If true, it keeps the blocks
Expand All @@ -132,5 +140,6 @@ func (opts *ReadOptions) SetReadaheadSize(value uint64) {
// Destroy deallocates the ReadOptions object.
func (opts *ReadOptions) Destroy() {
C.rocksdb_readoptions_destroy(opts.c)
opts.c = nil
C.free(unsafe.Pointer(opts.cIterateUpperBound))
*opts = ReadOptions{}
}