Skip to content

Commit

Permalink
Completed tests for existing features.
Browse files Browse the repository at this point in the history
  • Loading branch information
bhartnett committed Feb 20, 2024
1 parent 6ad22e6 commit 2ffcffc
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 50 deletions.
36 changes: 34 additions & 2 deletions rocksdb/backup.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import
./options/backupopts,
./rocksdb

export
results,
backupopts,
rocksdb

type
BackupEnginePtr* = ptr rocksdb_backup_engine_t

Expand Down Expand Up @@ -43,16 +48,43 @@ proc openBackupEngine*(
template isClosed*(backupEngine: BackupEngineRef): bool =
backupEngine.cPtr.isNil()

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

var errors: cstring
rocksdb_backup_engine_create_new_backup(
rocksdb_backup_engine_create_new_backup_flush(
backupEngine.cPtr,
db.cPtr,
flushBeforeBackup.uint8,
cast[cstringArray](errors.addr))
bailOnErrors(errors)

ok()

proc restoreDbFromLatestBackup*(
backupEngine: BackupEngineRef,
dbDir: string,
walDir = dbDir,
keepLogFiles = false): RocksDBResult[void] =
doAssert not backupEngine.isClosed()

let restoreOptions = rocksdb_restore_options_create()
rocksdb_restore_options_set_keep_log_files(restoreOptions, keepLogFiles.cint)

var errors: cstring
rocksdb_backup_engine_restore_db_from_latest_backup(
backupEngine.cPtr,
dbDir.cstring,
walDir.cstring,
restoreOptions,
cast[cstringArray](errors.addr))
bailOnErrors(errors)

rocksdb_restore_options_destroy(restoreOptions)

ok()

proc close*(backupEngine: var BackupEngineRef) =
Expand Down
7 changes: 3 additions & 4 deletions rocksdb/columnfamily/cfhandle.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
ColFamilyHandlePtr* = ptr rocksdb_column_family_handle_t
Expand Down Expand Up @@ -42,8 +41,8 @@ proc cPtr*(handle: ColFamilyHandleRef): ColFamilyHandlePtr =
# 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
# template isDefault*(handle: ColFamilyHandleRef): bool =
# handle.getName() == DEFAULT_COLUMN_FAMILY_NAME

proc close*(handle: var ColFamilyHandleRef) =
if not handle.isClosed():
Expand Down
3 changes: 2 additions & 1 deletion rocksdb/internal/cftable.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type
ColFamilyTableRef* = ref object
columnFamilies: TableRef[string, ColFamilyHandleRef]

proc newColFamilyTableRef*(): ColFamilyTableRef =
proc newColFamilyTable*(): ColFamilyTableRef =
ColFamilyTableRef(columnFamilies: newTable[string, ColFamilyHandleRef]())

template isClosed*(table: ColFamilyTableRef): bool =
Expand All @@ -28,6 +28,7 @@ proc put*(
name: string,
handle: ColFamilyHandlePtr) =
doAssert not table.isClosed()
doAssert not handle.isNil()
table.columnFamilies[name] = newColFamilyHandle(handle)

proc get*(table: ColFamilyTableRef, name: string): ColFamilyHandleRef =
Expand Down
4 changes: 2 additions & 2 deletions rocksdb/rocksdb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ proc openRocksDb*(
cast[cstringArray](errors.addr))
bailOnErrors(errors)

var cfTable = newColFamilyTableRef()
var cfTable = newColFamilyTable()
for i, cf in columnFamilies:
cfTable.put(cf.name(), columnFamilyHandles[i])

Expand Down Expand Up @@ -109,7 +109,7 @@ proc openRocksDbReadOnly*(
cast[cstringArray](errors.addr))
bailOnErrors(errors)

var cfTable = newColFamilyTableRef()
var cfTable = newColFamilyTable()
for i, cf in columnFamilies:
cfTable.put(cf.name(), columnFamilyHandles[i])

Expand Down
76 changes: 66 additions & 10 deletions tests/internal/test_cftable.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,74 @@

{.used.}

# import
# std/[cpuinfo, os],
# tempfile,
# unittest2,
# ../../rocksdb/lib/librocksdb
import
std/os,
tempfile,
unittest2,
../../rocksdb/lib/librocksdb,
../../rocksdb/columnfamily/cfhandle,
../../rocksdb/internal/cftable

suite "Test suite name":
# setup:
suite "ColFamilyTableRef Tests":

const TEST_CF_NAME = "test"

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

var
errors: cstring

test "Test name":
discard
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 newColFamilyTable":
var cfTable = newColFamilyTable()

cfTable.put(TEST_CF_NAME, cfHandlePtr)
check cfTable.get(TEST_CF_NAME).cPtr() == cfHandlePtr
check not cfTable.isClosed()

# second put, same result
cfTable.put(TEST_CF_NAME, cfHandlePtr)
check cfTable.get(TEST_CF_NAME).cPtr() == cfHandlePtr
check not cfTable.isClosed()

# doesn't exist
check cfTable.get("other").isNil()
check not cfTable.isClosed()

cfTable.close()

test "Test close":
var cfTable = newColFamilyTable()
cfTable.put(TEST_CF_NAME, cfHandlePtr)

let cfHandle = cfTable.get(TEST_CF_NAME)

check not cfHandle.isClosed()
check not cfTable.isClosed()
cfTable.close()
check cfHandle.isClosed()
check cfTable.isClosed()
cfTable.close()
check cfTable.isClosed()
2 changes: 1 addition & 1 deletion tests/lib/test_librocksdb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import
unittest2,
../../rocksdb/lib/librocksdb

suite "RocksDB C wrapper tests":
suite "librocksdb C wrapper Tests":
setup:
let
dbPath = mkdtemp().cstring
Expand Down
5 changes: 3 additions & 2 deletions tests/test_all.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import
./columnfamily/test_cfdescriptor,
./columnfamily/test_cfhandle,
./columnfamily/test_cfopts,
#./internal/test_cftable,
./internal/test_cftable,
./lib/test_librocksdb,
./options/test_backupopts,
./options/test_dbopts,
./options/test_readopts,
./options/test_writeopts,
./lib/test_librocksdb,
./test_backup,
./test_rocksdb
70 changes: 70 additions & 0 deletions tests/test_backup.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# 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/backup,
./test_helper

suite "BackupEngineRef Tests":

let
key = @[byte(1), 2, 3, 4, 5]
val = @[byte(1), 2, 3, 4, 5]

setup:
let
dbPath = mkdtemp() / "data"
dbBackupPath = mkdtemp() / "backup"
dbRestorePath = mkdtemp() / "restore"

var
db = initReadWriteDb(dbPath)

teardown:

db.close()
removeDir($dbPath)
removeDir($dbBackupPath)


test "Test backup":
var engine = initBackupEngine(dbBackupPath)

check:
db.put(key, val).isOk()
db.keyExists(key).value()

check engine.createNewBackup(db, true).isOk()

check:
db.delete(key).isOk()
not db.keyExists(key).value()

check engine.restoreDbFromLatestBackup(dbRestorePath).isOk()

let db2 = initReadWriteDb(dbRestorePath)
check db2.keyExists(key).value()

engine.close()

test "Test close":
let res = openBackupEngine(dbPath)
doAssert res.isOk()
var engine = res.get()

check not engine.isClosed()
engine.close()
check engine.isClosed()
engine.close()
check engine.isClosed()
46 changes: 46 additions & 0 deletions tests/test_helper.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 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/sequtils,
../rocksdb/backup,
../rocksdb/rocksdb


proc initReadWriteDb*(
path: string,
columnFamilyNames = @["default"]): RocksDbReadWriteRef =

let res = openRocksDb(
path,
columnFamilies = columnFamilyNames.mapIt(initColFamilyDescriptor(it)))
if res.isErr():
echo res.error()
doAssert res.isOk()
res.value()

proc initReadOnlyDb*(
path: string,
columnFamilyNames = @["default"]): RocksDbReadOnlyRef =

let res = openRocksDbReadOnly(
path,
columnFamilies = columnFamilyNames.mapIt(initColFamilyDescriptor(it)))
if res.isErr():
echo res.error()
doAssert res.isOk()
res.value()

proc initBackupEngine*(path: string): BackupEngineRef =

let res = openBackupEngine(path)
doAssert res.isOk()
res.value()
32 changes: 4 additions & 28 deletions tests/test_rocksdb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,13 @@
{.used.}

import
std/[os, sequtils],
std/os,
tempfile,
unittest2,
../rocksdb
../rocksdb/rocksdb,
./test_helper

proc initReadWriteDb(
path: string,
columnFamilyNames = @["default"]): RocksDbReadWriteRef =

let dataDir = path / "data"
createDir(dataDir)

var s = openRocksDb(dataDir,
columnFamilies = columnFamilyNames.mapIt(initColFamilyDescriptor(it)))
doAssert s.isOk()
s.value()

proc initReadOnlyDb(
path: string,
columnFamilyNames = @["default"]): RocksDbReadOnlyRef =

let dataDir = path / "data"
createDir(dataDir)

var s = openRocksDbReadOnly(
dataDir,
columnFamilies = columnFamilyNames.mapIt(initColFamilyDescriptor(it)))
doAssert s.isOk()
s.value()

suite "Nim API tests":
suite "RocksDbRef Tests":
const
CF_DEFAULT = "default"
CF_OTHER = "other"
Expand Down

0 comments on commit 2ffcffc

Please sign in to comment.