Format using nph (#54)

* Format entire project using nph.

* Add nph lint to CI.
This commit is contained in:
web3-developer 2024-06-26 23:00:10 +08:00 committed by GitHub
parent 45f7a9266f
commit a691d5b9d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
47 changed files with 5749 additions and 3072 deletions

View File

@ -7,6 +7,26 @@ on:
workflow_dispatch: workflow_dispatch:
jobs: jobs:
lint:
name: "nph Lint"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 2 # In PR, has extra merge commit: ^1 = PR, ^2 = base
- name: Check nph formatting
# Pin nph to a specific version to avoid sudden style differences.
# Updating nph version should be accompanied with running the new
# version on the project directory.
run: |
VERSION="v0.5.1"
ARCHIVE="nph-linux_x64.tar.gz"
curl -L "https://github.com/arnetheduck/nph/releases/download/${VERSION}/${ARCHIVE}" -o ${ARCHIVE}
tar -xzf ${ARCHIVE}
./nph .
git diff --exit-code
build: build:
strategy: strategy:
fail-fast: false fail-fast: false

View File

@ -23,5 +23,7 @@ when defined(rocksdb_static_linking):
switch("dynlibOverride", "lz4") switch("dynlibOverride", "lz4")
switch("dynlibOverride", "zstd") switch("dynlibOverride", "zstd")
--styleCheck:usages --styleCheck:
--styleCheck:error usages
--styleCheck:
error

View File

@ -17,10 +17,10 @@ proc main() =
# snappy support (for example Fedora 28, certain Ubuntu versions) # snappy support (for example Fedora 28, certain Ubuntu versions)
# rocksdb_options_optimize_level_style_compaction(options, 0); # rocksdb_options_optimize_level_style_compaction(options, 0);
# create the DB if it's not already present # create the DB if it's not already present
rocksdb_options_set_create_if_missing(options, 1); rocksdb_options_set_create_if_missing(options, 1)
# open DB var # open DB
var err: cstring # memory leak: example code does not free error string! err: cstring # memory leak: example code does not free error string!
db = rocksdb_open(options, dbPath, cast[cstringArray](err.addr)) db = rocksdb_open(options, dbPath, cast[cstringArray](err.addr))
doAssert err.isNil, $err doAssert err.isNil, $err
@ -32,15 +32,28 @@ proc main() =
var writeOptions = rocksdb_writeoptions_create() var writeOptions = rocksdb_writeoptions_create()
let key = "key" let key = "key"
let put_value = "value" let put_value = "value"
rocksdb_put(db, writeOptions, key.cstring, key.len.csize_t, put_value.cstring, rocksdb_put(
put_value.len.csize_t, cast[cstringArray](err.addr)) db,
writeOptions,
key.cstring,
key.len.csize_t,
put_value.cstring,
put_value.len.csize_t,
cast[cstringArray](err.addr),
)
doAssert err.isNil, $err doAssert err.isNil, $err
# Get value # Get value
var readOptions = rocksdb_readoptions_create() var readOptions = rocksdb_readoptions_create()
var len: csize_t var len: csize_t
let raw_value = rocksdb_get(db, readOptions, key.cstring, key.len.csize_t, addr len, let raw_value = rocksdb_get(
cast[cstringArray](err.addr)) # Important: rocksdb_get is not null-terminated db,
readOptions,
key.cstring,
key.len.csize_t,
addr len,
cast[cstringArray](err.addr),
) # Important: rocksdb_get is not null-terminated
doAssert err.isNil, $err doAssert err.isNil, $err
# Copy it to a regular Nim string (copyMem workaround because raw value is NOT null-terminated) # Copy it to a regular Nim string (copyMem workaround because raw value is NOT null-terminated)
@ -57,8 +70,9 @@ proc main() =
# If something is wrong, you might want to restore data from last backup # If something is wrong, you might want to restore data from last backup
var restoreOptions = rocksdb_restore_options_create() var restoreOptions = rocksdb_restore_options_create()
rocksdb_backup_engine_restore_db_from_latest_backup(be, dbPath, dbPath, rocksdb_backup_engine_restore_db_from_latest_backup(
restoreOptions, cast[cstringArray](err.addr)) be, dbPath, dbPath, restoreOptions, cast[cstringArray](err.addr)
)
doAssert err.isNil, $err doAssert err.isNil, $err
rocksdb_restore_options_destroy(restore_options) rocksdb_restore_options_destroy(restore_options)

View File

@ -8,19 +8,10 @@
# at your option. This file may not be copied, modified, or distributed except according to those terms. # at your option. This file may not be copied, modified, or distributed except according to those terms.
import import
./rocksdb/[backup, ./rocksdb/[
columnfamily, backup, columnfamily, rocksdb, rocksiterator, sstfilewriter, transactiondb,
rocksdb, writebatch,
rocksiterator, ]
sstfilewriter,
transactiondb,
writebatch]
export export
backup, backup, columnfamily, rocksdb, rocksiterator, sstfilewriter, transactiondb, writebatch
columnfamily,
rocksdb,
rocksiterator,
sstfilewriter,
transactiondb,
writebatch

View File

@ -1,16 +1,21 @@
packageName = "rocksdb" packageName = "rocksdb"
version = "0.4.0" version = "0.4.0"
author = "Status Research & Development GmbH" author = "Status Research & Development GmbH"
description = "A wrapper for Facebook's RocksDB, an embeddable, persistent key-value store for fast storage" description =
"A wrapper for Facebook's RocksDB, an embeddable, persistent key-value store for fast storage"
license = "Apache License 2.0 or GPLv2" license = "Apache License 2.0 or GPLv2"
skipDirs = @["examples", "tests"] skipDirs = @["examples", "tests"]
mode = ScriptMode.Verbose mode = ScriptMode.Verbose
### Dependencies ### Dependencies
requires "nim >= 1.6", requires "nim >= 1.6", "results", "tempfile", "unittest2"
"results",
"tempfile", # Format only works with nim version 2
"unittest2" task format, "Format nim code using nph":
# Using the latest nph commit for now because the latest tagged version
# doesn't work with the latest nim 2 version
exec "nimble install nph@#head"
exec "nph ."
task clean, "Remove temporary files": task clean, "Remove temporary files":
exec "rm -rf build" exec "rm -rf build"

View File

@ -12,16 +12,9 @@
{.push raises: [].} {.push raises: [].}
import import
./lib/librocksdb, ./lib/librocksdb, ./internal/utils, ./options/backupopts, ./rocksdb, ./rocksresult
./internal/utils,
./options/backupopts,
./rocksdb,
./rocksresult
export export backupopts, rocksdb, rocksresult
backupopts,
rocksdb,
rocksresult
type type
BackupEnginePtr* = ptr rocksdb_backup_engine_t BackupEnginePtr* = ptr rocksdb_backup_engine_t
@ -32,23 +25,20 @@ type
backupOpts: BackupEngineOptionsRef backupOpts: BackupEngineOptionsRef
proc openBackupEngine*( proc openBackupEngine*(
path: string, path: string, backupOpts = defaultBackupEngineOptions()
backupOpts = defaultBackupEngineOptions()): RocksDBResult[BackupEngineRef] = ): RocksDBResult[BackupEngineRef] =
## Create a new backup engine. The `path` parameter is the path of the backup ## Create a new backup engine. The `path` parameter is the path of the backup
## directory. Note that the same directory should not be used for both backups ## directory. Note that the same directory should not be used for both backups
## and the database itself. ## and the database itself.
var errors: cstring var errors: cstring
let backupEnginePtr = rocksdb_backup_engine_open( let backupEnginePtr = rocksdb_backup_engine_open(
backupOpts.cPtr, backupOpts.cPtr, path.cstring, cast[cstringArray](errors.addr)
path.cstring, )
cast[cstringArray](errors.addr))
bailOnErrors(errors) bailOnErrors(errors)
let engine = BackupEngineRef( let engine =
cPtr: backupEnginePtr, BackupEngineRef(cPtr: backupEnginePtr, path: path, backupOpts: backupOpts)
path: path,
backupOpts: backupOpts)
ok(engine) ok(engine)
proc isClosed*(backupEngine: BackupEngineRef): bool {.inline.} = proc isClosed*(backupEngine: BackupEngineRef): bool {.inline.} =
@ -56,26 +46,23 @@ proc isClosed*(backupEngine: BackupEngineRef): bool {.inline.} =
backupEngine.cPtr.isNil() backupEngine.cPtr.isNil()
proc createNewBackup*( proc createNewBackup*(
backupEngine: BackupEngineRef, backupEngine: BackupEngineRef, db: RocksDbRef
db: RocksDbRef): RocksDBResult[void] = ): RocksDBResult[void] =
## Create a new backup of the database. ## Create a new backup of the database.
doAssert not backupEngine.isClosed() doAssert not backupEngine.isClosed()
doAssert not db.isClosed() doAssert not db.isClosed()
var errors: cstring var errors: cstring
rocksdb_backup_engine_create_new_backup( rocksdb_backup_engine_create_new_backup(
backupEngine.cPtr, backupEngine.cPtr, db.cPtr, cast[cstringArray](errors.addr)
db.cPtr, )
cast[cstringArray](errors.addr))
bailOnErrors(errors) bailOnErrors(errors)
ok() ok()
proc restoreDbFromLatestBackup*( proc restoreDbFromLatestBackup*(
backupEngine: BackupEngineRef, backupEngine: BackupEngineRef, dbDir: string, walDir = dbDir, keepLogFiles = false
dbDir: string, ): RocksDBResult[void] =
walDir = dbDir,
keepLogFiles = false): RocksDBResult[void] =
## Restore the database from the latest backup. ## Restore the database from the latest backup.
doAssert not backupEngine.isClosed() doAssert not backupEngine.isClosed()
@ -88,7 +75,8 @@ proc restoreDbFromLatestBackup*(
dbDir.cstring, dbDir.cstring,
walDir.cstring, walDir.cstring,
restoreOptions, restoreOptions,
cast[cstringArray](errors.addr)) cast[cstringArray](errors.addr),
)
bailOnErrors(errors) bailOnErrors(errors)
rocksdb_restore_options_destroy(restoreOptions) rocksdb_restore_options_destroy(restoreOptions)

View File

