Skip to content

Commit

Permalink
Support setting default column family.
Browse files Browse the repository at this point in the history
  • Loading branch information
bhartnett committed Feb 27, 2024
1 parent 80804ba commit 53bc223
Show file tree
Hide file tree
Showing 21 changed files with 77 additions and 38 deletions.
2 changes: 1 addition & 1 deletion rocksdb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

import ./rocksdb/[backup, rocksdb, rocksiterator, transactiondb, writebatch]

export backup, rocksdb, rocksiterator, transactiondb, writebatch
export backup, rocksdb, rocksiterator, transactiondb, writebatch
2 changes: 0 additions & 2 deletions rocksdb/backup.nim
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,3 @@ proc close*(backupEngine: var BackupEngineRef) =
if not backupEngine.isClosed():
rocksdb_backup_engine_close(backupEngine.cPtr)
backupEngine.cPtr = nil


2 changes: 1 addition & 1 deletion rocksdb/columnfamily/cfhandle.nim
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ proc cPtr*(handle: ColFamilyHandleRef): ColFamilyHandlePtr =
proc close*(handle: var ColFamilyHandleRef) =
if not handle.isClosed():
rocksdb_column_family_handle_destroy(handle.cPtr)
handle.cPtr = nil
handle.cPtr = nil
1 change: 0 additions & 1 deletion rocksdb/columnfamily/cfopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,3 @@ proc close*(cfOpts: var ColFamilyOptionsRef) =
if not cfOpts.isClosed():
rocksdb_options_destroy(cfOpts.cPtr)
cfOpts.cPtr = nil

2 changes: 1 addition & 1 deletion rocksdb/internal/cftable.nim
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ proc close*(table: var ColFamilyTableRef) =
if not table.isClosed():
for _, v in table.columnFamilies.mpairs():
v.close()
table.columnFamilies = nil
table.columnFamilies = nil
2 changes: 1 addition & 1 deletion rocksdb/internal/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ template bailOnErrors*(errors: cstring): auto =
if not errors.isNil:
let res = err($(errors))
rocksdb_free(errors)
return res
return res
2 changes: 1 addition & 1 deletion rocksdb/lib/librocksdb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3202,4 +3202,4 @@ proc rocksdb_wait_for_compact_options_set_timeout*(
importc: "rocksdb_wait_for_compact_options_set_timeout", dynlib: librocksdb.}
proc rocksdb_wait_for_compact_options_get_timeout*(
opt: ptr rocksdb_wait_for_compact_options_t): uint64 {.cdecl,
importc: "rocksdb_wait_for_compact_options_get_timeout", dynlib: librocksdb.}
importc: "rocksdb_wait_for_compact_options_get_timeout", dynlib: librocksdb.}
1 change: 0 additions & 1 deletion rocksdb/options/backupopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,3 @@ proc close*(engineOpts: var BackupEngineOptionsRef) =
if not engineOpts.isClosed():
rocksdb_options_destroy(engineOpts.cPtr)
engineOpts.cPtr = nil

1 change: 0 additions & 1 deletion rocksdb/options/dbopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,3 @@ proc close*(dbOpts: var DbOptionsRef) =
if not dbOpts.isClosed():
rocksdb_options_destroy(dbOpts.cPtr)
dbOpts.cPtr = nil

1 change: 0 additions & 1 deletion rocksdb/options/readopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,3 @@ proc close*(readOpts: var ReadOptionsRef) =
if not readOpts.isClosed():
rocksdb_readoptions_destroy(readOpts.cPtr)
readOpts.cPtr = nil

1 change: 0 additions & 1 deletion rocksdb/options/writeopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,3 @@ proc close*(writeOpts: var WriteOptionsRef) =
if not writeOpts.isClosed():
rocksdb_writeoptions_destroy(writeOpts.cPtr)
writeOpts.cPtr = nil

37 changes: 24 additions & 13 deletions rocksdb/rocksdb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type
path: string
dbOpts: DbOptionsRef
readOpts: ReadOptionsRef
defaultCfName: string
cfTable: ColFamilyTableRef

