2024-03-05 03:12:37 +00:00
|
|
|
# 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.}
|
|
|
|
|
2024-06-26 15:00:10 +00:00
|
|
|
import std/os, tempfile, unittest2, ../rocksdb/[transactiondb], ./test_helper
|
2024-03-05 03:12:37 +00:00
|
|
|
|
|
|
|
suite "TransactionDbRef Tests":
|
|
|
|
const
|
|
|
|
CF_DEFAULT = "default"
|
|
|
|
CF_OTHER = "other"
|
|
|
|
|
|
|
|
let
|
|
|
|
key1 = @[byte(1)]
|
|
|
|
val1 = @[byte(1)]
|
|
|
|
key2 = @[byte(2)]
|
|
|
|
val2 = @[byte(2)]
|
|
|
|
key3 = @[byte(3)]
|
|
|
|
val3 = @[byte(3)]
|
|
|
|
|
|
|
|
setup:
|
2024-06-26 17:31:39 +00:00
|
|
|
let
|
|
|
|
dbPath = mkdtemp() / "data"
|
|
|
|
db = initTransactionDb(dbPath, columnFamilyNames = @[CF_OTHER])
|
|
|
|
defaultCfHandle = db.getColFamilyHandle(CF_DEFAULT).get()
|
|
|
|
otherCfHandle = db.getColFamilyHandle(CF_OTHER).get()
|
2024-03-05 03:12:37 +00:00
|
|
|
|
|
|
|
teardown:
|
|
|
|
db.close()
|
|
|
|
removeDir($dbPath)
|
|
|
|
|
2024-06-26 15:00:10 +00:00
|
|
|
# test multiple transactions
|
2024-03-05 03:12:37 +00:00
|
|
|
test "Test rollback using default column family":
|
|
|
|
var tx = db.beginTransaction()
|
2024-06-26 15:00:10 +00:00
|
|
|
defer:
|
|
|
|
tx.close()
|
2024-03-05 03:12:37 +00:00
|
|
|
check not tx.isClosed()
|
|
|
|
|
|
|
|
check:
|
|
|
|
tx.put(key1, val1).isOk()
|
|
|
|
tx.put(key2, val2).isOk()
|
|
|
|
tx.put(key3, val3).isOk()
|
|
|
|
|
|
|
|
tx.delete(key2).isOk()
|
|
|
|
not tx.isClosed()
|
|
|
|
|
|
|
|
check:
|
|
|
|
tx.get(key1).get() == val1
|
|
|
|
tx.get(key2).error() == ""
|
|
|
|
tx.get(key3).get() == val3
|
|
|
|
|
|
|
|
let res = tx.rollback()
|
|
|
|
check:
|
|
|
|
res.isOk()
|
|
|
|
tx.get(key1).error() == ""
|
|
|
|
tx.get(key2).error() == ""
|
|
|
|
tx.get(key3).error() == ""
|
|
|
|
|
|
|
|
test "Test commit using default column family":
|
|
|
|
var tx = db.beginTransaction()
|
2024-06-26 15:00:10 +00:00
|
|
|
defer:
|
|
|
|
tx.close()
|
2024-03-05 03:12:37 +00:00
|
|
|
check not tx.isClosed()
|
|
|
|
|
|
|
|
check:
|
|
|
|
tx.put(key1, val1).isOk()
|
|
|
|
tx.put(key2, val2).isOk()
|
|
|
|
tx.put(key3, val3).isOk()
|
|
|
|
|
|
|
|
tx.delete(key2).isOk()
|
|
|
|
not tx.isClosed()
|
|
|
|
|
|
|
|
check:
|
|
|
|
tx.get(key1).get() == val1
|
|
|
|
tx.get(key2).error() == ""
|
|
|
|
tx.get(key3).get() == val3
|
|
|
|
|
|
|
|
let res = tx.commit()
|
|
|
|
check:
|
|
|
|
res.isOk()
|
|
|
|
tx.get(key1).get() == val1
|
|
|
|
tx.get(key2).error() == ""
|
|
|
|
tx.get(key3).get() == val3
|
|
|
|
|
|
|
|
test "Test setting column family in beginTransaction":
|
2024-06-26 17:31:39 +00:00
|
|
|
var tx = db.beginTransaction(cfHandle = otherCfHandle)
|
2024-06-26 15:00:10 +00:00
|
|
|
defer:
|
|
|
|
tx.close()
|
2024-03-05 03:12:37 +00:00
|
|
|
check not tx.isClosed()
|
|
|
|
|
|
|
|
check:
|
|
|
|
tx.put(key1, val1).isOk()
|
|
|
|
tx.put(key2, val2).isOk()
|
|
|
|
tx.put(key3, val3).isOk()
|
|
|
|
|
|
|
|
tx.delete(key2).isOk()
|
|
|
|
not tx.isClosed()
|
|
|
|
|
|
|
|
check:
|
2024-06-26 17:31:39 +00:00
|
|
|
tx.get(key1, defaultCfHandle).error() == ""
|
|
|
|
tx.get(key2, defaultCfHandle).error() == ""
|
|
|
|
tx.get(key3, defaultCfHandle).error() == ""
|
|
|
|
tx.get(key1, otherCfHandle).get() == val1
|
|
|
|
tx.get(key2, otherCfHandle).error() == ""
|
|
|
|
tx.get(key3, otherCfHandle).get() == val3
|
2024-03-05 03:12:37 +00:00
|
|
|
|
|
|
|
test "Test rollback and commit with multiple transactions":
|
2024-06-26 17:31:39 +00:00
|
|
|
var tx1 = db.beginTransaction(cfHandle = defaultCfHandle)
|
2024-06-26 15:00:10 +00:00
|
|
|
defer:
|
|
|
|
tx1.close()
|
2024-03-05 03:12:37 +00:00
|
|
|
check not tx1.isClosed()
|
2024-06-26 17:31:39 +00:00
|
|
|
var tx2 = db.beginTransaction(cfHandle = otherCfHandle)
|
2024-06-26 15:00:10 +00:00
|
|
|
defer:
|
|
|
|
tx2.close()
|
2024-03-05 03:12:37 +00:00
|
|
|
check not tx2.isClosed()
|
|
|
|
|
|
|
|
check:
|
|
|
|
tx1.put(key1, val1).isOk()
|
|
|
|
tx1.put(key2, val2).isOk()
|
|
|
|
tx1.put(key3, val3).isOk()
|
|
|
|
tx1.delete(key2).isOk()
|
|
|
|
not tx1.isClosed()
|
|
|
|
tx2.put(key1, val1).isOk()
|
|
|
|
tx2.put(key2, val2).isOk()
|
|
|
|
tx2.put(key3, val3).isOk()
|
|
|
|
tx2.delete(key2).isOk()
|
|
|
|
not tx2.isClosed()
|
|
|
|
|
|
|
|
check:
|
2024-06-26 17:31:39 +00:00
|
|
|
tx1.get(key1, defaultCfHandle).get() == val1
|
|
|
|
tx1.get(key2, defaultCfHandle).error() == ""
|
|
|
|
tx1.get(key3, defaultCfHandle).get() == val3
|
|
|
|
tx1.get(key1, otherCfHandle).error() == ""
|
|
|
|
tx1.get(key2, otherCfHandle).error() == ""
|
|
|
|
tx1.get(key3, otherCfHandle).error() == ""
|
|
|
|
|
|
|
|
tx2.get(key1, defaultCfHandle).error() == ""
|
|
|
|
tx2.get(key2, defaultCfHandle).error() == ""
|
|
|
|
tx2.get(key3, defaultCfHandle).error() == ""
|
|
|
|
tx2.get(key1, otherCfHandle).get() == val1
|
|
|
|
tx2.get(key2, otherCfHandle).error() == ""
|
|
|
|
tx2.get(key3, otherCfHandle).get() == val3
|
2024-03-05 03:12:37 +00:00
|
|
|
|
|
|
|
block:
|
|
|
|
let res = tx1.rollback()
|
|
|
|
check:
|
|
|
|
res.isOk()
|
2024-06-26 17:31:39 +00:00
|
|
|
tx1.get(key1, defaultCfHandle).error() == ""
|
|
|
|
tx1.get(key2, defaultCfHandle).error() == ""
|
|
|
|
tx1.get(key3, defaultCfHandle).error() == ""
|
|
|
|
tx1.get(key1, otherCfHandle).error() == ""
|
|
|
|
tx1.get(key2, otherCfHandle).error() == ""
|
|
|
|
tx1.get(key3, otherCfHandle).error() == ""
|
2024-03-05 03:12:37 +00:00
|
|
|
|
|
|
|
block:
|
|
|
|
let res = tx2.commit()
|
|
|
|
check:
|
|
|
|
res.isOk()
|
2024-06-26 17:31:39 +00:00
|
|
|
tx2.get(key1, defaultCfHandle).error() == ""
|
|
|
|
tx2.get(key2, defaultCfHandle).error() == ""
|
|
|
|
tx2.get(key3, defaultCfHandle).error() == ""
|
|
|
|
tx2.get(key1, otherCfHandle).get() == val1
|
|
|
|
tx2.get(key2, otherCfHandle).error() == ""
|
|
|
|
tx2.get(key3, otherCfHandle).get() == val3
|
2024-03-05 03:12:37 +00:00
|
|
|
|
|
|
|
test "Test close":
|
|
|
|
var tx = db.beginTransaction()
|
|
|
|
|
|
|
|
check not tx.isClosed()
|
|
|
|
tx.close()
|
|
|
|
check tx.isClosed()
|
|
|
|
tx.close()
|
|
|
|
check tx.isClosed()
|
|
|
|
|
|
|
|
check not db.isClosed()
|
|
|
|
db.close()
|
|
|
|
check db.isClosed()
|
|
|
|
db.close()
|
|
|
|
check db.isClosed()
|
|
|
|
|
|
|
|
test "Test close multiple tx":
|
|
|
|
var tx1 = db.beginTransaction()
|
|
|
|
var tx2 = db.beginTransaction()
|
|
|
|
|
|
|
|
check not db.isClosed()
|
|
|
|
check not tx1.isClosed()
|
|
|
|
tx1.close()
|
|
|
|
check tx1.isClosed()
|
|
|
|
tx1.close()
|
|
|
|
check tx1.isClosed()
|
|
|
|
|
|
|
|
check not db.isClosed()
|
|
|
|
check not tx2.isClosed()
|
|
|
|
tx2.close()
|
|
|
|
check tx2.isClosed()
|
|
|
|
tx2.close()
|
|
|
|
check tx2.isClosed()
|