Skip to content

Commit

Permalink
Added tests for new types.
Browse files Browse the repository at this point in the history
  • Loading branch information
bhartnett committed Feb 20, 2024
1 parent dca162f commit 6ad22e6
Show file tree
Hide file tree
Showing 23 changed files with 444 additions and 54 deletions.
2 changes: 1 addition & 1 deletion rocksdb.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ proc test(args, path: string) =
" --outdir:build -r --hints:off --threads:on --skipParentCfg " & path

task test, "Run tests":
test "", "tests/all.nim"
test "", "tests/test_all.nim"
# Too troublesome to install "librocksdb.a" in CI, but this is how we would
# test it (we need the C++ linker profile because it's a C++ library):
# test "-d:LibrocksbStaticArgs='-l:librocksdb.a' --gcc.linkerexe=g++", "tests/all.nim"
Expand Down
5 changes: 4 additions & 1 deletion rocksdb/backup.nim
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ proc openBackupEngine*(
var errors: cstring
let backupEnginePtr = rocksdb_backup_engine_open(
backupOpts.cPtr,
path,
path.cstring,
cast[cstringArray](errors.addr))
bailOnErrors(errors)

Expand All @@ -40,6 +40,9 @@ proc openBackupEngine*(
backupOpts: backupOpts)
ok(engine)

template isClosed*(backupEngine: BackupEngineRef): bool =
backupEngine.cPtr.isNil()

proc backup*(backupEngine: BackupEngineRef, db: RocksDbRef): RocksDBResult[void] =
doAssert not backupEngine.isClosed()

Expand Down
6 changes: 5 additions & 1 deletion rocksdb/columnfamily/cfdescriptor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
{.push raises: [].}

import
../internal/utils,
./cfopts

export cfopts
Expand All @@ -31,10 +32,13 @@ template options*(descriptor: ColFamilyDescriptor): ColFamilyOptionsRef =
descriptor.options

template isDefault*(descriptor: ColFamilyDescriptor): bool =
descriptor.name() == DEFAULT_COLUMN_FAMILY_NAME
descriptor.name == DEFAULT_COLUMN_FAMILY_NAME

template defaultColFamilyDescriptor*(): ColFamilyDescriptor =
initColFamilyDescriptor(DEFAULT_COLUMN_FAMILY_NAME)

template isClosed*(descriptor: ColFamilyDescriptor): bool =
descriptor.options.isClosed()

template close*(descriptor: var ColFamilyDescriptor) =
descriptor.options.close()
23 changes: 14 additions & 9 deletions rocksdb/columnfamily/cfhandle.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import
../lib/librocksdb,
../internal/utils

const DEFAULT_COLUMN_FAMILY_NAME* = "default"

type
ColFamilyHandlePtr* = ptr rocksdb_column_family_handle_t

Expand All @@ -24,18 +22,25 @@ type
proc newColFamilyHandle*(cPtr: ColFamilyHandlePtr): ColFamilyHandleRef =
ColFamilyHandleRef(cPtr: cPtr)

template isClosed*(handle: ColFamilyHandleRef): bool =
handle.cPtr.isNil()

proc cPtr*(handle: ColFamilyHandleRef): ColFamilyHandlePtr =
doAssert not handle.isClosed()
handle.cPtr

proc getId*(handle: ColFamilyHandleRef): int =
doAssert not handle.isClosed()
rocksdb_column_family_handle_get_id(handle.cPtr).int
# TODO: These procs below will not work unless using the latest version of rocksdb
# Currently, when installing librocksdb-dev on linux the RocksDb version used is 6.11.4
# Need to complete this task: https://github.com/status-im/nim-rocksdb/issues/10

proc getName*(handle: ColFamilyHandleRef): string =
doAssert not handle.isClosed()
var nameLen: csize_t # do we need to use this?
$rocksdb_column_family_handle_get_name(handle.cPtr, nameLen.addr)
# proc getId*(handle: ColFamilyHandleRef): int =
# doAssert not handle.isClosed()
# rocksdb_column_family_handle_get_id(handle.cPtr).int

# proc getName*(handle: ColFamilyHandleRef): string =
# doAssert not handle.isClosed()
# var nameLen: csize_t
# $rocksdb_column_family_handle_get_name(handle.cPtr, nameLen.addr)

template isDefault*(handle: ColFamilyHandleRef): bool =
handle.getName() == DEFAULT_COLUMN_FAMILY_NAME
Expand Down
6 changes: 4 additions & 2 deletions rocksdb/columnfamily/cfopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
{.push raises: [].}

import
../lib/librocksdb,
../internal/utils
../lib/librocksdb

type
ColFamilyOptionsPtr* = ptr rocksdb_options_t
Expand All @@ -22,6 +21,9 @@ type
proc newColFamilyOptions*(): ColFamilyOptionsRef =
ColFamilyOptionsRef(cPtr: rocksdb_options_create())

template isClosed*(cfOpts: ColFamilyOptionsRef): bool =
cfOpts.cPtr.isNil()

proc cPtr*(cfOpts: ColFamilyOptionsRef): ColFamilyOptionsPtr =
doAssert not cfOpts.isClosed()
cfOpts.cPtr
Expand Down
2 changes: 1 addition & 1 deletion rocksdb/internal/cftable.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type
proc newColFamilyTableRef*(): ColFamilyTableRef =
ColFamilyTableRef(columnFamilies: newTable[string, ColFamilyHandleRef]())

template isClosed(table: ColFamilyTableRef): bool =
template isClosed*(table: ColFamilyTableRef): bool =
table.columnFamilies.isNil()

proc put*(
Expand Down
7 changes: 3 additions & 4 deletions rocksdb/internal/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
import
../lib/librocksdb

const DEFAULT_COLUMN_FAMILY_NAME* = "default"

template bailOnErrors*(errors: cstring): auto =
if not errors.isNil:
let res = err($(errors))
rocksdb_free(errors)
return res

template isClosed*(refObj: untyped): bool =
refObj.cPtr.isNil()
return res
6 changes: 4 additions & 2 deletions rocksdb/options/backupopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
{.push raises: [].}

import
../lib/librocksdb,
../internal/utils
../lib/librocksdb

type
BackupEngineOptionsPtr* = ptr rocksdb_options_t
Expand All @@ -22,6 +21,9 @@ type
proc newBackupEngineOptions*(): BackupEngineOptionsRef =
BackupEngineOptionsRef(cPtr: rocksdb_options_create())

template isClosed*(engineOpts: BackupEngineOptionsRef): bool =
engineOpts.cPtr.isNil()

proc cPtr*(engineOpts: BackupEngineOptionsRef): BackupEngineOptionsPtr =
doAssert not engineOpts.isClosed()
engineOpts.cPtr
Expand Down
6 changes: 4 additions & 2 deletions rocksdb/options/dbopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

import
std/cpuinfo,
../lib/librocksdb,
../internal/utils
../lib/librocksdb

type
DbOptionsPtr* = ptr rocksdb_options_t
Expand All @@ -23,6 +22,9 @@ type
proc newDbOptions*(): DbOptionsRef =
DbOptionsRef(cPtr: rocksdb_options_create())

template isClosed*(dbOpts: DbOptionsRef): bool =
dbOpts.cPtr.isNil()

proc cPtr*(dbOpts: DbOptionsRef): DbOptionsPtr =
doAssert not dbOpts.isClosed()
dbOpts.cPtr
Expand Down
6 changes: 4 additions & 2 deletions rocksdb/options/readopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
{.push raises: [].}

import
../lib/librocksdb,
../internal/utils
../lib/librocksdb

type
ReadOptionsPtr* = ptr rocksdb_readoptions_t
Expand All @@ -22,6 +21,9 @@ type
proc newReadOptions*(): ReadOptionsRef =
ReadOptionsRef(cPtr: rocksdb_readoptions_create())

template isClosed*(readOpts: ReadOptionsRef): bool =
readOpts.cPtr.isNil()

proc cPtr*(readOpts: ReadOptionsRef): ReadOptionsPtr =
doAssert not readOpts.isClosed()
readOpts.cPtr
Expand Down
6 changes: 4 additions & 2 deletions rocksdb/options/writeopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
{.push raises: [].}

import
../lib/librocksdb,
../internal/utils
../lib/librocksdb

type
WriteOptionsPtr* = ptr rocksdb_writeoptions_t
Expand All @@ -22,6 +21,9 @@ type
proc newWriteOptions*(): WriteOptionsRef =
WriteOptionsRef(cPtr: rocksdb_writeoptions_create())

template isClosed*(writeOpts: WriteOptionsRef): bool =
writeOpts.cPtr.isNil()

proc cPtr*(writeOpts: WriteOptionsRef): WriteOptionsPtr =
doAssert not writeOpts.isClosed()
writeOpts.cPtr
Expand Down
4 changes: 2 additions & 2 deletions rocksdb/rocksdb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ proc openRocksDb*(

let rocksDbPtr = rocksdb_open_column_families(
dbOpts.cPtr,
path,
path.cstring,
cfNames.len().cint,
cast[cstringArray](cfNames[0].addr),
cfOpts[0].addr,
Expand Down Expand Up @@ -100,7 +100,7 @@ proc openRocksDbReadOnly*(

let rocksDbPtr = rocksdb_open_for_read_only_column_families(
dbOpts.cPtr,
path,
path.cstring,
cfNames.len().cint,
cast[cstringArray](cfNames[0].addr),
cfOpts[0].addr,
Expand Down
59 changes: 59 additions & 0 deletions tests/columnfamily/test_cfdescriptor.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Nim-RocksDB
# Copyright 2024 Status Research & Development GmbH
# Licensed under either of
#
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
# * GPL license, version 2.0, ([LICENSE-GPLv2](LICENSE-GPLv2) or https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html)
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.

{.used.}

import
unittest2,
../../rocksdb/internal/utils,
../../rocksdb/columnfamily/cfdescriptor

suite "ColFamilyDescriptor Tests":

const TEST_CF_NAME = "test"

test "Test initColFamilyDescriptor":
var descriptor = initColFamilyDescriptor(TEST_CF_NAME)

check:
descriptor.name() == TEST_CF_NAME
not descriptor.options().isNil()
not descriptor.isDefault()

descriptor.close()

test "Test initColFamilyDescriptor with options":
var descriptor = initColFamilyDescriptor(TEST_CF_NAME, defaultColFamilyOptions())

check:
descriptor.name() == TEST_CF_NAME
not descriptor.options().isNil()
not descriptor.isDefault()

descriptor.close()

test "Test defaultColFamilyDescriptor":
var descriptor = defaultColFamilyDescriptor()

check:
descriptor.name() == DEFAULT_COLUMN_FAMILY_NAME
not descriptor.options().isNil()
descriptor.isDefault()

descriptor.close()

test "Test close":
var descriptor = defaultColFamilyDescriptor()

check not descriptor.isClosed()
descriptor.close()
check descriptor.isClosed()
descriptor.close()
check descriptor.isClosed()

67 changes: 67 additions & 0 deletions tests/columnfamily/test_cfhandle.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Nim-RocksDB
# Copyright 2024 Status Research & Development GmbH
# Licensed under either of
#
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
# * GPL license, version 2.0, ([LICENSE-GPLv2](LICENSE-GPLv2) or https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html)
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.

{.used.}

import
std/os,
tempfile,
unittest2,
../../rocksdb/lib/librocksdb,
../../rocksdb/columnfamily/cfhandle

suite "ColFamilyHandleRef Tests":

const TEST_CF_NAME = "test"

setup:
let
dbPath = mkdtemp() / "data"
dbOpts = rocksdb_options_create()
cfOpts = rocksdb_options_create()

var
errors: cstring

rocksdb_options_set_create_if_missing(dbOpts, 1);

let db = rocksdb_open(dbOpts, dbPath.cstring, cast[cstringArray](errors.addr))
doAssert errors.isNil()
doAssert not db.isNil()

let cfHandlePtr = rocksdb_create_column_family(
db,
cfOpts,
TEST_CF_NAME.cstring,
cast[cstringArray](errors.addr))
doAssert errors.isNil()
doAssert not cfHandlePtr.isNil()

teardown:
rocksdb_close(db)
removeDir($dbPath)

test "Test newColFamilyHandle":
var cfHandle = newColFamilyHandle(cfHandlePtr)

check:
not cfHandle.cPtr.isNil()
cfHandle.cPtr == cfHandlePtr

cfHandle.close()

test "Test close":
var cfHandle = newColFamilyHandle(cfHandlePtr)

check not cfHandle.isClosed()
cfHandle.close()
check cfHandle.isClosed()
cfHandle.close()
check cfHandle.isClosed()

Loading

0 comments on commit 6ad22e6

Please sign in to comment.