RocksDbReadOnlyRef* = ref object of RocksDbRef
Expand All @@ -57,7 +58,6 @@ proc openRocksDb*(
cfOpts = columnFamilies.mapIt(it.options.cPtr)
columnFamilyHandles = newSeq[ColFamilyHandlePtr](columnFamilies.len)
errors: cstring

let rocksDbPtr = rocksdb_open_column_families(
dbOpts.cPtr,
path.cstring,
Expand All @@ -74,6 +74,7 @@ proc openRocksDb*(
dbOpts: dbOpts,
readOpts: readOpts,
writeOpts: writeOpts,
defaultCfName: DEFAULT_COLUMN_FAMILY_NAME,
cfTable: newColFamilyTable(cfNames.mapIt($it), columnFamilyHandles))
ok(db)

Expand All @@ -92,7 +93,6 @@ proc openRocksDbReadOnly*(
cfOpts = columnFamilies.mapIt(it.options.cPtr)
columnFamilyHandles = newSeq[ColFamilyHandlePtr](columnFamilies.len)
errors: cstring

let rocksDbPtr = rocksdb_open_for_read_only_column_families(
dbOpts.cPtr,
path.cstring,
Expand All @@ -109,6 +109,7 @@ proc openRocksDbReadOnly*(
path: path,
dbOpts: dbOpts,
readOpts: readOpts,
defaultCfName: DEFAULT_COLUMN_FAMILY_NAME,
cfTable: newColFamilyTable(cfNames.mapIt($it), columnFamilyHandles))
ok(db)

Expand All @@ -119,11 +120,18 @@ proc cPtr*(db: RocksDbRef): RocksDbPtr =
doAssert not db.isClosed()
db.cPtr

proc withDefaultColFamily*(db: RocksDbRef | RocksDbReadWriteRef, name: string): auto =
db.defaultCfName = name
db

proc defaultColFamily*(db: RocksDbRef): string =
db.defaultCfName

proc get*(
db: RocksDbRef,
key: openArray[byte],
onData: DataProc,
columnFamily = DEFAULT_COLUMN_FAMILY_NAME): RocksDBResult[bool] =
columnFamily = db.defaultCfName): RocksDBResult[bool] =

if key.len() == 0:
return err("rocksdb: key is empty")
Expand Down Expand Up @@ -156,7 +164,7 @@ proc get*(
proc get*(
db: RocksDbRef,
key: openArray[byte],
columnFamily = DEFAULT_COLUMN_FAMILY_NAME): RocksDBResult[seq[byte]] =
columnFamily = db.defaultCfName): RocksDBResult[seq[byte]] =

var dataRes: RocksDBResult[seq[byte]]
proc onData(data: openArray[byte]) =
Expand All @@ -169,9 +177,9 @@ proc get*(
dataRes.err(res.error())

proc put*(
db: var RocksDbReadWriteRef,
db: RocksDbReadWriteRef,
key, val: openArray[byte],
columnFamily = DEFAULT_COLUMN_FAMILY_NAME): RocksDBResult[void] =
columnFamily = db.defaultCfName): RocksDBResult[void] =

if key.len() == 0:
return err("rocksdb: key is empty")
Expand All @@ -197,17 +205,17 @@ proc put*(
proc keyExists*(
db: RocksDbRef,
key: openArray[byte],
columnFamily = DEFAULT_COLUMN_FAMILY_NAME): RocksDBResult[bool] =
columnFamily = db.defaultCfName): RocksDBResult[bool] =

# TODO: Call rocksdb_key_may_exist_cf to improve performance for the case
# when the key does not exist

db.get(key, proc(data: openArray[byte]) = discard, columnFamily)

proc delete*(
db: var RocksDbReadWriteRef,
db: RocksDbReadWriteRef,
key: openArray[byte],
columnFamily = DEFAULT_COLUMN_FAMILY_NAME): RocksDBResult[void] =
columnFamily = db.defaultCfName): RocksDBResult[void] =

if key.len() == 0:
return err("rocksdb: key is empty")
Expand All @@ -230,7 +238,7 @@ proc delete*(

proc openIterator*(
db: RocksDbRef,
columnFamily = DEFAULT_COLUMN_FAMILY_NAME): RocksDBResult[RocksIteratorRef] =
columnFamily = db.defaultCfName): RocksDBResult[RocksIteratorRef] =
doAssert not db.isClosed()

let cfHandle = db.cfTable.get(columnFamily)
Expand All @@ -244,9 +252,12 @@ proc openIterator*(

ok(newRocksIterator(rocksIterPtr))

proc openWriteBatch*(db: RocksDbReadWriteRef): WriteBatchRef =
proc openWriteBatch*(
db: RocksDbReadWriteRef,
columnFamily = db.defaultCfName): WriteBatchRef =
doAssert not db.isClosed()
newWriteBatch(db.cfTable)

newWriteBatch(db.cfTable, columnFamily)

proc write*(db: var RocksDbReadWriteRef, updates: WriteBatchRef): RocksDBResult[void] =
doAssert not db.isClosed()
Expand All @@ -271,4 +282,4 @@ proc close*(db: RocksDbRef) =
db.RocksDbReadWriteRef.writeOpts.close()

rocksdb_close(db.cPtr)
db.cPtr = nil
db.cPtr = nil
2 changes: 1 addition & 1 deletion rocksdb/rocksresult.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ export
type
RocksDBResult*[T] = Result[T, string]

DataProc* = proc(val: openArray[byte]) {.gcsafe, raises: [].}
DataProc* = proc(val: openArray[byte]) {.gcsafe, raises: [].}
5 changes: 3 additions & 2 deletions rocksdb/transactiondb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ proc beginTransaction*(
db: TransactionDbRef,
readOpts = defaultReadOptions(),
writeOpts = defaultWriteOptions(),
txOpts = defaultTransactionOptions()): TransactionRef =
txOpts = defaultTransactionOptions(),
columnFamily = DEFAULT_COLUMN_FAMILY_NAME): TransactionRef =
doAssert not db.isClosed()

let txPtr = rocksdb_transaction_begin(
Expand All @@ -88,7 +89,7 @@ proc beginTransaction*(
txOpts.cPtr,
nil)

newTransaction(txPtr, readOpts, writeOpts, txOpts, db.cfTable)
newTransaction(txPtr, readOpts, writeOpts, txOpts, columnFamily, db.cfTable)

proc close*(db: var TransactionDbRef) =
if not db.isClosed():
Expand Down
18 changes: 14 additions & 4 deletions rocksdb/transactions/transaction.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,40 @@ type
readOpts: ReadOptionsRef
writeOpts: WriteOptionsRef
txOpts: TransactionOptionsRef
defaultCfName: string
cfTable: ColFamilyTableRef

proc newTransaction*(
cPtr: TransactionPtr,
readOpts: ReadOptionsRef,
writeOpts: WriteOptionsRef,
txOpts: TransactionOptionsRef,
defaultCfName: string,
cfTable: ColFamilyTableRef): TransactionRef =

TransactionRef(
cPtr: cPtr,
readOpts: readOpts,
writeOpts: writeOpts,
txOpts: txOpts,
defaultCfName: defaultCfName,
cfTable: cfTable)

template isClosed*(tx: TransactionRef): bool =
tx.cPtr.isNil()

proc withDefaultColFamily*(tx: var TransactionRef, name: string): TransactionRef =
tx.defaultCfName = name
tx

proc defaultColFamily*(tx: TransactionRef): string =
tx.defaultCfName

proc get*(
tx: TransactionRef,
key: openArray[byte],
onData: DataProc,
columnFamily = DEFAULT_COLUMN_FAMILY_NAME): RocksDBResult[bool] =
columnFamily = tx.defaultCfName): RocksDBResult[bool] =

if key.len() == 0:
return err("rocksdb: key is empty")
Expand Down Expand Up @@ -84,7 +94,7 @@ proc get*(
proc get*(
tx: TransactionRef,
key: openArray[byte],
columnFamily = DEFAULT_COLUMN_FAMILY_NAME): RocksDBResult[seq[byte]] =
columnFamily = tx.defaultCfName): RocksDBResult[seq[byte]] =

var dataRes: RocksDBResult[seq[byte]]
proc onData(data: openArray[byte]) =
Expand All @@ -99,7 +109,7 @@ proc get*(
proc put*(
tx: var TransactionRef,
key, val: openArray[byte],
columnFamily = DEFAULT_COLUMN_FAMILY_NAME): RocksDBResult[void] =
columnFamily = tx.defaultCfName): RocksDBResult[void] =

if key.len() == 0:
return err("rocksdb: key is empty")
Expand All @@ -124,7 +134,7 @@ proc put*(
proc delete*(
tx: var TransactionRef,
key: openArray[byte],
columnFamily = DEFAULT_COLUMN_FAMILY_NAME): RocksDBResult[void] =
columnFamily = tx.defaultCfName): RocksDBResult[void] =

if key.len() == 0:
return err("rocksdb: key is empty")
Expand Down
1 change: 0 additions & 1 deletion rocksdb/transactions/txdbopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,3 @@ proc close*(txDbOpts: var TransactionDbOptionsRef) =
if not txDbOpts.isClosed():
rocksdb_transactiondb_options_destroy(txDbOpts.cPtr)
txDbOpts.cPtr = nil

1 change: 0 additions & 1 deletion rocksdb/transactions/txopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,3 @@ proc close*(txOpts: var TransactionOptionsRef) =
if not txOpts.isClosed():
rocksdb_transaction_options_destroy(txOpts.cPtr)
txOpts.cPtr = nil

4 changes: 3 additions & 1 deletion rocksdb/writebatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ type

WriteBatchRef* = ref object
cPtr: WriteBatchPtr
defaultCfName: string
cfTable: ColFamilyTableRef

proc newWriteBatch*(cfTable: ColFamilyTableRef): WriteBatchRef =
proc newWriteBatch*(cfTable: ColFamilyTableRef, defaultCfName: string): WriteBatchRef =
WriteBatchRef(
cPtr: rocksdb_writebatch_create(),
defaultCfName: defaultCfName,
cfTable: cfTable)

template isClosed*(batch: WriteBatchRef): bool =
Expand Down
2 changes: 1 addition & 1 deletion tests/test_backup.nim
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ suite "BackupEngineRef Tests":
engine.close()
check engine.isClosed()
engine.close()
check engine.isClosed()
check engine.isClosed()
15 changes: 14 additions & 1 deletion tests/test_helper.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import
std/sequtils,
../rocksdb/backup,
../rocksdb/rocksdb
../rocksdb/rocksdb,
../rocksdb/transactiondb


proc initReadWriteDb*(
Expand Down Expand Up @@ -43,4 +44,16 @@ proc initBackupEngine*(path: string): BackupEngineRef =

let res = openBackupEngine(path)
doAssert res.isOk()
res.value()

proc initTransactionDb*(
path: string,
columnFamilyNames = @["default"]): TransactionDbRef =

let res = openTransactionDb(
path,
columnFamilies = columnFamilyNames.mapIt(initColFamilyDescriptor(it)))
if res.isErr():
echo res.error()
doAssert res.isOk()
res.value()
13 changes: 12 additions & 1 deletion tests/test_rocksdb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,15 @@ suite "RocksDbRef Tests":

db.close()
check db.isClosed()
removeDir(dbDir)
removeDir(dbDir)

test "Test withDefaultColFamily":
var
dbDir = mkdtemp()
db = initReadWriteDb(dbDir,
columnFamilyNames = @[CF_DEFAULT, CF_OTHER]).withDefaultColFamily(CF_OTHER)

check:
db.put(key, val).isOk()
not db.keyExists(key, CF_DEFAULT).get()
db.keyExists(key, CF_OTHER).get()

0 comments on commit 53bc223

Please sign in to comment.