Skip to content

Commit

Permalink
Expose latest opts properties (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
linxGnu authored Aug 27, 2022
1 parent b8d8d44 commit c88c046
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 35 deletions.
11 changes: 1 addition & 10 deletions backup_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package grocksdb

import (
"runtime"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -112,15 +111,7 @@ func TestBackupEngine(t *testing.T) {
// try to reopen restored db
backupDB, err := OpenDb(db.opts, dir)
require.Nil(t, err)

o, _ := LoadLatestOptions(dir, NewMemEnv(), true, NewLRUCache(1))
runtime.GC()
if o != nil {
o.Destroy()
}

_, err = LoadLatestOptions("", nil, true, nil)
require.Error(t, err)
defer backupDB.Close()

r := NewDefaultReadOptions()
defer r.Destroy()
Expand Down
28 changes: 9 additions & 19 deletions comparator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ package grocksdb
// #include "grocksdb.h"
import "C"

import (
"bytes"
)

// Comparing functor.
//
// Three-way comparison. Returns value:
// < 0 iff "a" < "b",
// == 0 iff "a" == "b",
// > 0 iff "a" > "b"
//
// < 0 iff "a" < "b",
// == 0 iff "a" == "b",
// > 0 iff "a" > "b"
//
// Note that Compare(a, b) also compares timestamp if timestamp size is
// non-zero. For the same user key with different timestamps, larger (newer)
// timestamp comes first.
Expand All @@ -22,9 +20,10 @@ type Comparing = func(a, b []byte) int
// ComparingWithoutTimestamp functor.
//
// Three-way comparison. Returns value:
// < 0 if "a" < "b",
// == 0 if "a" == "b",
// > 0 if "a" > "b"
//
// < 0 if "a" < "b",
// == 0 if "a" == "b",
// > 0 if "a" > "b"
type ComparingWithoutTimestamp = func(a []byte, aHasTs bool, b []byte, bHasTs bool) int

// NewComparator creates a Comparator object which contains native c-comparator pointer.
Expand Down Expand Up @@ -115,12 +114,3 @@ func gorocksdb_comparator_compare_without_ts(idx int, cKeyA *C.char, cKeyALen C.
func gorocksdb_comparator_name(idx int) *C.char {
return comperators.Get(idx).(comperatorWrapper).name
}

// for testing purpose only
type testBytesReverseComparator struct{}

func (cmp *testBytesReverseComparator) Name() string { return "grocksdb.bytes-reverse" }
func (cmp *testBytesReverseComparator) Compare(a, b []byte) int {
return bytes.Compare(a, b) * -1
}
func (cmp *testBytesReverseComparator) Destroy() {}
31 changes: 31 additions & 0 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package grocksdb

import (
"os"
"runtime"
"strconv"
"testing"

Expand Down Expand Up @@ -282,6 +283,36 @@ func TestDBMultiGet(t *testing.T) {
require.EqualValues(t, values[3].Data(), givenVal3)
}

func TestLoadLatestOpts(t *testing.T) {
dir := t.TempDir()

opts := NewDefaultOptions()
defer opts.Destroy()

opts.SetCreateIfMissing(true)
for i := 0; i < 100; i++ {
opts.SetEnv(NewDefaultEnv())
}

db, err := OpenDb(opts, dir)
require.NoError(t, err)
_, err = db.CreateColumnFamily(opts, "abc")
require.NoError(t, err)
require.NoError(t, db.Flush(NewDefaultFlushOptions()))
db.Close()

o, err := LoadLatestOptions(dir, NewDefaultEnv(), true, NewLRUCache(1))
runtime.GC()
require.NoError(t, err)
require.NotEmpty(t, o.ColumnFamilyNames())
require.NotEmpty(t, o.ColumnFamilyOpts())
o.Destroy()
runtime.GC()

_, err = LoadLatestOptions("", nil, true, nil)
require.Error(t, err)
}

func TestDBGetApproximateSizes(t *testing.T) {
db := newTestDB(t, nil)
defer db.Close()
Expand Down
26 changes: 20 additions & 6 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const (
// Options represent all of the available options when opening a database with Open.
type Options struct {
c *C.rocksdb_options_t
env *Env
env *C.rocksdb_env_t

// Hold references for GC.
bbto *BlockBasedTableOptions
Expand Down Expand Up @@ -306,13 +306,16 @@ func (opts *Options) SetDBPaths(dbpaths []*DBPath) {
// SetEnv sets the specified object to interact with the environment,
// e.g. to read/write files, schedule background work, etc.
//
// Note: move semantic. Don't use env after calling this function
// NOTE: move semantic. Don't use env after calling this function
func (opts *Options) SetEnv(env *Env) {
if opts.env != nil {
opts.env.Destroy()
C.rocksdb_env_destroy(opts.env)
}
opts.env = env

C.rocksdb_options_set_env(opts.c, env.c)
opts.env = env.c

env.c = nil
}

// SetInfoLogLevel sets the info log level.
Expand Down Expand Up @@ -2421,9 +2424,9 @@ func (opts *Options) Destroy() {
opts.cmo = nil

if opts.env != nil {
opts.env.Destroy()
C.rocksdb_env_destroy(opts.env)
opts.env = nil
}
opts.env = nil

opts.bbto = nil
}
Expand Down Expand Up @@ -2491,7 +2494,18 @@ func LoadLatestOptions(path string, env *Env, ignoreUnknownOpts bool, cache *Cac
return
}

// ColumnFamilyNames gets column family names.
func (l *LatestOptions) ColumnFamilyNames() []string {
return l.cfNames
}

// ColumnFamilyOpts returns corresponding options of column families.
func (l *LatestOptions) ColumnFamilyOpts() []Options {
return l.cfOptions
}

// Destroy release underlying db_options, column_family_names, and column_family_options.
func (l *LatestOptions) Destroy() {
C.rocksdb_load_latest_options_destroy(l.opts.c, l.cfNames_, l.cfOptions_, C.size_t(len(l.cfNames)))
l.opts.c = nil
}

0 comments on commit c88c046

Please sign in to comment.