From e75f6e4b28d8310b992da77c86a13761e5068ecb Mon Sep 17 00:00:00 2001 From: yihuang Date: Mon, 8 Apr 2024 23:18:34 +0800 Subject: [PATCH] feat: allow full control on rocksdb db opening (#104) * feat: allow full control on rocksdb db opening * Update prefixdb_test.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- CHANGELOG.md | 4 ++++ prefixdb_test.go | 7 +++++-- rocksdb.go | 12 +++++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa75ee0..849eb8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## UNRELEASED + +* Allow full control in rocksdb opening + ## [v1.0.2] - 2024-02-26 * Downgrade Go version in go.mod to 1.19 diff --git a/prefixdb_test.go b/prefixdb_test.go index 8e19070..e52df82 100644 --- a/prefixdb_test.go +++ b/prefixdb_test.go @@ -2,9 +2,9 @@ package db import ( "bytes" + "crypto/rand" "encoding/binary" "fmt" - "math/rand" "path/filepath" "sync" "testing" @@ -32,7 +32,10 @@ func taskKey(i, k int) []byte { func randomValue() []byte { b := make([]byte, 16) - rand.Read(b) //nolint:staticcheck,gosec + _, err := rand.Read(b) + if err != nil { + panic(fmt.Sprintf("random value generation failed: %v", err)) + } return b } diff --git a/rocksdb.go b/rocksdb.go index ff6d6e3..a21ae1c 100644 --- a/rocksdb.go +++ b/rocksdb.go @@ -71,7 +71,17 @@ func NewRocksDBWithOptions(name string, dir string, opts *grocksdb.Options) (*Ro wo := grocksdb.NewDefaultWriteOptions() woSync := grocksdb.NewDefaultWriteOptions() woSync.SetSync(true) - return NewRocksDBWithRaw(db, ro, wo, woSync), nil + return NewRocksDBWithRawDB(db, ro, wo, woSync), nil +} + +// NewRocksDBWithRawDB lets caller has full control on how the db instance is constructed +func NewRocksDBWithRawDB( + db *grocksdb.DB, + ro *grocksdb.ReadOptions, + wo *grocksdb.WriteOptions, + woSync *grocksdb.WriteOptions, +) *RocksDB { + return NewRocksDBWithRaw(db, ro, wo, woSync) } // NewRocksDBWithRaw is useful if user want to create the db in read-only or seconday-standby mode,