From 2c42bffc4a1acf54f4d9cff2938cce6a7ffcb028 Mon Sep 17 00:00:00 2001 From: linxGnu Date: Sat, 27 Aug 2022 09:27:23 +0900 Subject: [PATCH] Expose latest opts properties --- backup_test.go | 11 +---------- comparator.go | 28 +++++++++------------------- db_test.go | 31 +++++++++++++++++++++++++++++++ options.go | 26 ++++++++++++++++++++------ 4 files changed, 61 insertions(+), 35 deletions(-) diff --git a/backup_test.go b/backup_test.go index 0576cbe..b3e2189 100644 --- a/backup_test.go +++ b/backup_test.go @@ -1,7 +1,6 @@ package grocksdb import ( - "runtime" "testing" "github.com/stretchr/testify/require" @@ -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() diff --git a/comparator.go b/comparator.go index 87a6534..a681429 100644 --- a/comparator.go +++ b/comparator.go @@ -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. @@ -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. @@ -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() {} diff --git a/db_test.go b/db_test.go index 46c5c09..34fe90b 100644 --- a/db_test.go +++ b/db_test.go @@ -2,6 +2,7 @@ package grocksdb import ( "os" + "runtime" "strconv" "testing" @@ -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() diff --git a/options.go b/options.go index 69a75c2..6346aa3 100644 --- a/options.go +++ b/options.go @@ -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 @@ -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. @@ -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 } @@ -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 }