@ -19,8 +19,7 @@
{.push raises: [].} {.push raises: [].}
import import ./rocksdb
./rocksdb
export rocksdb export rocksdb
@ -34,8 +33,8 @@ type
name: string name: string
proc withColFamily*( proc withColFamily*(
db: RocksDbReadOnlyRef, db: RocksDbReadOnlyRef, name: string
name: string): RocksDBResult[ColFamilyReadOnly] = ): RocksDBResult[ColFamilyReadOnly] =
## Creates a new `ColFamilyReadOnly` from the given `RocksDbReadOnlyRef` and ## Creates a new `ColFamilyReadOnly` from the given `RocksDbReadOnlyRef` and
## column family name. ## column family name.
@ -46,8 +45,8 @@ proc withColFamily*(
ok(ColFamilyReadOnly(db: db, name: name)) ok(ColFamilyReadOnly(db: db, name: name))
proc withColFamily*( proc withColFamily*(
db: RocksDbReadWriteRef, db: RocksDbReadWriteRef, name: string
name: string): RocksDBResult[ColFamilyReadWrite] = ): RocksDBResult[ColFamilyReadWrite] =
## Create a new `ColFamilyReadWrite` from the given `RocksDbReadWriteRef` and ## Create a new `ColFamilyReadWrite` from the given `RocksDbReadWriteRef` and
## column family name. ## column family name.
@ -66,39 +65,39 @@ proc name*(cf: ColFamilyReadOnly | ColFamilyReadWrite): string {.inline.} =
cf.name cf.name
proc get*( proc get*(
cf: ColFamilyReadOnly | ColFamilyReadWrite, cf: ColFamilyReadOnly | ColFamilyReadWrite, key: openArray[byte], onData: DataProc
key: openArray[byte], ): RocksDBResult[bool] {.inline.} =
onData: DataProc): RocksDBResult[bool] {.inline.} =
## Gets the value of the given key from the column family using the `onData` ## Gets the value of the given key from the column family using the `onData`
## callback. ## callback.
cf.db.get(key, onData, cf.name) cf.db.get(key, onData, cf.name)
proc get*( proc get*(
cf: ColFamilyReadOnly | ColFamilyReadWrite, cf: ColFamilyReadOnly | ColFamilyReadWrite, key: openArray[byte]
key: openArray[byte]): RocksDBResult[seq[byte]] {.inline.} = ): RocksDBResult[seq[byte]] {.inline.} =
## Gets the value of the given key from the column family. ## Gets the value of the given key from the column family.
cf.db.get(key, cf.name) cf.db.get(key, cf.name)
proc put*( proc put*(
cf: ColFamilyReadWrite, cf: ColFamilyReadWrite, key, val: openArray[byte]
key, val: openArray[byte]): RocksDBResult[void] {.inline.} = ): RocksDBResult[void] {.inline.} =
## Puts a value for the given key into the column family. ## Puts a value for the given key into the column family.
cf.db.put(key, val, cf.name) cf.db.put(key, val, cf.name)
proc keyExists*( proc keyExists*(
cf: ColFamilyReadOnly | ColFamilyReadWrite, cf: ColFamilyReadOnly | ColFamilyReadWrite, key: openArray[byte]
key: openArray[byte]): RocksDBResult[bool] {.inline.} = ): RocksDBResult[bool] {.inline.} =
## Checks if the given key exists in the column family. ## Checks if the given key exists in the column family.
cf.db.keyExists(key, cf.name) cf.db.keyExists(key, cf.name)
proc delete*( proc delete*(
cf: ColFamilyReadWrite, cf: ColFamilyReadWrite, key: openArray[byte]
key: openArray[byte]): RocksDBResult[void] {.inline.} = ): RocksDBResult[void] {.inline.} =
## Deletes the given key from the column family. ## Deletes the given key from the column family.
cf.db.delete(key, cf.name) cf.db.delete(key, cf.name)
proc openIterator*( proc openIterator*(
cf: ColFamilyReadOnly | ColFamilyReadWrite): RocksDBResult[RocksIteratorRef] {.inline.} = cf: ColFamilyReadOnly | ColFamilyReadWrite
): RocksDBResult[RocksIteratorRef] {.inline.} =
## Opens an `RocksIteratorRef` for the given column family. ## Opens an `RocksIteratorRef` for the given column family.
cf.db.openIterator(cf.name) cf.db.openIterator(cf.name)
@ -107,7 +106,7 @@ proc openWriteBatch*(cf: ColFamilyReadWrite): WriteBatchRef {.inline.} =
cf.db.openWriteBatch(cf.name) cf.db.openWriteBatch(cf.name)
proc write*( proc write*(
cf: ColFamilyReadWrite, cf: ColFamilyReadWrite, updates: WriteBatchRef
updates: WriteBatchRef): RocksDBResult[void] {.inline.} = ): RocksDBResult[void] {.inline.} =
## Writes the updates in the `WriteBatchRef` to the column family. ## Writes the updates in the `WriteBatchRef` to the column family.
cf.db.write(updates) cf.db.write(updates)

View File

@ -9,20 +9,17 @@
{.push raises: [].} {.push raises: [].}
import import ../internal/utils, ./cfopts
../internal/utils,
./cfopts
export cfopts export cfopts
type type ColFamilyDescriptor* = object
ColFamilyDescriptor* = object
name: string name: string
options: ColFamilyOptionsRef options: ColFamilyOptionsRef
proc initColFamilyDescriptor*( proc initColFamilyDescriptor*(
name: string, name: string, options = defaultColFamilyOptions()
options = defaultColFamilyOptions()): ColFamilyDescriptor = ): ColFamilyDescriptor =
ColFamilyDescriptor(name: name, options: options) ColFamilyDescriptor(name: name, options: options)
proc name*(descriptor: ColFamilyDescriptor): string {.inline.} = proc name*(descriptor: ColFamilyDescriptor): string {.inline.} =

View File

@ -9,8 +9,7 @@
{.push raises: [].} {.push raises: [].}
import import ../lib/librocksdb
../lib/librocksdb
type type
ColFamilyHandlePtr* = ptr rocksdb_column_family_handle_t ColFamilyHandlePtr* = ptr rocksdb_column_family_handle_t

View File

@ -9,8 +9,7 @@
{.push raises: [].} {.push raises: [].}
import import ../lib/librocksdb, ../options/tableopts
../lib/librocksdb, ../options/tableopts
type type
SlicetransformPtr* = ptr rocksdb_slicetransform_t SlicetransformPtr* = ptr rocksdb_slicetransform_t
@ -127,21 +126,23 @@ proc `setPrefixExtractor`*(cfOpts: ColFamilyOptionsRef, value: SlicetransformRef
doAssert not cfOpts.isClosed() doAssert not cfOpts.isClosed()
rocksdb_options_set_prefix_extractor(cfOpts.cPtr, value.cPtr) rocksdb_options_set_prefix_extractor(cfOpts.cPtr, value.cPtr)
proc `blockBasedTableFactory=`*(cfOpts: ColFamilyOptionsRef, tableOpts: TableOptionsRef) = proc `blockBasedTableFactory=`*(
cfOpts: ColFamilyOptionsRef, tableOpts: TableOptionsRef
) =
doAssert not cfOpts.isClosed() doAssert not cfOpts.isClosed()
rocksdb_options_set_block_based_table_factory(cfOpts.cPtr, tableOpts.cPtr) rocksdb_options_set_block_based_table_factory(cfOpts.cPtr, tableOpts.cPtr)
# https://github.com/facebook/rocksdb/wiki/MemTable # https://github.com/facebook/rocksdb/wiki/MemTable
proc setHashSkipListRep*( proc setHashSkipListRep*(
cfOpts: ColFamilyOptionsRef, bucketCount, skipListHeight, cfOpts: ColFamilyOptionsRef,
skipListBranchingFactor: int) = bucketCount, skipListHeight, skipListBranchingFactor: int,
) =
doAssert not cfOpts.isClosed() doAssert not cfOpts.isClosed()
rocksdb_options_set_hash_skip_list_rep( rocksdb_options_set_hash_skip_list_rep(
cfOpts.cPtr, bucketCount.csize_t, skipListHeight.cint, cfOpts.cPtr, bucketCount.csize_t, skipListHeight.cint, skipListBranchingFactor.cint
skipListBranchingFactor.cint) )
proc setHashLinkListRep*( proc setHashLinkListRep*(cfOpts: ColFamilyOptionsRef, bucketCount: int) =
cfOpts: ColFamilyOptionsRef, bucketCount: int) =
doAssert not cfOpts.isClosed() doAssert not cfOpts.isClosed()
rocksdb_options_set_hash_link_list_rep(cfOpts.cPtr, bucketCount.csize_t) rocksdb_options_set_hash_link_list_rep(cfOpts.cPtr, bucketCount.csize_t)

View File

@ -9,20 +9,16 @@
{.push raises: [].} {.push raises: [].}
import import std/tables, ../columnfamily/cfhandle
std/tables,
../columnfamily/cfhandle
export export cfhandle
cfhandle
type type ColFamilyTableRef* = ref object
ColFamilyTableRef* = ref object
columnFamilies: TableRef[string, ColFamilyHandleRef] columnFamilies: TableRef[string, ColFamilyHandleRef]
proc newColFamilyTable*( proc newColFamilyTable*(
names: openArray[string], names: openArray[string], handles: openArray[ColFamilyHandlePtr]
handles: openArray[ColFamilyHandlePtr]): ColFamilyTableRef = ): ColFamilyTableRef =
doAssert names.len() == handles.len() doAssert names.len() == handles.len()
let cfTable = newTable[string, ColFamilyHandleRef]() let cfTable = newTable[string, ColFamilyHandleRef]()

View File

@ -9,9 +9,7 @@
{.push raises: [].} {.push raises: [].}
import import std/locks, ../lib/librocksdb
std/locks,
../lib/librocksdb
const DEFAULT_COLUMN_FAMILY_NAME* = "default" const DEFAULT_COLUMN_FAMILY_NAME* = "default"

File diff suppressed because it is too large Load Diff

View File

@ -9,8 +9,7 @@
{.push raises: [].} {.push raises: [].}
import import ../lib/librocksdb
../lib/librocksdb
type type
BackupEngineOptionsPtr* = ptr rocksdb_options_t BackupEngineOptionsPtr* = ptr rocksdb_options_t
@ -34,7 +33,6 @@ proc defaultBackupEngineOptions*(): BackupEngineOptionsRef {.inline.} =
let opts = newBackupEngineOptions() let opts = newBackupEngineOptions()
opts opts
proc close*(engineOpts: BackupEngineOptionsRef) = proc close*(engineOpts: BackupEngineOptionsRef) =
if not engineOpts.isClosed(): if not engineOpts.isClosed():
rocksdb_options_destroy(engineOpts.cPtr) rocksdb_options_destroy(engineOpts.cPtr)

View File

@ -1,5 +1,4 @@
import import ../lib/librocksdb
../lib/librocksdb
type type
CachePtr* = ptr rocksdb_cache_t CachePtr* = ptr rocksdb_cache_t

View File

@ -9,10 +9,7 @@
{.push raises: [].} {.push raises: [].}
import import std/cpuinfo, ../lib/librocksdb, ./[cache, tableopts]
std/cpuinfo,
../lib/librocksdb,
./[cache, tableopts]
export cache, tableopts export cache, tableopts

View File

@ -9,8 +9,7 @@
{.push raises: [].} {.push raises: [].}
import import ../lib/librocksdb
../lib/librocksdb
type type
ReadOptionsPtr* = ptr rocksdb_readoptions_t ReadOptionsPtr* = ptr rocksdb_readoptions_t

View File

@ -1,6 +1,4 @@
import import ../lib/librocksdb, ./cache
../lib/librocksdb,
./cache
type type
# TODO might eventually wrap this # TODO might eventually wrap this
@ -21,13 +19,18 @@ type
DataBlockIndexType* {.pure.} = enum DataBlockIndexType* {.pure.} = enum
binarySearch = rocksdb_block_based_table_data_block_index_type_binary_search binarySearch = rocksdb_block_based_table_data_block_index_type_binary_search
binarySearchAndHash = rocksdb_block_based_table_data_block_index_type_binary_search_and_hash binarySearchAndHash =
rocksdb_block_based_table_data_block_index_type_binary_search_and_hash
proc createRibbon*(bitsPerKey: float): FilterPolicyRef = proc createRibbon*(bitsPerKey: float): FilterPolicyRef =
FilterPolicyRef(cPtr: rocksdb_filterpolicy_create_ribbon(bitsPerKey)) FilterPolicyRef(cPtr: rocksdb_filterpolicy_create_ribbon(bitsPerKey))
proc createRibbonHybrid*(bitsPerKey: float, bloomBeforeLevel: int = 0): FilterPolicyRef = proc createRibbonHybrid*(
FilterPolicyRef(cPtr: rocksdb_filterpolicy_create_ribbon_hybrid(bitsPerKey, bloomBeforeLevel.cint)) bitsPerKey: float, bloomBeforeLevel: int = 0
): FilterPolicyRef =
FilterPolicyRef(
cPtr: rocksdb_filterpolicy_create_ribbon_hybrid(bitsPerKey, bloomBeforeLevel.cint)
)
proc isClosed*(policy: FilterPolicyRef): bool = proc isClosed*(policy: FilterPolicyRef): bool =
isNil(policy.cPtr) isNil(policy.cPtr)

View File

@ -9,8 +9,7 @@
{.push raises: [].} {.push raises: [].}
import import ../lib/librocksdb
../lib/librocksdb
type type
WriteOptionsPtr* = ptr rocksdb_writeoptions_t WriteOptionsPtr* = ptr rocksdb_writeoptions_t

View File

@ -35,14 +35,7 @@ import
./rocksresult, ./rocksresult,
./writebatch ./writebatch
export export rocksresult, dbopts, readopts, writeopts, cfdescriptor, rocksiterator, writebatch
rocksresult,
dbopts,
readopts,
writeopts,
cfdescriptor,
rocksiterator,
writebatch
type type
RocksDbPtr* = ptr rocksdb_t RocksDbPtr* = ptr rocksdb_t
@ -64,8 +57,7 @@ type
ingestOptsPtr: IngestExternalFilesOptionsPtr ingestOptsPtr: IngestExternalFilesOptionsPtr
proc listColumnFamilies*( proc listColumnFamilies*(
path: string; path: string, dbOpts = DbOptionsRef(nil)
dbOpts = DbOptionsRef(nil);
): RocksDBResult[seq[string]] = ): RocksDBResult[seq[string]] =
## List exisiting column families on disk. This might be used to find out ## List exisiting column families on disk. This might be used to find out
## whether there were some columns missing with the version on disk. ## whether there were some columns missing with the version on disk.
@ -78,25 +70,23 @@ proc listColumnFamilies*(
## above once rocksdb has been upgraded to the latest version, see comments ## above once rocksdb has been upgraded to the latest version, see comments
## at the end of ./columnfamily/cfhandle.nim. ## at the end of ./columnfamily/cfhandle.nim.
## ##
let let useDbOpts = (if dbOpts.isNil: defaultDbOptions() else: dbOpts)
useDbOpts = (if dbOpts.isNil: defaultDbOptions() else: dbOpts)
defer: defer:
if dbOpts.isNil: useDbOpts.close() if dbOpts.isNil:
useDbOpts.close()
var var
lencf: csize_t lencf: csize_t
errors: cstring errors: cstring
let let cList = rocksdb_list_column_families(
cList = rocksdb_list_column_families( useDbOpts.cPtr, path.cstring, addr lencf, cast[cstringArray](errors.addr)
useDbOpts.cPtr, )
path.cstring,
addr lencf,
cast[cstringArray](errors.addr))
bailOnErrors(errors) bailOnErrors(errors)
var cfs: seq[string] var cfs: seq[string]
if not cList.isNil: if not cList.isNil:
defer: rocksdb_free(cList) defer:
rocksdb_free(cList)
for n in 0 ..< lencf: for n in 0 ..< lencf:
if cList[n].isNil: if cList[n].isNil:
@ -112,11 +102,11 @@ proc listColumnFamilies*(
ok cfs ok cfs
proc openRocksDb*( proc openRocksDb*(
path: string; path: string,
dbOpts = DbOptionsRef(nil); dbOpts = DbOptionsRef(nil),
readOpts = ReadOptionsRef(nil); readOpts = ReadOptionsRef(nil),
writeOpts = WriteOptionsRef(nil); writeOpts = WriteOptionsRef(nil),
columnFamilies: openArray[ColFamilyDescriptor] = []; columnFamilies: openArray[ColFamilyDescriptor] = [],
): RocksDBResult[RocksDbReadWriteRef] = ): RocksDBResult[RocksDbReadWriteRef] =
## Open a RocksDB instance in read-write mode. If `columnFamilies` is empty ## Open a RocksDB instance in read-write mode. If `columnFamilies` is empty
## then it will open the default column family. If `dbOpts`, `readOpts`, or ## then it will open the default column family. If `dbOpts`, `readOpts`, or
@ -124,10 +114,10 @@ proc openRocksDb*(
## By default, column families will be created if they don't yet exist. ## By default, column families will be created if they don't yet exist.
## All existing column families must be specified if the database has ## All existing column families must be specified if the database has
## previously created any column families. ## previously created any column families.
let let useDbOpts = (if dbOpts.isNil: defaultDbOptions() else: dbOpts)
useDbOpts = (if dbOpts.isNil: defaultDbOptions() else: dbOpts)
defer: defer:
if dbOpts.isNil: useDbOpts.close() if dbOpts.isNil:
useDbOpts.close()
var cfs = columnFamilies.toSeq() var cfs = columnFamilies.toSeq()
if DEFAULT_COLUMN_FAMILY_NAME notin columnFamilies.mapIt(it.name()): if DEFAULT_COLUMN_FAMILY_NAME notin columnFamilies.mapIt(it.name()):
@ -145,7 +135,8 @@ proc openRocksDb*(
cast[cstringArray](cfNames[0].addr), cast[cstringArray](cfNames[0].addr),
cfOpts[0].addr, cfOpts[0].addr,
cfHandles[0].addr, cfHandles[0].addr,
cast[cstringArray](errors.addr)) cast[cstringArray](errors.addr),
)
bailOnErrors(errors) bailOnErrors(errors)
let let
@ -161,15 +152,16 @@ proc openRocksDb*(
writeOpts: writeOpts, writeOpts: writeOpts,
ingestOptsPtr: rocksdb_ingestexternalfileoptions_create(), ingestOptsPtr: rocksdb_ingestexternalfileoptions_create(),
defaultCfName: DEFAULT_COLUMN_FAMILY_NAME, defaultCfName: DEFAULT_COLUMN_FAMILY_NAME,
cfTable: newColFamilyTable(cfNames.mapIt($it), cfHandles)) cfTable: newColFamilyTable(cfNames.mapIt($it), cfHandles),
)
ok(db) ok(db)
proc openRocksDbReadOnly*( proc openRocksDbReadOnly*(
path: string; path: string,
dbOpts = DbOptionsRef(nil); dbOpts = DbOptionsRef(nil),
readOpts = ReadOptionsRef(nil); readOpts = ReadOptionsRef(nil),
columnFamilies: openArray[ColFamilyDescriptor] = []; columnFamilies: openArray[ColFamilyDescriptor] = [],
errorIfWalFileExists = false; errorIfWalFileExists = false,
): RocksDBResult[RocksDbReadOnlyRef] = ): RocksDBResult[RocksDbReadOnlyRef] =
## Open a RocksDB instance in read-only mode. If `columnFamilies` is empty ## Open a RocksDB instance in read-only mode. If `columnFamilies` is empty
## then it will open the default column family. If `dbOpts` or `readOpts` are ## then it will open the default column family. If `dbOpts` or `readOpts` are
@ -177,10 +169,10 @@ proc openRocksDbReadOnly*(
## families will be created if they don't yet exist. If the database already ## families will be created if they don't yet exist. If the database already
## contains any column families, then all or a subset of the existing column ## contains any column families, then all or a subset of the existing column
## families can be opened for reading. ## families can be opened for reading.
let let useDbOpts = (if dbOpts.isNil: defaultDbOptions() else: dbOpts)
useDbOpts = (if dbOpts.isNil: defaultDbOptions() else: dbOpts)
defer: defer:
if dbOpts.isNil: useDbOpts.close() if dbOpts.isNil:
useDbOpts.close()
var cfs = columnFamilies.toSeq() var cfs = columnFamilies.toSeq()
if DEFAULT_COLUMN_FAMILY_NAME notin columnFamilies.mapIt(it.name()): if DEFAULT_COLUMN_FAMILY_NAME notin columnFamilies.mapIt(it.name()):
@ -199,7 +191,8 @@ proc openRocksDbReadOnly*(
cfOpts[0].addr, cfOpts[0].addr,
cfHandles[0].addr, cfHandles[0].addr,
errorIfWalFileExists.uint8, errorIfWalFileExists.uint8,
cast[cstringArray](errors.addr)) cast[cstringArray](errors.addr),
)
bailOnErrors(errors) bailOnErrors(errors)
let let
@ -212,7 +205,8 @@ proc openRocksDbReadOnly*(
dbOpts: dbOpts, dbOpts: dbOpts,
readOpts: readOpts, readOpts: readOpts,
defaultCfName: DEFAULT_COLUMN_FAMILY_NAME, defaultCfName: DEFAULT_COLUMN_FAMILY_NAME,
cfTable: newColFamilyTable(cfNames.mapIt($it), cfHandles)) cfTable: newColFamilyTable(cfNames.mapIt($it), cfHandles),
)
ok(db) ok(db)
proc isClosed*(db: RocksDbRef): bool {.inline.} = proc isClosed*(db: RocksDbRef): bool {.inline.} =
@ -228,7 +222,8 @@ proc get*(
db: RocksDbRef, db: RocksDbRef,
key: openArray[byte], key: openArray[byte],
onData: DataProc, onData: DataProc,
columnFamily = db.defaultCfName): RocksDBResult[bool] = columnFamily = db.defaultCfName,
): RocksDBResult[bool] =
## Get the value for the given key from the specified column family. ## Get the value for the given key from the specified column family.
## If the value does not exist, `false` will be returned in the result ## If the value does not exist, `false` will be returned in the result
## and `onData` will not be called. If the value does exist, `true` will be ## and `onData` will not be called. If the value does exist, `true` will be
@ -253,7 +248,8 @@ proc get*(
cast[cstring](unsafeAddr key[0]), cast[cstring](unsafeAddr key[0]),
csize_t(key.len), csize_t(key.len),
len.addr, len.addr,
cast[cstringArray](errors.addr)) cast[cstringArray](errors.addr),
)
bailOnErrors(errors) bailOnErrors(errors)
if data.isNil(): if data.isNil():
@ -265,15 +261,15 @@ proc get*(
ok(true) ok(true)
proc get*( proc get*(
db: RocksDbRef, db: RocksDbRef, key: openArray[byte], columnFamily = db.defaultCfName
key: openArray[byte], ): RocksDBResult[seq[byte]] =
columnFamily = db.defaultCfName): RocksDBResult[seq[byte]] =
## Get the value for the given key from the specified column family. ## Get the value for the given key from the specified column family.
## If the value does not exist, an empty error will be returned in the result. ## If the value does not exist, an empty error will be returned in the result.
## If the value does exist, the value will be returned in the result. ## If the value does exist, the value will be returned in the result.
var dataRes: RocksDBResult[seq[byte]] var dataRes: RocksDBResult[seq[byte]]
proc onData(data: openArray[byte]) = dataRes.ok(@data) proc onData(data: openArray[byte]) =
dataRes.ok(@data)
let res = db.get(key, onData, columnFamily) let res = db.get(key, onData, columnFamily)
if res.isOk(): if res.isOk():
@ -282,9 +278,8 @@ proc get*(
dataRes.err(res.error()) dataRes.err(res.error())
proc put*( proc put*(
db: RocksDbReadWriteRef, db: RocksDbReadWriteRef, key, val: openArray[byte], columnFamily = db.defaultCfName
key, val: openArray[byte], ): RocksDBResult[void] =
columnFamily = db.defaultCfName): RocksDBResult[void] =
## Put the value for the given key into the specified column family. ## Put the value for the given key into the specified column family.
if key.len() == 0: if key.len() == 0:
@ -301,17 +296,21 @@ proc put*(
cfHandle.cPtr, cfHandle.cPtr,
cast[cstring](unsafeAddr key[0]), cast[cstring](unsafeAddr key[0]),
csize_t(key.len), csize_t(key.len),
cast[cstring](if val.len > 0: unsafeAddr val[0] else: nil), cast[cstring](if val.len > 0:
unsafeAddr val[0]
else:
nil
),
csize_t(val.len), csize_t(val.len),
cast[cstringArray](errors.addr)) cast[cstringArray](errors.addr),
)
bailOnErrors(errors) bailOnErrors(errors)
ok() ok()
proc keyExists*( proc keyExists*(
db: RocksDbRef, db: RocksDbRef, key: openArray[byte], columnFamily = db.defaultCfName
key: openArray[byte], ): RocksDBResult[bool] =
columnFamily = db.defaultCfName): RocksDBResult[bool] =
## Check if the key exists in the specified column family. ## Check if the key exists in the specified column family.
## Returns a result containing `true` if the key exists or a result ## Returns a result containing `true` if the key exists or a result
## containing `false` otherwise. ## containing `false` otherwise.
@ -319,12 +318,17 @@ proc keyExists*(
# TODO: Call rocksdb_key_may_exist_cf to improve performance for the case # TODO: Call rocksdb_key_may_exist_cf to improve performance for the case
# when the key does not exist # when the key does not exist
db.get(key, proc(data: openArray[byte]) = discard, columnFamily) db.get(
key,
proc(data: openArray[byte]) =
discard
,
columnFamily,
)
proc delete*( proc delete*(
db: RocksDbReadWriteRef, db: RocksDbReadWriteRef, key: openArray[byte], columnFamily = db.defaultCfName
key: openArray[byte], ): RocksDBResult[void] =
columnFamily = db.defaultCfName): RocksDBResult[void] =
## Delete the value for the given key from the specified column family. ## Delete the value for the given key from the specified column family.
## If the value does not exist, the delete will be a no-op. ## If the value does not exist, the delete will be a no-op.
## To check if the value exists before or after a delete, use `keyExists`. ## To check if the value exists before or after a delete, use `keyExists`.
@ -343,14 +347,15 @@ proc delete*(
cfHandle.cPtr, cfHandle.cPtr,
cast[cstring](unsafeAddr key[0]), cast[cstring](unsafeAddr key[0]),
csize_t(key.len), csize_t(key.len),
cast[cstringArray](errors.addr)) cast[cstringArray](errors.addr),
)
bailOnErrors(errors) bailOnErrors(errors)
ok() ok()
proc openIterator*( proc openIterator*(
db: RocksDbRef, db: RocksDbRef, columnFamily = db.defaultCfName
columnFamily = db.defaultCfName): RocksDBResult[RocksIteratorRef] = ): RocksDBResult[RocksIteratorRef] =
## Opens an `RocksIteratorRef` for the specified column family. ## Opens an `RocksIteratorRef` for the specified column family.
doAssert not db.isClosed() doAssert not db.isClosed()
@ -358,41 +363,34 @@ proc openIterator*(
if cfHandle.isNil(): if cfHandle.isNil():
return err("rocksdb: unknown column family") return err("rocksdb: unknown column family")
let rocksIterPtr = rocksdb_create_iterator_cf( let rocksIterPtr =
db.cPtr, rocksdb_create_iterator_cf(db.cPtr, db.readOpts.cPtr, cfHandle.cPtr)
db.readOpts.cPtr,
cfHandle.cPtr)
ok(newRocksIterator(rocksIterPtr)) ok(newRocksIterator(rocksIterPtr))
proc openWriteBatch*( proc openWriteBatch*(
db: RocksDbReadWriteRef, db: RocksDbReadWriteRef, columnFamily = db.defaultCfName
columnFamily = db.defaultCfName): WriteBatchRef = ): WriteBatchRef =
## Opens a `WriteBatchRef` which defaults to using the specified column family. ## Opens a `WriteBatchRef` which defaults to using the specified column family.
doAssert not db.isClosed() doAssert not db.isClosed()
newWriteBatch(db.cfTable, columnFamily) newWriteBatch(db.cfTable, columnFamily)
proc write*( proc write*(db: RocksDbReadWriteRef, updates: WriteBatchRef): RocksDBResult[void] =
db: RocksDbReadWriteRef,
updates: WriteBatchRef): RocksDBResult[void] =
## Apply the updates in the `WriteBatchRef` to the database. ## Apply the updates in the `WriteBatchRef` to the database.
doAssert not db.isClosed() doAssert not db.isClosed()
var errors: cstring var errors: cstring
rocksdb_write( rocksdb_write(
db.cPtr, db.cPtr, db.writeOpts.cPtr, updates.cPtr, cast[cstringArray](errors.addr)
db.writeOpts.cPtr, )
updates.cPtr,
cast[cstringArray](errors.addr))
bailOnErrors(errors) bailOnErrors(errors)
ok() ok()
proc ingestExternalFile*( proc ingestExternalFile*(
db: RocksDbReadWriteRef, db: RocksDbReadWriteRef, filePath: string, columnFamily = db.defaultCfName
filePath: string, ): RocksDBResult[void] =
columnFamily = db.defaultCfName): RocksDBResult[void] =
## Ingest an external sst file into the database. The file will be ingested ## Ingest an external sst file into the database. The file will be ingested
## into the specified column family or the default column family if none is ## into the specified column family or the default column family if none is
## provided. ## provided.
@ -408,9 +406,11 @@ proc ingestExternalFile*(
rocksdb_ingest_external_file_cf( rocksdb_ingest_external_file_cf(
db.cPtr, db.cPtr,
cfHandle.cPtr, cfHandle.cPtr,
cast[cstringArray](sstPath.addr), csize_t(1), cast[cstringArray](sstPath.addr),
csize_t(1),
db.ingestOptsPtr, db.ingestOptsPtr,
cast[cstringArray](errors.addr)) cast[cstringArray](errors.addr),
)
bailOnErrors(errors) bailOnErrors(errors)
ok() ok()

View File

@ -12,13 +12,9 @@
{.push raises: [].} {.push raises: [].}
import import ./lib/librocksdb, ./internal/utils, ./rocksresult
./lib/librocksdb,
./internal/utils,
./rocksresult
export export rocksresult
rocksresult
type type
RocksIteratorPtr* = ptr rocksdb_iterator_t RocksIteratorPtr* = ptr rocksdb_iterator_t
@ -138,15 +134,22 @@ iterator pairs*(iter: RocksIteratorRef): tuple[key: seq[byte], value: seq[byte]]
## the form of a tuple. The iterator is automatically closed after the ## the form of a tuple. The iterator is automatically closed after the
## iteration. ## iteration.
doAssert not iter.isClosed() doAssert not iter.isClosed()
defer: iter.close() defer:
iter.close()
iter.seekToFirst() iter.seekToFirst()
while iter.isValid(): while iter.isValid():
var var
key: seq[byte] key: seq[byte]
value: seq[byte] value: seq[byte]
iter.key(proc(data: openArray[byte]) = key = @data) iter.key(
iter.value(proc(data: openArray[byte]) = value = @data) proc(data: openArray[byte]) =
key = @data
)
iter.value(
proc(data: openArray[byte]) =
value = @data
)
iter.next() iter.next()
yield (key, value) yield (key, value)

View File

@ -9,11 +9,9 @@
{.push raises: [].} {.push raises: [].}
import import results
results
export export results
results
type type
RocksDBResult*[T] = Result[T, string] RocksDBResult*[T] = Result[T, string]

View File

@ -11,14 +11,9 @@
{.push raises: [].} {.push raises: [].}
import import ./lib/librocksdb, ./internal/utils, ./options/dbopts, ./rocksresult
./lib/librocksdb,
./internal/utils,
./options/dbopts,
./rocksresult
export export rocksresult
rocksresult
type type
SstFileWriterPtr* = ptr rocksdb_sstfilewriter_t SstFileWriterPtr* = ptr rocksdb_sstfilewriter_t
@ -30,8 +25,7 @@ type
dbOpts: DbOptionsRef dbOpts: DbOptionsRef
proc openSstFileWriter*( proc openSstFileWriter*(
filePath: string; filePath: string, dbOpts = DbOptionsRef(nil)
dbOpts = DbOptionsRef(nil);
): RocksDBResult[SstFileWriterRef] = ): RocksDBResult[SstFileWriterRef] =
## Creates a new `SstFileWriterRef` and opens the file at the given `filePath`. ## Creates a new `SstFileWriterRef` and opens the file at the given `filePath`.
let dbOpts = (if dbOpts.isNil: defaultDbOptions() else: dbOpts) let dbOpts = (if dbOpts.isNil: defaultDbOptions() else: dbOpts)
@ -41,13 +35,13 @@ proc openSstFileWriter*(
let writer = SstFileWriterRef( let writer = SstFileWriterRef(
cPtr: rocksdb_sstfilewriter_create(envOptsPtr, dbOpts.cPtr), cPtr: rocksdb_sstfilewriter_create(envOptsPtr, dbOpts.cPtr),
envOptsPtr: envOptsPtr, envOptsPtr: envOptsPtr,
dbOpts: dbOpts) dbOpts: dbOpts,
)
var errors: cstring var errors: cstring
rocksdb_sstfilewriter_open( rocksdb_sstfilewriter_open(
writer.cPtr, writer.cPtr, filePath.cstring, cast[cstringArray](errors.addr)
filePath.cstring, )
cast[cstringArray](errors.addr))
bailOnErrors(errors) bailOnErrors(errors)
ok(writer) ok(writer)
@ -57,17 +51,19 @@ proc isClosed*(writer: SstFileWriterRef): bool {.inline.} =
writer.cPtr.isNil() writer.cPtr.isNil()
proc put*( proc put*(
writer: SstFileWriterRef, writer: SstFileWriterRef, key: openArray[byte], val: openArray[byte]
key: openArray[byte], ): RocksDBResult[void] =
val: openArray[byte]): RocksDBResult[void] =
## Add a key-value pair to the sst file. ## Add a key-value pair to the sst file.
var errors: cstring var errors: cstring
rocksdb_sstfilewriter_put( rocksdb_sstfilewriter_put(
writer.cPtr, writer.cPtr,
cast[cstring](unsafeAddr key[0]), csize_t(key.len), cast[cstring](unsafeAddr key[0]),
cast[cstring](unsafeAddr val[0]), csize_t(val.len), csize_t(key.len),
cast[cstringArray](errors.addr)) cast[cstring](unsafeAddr val[0]),
csize_t(val.len),
cast[cstringArray](errors.addr),
)
bailOnErrors(errors) bailOnErrors(errors)
ok() ok()
@ -78,8 +74,10 @@ proc delete*(writer: SstFileWriterRef, key: openArray[byte]): RocksDBResult[void
var errors: cstring var errors: cstring
rocksdb_sstfilewriter_delete( rocksdb_sstfilewriter_delete(
writer.cPtr, writer.cPtr,
cast[cstring](unsafeAddr key[0]), csize_t(key.len), cast[cstring](unsafeAddr key[0]),
cast[cstringArray](errors.addr)) csize_t(key.len),
cast[cstringArray](errors.addr),
)
bailOnErrors(errors) bailOnErrors(errors)
ok() ok()

View File

@ -25,14 +25,7 @@ import
./rocksresult ./rocksresult
export export
dbopts, dbopts, txdbopts, cfdescriptor, readopts, writeopts, txopts, transaction, rocksresult
txdbopts,
cfdescriptor,
readopts,
writeopts,
txopts,
transaction,
rocksresult
type type
TransactionDbPtr* = ptr rocksdb_transactiondb_t TransactionDbPtr* = ptr rocksdb_transactiondb_t
@ -47,17 +40,17 @@ type
proc openTransactionDb*( proc openTransactionDb*(
path: string, path: string,
dbOpts = DbOptionsRef(nil); dbOpts = DbOptionsRef(nil),
txDbOpts = TransactionDbOptionsRef(nil); txDbOpts = TransactionDbOptionsRef(nil),
columnFamilies: openArray[ColFamilyDescriptor] = []; columnFamilies: openArray[ColFamilyDescriptor] = [],
): RocksDBResult[TransactionDbRef] = ): RocksDBResult[TransactionDbRef] =
## Open a `TransactionDbRef` with the given options and column families. ## Open a `TransactionDbRef` with the given options and column families.
## If no column families are provided the default column family will be used. ## If no column families are provided the default column family will be used.
## If no options are provided the default options will be used. ## If no options are provided the default options will be used.
let let useDbOpts = (if dbOpts.isNil: defaultDbOptions() else: dbOpts)
useDbOpts = (if dbOpts.isNil: defaultDbOptions() else: dbOpts)
defer: defer:
if dbOpts.isNil: useDbOpts.close() if dbOpts.isNil:
useDbOpts.close()
var cfs = columnFamilies.toSeq() var cfs = columnFamilies.toSeq()
if DEFAULT_COLUMN_FAMILY_NAME notin columnFamilies.mapIt(it.name()): if DEFAULT_COLUMN_FAMILY_NAME notin columnFamilies.mapIt(it.name()):
@ -77,19 +70,23 @@ proc openTransactionDb*(
cast[cstringArray](cfNames[0].addr), cast[cstringArray](cfNames[0].addr),
cfOpts[0].addr, cfOpts[0].addr,
cfHandles[0].addr, cfHandles[0].addr,
cast[cstringArray](errors.addr)) cast[cstringArray](errors.addr),
)
bailOnErrors(errors) bailOnErrors(errors)
let let
dbOpts = useDbOpts # don't close on exit dbOpts = useDbOpts # don't close on exit
txDbOpts = (if txDbOpts.isNil: defaultTransactionDbOptions() else: txDbOpts) txDbOpts = (if txDbOpts.isNil: defaultTransactionDbOptions()
else: txDbOpts
)
db = TransactionDbRef( db = TransactionDbRef(
lock: createLock(), lock: createLock(),
cPtr: txDbPtr, cPtr: txDbPtr,
path: path, path: path,
dbOpts: dbOpts, dbOpts: dbOpts,
txDbOpts: txDbOpts, txDbOpts: txDbOpts,
cfTable: newColFamilyTable(cfNames.mapIt($it), cfHandles)) cfTable: newColFamilyTable(cfNames.mapIt($it), cfHandles),
)
ok(db) ok(db)
proc isClosed*(db: TransactionDbRef): bool {.inline.} = proc isClosed*(db: TransactionDbRef): bool {.inline.} =
@ -97,27 +94,25 @@ proc isClosed*(db: TransactionDbRef): bool {.inline.} =
db.cPtr.isNil() db.cPtr.isNil()
proc beginTransaction*( proc beginTransaction*(
db: TransactionDbRef; db: TransactionDbRef,
readOpts = ReadOptionsRef(nil); readOpts = ReadOptionsRef(nil),
writeOpts = WriteOptionsRef(nil); writeOpts = WriteOptionsRef(nil),
txDbOpts = TransactionDbOptionsRef(nil); txDbOpts = TransactionDbOptionsRef(nil),
txOpts = defaultTransactionOptions(); txOpts = defaultTransactionOptions(),
columnFamily = DEFAULT_COLUMN_FAMILY_NAME; columnFamily = DEFAULT_COLUMN_FAMILY_NAME,
): TransactionRef = ): TransactionRef =
## Begin a new transaction against the database. The transaction will default ## Begin a new transaction against the database. The transaction will default
## to using the specified column family. If no column family is specified ## to using the specified column family. If no column family is specified
## then the default column family will be used. ## then the default column family will be used.
doAssert not db.isClosed() doAssert not db.isClosed()
let let
txDbOpts = (if txDbOpts.isNil: defaultTransactionDbOptions() else: txDbOpts) txDbOpts = (if txDbOpts.isNil: defaultTransactionDbOptions()
else: txDbOpts
)
readOpts = (if readOpts.isNil: defaultReadOptions() else: readOpts) readOpts = (if readOpts.isNil: defaultReadOptions() else: readOpts)
writeOpts = (if writeOpts.isNil: defaultWriteOptions() else: writeOpts) writeOpts = (if writeOpts.isNil: defaultWriteOptions() else: writeOpts)
let txPtr = rocksdb_transaction_begin( let txPtr = rocksdb_transaction_begin(db.cPtr, writeOpts.cPtr, txOpts.cPtr, nil)
db.cPtr,
writeOpts.cPtr,
txOpts.cPtr,
nil)
newTransaction(txPtr, readOpts, writeOpts, txOpts, columnFamily, db.cfTable) newTransaction(txPtr, readOpts, writeOpts, txOpts, columnFamily, db.cfTable)

View File

@ -26,8 +26,7 @@ import
../rocksresult, ../rocksresult,
./txopts ./txopts
export export rocksresult
rocksresult
type type
TransactionPtr* = ptr rocksdb_transaction_t TransactionPtr* = ptr rocksdb_transaction_t
@ -46,15 +45,16 @@ proc newTransaction*(
writeOpts: WriteOptionsRef, writeOpts: WriteOptionsRef,
txOpts: TransactionOptionsRef, txOpts: TransactionOptionsRef,
defaultCfName: string, defaultCfName: string,
cfTable: ColFamilyTableRef): TransactionRef = cfTable: ColFamilyTableRef,
): TransactionRef =
TransactionRef( TransactionRef(
cPtr: cPtr, cPtr: cPtr,
readOpts: readOpts, readOpts: readOpts,
writeOpts: writeOpts, writeOpts: writeOpts,
txOpts: txOpts, txOpts: txOpts,
defaultCfName: defaultCfName, defaultCfName: defaultCfName,
cfTable: cfTable) cfTable: cfTable,
)
proc isClosed*(tx: TransactionRef): bool {.inline.} = proc isClosed*(tx: TransactionRef): bool {.inline.} =
## Returns `true` if the `TransactionRef` has been closed. ## Returns `true` if the `TransactionRef` has been closed.
@ -64,7 +64,8 @@ proc get*(
tx: TransactionRef, tx: TransactionRef,
key: openArray[byte], key: openArray[byte],
onData: DataProc, onData: DataProc,
columnFamily = tx.defaultCfName): RocksDBResult[bool] = columnFamily = tx.defaultCfName,
): RocksDBResult[bool] =
## Get the value for a given key from the transaction using the provided ## Get the value for a given key from the transaction using the provided
## `onData` callback. ## `onData` callback.
@ -85,7 +86,8 @@ proc get*(
cast[cstring](unsafeAddr key[0]), cast[cstring](unsafeAddr key[0]),
csize_t(key.len), csize_t(key.len),
len.addr, len.addr,
cast[cstringArray](errors.addr)) cast[cstringArray](errors.addr),
)
bailOnErrors(errors) bailOnErrors(errors)
if data.isNil(): if data.isNil():
@ -97,9 +99,8 @@ proc get*(
ok(true) ok(true)
proc get*( proc get*(
tx: TransactionRef, tx: TransactionRef, key: openArray[byte], columnFamily = tx.defaultCfName
key: openArray[byte], ): RocksDBResult[seq[byte]] =
columnFamily = tx.defaultCfName): RocksDBResult[seq[byte]] =
## Get the value for a given key from the transaction. ## Get the value for a given key from the transaction.
var dataRes: RocksDBResult[seq[byte]] var dataRes: RocksDBResult[seq[byte]]
@ -113,9 +114,8 @@ proc get*(
dataRes.err(res.error()) dataRes.err(res.error())
proc put*( proc put*(
tx: TransactionRef, tx: TransactionRef, key, val: openArray[byte], columnFamily = tx.defaultCfName
key, val: openArray[byte], ): RocksDBResult[void] =
columnFamily = tx.defaultCfName): RocksDBResult[void] =
## Put the value for the given key into the transaction. ## Put the value for the given key into the transaction.
if key.len() == 0: if key.len() == 0:
@ -131,17 +131,21 @@ proc put*(
cfHandle.cPtr, cfHandle.cPtr,
cast[cstring](unsafeAddr key[0]), cast[cstring](unsafeAddr key[0]),
csize_t(key.len), csize_t(key.len),
cast[cstring](if val.len > 0: unsafeAddr val[0] else: nil), cast[cstring](if val.len > 0:
unsafeAddr val[0]
else:
nil
),
csize_t(val.len), csize_t(val.len),
cast[cstringArray](errors.addr)) cast[cstringArray](errors.addr),
)
bailOnErrors(errors) bailOnErrors(errors)
ok() ok()
proc delete*( proc delete*(
tx: TransactionRef, tx: TransactionRef, key: openArray[byte], columnFamily = tx.defaultCfName
key: openArray[byte], ): RocksDBResult[void] =
columnFamily = tx.defaultCfName): RocksDBResult[void] =
## Delete the value for the given key from the transaction. ## Delete the value for the given key from the transaction.
if key.len() == 0: if key.len() == 0:
@ -157,7 +161,8 @@ proc delete*(
cfHandle.cPtr, cfHandle.cPtr,
cast[cstring](unsafeAddr key[0]), cast[cstring](unsafeAddr key[0]),
csize_t(key.len), csize_t(key.len),
cast[cstringArray](errors.addr)) cast[cstringArray](errors.addr),
)
bailOnErrors(errors) bailOnErrors(errors)
ok() ok()

View File

@ -9,8 +9,7 @@
{.push raises: [].} {.push raises: [].}
import import ../lib/librocksdb
../lib/librocksdb
type type
TransactionDbOptionsPtr* = ptr rocksdb_transactiondb_options_t TransactionDbOptionsPtr* = ptr rocksdb_transactiondb_options_t

View File

@ -9,8 +9,7 @@
{.push raises: [].} {.push raises: [].}
import import ../lib/librocksdb
../lib/librocksdb
type type
TransactionOptionsPtr* = ptr rocksdb_transaction_options_t TransactionOptionsPtr* = ptr rocksdb_transaction_options_t

View File

@ -11,13 +11,9 @@
{.push raises: [].} {.push raises: [].}
import import ./lib/librocksdb, ./internal/[cftable, utils], ./rocksresult
./lib/librocksdb,
./internal/[cftable, utils],
./rocksresult
export export rocksresult
rocksresult
type type
WriteBatchPtr* = ptr rocksdb_writebatch_t WriteBatchPtr* = ptr rocksdb_writebatch_t
@ -29,9 +25,8 @@ type
proc newWriteBatch*(cfTable: ColFamilyTableRef, defaultCfName: string): WriteBatchRef = proc newWriteBatch*(cfTable: ColFamilyTableRef, defaultCfName: string): WriteBatchRef =
WriteBatchRef( WriteBatchRef(
cPtr: rocksdb_writebatch_create(), cPtr: rocksdb_writebatch_create(), defaultCfName: defaultCfName, cfTable: cfTable
defaultCfName: defaultCfName, )
cfTable: cfTable)
proc isClosed*(batch: WriteBatchRef): bool {.inline.} = proc isClosed*(batch: WriteBatchRef): bool {.inline.} =
## Returns `true` if the `WriteBatchRef` has been closed and `false` otherwise. ## Returns `true` if the `WriteBatchRef` has been closed and `false` otherwise.
@ -55,7 +50,8 @@ proc count*(batch: WriteBatchRef): int =
proc put*( proc put*(
batch: WriteBatchRef, batch: WriteBatchRef,
key, val: openArray[byte], key, val: openArray[byte],
columnFamily = DEFAULT_COLUMN_FAMILY_NAME): RocksDBResult[void] = columnFamily = DEFAULT_COLUMN_FAMILY_NAME,
): RocksDBResult[void] =
## Add a put operation to the write batch. ## Add a put operation to the write batch.
if key.len() == 0: if key.len() == 0:
@ -70,15 +66,21 @@ proc put*(
cfHandle.cPtr, cfHandle.cPtr,
cast[cstring](unsafeAddr key[0]), cast[cstring](unsafeAddr key[0]),
csize_t(key.len), csize_t(key.len),
cast[cstring](if val.len > 0: unsafeAddr val[0] else: nil), cast[cstring](if val.len > 0:
csize_t(val.len)) unsafeAddr val[0]
else:
nil
),
csize_t(val.len),
)
ok() ok()
proc delete*( proc delete*(
batch: WriteBatchRef, batch: WriteBatchRef,
key: openArray[byte], key: openArray[byte],
columnFamily = DEFAULT_COLUMN_FAMILY_NAME): RocksDBResult[void] = columnFamily = DEFAULT_COLUMN_FAMILY_NAME,
): RocksDBResult[void] =
## Add a delete operation to the write batch. ## Add a delete operation to the write batch.
if key.len() == 0: if key.len() == 0:
@ -89,10 +91,8 @@ proc delete*(
return err("rocksdb: unknown column family") return err("rocksdb: unknown column family")
rocksdb_writebatch_delete_cf( rocksdb_writebatch_delete_cf(
batch.cPtr, batch.cPtr, cfHandle.cPtr, cast[cstring](unsafeAddr key[0]), csize_t(key.len)
cfHandle.cPtr, )
cast[cstring](unsafeAddr key[0]),
csize_t(key.len))
ok() ok()

View File

@ -9,13 +9,9 @@
{.used.} {.used.}
import import unittest2, ../../rocksdb/internal/utils, ../../rocksdb/columnfamily/cfdescriptor
unittest2,
../../rocksdb/internal/utils,
../../rocksdb/columnfamily/cfdescriptor
suite "ColFamilyDescriptor Tests": suite "ColFamilyDescriptor Tests":
const TEST_CF_NAME = "test" const TEST_CF_NAME = "test"
test "Test initColFamilyDescriptor": test "Test initColFamilyDescriptor":
@ -56,4 +52,3 @@ suite "ColFamilyDescriptor Tests":
check descriptor.isClosed() check descriptor.isClosed()
descriptor.close() descriptor.close()
check descriptor.isClosed() check descriptor.isClosed()

View File

@ -17,7 +17,6 @@ import
../../rocksdb/columnfamily/cfhandle ../../rocksdb/columnfamily/cfhandle
suite "ColFamilyHandleRef Tests": suite "ColFamilyHandleRef Tests":
const TEST_CF_NAME = "test" const TEST_CF_NAME = "test"
setup: setup:
@ -26,20 +25,17 @@ suite "ColFamilyHandleRef Tests":
dbOpts = rocksdb_options_create() dbOpts = rocksdb_options_create()
cfOpts = rocksdb_options_create() cfOpts = rocksdb_options_create()
var var errors: cstring
errors: cstring
rocksdb_options_set_create_if_missing(dbOpts, 1); rocksdb_options_set_create_if_missing(dbOpts, 1)
let db = rocksdb_open(dbOpts, dbPath.cstring, cast[cstringArray](errors.addr)) let db = rocksdb_open(dbOpts, dbPath.cstring, cast[cstringArray](errors.addr))
doAssert errors.isNil() doAssert errors.isNil()
doAssert not db.isNil() doAssert not db.isNil()
let cfHandlePtr = rocksdb_create_column_family( let cfHandlePtr = rocksdb_create_column_family(
db, db, cfOpts, TEST_CF_NAME.cstring, cast[cstringArray](errors.addr)
cfOpts, )
TEST_CF_NAME.cstring,
cast[cstringArray](errors.addr))
doAssert errors.isNil() doAssert errors.isNil()
doAssert not cfHandlePtr.isNil() doAssert not cfHandlePtr.isNil()
@ -64,4 +60,3 @@ suite "ColFamilyHandleRef Tests":
check cfHandle.isClosed() check cfHandle.isClosed()
cfHandle.close() cfHandle.close()
check cfHandle.isClosed() check cfHandle.isClosed()

View File

@ -9,9 +9,7 @@
{.used.} {.used.}
import import unittest2, ../../rocksdb/columnfamily/cfopts
unittest2,
../../rocksdb/columnfamily/cfopts
suite "ColFamilyOptionsRef Tests": suite "ColFamilyOptionsRef Tests":
test "Test close": test "Test close":

View File

@ -18,7 +18,6 @@ import
../../rocksdb/internal/cftable ../../rocksdb/internal/cftable
suite "ColFamilyTableRef Tests": suite "ColFamilyTableRef Tests":
const TEST_CF_NAME = "test" const TEST_CF_NAME = "test"
setup: setup:
@ -27,20 +26,17 @@ suite "ColFamilyTableRef Tests":
dbOpts = rocksdb_options_create() dbOpts = rocksdb_options_create()
cfOpts = rocksdb_options_create() cfOpts = rocksdb_options_create()
var var errors: cstring
errors: cstring
rocksdb_options_set_create_if_missing(dbOpts, 1); rocksdb_options_set_create_if_missing(dbOpts, 1)
let db = rocksdb_open(dbOpts, dbPath.cstring, cast[cstringArray](errors.addr)) let db = rocksdb_open(dbOpts, dbPath.cstring, cast[cstringArray](errors.addr))
doAssert errors.isNil() doAssert errors.isNil()
doAssert not db.isNil() doAssert not db.isNil()
let cfHandlePtr = rocksdb_create_column_family( let cfHandlePtr = rocksdb_create_column_family(
db, db, cfOpts, TEST_CF_NAME.cstring, cast[cstringArray](errors.addr)
cfOpts, )
TEST_CF_NAME.cstring,
cast[cstringArray](errors.addr))
doAssert errors.isNil() doAssert errors.isNil()
doAssert not cfHandlePtr.isNil() doAssert not cfHandlePtr.isNil()
@ -48,11 +44,9 @@ suite "ColFamilyTableRef Tests":
rocksdb_close(db) rocksdb_close(db)
removeDir($dbPath) removeDir($dbPath)
test "Test newColFamilyTable": test "Test newColFamilyTable":
var cfTable = newColFamilyTable( var cfTable =
@[TEST_CF_NAME, TEST_CF_NAME], newColFamilyTable(@[TEST_CF_NAME, TEST_CF_NAME], @[cfHandlePtr, cfHandlePtr])
@[cfHandlePtr, cfHandlePtr])
check cfTable.get(TEST_CF_NAME).cPtr() == cfHandlePtr check cfTable.get(TEST_CF_NAME).cPtr() == cfHandlePtr
check not cfTable.isClosed() check not cfTable.isClosed()

View File

@ -9,11 +9,7 @@
{.used.} {.used.}
import import std/[cpuinfo, os], tempfile, unittest2, ../../rocksdb/lib/librocksdb
std/[cpuinfo, os],
tempfile,
unittest2,
../../rocksdb/lib/librocksdb
suite "librocksdb C wrapper Tests": suite "librocksdb C wrapper Tests":
setup: setup:
@ -37,54 +33,70 @@ suite "librocksdb C wrapper Tests":
# snappy support (for example Fedora 28, certain Ubuntu versions) # snappy support (for example Fedora 28, certain Ubuntu versions)
# rocksdb_options_optimize_level_style_compaction(options, 0); # rocksdb_options_optimize_level_style_compaction(options, 0);
# create the DB if it's not already present # create the DB if it's not already present
rocksdb_options_set_create_if_missing(options, 1); rocksdb_options_set_create_if_missing(options, 1)
# open DB var # open DB
var err: cstringArray # memory leak: example code does not free error string! err: cstringArray # memory leak: example code does not free error string!
db = rocksdb_open(options, dbPath, err) db = rocksdb_open(options, dbPath, err)
check: err.isNil check:
err.isNil
# open Backup Engine that we will use for backing up our database # open Backup Engine that we will use for backing up our database
be = rocksdb_backup_engine_open(options, dbBackupPath, err) be = rocksdb_backup_engine_open(options, dbBackupPath, err)
check: err.isNil check:
err.isNil
# Put key-value # Put key-value
var writeOptions = rocksdb_writeoptions_create() var writeOptions = rocksdb_writeoptions_create()
let key = "key" let key = "key"
let put_value = "value" let put_value = "value"
rocksdb_put( rocksdb_put(
db, writeOptions, key.cstring, csize_t(key.len), db,
put_value.cstring, csize_t(put_value.len), err) writeOptions,
check: err.isNil key.cstring,
csize_t(key.len),
put_value.cstring,
csize_t(put_value.len),
err,
)
check:
err.isNil
# Get value # Get value
var readOptions = rocksdb_readoptions_create() var readOptions = rocksdb_readoptions_create()
var len: csize_t var len: csize_t
let raw_value = rocksdb_get( let raw_value =
db, readOptions, key.cstring, csize_t(key.len), addr len, err) # Important: rocksdb_get is not null-terminated rocksdb_get(db, readOptions, key.cstring, csize_t(key.len), addr len, err)
check: err.isNil # Important: rocksdb_get is not null-terminated
check:
err.isNil
# Copy it to a regular Nim string (copyMem workaround because non-null terminated) # Copy it to a regular Nim string (copyMem workaround because non-null terminated)
var get_value = newString(int(len)) var get_value = newString(int(len))
copyMem(addr get_value[0], unsafeAddr raw_value[0], int(len) * sizeof(char)) copyMem(addr get_value[0], unsafeAddr raw_value[0], int(len) * sizeof(char))
check: $get_value == $put_value check:
$get_value == $put_value
# create new backup in a directory specified by DBBackupPath # create new backup in a directory specified by DBBackupPath
rocksdb_backup_engine_create_new_backup(be, db, err) rocksdb_backup_engine_create_new_backup(be, db, err)
check: err.isNil check:
err.isNil
rocksdb_close(db) rocksdb_close(db)
# If something is wrong, you might want to restore data from last backup # If something is wrong, you might want to restore data from last backup
var restoreOptions = rocksdb_restore_options_create() var restoreOptions = rocksdb_restore_options_create()
rocksdb_backup_engine_restore_db_from_latest_backup(be, dbPath, dbPath, rocksdb_backup_engine_restore_db_from_latest_backup(
restoreOptions, err) be, dbPath, dbPath, restoreOptions, err
check: err.isNil )
check:
err.isNil
rocksdb_restore_options_destroy(restoreOptions) rocksdb_restore_options_destroy(restoreOptions)
db = rocksdb_open(options, dbPath, err) db = rocksdb_open(options, dbPath, err)
check: err.isNil check:
err.isNil
# cleanup # cleanup
rocksdb_writeoptions_destroy(writeOptions) rocksdb_writeoptions_destroy(writeOptions)

View File

@ -9,12 +9,9 @@
{.used.} {.used.}
import import unittest2, ../../rocksdb/options/backupopts
unittest2,
../../rocksdb/options/backupopts
suite "BackupEngineOptionsRef Tests": suite "BackupEngineOptionsRef Tests":
test "Test newBackupEngineOptions": test "Test newBackupEngineOptions":
var backupOpts = newBackupEngineOptions() var backupOpts = newBackupEngineOptions()

View File

@ -9,12 +9,9 @@
{.used.} {.used.}
import import unittest2, ../../rocksdb/options/dbopts
unittest2,
../../rocksdb/options/dbopts
suite "DbOptionsRef Tests": suite "DbOptionsRef Tests":
test "Test newDbOptions": test "Test newDbOptions":
var dbOpts = newDbOptions() var dbOpts = newDbOptions()

View File

@ -9,12 +9,9 @@
{.used.} {.used.}
import import unittest2, ../../rocksdb/options/readopts
unittest2,
../../rocksdb/options/readopts
suite "ReadOptionsRef Tests": suite "ReadOptionsRef Tests":
test "Test newReadOptions": test "Test newReadOptions":
var readOpts = newReadOptions() var readOpts = newReadOptions()

View File

@ -9,12 +9,9 @@
{.used.} {.used.}
import import unittest2, ../../rocksdb/options/writeopts
unittest2,
../../rocksdb/options/writeopts
suite "WriteOptionsRef Tests": suite "WriteOptionsRef Tests":
test "Test newWriteOptions": test "Test newWriteOptions":
var writeOpts = newWriteOptions() var writeOpts = newWriteOptions()

View File

@ -9,15 +9,9 @@
{.used.} {.used.}
import import std/os, tempfile, unittest2, ../rocksdb/backup, ./test_helper
std/os,
tempfile,
unittest2,
../rocksdb/backup,
./test_helper
suite "BackupEngineRef Tests": suite "BackupEngineRef Tests":
let let
key = @[byte(1), 2, 3, 4, 5] key = @[byte(1), 2, 3, 4, 5]
val = @[byte(1), 2, 3, 4, 5] val = @[byte(1), 2, 3, 4, 5]
@ -28,16 +22,13 @@ suite "BackupEngineRef Tests":
dbBackupPath = mkdtemp() / "backup" dbBackupPath = mkdtemp() / "backup"
dbRestorePath = mkdtemp() / "restore" dbRestorePath = mkdtemp() / "restore"
var var db = initReadWriteDb(dbPath)
db = initReadWriteDb(dbPath)
teardown: teardown:
db.close() db.close()
removeDir($dbPath) removeDir($dbPath)
removeDir($dbBackupPath) removeDir($dbBackupPath)
test "Test backup": test "Test backup":
var engine = initBackupEngine(dbBackupPath) var engine = initBackupEngine(dbBackupPath)

View File

@ -9,12 +9,7 @@
{.used.} {.used.}
import import std/os, tempfile, unittest2, ../rocksdb/columnfamily, ./test_helper
std/os,
tempfile,
unittest2,
../rocksdb/columnfamily,
./test_helper
suite "ColFamily Tests": suite "ColFamily Tests":
const const
@ -43,8 +38,18 @@ suite "ColFamily Tests":
check cf.put(key, val).isOk() check cf.put(key, val).isOk()
var bytes: seq[byte] var bytes: seq[byte]
check cf.get(key, proc(data: openArray[byte]) = bytes = @data)[] check cf.get(
check not cf.get(otherKey, proc(data: openArray[byte]) = bytes = @data)[] key,
proc(data: openArray[byte]) =
bytes = @data
,
)[]
check not cf.get(
otherKey,
proc(data: openArray[byte]) =
bytes = @data
,
)[]
var r1 = cf.get(key) var r1 = cf.get(key)
check r1.isOk() and r1.value == val check r1.isOk() and r1.value == val

View File

@ -9,50 +9,41 @@
{.used.} {.used.}
import import std/sequtils, ../rocksdb/backup, ../rocksdb/rocksdb, ../rocksdb/transactiondb
std/sequtils,
../rocksdb/backup,
../rocksdb/rocksdb,
../rocksdb/transactiondb
proc initReadWriteDb*( proc initReadWriteDb*(
path: string, path: string, columnFamilyNames: openArray[string] = @[]
columnFamilyNames: openArray[string] = @[]): RocksDbReadWriteRef = ): RocksDbReadWriteRef =
let res = openRocksDb( let res = openRocksDb(
path, path, columnFamilies = columnFamilyNames.mapIt(initColFamilyDescriptor(it))
columnFamilies = columnFamilyNames.mapIt(initColFamilyDescriptor(it))) )
if res.isErr(): if res.isErr():
echo res.error() echo res.error()
doAssert res.isOk() doAssert res.isOk()
res.value() res.value()
proc initReadOnlyDb*( proc initReadOnlyDb*(
path: string, path: string, columnFamilyNames: openArray[string] = @[]
columnFamilyNames: openArray[string] = @[]): RocksDbReadOnlyRef = ): RocksDbReadOnlyRef =
let res = openRocksDbReadOnly( let res = openRocksDbReadOnly(
path, path, columnFamilies = columnFamilyNames.mapIt(initColFamilyDescriptor(it))
columnFamilies = columnFamilyNames.mapIt(initColFamilyDescriptor(it))) )
if res.isErr(): if res.isErr():
echo res.error() echo res.error()
doAssert res.isOk() doAssert res.isOk()
res.value() res.value()
proc initBackupEngine*(path: string): BackupEngineRef = proc initBackupEngine*(path: string): BackupEngineRef =
let res = openBackupEngine(path) let res = openBackupEngine(path)
doAssert res.isOk() doAssert res.isOk()
res.value() res.value()
proc initTransactionDb*( proc initTransactionDb*(
path: string, path: string, columnFamilyNames: openArray[string] = @[]
columnFamilyNames: openArray[string] = @[]): TransactionDbRef = ): TransactionDbRef =
let res = openTransactionDb( let res = openTransactionDb(
path, path, columnFamilies = columnFamilyNames.mapIt(initColFamilyDescriptor(it))
columnFamilies = columnFamilyNames.mapIt(initColFamilyDescriptor(it))) )
if res.isErr(): if res.isErr():
echo res.error() echo res.error()
doAssert res.isOk() doAssert res.isOk()

View File

@ -9,12 +9,7 @@
{.used.} {.used.}
import import std/os, tempfile, unittest2, ../rocksdb/rocksdb, ./test_helper
std/os,
tempfile,
unittest2,
../rocksdb/rocksdb,
./test_helper
suite "RocksDbRef Tests": suite "RocksDbRef Tests":
const const
@ -36,13 +31,22 @@ suite "RocksDbRef Tests":
removeDir($dbPath) removeDir($dbPath)
test "Basic operations": test "Basic operations":
var s = db.put(key, val) var s = db.put(key, val)
check s.isOk() check s.isOk()
var bytes: seq[byte] var bytes: seq[byte]
check db.get(key, proc(data: openArray[byte]) = bytes = @data)[] check db.get(
check not db.get(otherKey, proc(data: openArray[byte]) = bytes = @data)[] key,
proc(data: openArray[byte]) =
bytes = @data
,
)[]
check not db.get(
otherKey,
proc(data: openArray[byte]) =
bytes = @data
,
)[]
var r1 = db.get(key) var r1 = db.get(key)
check r1.isOk() and r1.value == val check r1.isOk() and r1.value == val
@ -84,13 +88,24 @@ suite "RocksDbRef Tests":
check readOnlyDb.isClosed() check readOnlyDb.isClosed()
test "Basic operations - default column family": test "Basic operations - default column family":
var s = db.put(key, val, CF_DEFAULT) var s = db.put(key, val, CF_DEFAULT)
check s.isOk() check s.isOk()
var bytes: seq[byte] var bytes: seq[byte]
check db.get(key, proc(data: openArray[byte]) = bytes = @data, CF_DEFAULT)[] check db.get(
check not db.get(otherKey, proc(data: openArray[byte]) = bytes = @data, CF_DEFAULT)[] key,
proc(data: openArray[byte]) =
bytes = @data
,
CF_DEFAULT,
)[]
check not db.get(
otherKey,
proc(data: openArray[byte]) =
bytes = @data
,
CF_DEFAULT,
)[]
var r1 = db.get(key) var r1 = db.get(key)
check r1.isOk() and r1.value == val check r1.isOk() and r1.value == val
@ -132,7 +147,6 @@ suite "RocksDbRef Tests":
check readOnlyDb.isClosed() check readOnlyDb.isClosed()
test "Basic operations - multiple column families": test "Basic operations - multiple column families":
var s = db.put(key, val, CF_DEFAULT) var s = db.put(key, val, CF_DEFAULT)
check s.isOk() check s.isOk()
@ -140,12 +154,36 @@ suite "RocksDbRef Tests":
check s2.isOk() check s2.isOk()
var bytes: seq[byte] var bytes: seq[byte]
check db.get(key, proc(data: openArray[byte]) = bytes = @data, CF_DEFAULT)[] check db.get(
check not db.get(otherKey, proc(data: openArray[byte]) = bytes = @data, CF_DEFAULT)[] key,
proc(data: openArray[byte]) =
bytes = @data
,
CF_DEFAULT,
)[]
check not db.get(
otherKey,
proc(data: openArray[byte]) =
bytes = @data
,
CF_DEFAULT,
)[]
var bytes2: seq[byte] var bytes2: seq[byte]
check db.get(otherKey, proc(data: openArray[byte]) = bytes2 = @data, CF_OTHER)[] check db.get(
check not db.get(key, proc(data: openArray[byte]) = bytes2 = @data, CF_OTHER)[] otherKey,
proc(data: openArray[byte]) =
bytes2 = @data
,
CF_OTHER,
)[]
check not db.get(
key,
proc(data: openArray[byte]) =
bytes2 = @data
,
CF_OTHER,
)[]
var e1 = db.keyExists(key, CF_DEFAULT) var e1 = db.keyExists(key, CF_DEFAULT)
check e1.isOk() and e1.value == true check e1.isOk() and e1.value == true
@ -178,8 +216,8 @@ suite "RocksDbRef Tests":
# Open database in read only mode # Open database in read only mode
block: block:
var var readOnlyDb =
readOnlyDb = initReadOnlyDb(dbPath, columnFamilyNames = @[CF_DEFAULT, CF_OTHER]) initReadOnlyDb(dbPath, columnFamilyNames = @[CF_DEFAULT, CF_OTHER])
var r = readOnlyDb.keyExists(key, CF_OTHER) var r = readOnlyDb.keyExists(key, CF_OTHER)
check r.isOk() and r.value == false check r.isOk() and r.value == false
@ -192,7 +230,6 @@ suite "RocksDbRef Tests":
check readOnlyDb.isClosed() check readOnlyDb.isClosed()
test "Close multiple times": test "Close multiple times":
check not db.isClosed() check not db.isClosed()
db.close() db.close()
check db.isClosed() check db.isClosed()
@ -206,7 +243,13 @@ suite "RocksDbRef Tests":
check r.isErr() and r.error() == "rocksdb: unknown column family" check r.isErr() and r.error() == "rocksdb: unknown column family"
var bytes: seq[byte] var bytes: seq[byte]
let r2 = db.get(key, proc(data: openArray[byte]) = bytes = @data, CF_UNKNOWN) let r2 = db.get(
key,
proc(data: openArray[byte]) =
bytes = @data
,
CF_UNKNOWN,
)
check r2.isErr() and r2.error() == "rocksdb: unknown column family" check r2.isErr() and r2.error() == "rocksdb: unknown column family"
let r3 = db.keyExists(key, CF_UNKNOWN) let r3 = db.keyExists(key, CF_UNKNOWN)
@ -240,7 +283,12 @@ suite "RocksDbRef Tests":
block: block:
var v: seq[byte] var v: seq[byte]
let r = db.get(key1, proc(data: openArray[byte]) = v = @data) let r = db.get(
key1,
proc(data: openArray[byte]) =
v = @data
,
)
check: check:
r.isOk() r.isOk()
r.value() == true r.value() == true
@ -249,7 +297,12 @@ suite "RocksDbRef Tests":
block: block:
var v: seq[byte] var v: seq[byte]
let r = db.get(key2, proc(data: openArray[byte]) = v = @data) let r = db.get(
key2,
proc(data: openArray[byte]) =
v = @data
,
)
check: check:
r.isOk() r.isOk()
r.value() == true r.value() == true
@ -258,7 +311,12 @@ suite "RocksDbRef Tests":
block: block:
var v: seq[byte] var v: seq[byte]
let r = db.get(key3, proc(data: openArray[byte]) = v = @data) let r = db.get(
key3,
proc(data: openArray[byte]) =
v = @data
,
)
check: check:
r.isOk() r.isOk()
r.value() == true r.value() == true
@ -267,7 +325,12 @@ suite "RocksDbRef Tests":
block: block:
var v: seq[byte] var v: seq[byte]
let r = db.get(key4, proc(data: openArray[byte]) = v = @data) let r = db.get(
key4,
proc(data: openArray[byte]) =
v = @data
,
)
check: check:
r.isOk() r.isOk()
r.value() == false r.value() == false
@ -276,7 +339,12 @@ suite "RocksDbRef Tests":
block: block:
var v: seq[byte] var v: seq[byte]
let r = db.get(key5, proc(data: openArray[byte]) = v = @data) let r = db.get(
key5,
proc(data: openArray[byte]) =
v = @data
,
)
check: check:
r.isOk() r.isOk()
r.value() == false r.value() == false

View File

@ -9,15 +9,9 @@
{.used.} {.used.}
import import std/os, tempfile, unittest2, ../rocksdb/[rocksdb, rocksiterator], ./test_helper
std/os,
tempfile,
unittest2,
../rocksdb/[rocksdb, rocksiterator],
./test_helper
suite "RocksIteratorRef Tests": suite "RocksIteratorRef Tests":
const const
CF_DEFAULT = "default" CF_DEFAULT = "default"
CF_OTHER = "other" CF_OTHER = "other"
@ -34,8 +28,8 @@ suite "RocksIteratorRef Tests":
setup: setup:
let let
dbPath = mkdtemp() / "data" dbPath = mkdtemp() / "data"
db = initReadWriteDb(dbPath, db =
columnFamilyNames = @[CF_DEFAULT, CF_OTHER, CF_EMPTY]) initReadWriteDb(dbPath, columnFamilyNames = @[CF_DEFAULT, CF_OTHER, CF_EMPTY])
doAssert db.put(key1, val1).isOk() doAssert db.put(key1, val1).isOk()
doAssert db.put(key2, val2).isOk() doAssert db.put(key2, val2).isOk()
@ -53,7 +47,8 @@ suite "RocksIteratorRef Tests":
check res.isOk() check res.isOk()
var iter = res.get() var iter = res.get()
defer: iter.close() defer:
iter.close()
iter.seekToFirst() iter.seekToFirst()
check iter.isValid() check iter.isValid()
@ -78,18 +73,24 @@ suite "RocksIteratorRef Tests":
check res.isOk() check res.isOk()
var iter = res.get() var iter = res.get()
defer: iter.close() defer:
iter.close()
iter.seekToLast() iter.seekToLast()
check iter.isValid() check iter.isValid()
var expected = byte(3) var expected = byte(3)
while iter.isValid(): while iter.isValid():
var key: seq[byte] var key: seq[byte]
iter.key(proc(data: openArray[byte]) = key = @data) iter.key(
proc(data: openArray[byte]) =
key = @data
)
var val: seq[byte] var val: seq[byte]
iter.value(proc(data: openArray[byte]) = val = @data) iter.value(
proc(data: openArray[byte]) =
val = @data
)
check: check:
key == @[expected] key == @[expected]
@ -105,11 +106,13 @@ suite "RocksIteratorRef Tests":
let res1 = db.openIterator(CF_DEFAULT) let res1 = db.openIterator(CF_DEFAULT)
check res1.isOk() check res1.isOk()
var iter1 = res1.get() var iter1 = res1.get()
defer: iter1.close() defer:
iter1.close()
let res2 = db.openIterator(CF_DEFAULT) let res2 = db.openIterator(CF_DEFAULT)
check res2.isOk() check res2.isOk()
var iter2 = res2.get() var iter2 = res2.get()
defer: iter2.close() defer:
iter2.close()
iter1.seekToFirst() iter1.seekToFirst()
check iter1.isValid() check iter1.isValid()
@ -126,11 +129,13 @@ suite "RocksIteratorRef Tests":
let res1 = db.openIterator(CF_DEFAULT) let res1 = db.openIterator(CF_DEFAULT)
check res1.isOk() check res1.isOk()
var iter1 = res1.get() var iter1 = res1.get()
defer: iter1.close() defer:
iter1.close()
let res2 = db.openIterator(CF_OTHER) let res2 = db.openIterator(CF_OTHER)
check res2.isOk() check res2.isOk()
var iter2 = res2.get() var iter2 = res2.get()
defer: iter2.close() defer:
iter2.close()
iter1.seekToFirst() iter1.seekToFirst()
check iter1.isValid() check iter1.isValid()
@ -153,7 +158,8 @@ suite "RocksIteratorRef Tests":
let res = db.openIterator(CF_EMPTY) let res = db.openIterator(CF_EMPTY)
check res.isOk() check res.isOk()
var iter = res.get() var iter = res.get()
defer: iter.close() defer:
iter.close()
iter.seekToFirst() iter.seekToFirst()
check not iter.isValid() check not iter.isValid()
@ -165,7 +171,8 @@ suite "RocksIteratorRef Tests":
let res = db.openIterator(CF_EMPTY) let res = db.openIterator(CF_EMPTY)
check res.isOk() check res.isOk()
var iter = res.get() var iter = res.get()
defer: iter.close() defer:
iter.close()
check iter.status().isOk() check iter.status().isOk()
iter.seekToLast() iter.seekToLast()

View File

@ -9,15 +9,9 @@
{.used.} {.used.}
import import std/os, tempfile, unittest2, ../rocksdb/[rocksdb, sstfilewriter], ./test_helper
std/os,
tempfile,
unittest2,
../rocksdb/[rocksdb, sstfilewriter],
./test_helper
suite "SstFileWriterRef Tests": suite "SstFileWriterRef Tests":
const const
CF_DEFAULT = "default" CF_DEFAULT = "default"
CF_OTHER = "other" CF_OTHER = "other"
@ -34,8 +28,7 @@ suite "SstFileWriterRef Tests":
let let
dbPath = mkdtemp() / "data" dbPath = mkdtemp() / "data"
sstFilePath = mkdtemp() / "sst" sstFilePath = mkdtemp() / "sst"
db = initReadWriteDb(dbPath, db = initReadWriteDb(dbPath, columnFamilyNames = @[CF_DEFAULT, CF_OTHER])
columnFamilyNames = @[CF_DEFAULT, CF_OTHER])
teardown: teardown:
db.close() db.close()
@ -45,7 +38,8 @@ suite "SstFileWriterRef Tests":
let res = openSstFileWriter(sstFilePath) let res = openSstFileWriter(sstFilePath)
check res.isOk() check res.isOk()
let writer = res.get() let writer = res.get()
defer: writer.close() defer:
writer.close()
check: check:
writer.put(key1, val1).isOk() writer.put(key1, val1).isOk()
@ -63,7 +57,8 @@ suite "SstFileWriterRef Tests":
let res = openSstFileWriter(sstFilePath) let res = openSstFileWriter(sstFilePath)
check res.isOk() check res.isOk()
let writer = res.get() let writer = res.get()
defer: writer.close() defer:
writer.close()
check: check:
writer.put(key1, val1).isOk() writer.put(key1, val1).isOk()

View File

@ -9,15 +9,9 @@
{.used.} {.used.}
import import std/os, tempfile, unittest2, ../rocksdb/[transactiondb], ./test_helper
std/os,
tempfile,
unittest2,
../rocksdb/[transactiondb],
./test_helper
suite "TransactionDbRef Tests": suite "TransactionDbRef Tests":
const const
CF_DEFAULT = "default" CF_DEFAULT = "default"
CF_OTHER = "other" CF_OTHER = "other"
@ -39,10 +33,10 @@ suite "TransactionDbRef Tests":
removeDir($dbPath) removeDir($dbPath)
# test multiple transactions # test multiple transactions
test "Test rollback using default column family": test "Test rollback using default column family":
var tx = db.beginTransaction() var tx = db.beginTransaction()
defer: tx.close() defer:
tx.close()
check not tx.isClosed() check not tx.isClosed()
check: check:
@ -67,7 +61,8 @@ suite "TransactionDbRef Tests":
test "Test commit using default column family": test "Test commit using default column family":
var tx = db.beginTransaction() var tx = db.beginTransaction()
defer: tx.close() defer:
tx.close()
check not tx.isClosed() check not tx.isClosed()
check: check:
@ -92,7 +87,8 @@ suite "TransactionDbRef Tests":
test "Test setting column family in beginTransaction": test "Test setting column family in beginTransaction":
var tx = db.beginTransaction(columnFamily = CF_OTHER) var tx = db.beginTransaction(columnFamily = CF_OTHER)
defer: tx.close() defer:
tx.close()
check not tx.isClosed() check not tx.isClosed()
check: check:
@ -111,13 +107,14 @@ suite "TransactionDbRef Tests":
tx.get(key2, CF_OTHER).error() == "" tx.get(key2, CF_OTHER).error() == ""
tx.get(key3, CF_OTHER).get() == val3 tx.get(key3, CF_OTHER).get() == val3
test "Test rollback and commit with multiple transactions": test "Test rollback and commit with multiple transactions":
var tx1 = db.beginTransaction(columnFamily = CF_DEFAULT) var tx1 = db.beginTransaction(columnFamily = CF_DEFAULT)
defer: tx1.close() defer:
tx1.close()
check not tx1.isClosed() check not tx1.isClosed()
var tx2 = db.beginTransaction(columnFamily = CF_OTHER) var tx2 = db.beginTransaction(columnFamily = CF_OTHER)
defer: tx2.close() defer:
tx2.close()
check not tx2.isClosed() check not tx2.isClosed()
check: check:

View File

@ -9,15 +9,9 @@
{.used.} {.used.}
import import std/os, tempfile, unittest2, ../rocksdb/[rocksdb, writebatch], ./test_helper
std/os,
tempfile,
unittest2,
../rocksdb/[rocksdb, writebatch],
./test_helper
suite "WriteBatchRef Tests": suite "WriteBatchRef Tests":
const const
CF_DEFAULT = "default" CF_DEFAULT = "default"
CF_OTHER = "other" CF_OTHER = "other"
@ -40,7 +34,8 @@ suite "WriteBatchRef Tests":
test "Test writing batch to the default column family": test "Test writing batch to the default column family":
var batch = db.openWriteBatch() var batch = db.openWriteBatch()
defer: batch.close() defer:
batch.close()
check not batch.isClosed() check not batch.isClosed()
check: check:
@ -68,7 +63,8 @@ suite "WriteBatchRef Tests":
test "Test writing batch to column family": test "Test writing batch to column family":
var batch = db.openWriteBatch() var batch = db.openWriteBatch()
defer: batch.close() defer:
batch.close()
check not batch.isClosed() check not batch.isClosed()
check: check:
@ -95,7 +91,8 @@ suite "WriteBatchRef Tests":
test "Test writing to multiple column families in single batch": test "Test writing to multiple column families in single batch":
var batch = db.openWriteBatch() var batch = db.openWriteBatch()
defer: batch.close() defer:
batch.close()
check not batch.isClosed() check not batch.isClosed()
check: check:
@ -124,11 +121,13 @@ suite "WriteBatchRef Tests":
test "Test writing to multiple column families in multiple batches": test "Test writing to multiple column families in multiple batches":
var batch1 = db.openWriteBatch() var batch1 = db.openWriteBatch()
defer: batch1.close() defer:
batch1.close()
check not batch1.isClosed() check not batch1.isClosed()
var batch2 = db.openWriteBatch() var batch2 = db.openWriteBatch()
defer: batch2.close() defer:
batch2.close()
check not batch2.isClosed() check not batch2.isClosed()
check: check:
@ -157,7 +156,8 @@ suite "WriteBatchRef Tests":
const CF_UNKNOWN = "unknown" const CF_UNKNOWN = "unknown"
var batch = db.openWriteBatch() var batch = db.openWriteBatch()
defer: batch.close() defer:
batch.close()
check not batch.isClosed() check not batch.isClosed()
let r = batch.put(key1, val1, CF_UNKNOWN) let r = batch.put(key1, val1, CF_UNKNOWN)
@ -168,7 +168,8 @@ suite "WriteBatchRef Tests":
test "Test write empty batch": test "Test write empty batch":
var batch = db.openWriteBatch() var batch = db.openWriteBatch()
defer: batch.close() defer:
batch.close()
check not batch.isClosed() check not batch.isClosed()
check batch.count() == 0 check batch.count() == 0

View File

@ -9,12 +9,9 @@
{.used.} {.used.}
import import unittest2, ../../rocksdb/transactions/txdbopts
unittest2,
../../rocksdb/transactions/txdbopts
suite "TransactionDbOptionsRef Tests": suite "TransactionDbOptionsRef Tests":
test "Test newTransactionDbOptions": test "Test newTransactionDbOptions":
var txDbOpts = newTransactionDbOptions() var txDbOpts = newTransactionDbOptions()

View File

@ -9,12 +9,9 @@
{.used.} {.used.}
import import unittest2, ../../rocksdb/transactions/txopts
unittest2,
../../rocksdb/transactions/txopts
suite "TransactionOptionsRef Tests": suite "TransactionOptionsRef Tests":
test "Test newTransactionOptions": test "Test newTransactionOptions":
var txOpts = newTransactionOptions() var txOpts = newTransactionOptions()