diff --git a/rocksdb.nim b/rocksdb.nim index e924873..df9871c 100644 --- a/rocksdb.nim +++ b/rocksdb.nim @@ -9,4 +9,4 @@ import ./rocksdb/[backup, rocksdb, rocksiterator, transactiondb, writebatch] -export backup, rocksdb, rocksiterator, transactiondb, writebatch \ No newline at end of file +export backup, rocksdb, rocksiterator, transactiondb, writebatch diff --git a/rocksdb/backup.nim b/rocksdb/backup.nim index a797914..2591362 100644 --- a/rocksdb/backup.nim +++ b/rocksdb/backup.nim @@ -90,5 +90,3 @@ proc close*(backupEngine: var BackupEngineRef) = if not backupEngine.isClosed(): rocksdb_backup_engine_close(backupEngine.cPtr) backupEngine.cPtr = nil - - diff --git a/rocksdb/columnfamily/cfhandle.nim b/rocksdb/columnfamily/cfhandle.nim index 1a0a625..9b8dc60 100644 --- a/rocksdb/columnfamily/cfhandle.nim +++ b/rocksdb/columnfamily/cfhandle.nim @@ -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 \ No newline at end of file + handle.cPtr = nil diff --git a/rocksdb/columnfamily/cfopts.nim b/rocksdb/columnfamily/cfopts.nim index be04b40..d5eed30 100644 --- a/rocksdb/columnfamily/cfopts.nim +++ b/rocksdb/columnfamily/cfopts.nim @@ -50,4 +50,3 @@ proc close*(cfOpts: var ColFamilyOptionsRef) = if not cfOpts.isClosed(): rocksdb_options_destroy(cfOpts.cPtr) cfOpts.cPtr = nil - diff --git a/rocksdb/internal/cftable.nim b/rocksdb/internal/cftable.nim index c397eb3..20c7fb6 100644 --- a/rocksdb/internal/cftable.nim +++ b/rocksdb/internal/cftable.nim @@ -41,4 +41,4 @@ proc close*(table: var ColFamilyTableRef) = if not table.isClosed(): for _, v in table.columnFamilies.mpairs(): v.close() - table.columnFamilies = nil \ No newline at end of file + table.columnFamilies = nil diff --git a/rocksdb/internal/utils.nim b/rocksdb/internal/utils.nim index 045f3ba..0fa116f 100644 --- a/rocksdb/internal/utils.nim +++ b/rocksdb/internal/utils.nim @@ -18,4 +18,4 @@ template bailOnErrors*(errors: cstring): auto = if not errors.isNil: let res = err($(errors)) rocksdb_free(errors) - return res \ No newline at end of file + return res diff --git a/rocksdb/lib/librocksdb.nim b/rocksdb/lib/librocksdb.nim index db11310..b64faef 100644 --- a/rocksdb/lib/librocksdb.nim +++ b/rocksdb/lib/librocksdb.nim @@ -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.} \ No newline at end of file + importc: "rocksdb_wait_for_compact_options_get_timeout", dynlib: librocksdb.} diff --git a/rocksdb/options/backupopts.nim b/rocksdb/options/backupopts.nim index c7815fa..2161544 100644 --- a/rocksdb/options/backupopts.nim +++ b/rocksdb/options/backupopts.nim @@ -38,4 +38,3 @@ proc close*(engineOpts: var BackupEngineOptionsRef) = if not engineOpts.isClosed(): rocksdb_options_destroy(engineOpts.cPtr) engineOpts.cPtr = nil - diff --git a/rocksdb/options/dbopts.nim b/rocksdb/options/dbopts.nim index e99e120..4edf22e 100644 --- a/rocksdb/options/dbopts.nim +++ b/rocksdb/options/dbopts.nim @@ -82,4 +82,3 @@ proc close*(dbOpts: var DbOptionsRef) = if not dbOpts.isClosed(): rocksdb_options_destroy(dbOpts.cPtr) dbOpts.cPtr = nil - diff --git a/rocksdb/options/readopts.nim b/rocksdb/options/readopts.nim index 8cc86b9..05be81c 100644 --- a/rocksdb/options/readopts.nim +++ b/rocksdb/options/readopts.nim @@ -38,4 +38,3 @@ proc close*(readOpts: var ReadOptionsRef) = if not readOpts.isClosed(): rocksdb_readoptions_destroy(readOpts.cPtr) readOpts.cPtr = nil - diff --git a/rocksdb/options/writeopts.nim b/rocksdb/options/writeopts.nim index 5a635f9..abdfe7d 100644 --- a/rocksdb/options/writeopts.nim +++ b/rocksdb/options/writeopts.nim @@ -38,4 +38,3 @@ proc close*(writeOpts: var WriteOptionsRef) = if not writeOpts.isClosed(): rocksdb_writeoptions_destroy(writeOpts.cPtr) writeOpts.cPtr = nil - diff --git a/rocksdb/rocksdb.nim b/rocksdb/rocksdb.nim index a19e815..f858190 100644 --- a/rocksdb/rocksdb.nim +++ b/rocksdb/rocksdb.nim @@ -35,6 +35,7 @@ type path: string dbOpts: DbOptionsRef readOpts: ReadOptionsRef + defaultCfName: string cfTable: ColFamilyTableRef RocksDbReadOnlyRef* = ref object of RocksDbRef @@ -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, @@ -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) @@ -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, @@ -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) @@ -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") @@ -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]) = @@ -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") @@ -197,7 +205,7 @@ 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 @@ -205,9 +213,9 @@ proc keyExists*( 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") @@ -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) @@ -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() @@ -271,4 +282,4 @@ proc close*(db: RocksDbRef) = db.RocksDbReadWriteRef.writeOpts.close() rocksdb_close(db.cPtr) - db.cPtr = nil \ No newline at end of file + db.cPtr = nil diff --git a/rocksdb/rocksresult.nim b/rocksdb/rocksresult.nim index d1b1438..290005d 100644 --- a/rocksdb/rocksresult.nim +++ b/rocksdb/rocksresult.nim @@ -18,4 +18,4 @@ export type RocksDBResult*[T] = Result[T, string] - DataProc* = proc(val: openArray[byte]) {.gcsafe, raises: [].} \ No newline at end of file + DataProc* = proc(val: openArray[byte]) {.gcsafe, raises: [].} diff --git a/rocksdb/transactiondb.nim b/rocksdb/transactiondb.nim index 091d86b..8d306e6 100644 --- a/rocksdb/transactiondb.nim +++ b/rocksdb/transactiondb.nim @@ -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( @@ -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(): diff --git a/rocksdb/transactions/transaction.nim b/rocksdb/transactions/transaction.nim index 06c28eb..f5adaea 100644 --- a/rocksdb/transactions/transaction.nim +++ b/rocksdb/transactions/transaction.nim @@ -28,6 +28,7 @@ type readOpts: ReadOptionsRef writeOpts: WriteOptionsRef txOpts: TransactionOptionsRef + defaultCfName: string cfTable: ColFamilyTableRef proc newTransaction*( @@ -35,6 +36,7 @@ proc newTransaction*( readOpts: ReadOptionsRef, writeOpts: WriteOptionsRef, txOpts: TransactionOptionsRef, + defaultCfName: string, cfTable: ColFamilyTableRef): TransactionRef = TransactionRef( @@ -42,16 +44,24 @@ proc newTransaction*( 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") @@ -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]) = @@ -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") @@ -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") diff --git a/rocksdb/transactions/txdbopts.nim b/rocksdb/transactions/txdbopts.nim index e8b84cf..729f3e1 100644 --- a/rocksdb/transactions/txdbopts.nim +++ b/rocksdb/transactions/txdbopts.nim @@ -38,4 +38,3 @@ proc close*(txDbOpts: var TransactionDbOptionsRef) = if not txDbOpts.isClosed(): rocksdb_transactiondb_options_destroy(txDbOpts.cPtr) txDbOpts.cPtr = nil - diff --git a/rocksdb/transactions/txopts.nim b/rocksdb/transactions/txopts.nim index 1e4de66..4ab867e 100644 --- a/rocksdb/transactions/txopts.nim +++ b/rocksdb/transactions/txopts.nim @@ -38,4 +38,3 @@ proc close*(txOpts: var TransactionOptionsRef) = if not txOpts.isClosed(): rocksdb_transaction_options_destroy(txOpts.cPtr) txOpts.cPtr = nil - diff --git a/rocksdb/writebatch.nim b/rocksdb/writebatch.nim index 580bbc9..249ef44 100644 --- a/rocksdb/writebatch.nim +++ b/rocksdb/writebatch.nim @@ -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 = diff --git a/tests/test_backup.nim b/tests/test_backup.nim index f8b9f9f..376d142 100644 --- a/tests/test_backup.nim +++ b/tests/test_backup.nim @@ -67,4 +67,4 @@ suite "BackupEngineRef Tests": engine.close() check engine.isClosed() engine.close() - check engine.isClosed() \ No newline at end of file + check engine.isClosed() diff --git a/tests/test_helper.nim b/tests/test_helper.nim index 4493950..208317a 100644 --- a/tests/test_helper.nim +++ b/tests/test_helper.nim @@ -12,7 +12,8 @@ import std/sequtils, ../rocksdb/backup, - ../rocksdb/rocksdb + ../rocksdb/rocksdb, + ../rocksdb/transactiondb proc initReadWriteDb*( @@ -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() \ No newline at end of file diff --git a/tests/test_rocksdb.nim b/tests/test_rocksdb.nim index beb4208..3490da1 100644 --- a/tests/test_rocksdb.nim +++ b/tests/test_rocksdb.nim @@ -234,4 +234,15 @@ suite "RocksDbRef Tests": db.close() check db.isClosed() - removeDir(dbDir) \ No newline at end of file + 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()