Skip to content
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

Add go.mod file, update for 6.16 #216

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
vendor
82 changes: 53 additions & 29 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ type Range struct {

// DB is a reusable handle to a RocksDB database on disk, created by Open.
type DB struct {
c *C.rocksdb_t
name string
opts *Options
c *C.rocksdb_t
closer func(*C.rocksdb_t)
name string
opts *Options
}

func dbClose(c *C.rocksdb_t) {
C.rocksdb_close(c)
}

// OpenDb opens a database with the specified options.
Expand All @@ -36,9 +41,10 @@ func OpenDb(opts *Options, name string) (*DB, error) {
return nil, errors.New(C.GoString(cErr))
}
return &DB{
name: name,
c: db,
opts: opts,
c: db,
closer: dbClose,
name: name,
opts: opts,
}, nil
}

Expand All @@ -55,9 +61,10 @@ func OpenDbWithTTL(opts *Options, name string, ttl int) (*DB, error) {
return nil, errors.New(C.GoString(cErr))
}
return &DB{
name: name,
c: db,
opts: opts,
c: db,
closer: dbClose,
name: name,
opts: opts,
}, nil
}

Expand All @@ -74,9 +81,10 @@ func OpenDbForReadOnly(opts *Options, name string, errorIfLogFileExist bool) (*D
return nil, errors.New(C.GoString(cErr))
}
return &DB{
name: name,
c: db,
opts: opts,
c: db,
closer: dbClose,
name: name,
opts: opts,
}, nil
}

Expand Down Expand Up @@ -133,9 +141,10 @@ func OpenDbColumnFamilies(
}

return &DB{
name: name,
c: db,
opts: opts,
c: db,
closer: dbClose,
name: name,
opts: opts,
}, cfHandles, nil
}

Expand Down Expand Up @@ -195,9 +204,10 @@ func OpenDbForReadOnlyColumnFamilies(
}

return &DB{
name: name,
c: db,
opts: opts,
c: db,
closer: dbClose,
name: name,
opts: opts,
}, cfHandles, nil
}

Expand Down Expand Up @@ -582,10 +592,11 @@ func (db *DB) DropColumnFamily(c *ColumnFamilyHandle) error {
//
// The keys counted will begin at Range.Start and end on the key before
// Range.Limit.
func (db *DB) GetApproximateSizes(ranges []Range) []uint64 {
sizes := make([]uint64, len(ranges))
func (db *DB) GetApproximateSizes(ranges []Range) (sizes []uint64, err error) {
var cErr *C.char
sizes = make([]uint64, len(ranges))
if len(ranges) == 0 {
return sizes
return
}

cStarts := make([]*C.char, len(ranges))
Expand Down Expand Up @@ -613,20 +624,27 @@ func (db *DB) GetApproximateSizes(ranges []Range) []uint64 {
&cStartLens[0],
&cLimits[0],
&cLimitLens[0],
(*C.uint64_t)(&sizes[0]))
(*C.uint64_t)(&sizes[0]),
&cErr,
)
if cErr != nil {
defer C.rocksdb_free(unsafe.Pointer(cErr))
err = errors.New(C.GoString(cErr))
}

return sizes
return
}

// GetApproximateSizesCF returns the approximate number of bytes of file system
// space used by one or more key ranges in the column family.
//
// The keys counted will begin at Range.Start and end on the key before
// Range.Limit.
func (db *DB) GetApproximateSizesCF(cf *ColumnFamilyHandle, ranges []Range) []uint64 {
sizes := make([]uint64, len(ranges))
func (db *DB) GetApproximateSizesCF(cf *ColumnFamilyHandle, ranges []Range) (sizes []uint64, err error) {
var cErr *C.char
sizes = make([]uint64, len(ranges))
if len(ranges) == 0 {
return sizes
return
}

cStarts := make([]*C.char, len(ranges))
Expand Down Expand Up @@ -655,9 +673,15 @@ func (db *DB) GetApproximateSizesCF(cf *ColumnFamilyHandle, ranges []Range) []ui
&cStartLens[0],
&cLimits[0],
&cLimitLens[0],
(*C.uint64_t)(&sizes[0]))
(*C.uint64_t)(&sizes[0]),
&cErr,
)
if cErr != nil {
defer C.rocksdb_free(unsafe.Pointer(cErr))
err = errors.New(C.GoString(cErr))
}

return sizes
return
}

