2019-01-10 23:39:17 +00:00
|
|
|
# Nim-RocksDB
|
2024-03-05 03:12:37 +00:00
|
|
|
# Copyright 2018-2024 Status Research & Development GmbH
|
2019-01-10 23:39:17 +00:00
|
|
|
# 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.
|
|
|
|
|
2020-04-17 22:18:04 +00:00
|
|
|
{.used.}
|
|
|
|
|
2024-06-26 15:00:10 +00:00
|
|
|
import std/os, tempfile, unittest2, ../rocksdb/rocksdb, ./test_helper
|
2018-06-24 23:13:10 +00:00
|
|
|
|
2024-03-05 03:12:37 +00:00
|
|
|
suite "RocksDbRef Tests":
|
|
|
|
const
|
|
|
|
CF_DEFAULT = "default"
|
|
|
|
CF_OTHER = "other"
|
2018-06-24 23:13:10 +00:00
|
|
|
|
|
|
|
let
|
2024-03-05 03:12:37 +00:00
|
|
|
key = @[byte(1), 2, 3, 4, 5]
|
|
|
|
otherKey = @[byte(1), 2, 3, 4, 5, 6]
|
|
|
|
val = @[byte(1), 2, 3, 4, 5]
|
2018-06-24 23:13:10 +00:00
|
|
|
|
2019-01-10 23:39:17 +00:00
|
|
|
setup:
|
2024-03-05 03:12:37 +00:00
|
|
|
let
|
|
|
|
dbPath = mkdtemp() / "data"
|
|
|
|
db = initReadWriteDb(dbPath, columnFamilyNames = @[CF_DEFAULT, CF_OTHER])
|
2024-06-26 17:31:39 +00:00
|
|
|
defaultCfHandle = db.getColFamilyHandle(CF_DEFAULT).get()
|
|
|
|
otherCfHandle = db.getColFamilyHandle(CF_OTHER).get()
|
2018-06-24 23:13:10 +00:00
|
|
|
|
2019-01-10 23:39:17 +00:00
|
|
|
teardown:
|
2024-03-05 03:12:37 +00:00
|
|
|
db.close()
|
|
|
|
removeDir($dbPath)
|
2018-06-24 23:13:10 +00:00
|
|
|
|
2019-01-10 23:39:17 +00:00
|
|
|
test "Basic operations":
|
2024-03-05 03:12:37 +00:00
|
|
|
var s = db.put(key, val)
|
|
|
|
check s.isOk()
|
|
|
|
|
|
|
|
var bytes: seq[byte]
|
2024-06-26 15:00:10 +00:00
|
|
|
check db.get(
|
|
|
|
key,
|
|
|
|
proc(data: openArray[byte]) =
|
|
|
|
bytes = @data
|
|
|
|
,
|
|
|
|
)[]
|
|
|
|
check not db.get(
|
|
|
|
otherKey,
|
|
|
|
proc(data: openArray[byte]) =
|
|
|
|
bytes = @data
|
|
|
|
,
|
|
|
|
)[]
|
2024-03-05 03:12:37 +00:00
|
|
|
|
|
|
|
var r1 = db.get(key)
|
|
|
|
check r1.isOk() and r1.value == val
|
|
|
|
|
|
|
|
var r2 = db.get(otherKey)
|
|
|
|
# there's no error string for missing keys
|
|
|
|
check r2.isOk() == false and r2.error.len == 0
|
|
|
|
|
|
|
|
var e1 = db.keyExists(key)
|
|
|
|
check e1.isOk() and e1.value == true
|
|
|
|
|
|
|
|
var e2 = db.keyExists(otherKey)
|
|
|
|
check e2.isOk() and e2.value == false
|
|
|
|
|
|
|
|
var d = db.delete(key)
|
|
|
|
check d.isOk()
|
|
|
|
|
|
|
|
e1 = db.keyExists(key)
|
|
|
|
check e1.isOk() and e1.value == false
|
|
|
|
|
|
|
|
d = db.delete(otherKey)
|
|
|
|
check d.isOk()
|
|
|
|
|
|
|
|
close(db)
|
|
|
|
check db.isClosed()
|
|
|
|
|
|
|
|
# Open database in read only mode
|
|
|
|
block:
|
|
|
|
var
|
|
|
|
readOnlyDb = initReadOnlyDb(dbPath)
|
|
|
|
r = readOnlyDb.keyExists(key)
|
|
|
|
check r.isOk() and r.value == false
|
|
|
|
|
|
|
|
# This won't compile as designed:
|
|
|
|
# var r2 = readOnlyDb.put(key, @[123.byte])
|
|
|
|
# check r2.isErr()
|
|
|
|
|
|
|
|
readOnlyDb.close()
|
|
|
|
check readOnlyDb.isClosed()
|
|
|
|
|
|
|
|
test "Basic operations - default column family":
|
2024-06-26 17:31:39 +00:00
|
|
|
var s = db.put(key, val, defaultCfHandle)
|
2024-03-05 03:12:37 +00:00
|
|
|
check s.isOk()
|
|
|
|
|
|
|
|
var bytes: seq[byte]
|
2024-06-26 15:00:10 +00:00
|
|
|
check db.get(
|
|
|
|
key,
|
|
|
|
proc(data: openArray[byte]) =
|
|
|
|
bytes = @data
|
|
|
|
,
|
2024-06-26 17:31:39 +00:00
|
|
|
defaultCfHandle,
|
2024-06-26 15:00:10 +00:00
|
|
|
)[]
|
|
|
|
check not db.get(
|
|
|
|
otherKey,
|
|
|
|
proc(data: openArray[byte]) =
|
|
|
|
bytes = @data
|
|
|
|
,
|
2024-06-26 17:31:39 +00:00
|
|
|
defaultCfHandle,
|
2024-06-26 15:00:10 +00:00
|
|
|
)[]
|
2024-03-05 03:12:37 +00:00
|
|
|
|
|
|
|
var r1 = db.get(key)
|
|
|
|
check r1.isOk() and r1.value == val
|
|
|
|
|
|
|
|
var r2 = db.get(otherKey)
|
|
|
|
# there's no error string for missing keys
|
|
|
|
check r2.isOk() == false and r2.error.len == 0
|
|
|
|
|
2024-06-26 17:31:39 +00:00
|
|
|
var e1 = db.keyExists(key, defaultCfHandle)
|
2024-03-05 03:12:37 +00:00
|
|
|
check e1.isOk() and e1.value == true
|
|
|
|
|
2024-06-26 17:31:39 +00:00
|
|
|
var e2 = db.keyExists(otherKey, defaultCfHandle)
|
2024-03-05 03:12:37 +00:00
|
|
|
check e2.isOk() and e2.value == false
|
|
|
|
|
2024-06-26 17:31:39 +00:00
|
|
|
var d = db.delete(key, defaultCfHandle)
|
2024-03-05 03:12:37 +00:00
|
|
|
check d.isOk()
|
|
|
|
|
2024-06-26 17:31:39 +00:00
|
|
|
e1 = db.keyExists(key, defaultCfHandle)
|
2024-03-05 03:12:37 +00:00
|
|
|
check e1.isOk() and e1.value == false
|
|
|
|
|
2024-06-26 17:31:39 +00:00
|
|
|
d = db.delete(otherKey, defaultCfHandle)
|
2024-03-05 03:12:37 +00:00
|
|
|
check d.isOk()
|
|
|
|
|
2024-06-26 17:31:39 +00:00
|
|
|
db.close()
|
2024-03-05 03:12:37 +00:00
|
|
|
check db.isClosed()
|
|
|
|
|
|
|
|
# Open database in read only mode
|
|
|
|
block:
|
|
|
|
var
|
|
|
|
readOnlyDb = initReadOnlyDb(dbPath, columnFamilyNames = @[CF_DEFAULT])
|
2024-06-26 17:31:39 +00:00
|
|
|
r = readOnlyDb.keyExists(key)
|
2024-03-05 03:12:37 +00:00
|
|
|
check r.isOk() and r.value == false
|
|
|
|
|
|
|
|
# Won't compile as designed:
|
2024-06-26 17:31:39 +00:00
|
|
|
# var r2 = readOnlyDb.put(key, @[123.byte], defaultCfHandle)
|
2024-03-05 03:12:37 +00:00
|
|
|
# check r2.isErr()
|
|
|
|
|
|
|
|
readOnlyDb.close()
|
|
|
|
check readOnlyDb.isClosed()
|
|
|
|
|
|
|
|
test "Basic operations - multiple column families":
|
2024-06-26 17:31:39 +00:00
|
|
|
var s = db.put(key, val, defaultCfHandle)
|
2024-03-05 03:12:37 +00:00
|
|
|
check s.isOk()
|
|
|
|
|
2024-06-26 17:31:39 +00:00
|
|
|
var s2 = db.put(otherKey, val, otherCfHandle)
|
2024-03-05 03:12:37 +00:00
|
|
|
check s2.isOk()
|
|
|
|
|
|
|
|
var bytes: seq[byte]
|
2024-06-26 15:00:10 +00:00
|
|
|
check db.get(
|
|
|
|
key,
|
|
|
|
proc(data: openArray[byte]) =
|
|
|
|
bytes = @data
|
|
|
|
,
|
2024-06-26 17:31:39 +00:00
|
|
|
defaultCfHandle,
|
2024-06-26 15:00:10 +00:00
|
|
|
)[]
|
|
|
|
check not db.get(
|
|
|
|
otherKey,
|
|
|
|
proc(data: openArray[byte]) =
|
|
|
|
bytes = @data
|
|
|
|
,
|
2024-06-26 17:31:39 +00:00
|
|
|
defaultCfHandle,
|
2024-06-26 15:00:10 +00:00
|
|
|
)[]
|
2024-03-05 03:12:37 +00:00
|
|
|
|
|
|
|
var bytes2: seq[byte]
|
2024-06-26 15:00:10 +00:00
|
|
|
check db.get(
|
|
|
|
otherKey,
|
|
|
|
proc(data: openArray[byte]) =
|
|
|
|
bytes2 = @data
|
|
|
|
,
|
2024-06-26 17:31:39 +00:00
|
|
|
otherCfHandle,
|
2024-06-26 15:00:10 +00:00
|
|
|
)[]
|
|
|
|
check not db.get(
|
|
|
|
key,
|
|
|
|
proc(data: openArray[byte]) =
|
|
|
|
bytes2 = @data
|
|
|
|
,
|
2024-06-26 17:31:39 +00:00
|
|
|
otherCfHandle,
|
2024-06-26 15:00:10 +00:00
|
|
|
)[]
|
2024-03-05 03:12:37 +00:00
|
|
|
|
2024-06-26 17:31:39 +00:00
|
|
|
var e1 = db.keyExists(key, defaultCfHandle)
|
2024-03-05 03:12:37 +00:00
|
|
|
check e1.isOk() and e1.value == true
|
2024-06-26 17:31:39 +00:00
|
|
|
var e2 = db.keyExists(otherKey, defaultCfHandle)
|
2024-03-05 03:12:37 +00:00
|
|
|
check e2.isOk() and e2.value == false
|
|
|
|
|
2024-06-26 17:31:39 +00:00
|
|
|
var e3 = db.keyExists(key, otherCfHandle)
|
2024-03-05 03:12:37 +00:00
|
|
|
check e3.isOk() and e3.value == false
|
2024-06-26 17:31:39 +00:00
|
|
|
var e4 = db.keyExists(otherKey, otherCfHandle)
|
2024-03-05 03:12:37 +00:00
|
|
|
check e4.isOk() and e4.value == true
|
|
|
|
|
2024-06-26 17:31:39 +00:00
|
|
|
var d = db.delete(key, defaultCfHandle)
|
2024-03-05 03:12:37 +00:00
|
|
|
check d.isOk()
|
2024-06-26 17:31:39 +00:00
|
|
|
e1 = db.keyExists(key, defaultCfHandle)
|
2024-03-05 03:12:37 +00:00
|
|
|
check e1.isOk() and e1.value == false
|
2024-06-26 17:31:39 +00:00
|
|
|
d = db.delete(otherKey, defaultCfHandle)
|
2024-03-05 03:12:37 +00:00
|
|
|
check d.isOk()
|
|
|
|
|
2024-06-26 17:31:39 +00:00
|
|
|
var d2 = db.delete(key, otherCfHandle)
|
2024-03-05 03:12:37 +00:00
|
|
|
check d2.isOk()
|
2024-06-26 17:31:39 +00:00
|
|
|
e3 = db.keyExists(key, otherCfHandle)
|
2024-03-05 03:12:37 +00:00
|
|
|
check e3.isOk() and e3.value == false
|
2024-06-26 17:31:39 +00:00
|
|
|
d2 = db.delete(otherKey, otherCfHandle)
|
2024-03-05 03:12:37 +00:00
|
|
|
check d2.isOk()
|
2024-06-26 17:31:39 +00:00
|
|
|
d2 = db.delete(otherKey, otherCfHandle)
|
2024-03-05 03:12:37 +00:00
|
|
|
check d2.isOk()
|
|
|
|
|
|
|
|
db.close()
|
|
|
|
check db.isClosed()
|
|
|
|
|
|
|
|
# Open database in read only mode
|
|
|
|
block:
|
2024-06-26 15:00:10 +00:00
|
|
|
var readOnlyDb =
|
|
|
|
initReadOnlyDb(dbPath, columnFamilyNames = @[CF_DEFAULT, CF_OTHER])
|
2024-03-05 03:12:37 +00:00
|
|
|
|
2024-06-26 17:31:39 +00:00
|
|
|
var r = readOnlyDb.keyExists(key, readOnlyDb.getColFamilyHandle(CF_OTHER).get())
|
2024-03-05 03:12:37 +00:00
|
|
|
check r.isOk() and r.value == false
|
|
|
|
|
|
|
|
# Does not compile as designed:
|
2024-06-26 17:31:39 +00:00
|
|
|
# var r2 = readOnlyDb.put(key, @[123.byte], otherCfHandle)
|
2024-03-05 03:12:37 +00:00
|
|
|
# check r2.isErr()
|
|
|
|
|
|
|
|
readOnlyDb.close()
|
|
|
|
check readOnlyDb.isClosed()
|
|
|
|
|
|
|
|
test "Test missing key and values":
|
|
|
|
let
|
|
|
|
key1 = @[byte(1)] # exists with non empty value
|
|
|
|
val1 = @[byte(1)]
|
|
|
|
key2 = @[byte(2)] # exists with empty seq value
|
|
|
|
val2: seq[byte] = @[]
|
|
|
|
key3 = @[byte(3)] # exists with empty array value
|
|
|
|
val3: array[0, byte] = []
|
|
|
|
key4 = @[byte(4)] # deleted key
|
|
|
|
key5 = @[byte(5)] # key not created
|
2018-06-24 23:13:10 +00:00
|
|
|
|
2024-03-05 03:12:37 +00:00
|
|
|
check:
|
|
|
|
db.put(key1, val1).isOk()
|
|
|
|
db.put(key2, val2).isOk()
|
|
|
|
db.put(key3, val3).isOk()
|
|
|
|
db.delete(key4).isOk()
|
2018-06-24 23:13:10 +00:00
|
|
|
|
2024-03-05 03:12:37 +00:00
|
|
|
db.keyExists(key1).get() == true
|
|
|
|
db.keyExists(key2).get() == true
|
|
|
|
db.keyExists(key3).get() == true
|
|
|
|
db.keyExists(key4).get() == false
|
|
|
|
db.keyExists(key5).get() == false
|
2018-06-24 23:13:10 +00:00
|
|
|
|
2024-03-05 03:12:37 +00:00
|
|
|
block:
|
|
|
|
var v: seq[byte]
|
2024-06-26 15:00:10 +00:00
|
|
|
let r = db.get(
|
|
|
|
key1,
|
|
|
|
proc(data: openArray[byte]) =
|
|
|
|
v = @data
|
|
|
|
,
|
|
|
|
)
|
2024-03-05 03:12:37 +00:00
|
|
|
check:
|
|
|
|
r.isOk()
|
|
|
|
r.value() == true
|
|
|
|
v == val1
|
|
|
|
db.get(key1).isOk()
|
2018-06-24 23:13:10 +00:00
|
|
|
|
2024-03-05 03:12:37 +00:00
|
|
|
block:
|
|
|
|
var v: seq[byte]
|
2024-06-26 15:00:10 +00:00
|
|
|
let r = db.get(
|
|
|
|
key2,
|
|
|
|
proc(data: openArray[byte]) =
|
|
|
|
v = @data
|
|
|
|
,
|
|
|
|
)
|
2024-03-05 03:12:37 +00:00
|
|
|
check:
|
|
|
|
r.isOk()
|
|
|
|
r.value() == true
|
|
|
|
v.len() == 0
|
|
|
|
db.get(key2).isOk()
|
2018-06-24 23:13:10 +00:00
|
|
|
|
2024-03-05 03:12:37 +00:00
|
|
|
block:
|
|
|
|
var v: seq[byte]
|
2024-06-26 15:00:10 +00:00
|
|
|
let r = db.get(
|
|
|
|
key3,
|
|
|
|
proc(data: openArray[byte]) =
|
|
|
|
v = @data
|
|
|
|
,
|
|
|
|
)
|
2024-03-05 03:12:37 +00:00
|
|
|
check:
|
|
|
|
r.isOk()
|
|
|
|
r.value() == true
|
|
|
|
v.len() == 0
|
|
|
|
db.get(key3).isOk()
|
2018-06-24 23:13:10 +00:00
|
|
|
|
2024-03-05 03:12:37 +00:00
|
|
|
block:
|
|
|
|
var v: seq[byte]
|
2024-06-26 15:00:10 +00:00
|
|
|
let r = db.get(
|
|
|
|
key4,
|
|
|
|
proc(data: openArray[byte]) =
|
|
|
|
v = @data
|
|
|
|
,
|
|
|
|
)
|
2024-03-05 03:12:37 +00:00
|
|
|
check:
|
|
|
|
r.isOk()
|
|
|
|
r.value() == false
|
|
|
|
v.len() == 0
|
|
|
|
db.get(key4).isErr()
|
2020-04-23 18:55:21 +00:00
|
|
|
|
2024-03-05 03:12:37 +00:00
|
|
|
block:
|
|
|
|
var v: seq[byte]
|
2024-06-26 15:00:10 +00:00
|
|
|
let r = db.get(
|
|
|
|
key5,
|
|
|
|
proc(data: openArray[byte]) =
|
|
|
|
v = @data
|
|
|
|
,
|
|
|
|
)
|
2024-03-05 03:12:37 +00:00
|
|
|
check:
|
|
|
|
r.isOk()
|
|
|
|
r.value() == false
|
|
|
|
v.len() == 0
|
|
|
|
db.get(key5).isErr()
|
2024-06-28 02:04:37 +00:00
|
|
|
|
|
|
|
test "List column familes":
|
|
|
|
let cfRes1 = listColumnFamilies(dbPath)
|
|
|
|
check:
|
|
|
|
cfRes1.isOk()
|
|
|
|
cfRes1.value() == @[CF_DEFAULT, CF_OTHER]
|
|
|
|
|
|
|
|
let
|
|
|
|
dbPath2 = dbPath & "2"
|
|
|
|
db2 = initReadWriteDb(dbPath2, columnFamilyNames = @[CF_DEFAULT])
|
|
|
|
cfRes2 = listColumnFamilies(dbPath2)
|
|
|
|
check:
|
|
|
|
cfRes2.isOk()
|
|
|
|
cfRes2.value() == @[CF_DEFAULT]
|
|
|
|
|
|
|
|
test "Unknown column family":
|
|
|
|
const CF_UNKNOWN = "unknown"
|
|
|
|
let cfHandleRes = db.getColFamilyHandle(CF_UNKNOWN)
|
|
|
|
check cfHandleRes.isErr() and cfHandleRes.error() == "rocksdb: unknown column family"
|
|
|
|
|
|
|
|
test "Close multiple times":
|
|
|
|
check not db.isClosed()
|
|
|
|
db.close()
|
|
|
|
check db.isClosed()
|
|
|
|
db.close()
|
|
|
|
check db.isClosed()
|
|
|
|
|
2024-06-28 15:57:34 +00:00
|
|
|
test "Test auto close enabled":
|
2024-06-28 02:04:37 +00:00
|
|
|
let
|
2024-06-28 15:57:34 +00:00
|
|
|
dbPath = mkdtemp() / "autoclose-enabled"
|
|
|
|
dbOpts = defaultDbOptions(autoClose = true)
|
2024-06-28 02:04:37 +00:00
|
|
|
readOpts = defaultReadOptions(autoClose = true)
|
2024-06-28 15:57:34 +00:00
|
|
|
writeOpts = defaultWriteOptions(autoClose = true)
|
|
|
|
columnFamilies =
|
|
|
|
@[
|
|
|
|
initColFamilyDescriptor(CF_DEFAULT, defaultColFamilyOptions(autoClose = true))
|
|
|
|
]
|
|
|
|
db = openRocksDb(dbPath, dbOpts, readOpts, writeOpts, columnFamilies).get()
|
2024-06-28 02:04:37 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
dbOpts.isClosed() == false
|
|
|
|
readOpts.isClosed() == false
|
2024-06-28 15:57:34 +00:00
|
|
|
writeOpts.isClosed() == false
|
|
|
|
columnFamilies[0].isClosed() == false
|
2024-06-28 02:04:37 +00:00
|
|
|
db.isClosed() == false
|
|
|
|
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
check:
|
2024-06-28 15:57:34 +00:00
|
|
|
dbOpts.isClosed() == true
|
2024-06-28 02:04:37 +00:00
|
|
|
readOpts.isClosed() == true
|
2024-06-28 15:57:34 +00:00
|
|
|
writeOpts.isClosed() == true
|
|
|
|
columnFamilies[0].isClosed() == true
|
|
|
|
db.isClosed() == true
|
|
|
|
|
|
|
|
test "Test auto close disabled":
|
|
|
|
let
|
|
|
|
dbPath = mkdtemp() / "autoclose-enabled"
|
|
|
|
dbOpts = defaultDbOptions(autoClose = false)
|
|
|
|
readOpts = defaultReadOptions(autoClose = false)
|
|
|
|
writeOpts = defaultWriteOptions(autoClose = false)
|
|
|
|
columnFamilies =
|
|
|
|
@[
|
|
|
|
initColFamilyDescriptor(
|
|
|
|
CF_DEFAULT, defaultColFamilyOptions(autoClose = false)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
db = openRocksDb(dbPath, dbOpts, readOpts, writeOpts, columnFamilies).get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
dbOpts.isClosed() == false
|
|
|
|
readOpts.isClosed() == false
|
|
|
|
writeOpts.isClosed() == false
|
|
|
|
columnFamilies[0].isClosed() == false
|
|
|
|
db.isClosed() == false
|
|
|
|
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
check:
|
|
|
|
dbOpts.isClosed() == false
|
|
|
|
readOpts.isClosed() == false
|
|
|
|
writeOpts.isClosed() == false
|
|
|
|
columnFamilies[0].isClosed() == false
|
2024-06-28 02:04:37 +00:00
|
|
|
db.isClosed() == true
|