// SetOptions dynamically changes options through the SetOptions API.
Expand Down Expand Up @@ -904,7 +928,7 @@ func (db *DB) NewCheckpoint() (*Checkpoint, error) {

// Close closes the database.
func (db *DB) Close() {
C.rocksdb_close(db.c)
db.closer(db.c)
}

// DestroyDb removes a database entirely, removing everything from the
Expand Down
18 changes: 12 additions & 6 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,18 @@ func TestDBGetApproximateSizes(t *testing.T) {
defer db.Close()

// no ranges
sizes := db.GetApproximateSizes(nil)
sizes, err := db.GetApproximateSizes(nil)
ensure.Nil(t, err)
ensure.DeepEqual(t, len(sizes), 0)

// range will nil start and limit
sizes = db.GetApproximateSizes([]Range{{Start: nil, Limit: nil}})
sizes, err = db.GetApproximateSizes([]Range{{Start: nil, Limit: nil}})
ensure.Nil(t, err)
ensure.DeepEqual(t, sizes, []uint64{0})

// valid range
sizes = db.GetApproximateSizes([]Range{{Start: []byte{0x00}, Limit: []byte{0xFF}}})
sizes, err = db.GetApproximateSizes([]Range{{Start: []byte{0x00}, Limit: []byte{0xFF}}})
ensure.Nil(t, err)
ensure.DeepEqual(t, sizes, []uint64{0})
}

Expand All @@ -232,14 +235,17 @@ func TestDBGetApproximateSizesCF(t *testing.T) {
ensure.Nil(t, err)

// no ranges
sizes := db.GetApproximateSizesCF(cf, nil)
sizes, err := db.GetApproximateSizesCF(cf, nil)
ensure.Nil(t, err)
ensure.DeepEqual(t, len(sizes), 0)

// range will nil start and limit
sizes = db.GetApproximateSizesCF(cf, []Range{{Start: nil, Limit: nil}})
sizes, err = db.GetApproximateSizesCF(cf, []Range{{Start: nil, Limit: nil}})
ensure.Nil(t, err)
ensure.DeepEqual(t, sizes, []uint64{0})

// valid range
sizes = db.GetApproximateSizesCF(cf, []Range{{Start: []byte{0x00}, Limit: []byte{0xFF}}})
sizes, err = db.GetApproximateSizesCF(cf, []Range{{Start: []byte{0x00}, Limit: []byte{0xFF}}})
ensure.Nil(t, err)
ensure.DeepEqual(t, sizes, []uint64{0})
}
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/tecbot/gorocksdb

go 1.13

require (
github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 // indirect
github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4 // indirect
github.com/stretchr/testify v1.7.0
)
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c h1:8ISkoahWXwZR41ois5lSJBSVw4D0OV19Ht/JSTzvSv0=
github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c/go.mod h1:Yg+htXGokKKdzcwhuNDwVvN+uBxDGXJ7G/VN1d8fa64=
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 h1:JWuenKqqX8nojtoVVWjGfOF9635RETekkoH6Cc9SX0A=
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052/go.mod h1:UbMTZqLaRiH3MsBH8va0n7s1pQYcu3uTb8G4tygF4Zg=
github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4 h1:7HZCaLC5+BZpmbhCOZJ293Lz68O7PYrF2EzeiFMwCLk=
github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4/go.mod h1:5tD+neXqOorC30/tWg0LCSkrqj/AR6gu8yY8/fpw1q0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
33 changes: 33 additions & 0 deletions iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,36 @@ func TestIterator(t *testing.T) {
ensure.Nil(t, iter.Err())
ensure.DeepEqual(t, actualKeys, givenKeys)
}

func TestIteratorWithRange(t *testing.T) {
db := newTestDB(t, "TestIterator", nil)
defer db.Close()

// insert keys
givenKeys := [][]byte{
[]byte("key1"),
[]byte("key2"),
[]byte("key3"),
[]byte("key4"),
[]byte("key5"),
}
wo := NewDefaultWriteOptions()
for _, k := range givenKeys {
ensure.Nil(t, db.Put(wo, k, []byte("val")))
}

ro := NewDefaultReadOptions()
ro.SetIterateLowerBound([]byte("key2"))
ro.SetIterateUpperBound([]byte("key4"))

iter := db.NewIterator(ro)
defer iter.Close()
var actualKeys [][]byte
for iter.SeekToFirst(); iter.Valid(); iter.Next() {
key := make([]byte, 4)
copy(key, iter.Key().Data())
actualKeys = append(actualKeys, key)
}
ensure.Nil(t, iter.Err())
ensure.DeepEqual(t, len(actualKeys), 2)
}
18 changes: 16 additions & 2 deletions options_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,29 @@ func (opts *ReadOptions) SetTailing(value bool) {
// not a valid entry. If iterator_extractor is not null, the Seek target
// and iterator_upper_bound need to have the same prefix.
// This is because ordering is not guaranteed outside of prefix domain.
// There is no lower bound on the iterator. If needed, that can be easily
// implemented.
// Default: nullptr
func (opts *ReadOptions) SetIterateUpperBound(key []byte) {
cKey := byteToChar(key)
cKeyLen := C.size_t(len(key))
C.rocksdb_readoptions_set_iterate_upper_bound(opts.c, cKey, cKeyLen)
}

// SetIterateLowerBound specifies "iterate_lower_bound", which defines
// the smallest key at which the backward iterator can return an entry.
// Once the bound is passed, Valid() will be false.
// `iterate_lower_bound` is inclusive ie the bound value is a valid
// entry.
//
// If prefix_extractor is not null, the Seek target and `iterate_lower_bound`
// need to have the same prefix. This is because ordering is not guaranteed
// outside of prefix domain.
// Default: nullptr
func (opts *ReadOptions) SetIterateLowerBound(key []byte) {
cKey := byteToChar(key)
cKeyLen := C.size_t(len(key))
C.rocksdb_readoptions_set_iterate_lower_bound(opts.c, cKey, cKeyLen)
}

// SetPinData specifies the value of "pin_data". If true, it keeps the blocks
// loaded by the iterator pinned in memory as long as the iterator is not deleted,
// If used when reading from tables created with
Expand Down
28 changes: 28 additions & 0 deletions options_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,31 @@ func (opts *TransactionOptions) Destroy() {
C.rocksdb_transaction_options_destroy(opts.c)
opts.c = nil
}

// OptimisticTransactionOptions represent all of the available options for
// a optimistic transaction on the database.
type OptimisticTransactionOptions struct {
c *C.rocksdb_optimistictransaction_options_t
}

// NewDefaultOptimisticTransactionOptions creates a default OptimisticTransactionOptions object.
func NewDefaultOptimisticTransactionOptions() *OptimisticTransactionOptions {
return NewNativeOptimisticTransactionOptions(C.rocksdb_optimistictransaction_options_create())
}

// NewNativeOptimisticTransactionOptions creates a OptimisticTransactionOptions object.
func NewNativeOptimisticTransactionOptions(c *C.rocksdb_optimistictransaction_options_t) *OptimisticTransactionOptions {
return &OptimisticTransactionOptions{c}
}

// Destroy deallocates the OptimisticTransactionOptions object.
func (opts *OptimisticTransactionOptions) Destroy() {
C.rocksdb_optimistictransaction_options_destroy(opts.c)
opts.c = nil
}

// SetSetSnapshot to true is the same as calling
// Transaction::SetSnapshot().
func (opts *OptimisticTransactionOptions) SetSetSnapshot(value bool) {
C.rocksdb_optimistictransaction_options_set_set_snapshot(opts.c, boolToChar(value))
}
Loading