diff --git a/rocksdb/columnfamily/cfopts.nim b/rocksdb/columnfamily/cfopts.nim index f73c8f6..42726a8 100644 --- a/rocksdb/columnfamily/cfopts.nim +++ b/rocksdb/columnfamily/cfopts.nim @@ -11,12 +11,13 @@ import ../lib/librocksdb, ../internal/utils, ../options/tableopts +export tableopts + type SlicetransformPtr* = ptr rocksdb_slicetransform_t SlicetransformRef* = ref object cPtr: SlicetransformPtr - autoClose*: bool # if true then close will be called when it's parent is closed ColFamilyOptionsPtr* = ptr rocksdb_options_t @@ -25,7 +26,6 @@ type # type - CF options are a subset of rocksdb_options_t - when in doubt, check: # https://github.com/facebook/rocksdb/blob/b8c9a2576af6a1d0ffcfbb517dfcb7e7037bd460/include/rocksdb/options.h#L66 cPtr: ColFamilyOptionsPtr - sliceTransform: SlicetransformRef tableOpts: TableOptionsRef autoClose*: bool # if true then close will be called when the database is closed @@ -40,11 +40,8 @@ type xpressCompression = rocksdb_xpress_compression zstdCompression = rocksdb_zstd_compression -proc createFixedPrefix*(value: int, autoClose = false): SlicetransformRef = - SlicetransformRef( - cPtr: rocksdb_slicetransform_create_fixed_prefix(value.csize_t), - autoClose: autoClose, - ) +proc createFixedPrefix*(value: int): SlicetransformRef = + SlicetransformRef(cPtr: rocksdb_slicetransform_create_fixed_prefix(value.csize_t)) proc isClosed*(s: SlicetransformRef): bool {.inline.} = s.cPtr.isNil() @@ -58,7 +55,7 @@ proc close*(s: SlicetransformRef) = rocksdb_slicetransform_destroy(s.cPtr) s.cPtr = nil -proc newColFamilyOptions*(autoClose = false): ColFamilyOptionsRef = +proc createColFamilyOptions*(autoClose = false): ColFamilyOptionsRef = ColFamilyOptionsRef(cPtr: rocksdb_options_create(), autoClose: autoClose) proc isClosed*(cfOpts: ColFamilyOptionsRef): bool {.inline.} = @@ -73,7 +70,6 @@ proc close*(cfOpts: ColFamilyOptionsRef) = rocksdb_options_destroy(cfOpts.cPtr) cfOpts.cPtr = nil - autoCloseNonNil(cfOpts.sliceTransform) autoCloseNonNil(cfOpts.tableOpts) template opt(nname, ntyp, ctyp: untyped) = @@ -126,7 +122,7 @@ opt blobCompactionReadaheadSize, int, uint64 opt blobFileStartingLevel, int, cint proc defaultColFamilyOptions*(autoClose = false): ColFamilyOptionsRef = - newColFamilyOptions(autoClose) + createColFamilyOptions(autoClose) # proc setFixedPrefixExtractor*(dbOpts: ColFamilyOptionsRef, length: int) = # doAssert not dbOpts.isClosed() @@ -135,11 +131,14 @@ proc defaultColFamilyOptions*(autoClose = false): ColFamilyOptionsRef = proc `setPrefixExtractor`*(cfOpts: ColFamilyOptionsRef, value: SlicetransformRef) = doAssert not cfOpts.isClosed() - doAssert cfOpts.sliceTransform.isNil() - # don't allow overwriting an existing sliceTransform which could leak memory + # Destroys the existing slice transform if there is one attached to the column + # family options and takes ownership of the passed slice transform. After this call, + # the ColFamilyOptionsRef is responsible for cleaning up the policy when it is no + # longer needed so we set the filter policy to nil so that isClosed() will return true + # and prevent the filter policy from being double freed which was causing a seg fault. rocksdb_options_set_prefix_extractor(cfOpts.cPtr, value.cPtr) - cfOpts.sliceTransform = value + value.cPtr = nil proc `blockBasedTableFactory=`*( cfOpts: ColFamilyOptionsRef, tableOpts: TableOptionsRef diff --git a/rocksdb/options/backupopts.nim b/rocksdb/options/backupopts.nim index 75debb6..7d01294 100644 --- a/rocksdb/options/backupopts.nim +++ b/rocksdb/options/backupopts.nim @@ -18,7 +18,7 @@ type cPtr: BackupEngineOptionsPtr autoClose*: bool # if true then close will be called when the backup engine is closed -proc newBackupEngineOptions*(autoClose = false): BackupEngineOptionsRef = +proc createBackupEngineOptions*(autoClose = false): BackupEngineOptionsRef = BackupEngineOptionsRef(cPtr: rocksdb_options_create(), autoClose: autoClose) proc isClosed*(engineOpts: BackupEngineOptionsRef): bool {.inline.} = @@ -31,7 +31,7 @@ proc cPtr*(engineOpts: BackupEngineOptionsRef): BackupEngineOptionsPtr = # TODO: Add setters and getters for backup options properties. proc defaultBackupEngineOptions*(autoClose = false): BackupEngineOptionsRef {.inline.} = - let opts = newBackupEngineOptions(autoClose) + let opts = createBackupEngineOptions(autoClose) # TODO: set defaults here diff --git a/rocksdb/options/cache.nim b/rocksdb/options/cache.nim index 2024852..f439a13 100644 --- a/rocksdb/options/cache.nim +++ b/rocksdb/options/cache.nim @@ -4,12 +4,19 @@ type CachePtr* = ptr rocksdb_cache_t CacheRef* = ref object - cPtr*: CachePtr + cPtr: CachePtr autoClose*: bool # if true then close will be called when it's parent is closed proc cacheCreateLRU*(size: int, autoClose = false): CacheRef = CacheRef(cPtr: rocksdb_cache_create_lru(size.csize_t), autoClose: autoClose) +proc isClosed*(cache: CacheRef): bool = + isNil(cache.cPtr) + +proc cPtr*(cache: CacheRef): CachePtr = + doAssert not cache.isClosed() + cache.cPtr + proc close*(cache: CacheRef) = if cache.cPtr != nil: rocksdb_cache_destroy(cache.cPtr) diff --git a/rocksdb/options/dbopts.nim b/rocksdb/options/dbopts.nim index 36fc08a..2349f08 100644 --- a/rocksdb/options/dbopts.nim +++ b/rocksdb/options/dbopts.nim @@ -21,7 +21,7 @@ type cache: CacheRef autoClose*: bool # if true then close will be called when the database is closed -proc newDbOptions*(autoClose = false): DbOptionsRef = +proc createDbOptions*(autoClose = false): DbOptionsRef = DbOptionsRef(cPtr: rocksdb_options_create(), autoClose: autoClose) proc isClosed*(dbOpts: DbOptionsRef): bool {.inline.} = @@ -102,7 +102,7 @@ proc `rowCache=`*(dbOpts: DbOptionsRef, cache: CacheRef) = dbOpts.cache = cache proc defaultDbOptions*(autoClose = false): DbOptionsRef = - let opts: DbOptionsRef = newDbOptions(autoClose) + let opts: DbOptionsRef = createDbOptions(autoClose) # Optimize RocksDB. This is the easiest way to get RocksDB to perform well: opts.increaseParallelism(countProcessors()) diff --git a/rocksdb/options/readopts.nim b/rocksdb/options/readopts.nim index fc15960..f60269e 100644 --- a/rocksdb/options/readopts.nim +++ b/rocksdb/options/readopts.nim @@ -18,7 +18,7 @@ type cPtr: ReadOptionsPtr autoClose*: bool # if true then close will be called when the database is closed -proc newReadOptions*(autoClose = false): ReadOptionsRef = +proc createReadOptions*(autoClose = false): ReadOptionsRef = ReadOptionsRef(cPtr: rocksdb_readoptions_create(), autoClose: autoClose) proc isClosed*(readOpts: ReadOptionsRef): bool {.inline.} = @@ -31,7 +31,7 @@ proc cPtr*(readOpts: ReadOptionsRef): ReadOptionsPtr = # TODO: Add setters and getters for read options properties. proc defaultReadOptions*(autoClose = false): ReadOptionsRef {.inline.} = - newReadOptions(autoClose) + createReadOptions(autoClose) # TODO: set prefered defaults proc close*(readOpts: ReadOptionsRef) = diff --git a/rocksdb/options/tableopts.nim b/rocksdb/options/tableopts.nim index ec8e31d..a980a28 100644 --- a/rocksdb/options/tableopts.nim +++ b/rocksdb/options/tableopts.nim @@ -1,20 +1,20 @@ import ../lib/librocksdb, ../internal/utils, ./cache +export cache + type # TODO might eventually wrap this TableOptionsPtr* = ptr rocksdb_block_based_table_options_t TableOptionsRef* = ref object - cPtr*: TableOptionsPtr + cPtr: TableOptionsPtr cache: CacheRef - filterPolicy: FilterPolicyRef autoClose*: bool # if true then close will be called when it's parent is closed FilterPolicyPtr* = ptr rocksdb_filterpolicy_t FilterPolicyRef* = ref object - cPtr*: FilterPolicyPtr - autoClose*: bool # if true then close will be called when it's parent is closed + cPtr: FilterPolicyPtr IndexType* {.pure.} = enum binarySearch = rocksdb_block_based_table_index_type_binary_search @@ -27,21 +27,22 @@ type rocksdb_block_based_table_data_block_index_type_binary_search_and_hash proc createRibbon*(bitsPerKey: float, autoClose = false): FilterPolicyRef = - FilterPolicyRef( - cPtr: rocksdb_filterpolicy_create_ribbon(bitsPerKey), autoClose: autoClose - ) + FilterPolicyRef(cPtr: rocksdb_filterpolicy_create_ribbon(bitsPerKey)) proc createRibbonHybrid*( bitsPerKey: float, bloomBeforeLevel: int = 0, autoClose = false ): FilterPolicyRef = FilterPolicyRef( - cPtr: rocksdb_filterpolicy_create_ribbon_hybrid(bitsPerKey, bloomBeforeLevel.cint), - autoClose: autoClose, + cPtr: rocksdb_filterpolicy_create_ribbon_hybrid(bitsPerKey, bloomBeforeLevel.cint) ) proc isClosed*(policy: FilterPolicyRef): bool = isNil(policy.cPtr) +proc cPtr*(policy: FilterPolicyRef): FilterPolicyPtr = + doAssert not policy.isClosed() + policy.cPtr + proc close*(policy: FilterPolicyRef) = if not isClosed(policy): rocksdb_filterpolicy_destroy(policy.cPtr) @@ -53,13 +54,9 @@ proc createTableOptions*(autoClose = false): TableOptionsRef = proc isClosed*(opts: TableOptionsRef): bool = isNil(opts.cPtr) -proc close*(opts: TableOptionsRef) = - if not isClosed(opts): - rocksdb_block_based_options_destroy(opts.cPtr) - opts.cPtr = nil - - autoCloseNonNil(opts.cache) - autoCloseNonNil(opts.filterPolicy) +proc cPtr*(opts: TableOptionsRef): TableOptionsPtr = + doAssert not opts.isClosed() + opts.cPtr template opt(nname, ntyp, ctyp: untyped) = proc `nname=`*(opts: TableOptionsRef, value: ntyp) = @@ -95,11 +92,15 @@ proc `blockCache=`*(opts: TableOptionsRef, cache: CacheRef) = proc `filterPolicy=`*(opts: TableOptionsRef, policy: FilterPolicyRef) = doAssert not opts.isClosed() - doAssert opts.filterPolicy.isNil() - # don't allow overwriting an existing policy which could leak memory + # Destroys the existing policy if there is one attached to the table options + # and takes ownership of the passed in policy. After this call, the TableOptionsRef + # is responsible for cleaning up the policy when it is no longer needed + # so we set the filter policy to nil so that isClosed() will return true + # and prevent the filter policy from being double freed which was causing a seg fault. + # See here: https://github.com/facebook/rocksdb/blob/22fe23edc89e9842ed72b613de172cd80d3b00da/include/rocksdb/filter_policy.h#L152 rocksdb_block_based_options_set_filter_policy(opts.cPtr, policy.cPtr) - opts.filterPolicy = policy + policy.cPtr = nil proc defaultTableOptions*(autoClose = false): TableOptionsRef = # https://github.com/facebook/rocksdb/wiki/Setup-Options-and-Basic-Tuning#other-general-options @@ -109,3 +110,10 @@ proc defaultTableOptions*(autoClose = false): TableOptionsRef = opts.pinL0FilterAndIndexBlocksInCache = true opts + +proc close*(opts: TableOptionsRef) = + if not isClosed(opts): + rocksdb_block_based_options_destroy(opts.cPtr) + opts.cPtr = nil + + autoCloseNonNil(opts.cache) diff --git a/rocksdb/options/writeopts.nim b/rocksdb/options/writeopts.nim index 3702ed3..f621e7f 100644 --- a/rocksdb/options/writeopts.nim +++ b/rocksdb/options/writeopts.nim @@ -18,7 +18,7 @@ type cPtr: WriteOptionsPtr autoClose*: bool # if true then close will be called when the database is closed -proc newWriteOptions*(autoClose = false): WriteOptionsRef = +proc createWriteOptions*(autoClose = false): WriteOptionsRef = WriteOptionsRef(cPtr: rocksdb_writeoptions_create(), autoClose: autoClose) proc isClosed*(writeOpts: WriteOptionsRef): bool {.inline.} = @@ -31,7 +31,7 @@ proc cPtr*(writeOpts: WriteOptionsRef): WriteOptionsPtr = # TODO: Add setters and getters for write options properties. proc defaultWriteOptions*(autoClose = false): WriteOptionsRef {.inline.} = - newWriteOptions(autoClose) + createWriteOptions(autoClose) # TODO: set prefered defaults proc close*(writeOpts: WriteOptionsRef) = diff --git a/rocksdb/rocksdb.nim b/rocksdb/rocksdb.nim index e757313..abb60c0 100644 --- a/rocksdb/rocksdb.nim +++ b/rocksdb/rocksdb.nim @@ -363,7 +363,7 @@ proc openWriteBatch*( ## Opens a `WriteBatchRef` which defaults to using the specified column family. doAssert not db.isClosed() - newWriteBatch(cfHandle) + createWriteBatch(cfHandle) proc write*(db: RocksDbReadWriteRef, updates: WriteBatchRef): RocksDBResult[void] = ## Apply the updates in the `WriteBatchRef` to the database. @@ -417,10 +417,7 @@ proc close*(db: RocksDbRef) = # opts should be closed after the database is closed autoCloseNonNil(db.dbOpts) autoCloseNonNil(db.readOpts) - - for cfDesc in db.cfDescriptors: - if cfDesc.autoClose: - cfDesc.close() + autoCloseAll(db.cfDescriptors) if db of RocksDbReadWriteRef: let db = RocksDbReadWriteRef(db) diff --git a/rocksdb/transactions/txdbopts.nim b/rocksdb/transactions/txdbopts.nim index db019cb..b2d934b 100644 --- a/rocksdb/transactions/txdbopts.nim +++ b/rocksdb/transactions/txdbopts.nim @@ -18,7 +18,7 @@ type cPtr: TransactionDbOptionsPtr autoClose*: bool # if true then close will be called when the database is closed -proc newTransactionDbOptions*(autoClose = false): TransactionDbOptionsRef = +proc createTransactionDbOptions*(autoClose = false): TransactionDbOptionsRef = TransactionDbOptionsRef( cPtr: rocksdb_transactiondb_options_create(), autoClose: autoClose ) @@ -35,7 +35,7 @@ proc cPtr*(txDbOpts: TransactionDbOptionsRef): TransactionDbOptionsPtr = proc defaultTransactionDbOptions*( autoClose = false ): TransactionDbOptionsRef {.inline.} = - newTransactionDbOptions(autoClose) + createTransactionDbOptions(autoClose) # TODO: set prefered defaults proc close*(txDbOpts: TransactionDbOptionsRef) = diff --git a/rocksdb/transactions/txopts.nim b/rocksdb/transactions/txopts.nim index e31d7c7..a22f2fa 100644 --- a/rocksdb/transactions/txopts.nim +++ b/rocksdb/transactions/txopts.nim @@ -18,7 +18,7 @@ type cPtr: TransactionOptionsPtr autoClose*: bool # if true then close will be called when the transaction is closed -proc newTransactionOptions*(autoClose = false): TransactionOptionsRef = +proc createTransactionOptions*(autoClose = false): TransactionOptionsRef = TransactionOptionsRef( cPtr: rocksdb_transaction_options_create(), autoClose: autoClose ) @@ -33,7 +33,7 @@ proc cPtr*(txOpts: TransactionOptionsRef): TransactionOptionsPtr = # TODO: Add setters and getters for backup options properties. proc defaultTransactionOptions*(autoClose = false): TransactionOptionsRef {.inline.} = - newTransactionOptions(autoClose) + createTransactionOptions(autoClose) # TODO: set prefered defaults proc close*(txOpts: TransactionOptionsRef) = diff --git a/rocksdb/writebatch.nim b/rocksdb/writebatch.nim index 3a3672f..d357792 100644 --- a/rocksdb/writebatch.nim +++ b/rocksdb/writebatch.nim @@ -22,7 +22,7 @@ type cPtr: WriteBatchPtr defaultCfHandle: ColFamilyHandleRef -proc newWriteBatch*(defaultCfHandle: ColFamilyHandleRef): WriteBatchRef = +proc createWriteBatch*(defaultCfHandle: ColFamilyHandleRef): WriteBatchRef = WriteBatchRef(cPtr: rocksdb_writebatch_create(), defaultCfHandle: defaultCfHandle) proc isClosed*(batch: WriteBatchRef): bool {.inline.} = diff --git a/tests/columnfamily/test_cfhandle.nim b/tests/columnfamily/test_cfhandle.nim index 5f3be9c..cb05562 100644 --- a/tests/columnfamily/test_cfhandle.nim +++ b/tests/columnfamily/test_cfhandle.nim @@ -44,7 +44,7 @@ suite "ColFamilyHandleRef Tests": removeDir($dbPath) test "Test newColFamilyHandle": - var cfHandle = newColFamilyHandle(cfHandlePtr) + let cfHandle = newColFamilyHandle(cfHandlePtr) check: not cfHandle.cPtr.isNil() @@ -53,7 +53,7 @@ suite "ColFamilyHandleRef Tests": cfHandle.close() test "Test close": - var cfHandle = newColFamilyHandle(cfHandlePtr) + let cfHandle = newColFamilyHandle(cfHandlePtr) check not cfHandle.isClosed() cfHandle.close() diff --git a/tests/columnfamily/test_cfopts.nim b/tests/columnfamily/test_cfopts.nim index 28d25a3..5b5d2d6 100644 --- a/tests/columnfamily/test_cfopts.nim +++ b/tests/columnfamily/test_cfopts.nim @@ -20,3 +20,45 @@ suite "ColFamilyOptionsRef Tests": check cfOpts.isClosed() cfOpts.close() check cfOpts.isClosed() + + test "Test auto close enabled": + let + cfOpts = defaultColFamilyOptions() + tableOpts = defaultTableOptions(autoClose = true) + sliceTransform = createFixedPrefix(1000) + + cfOpts.blockBasedTableFactory = tableOpts + cfOpts.setPrefixExtractor(sliceTransform) + + check: + cfOpts.isClosed() == false + tableOpts.isClosed() == false + sliceTransform.isClosed() == true # closed because tableopts takes ownership + + cfOpts.close() + + check: + cfOpts.isClosed() == true + tableOpts.isClosed() == true + sliceTransform.isClosed() == true + + test "Test auto close disabled": + let + cfOpts = defaultColFamilyOptions() + tableOpts = defaultTableOptions(autoClose = false) + sliceTransform = createFixedPrefix(1000) + + cfOpts.blockBasedTableFactory = tableOpts + cfOpts.setPrefixExtractor(sliceTransform) + + check: + cfOpts.isClosed() == false + tableOpts.isClosed() == false + sliceTransform.isClosed() == true # closed because tableopts takes ownership + + cfOpts.close() + + check: + cfOpts.isClosed() == true + tableOpts.isClosed() == false + sliceTransform.isClosed() == true diff --git a/tests/options/test_backupopts.nim b/tests/options/test_backupopts.nim index 3b10b50..9d0e21a 100644 --- a/tests/options/test_backupopts.nim +++ b/tests/options/test_backupopts.nim @@ -13,7 +13,7 @@ import unittest2, ../../rocksdb/options/backupopts suite "BackupEngineOptionsRef Tests": test "Test newBackupEngineOptions": - var backupOpts = newBackupEngineOptions() + var backupOpts = createBackupEngineOptions() check not backupOpts.cPtr.isNil() diff --git a/tests/options/test_dbopts.nim b/tests/options/test_dbopts.nim index b0f9513..66b7921 100644 --- a/tests/options/test_dbopts.nim +++ b/tests/options/test_dbopts.nim @@ -13,7 +13,7 @@ import unittest2, ../../rocksdb/options/dbopts suite "DbOptionsRef Tests": test "Test newDbOptions": - var dbOpts = newDbOptions() + let dbOpts = createDbOptions() check not dbOpts.cPtr.isNil() @@ -28,10 +28,28 @@ suite "DbOptionsRef Tests": dbOpts.close() test "Test close": - var dbOpts = defaultDbOptions() + let dbOpts = defaultDbOptions() check not dbOpts.isClosed() dbOpts.close() check dbOpts.isClosed() dbOpts.close() check dbOpts.isClosed() + + # This is currently failing in MacOS CI due to older version of RocksDb + # test "Test auto close enabled": + # let + # dbOpts = defaultDbOptions() + # cache = cacheCreateLRU(1000, autoClose = true) + + # dbOpts.rowCache = cache + + # check: + # dbOpts.isClosed() == false + # cache.isClosed() == false + + # dbOpts.close() + + # check: + # dbOpts.isClosed() == true + # cache.isClosed() == true diff --git a/tests/options/test_readopts.nim b/tests/options/test_readopts.nim index 2651f3f..3c7bce9 100644 --- a/tests/options/test_readopts.nim +++ b/tests/options/test_readopts.nim @@ -13,7 +13,7 @@ import unittest2, ../../rocksdb/options/readopts suite "ReadOptionsRef Tests": test "Test newReadOptions": - var readOpts = newReadOptions() + var readOpts = createReadOptions() check not readOpts.cPtr.isNil() diff --git a/tests/options/test_tableopts.nim b/tests/options/test_tableopts.nim new file mode 100644 index 0000000..076fbf2 --- /dev/null +++ b/tests/options/test_tableopts.nim @@ -0,0 +1,80 @@ +# 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/options/tableopts + +suite "TableOptionsRef Tests": + test "Test createTableOptions": + let tableOpts = createTableOptions() + + check not tableOpts.cPtr.isNil() + + tableOpts.close() + + test "Test defaultTableOptions": + let tableOpts = defaultTableOptions() + + check not tableOpts.cPtr.isNil() + + tableOpts.close() + + test "Test close": + let tableOpts = defaultTableOptions() + + check not tableOpts.isClosed() + tableOpts.close() + check tableOpts.isClosed() + tableOpts.close() + check tableOpts.isClosed() + + test "Test auto close enabled": + # TODO: enable filter policy once creating updated DLL build + let + tableOpts = defaultTableOptions() + cache = cacheCreateLRU(1000, autoClose = true) + # filter = createRibbon(9.9, autoClose = true) + + tableOpts.blockCache = cache + # tableOpts.filterPolicy = filter + + check: + tableOpts.isClosed() == false + cache.isClosed() == false + # filter.isClosed() == true # closed because tableopts takes ownership + + tableOpts.close() + + check: + tableOpts.isClosed() == true + cache.isClosed() == true + # filter.isClosed() == true + + test "Test auto close disabled": + # TODO: enable filter policy once creating updated DLL build + let + tableOpts = defaultTableOptions() + cache = cacheCreateLRU(1000, autoClose = false) + # filter = createRibbon(9.9, autoClose = true) + + tableOpts.blockCache = cache + # tableOpts.filterPolicy = filter + + check: + tableOpts.isClosed() == false + cache.isClosed() == false + # filter.isClosed() == true # closed because tableopts takes ownership + + tableOpts.close() + + check: + tableOpts.isClosed() == true + cache.isClosed() == false + # filter.isClosed() == true diff --git a/tests/options/test_writeopts.nim b/tests/options/test_writeopts.nim index 38981d0..7d33212 100644 --- a/tests/options/test_writeopts.nim +++ b/tests/options/test_writeopts.nim @@ -13,7 +13,7 @@ import unittest2, ../../rocksdb/options/writeopts suite "WriteOptionsRef Tests": test "Test newWriteOptions": - var writeOpts = newWriteOptions() + var writeOpts = createWriteOptions() check not writeOpts.cPtr.isNil() diff --git a/tests/test_all.nim b/tests/test_all.nim index f236e72..3b6287a 100644 --- a/tests/test_all.nim +++ b/tests/test_all.nim @@ -17,6 +17,7 @@ import ./options/test_dbopts, ./options/test_readopts, ./options/test_writeopts, + ./options/test_tableopts, ./transactions/test_txdbopts, ./transactions/test_txopts, ./test_backup, diff --git a/tests/test_backup.nim b/tests/test_backup.nim index b2ba55a..4e50650 100644 --- a/tests/test_backup.nim +++ b/tests/test_backup.nim @@ -59,3 +59,33 @@ suite "BackupEngineRef Tests": check engine.isClosed() engine.close() check engine.isClosed() + + test "Test auto close enabled": + let + backupOpts = defaultBackupEngineOptions(autoClose = true) + backupEngine = openBackupEngine(dbPath, backupOpts).get() + + check: + backupOpts.isClosed() == false + backupEngine.isClosed() == false + + backupEngine.close() + + check: + backupOpts.isClosed() == true + backupEngine.isClosed() == true + + test "Test auto close disabled": + let + backupOpts = defaultBackupEngineOptions(autoClose = false) + backupEngine = openBackupEngine(dbPath, backupOpts).get() + + check: + backupOpts.isClosed() == false + backupEngine.isClosed() == false + + backupEngine.close() + + check: + backupOpts.isClosed() == false + backupEngine.isClosed() == true diff --git a/tests/test_rocksdb.nim b/tests/test_rocksdb.nim index 2d58052..160609a 100644 --- a/tests/test_rocksdb.nim +++ b/tests/test_rocksdb.nim @@ -380,7 +380,7 @@ suite "RocksDbRef Tests": test "Test auto close disabled": let - dbPath = mkdtemp() / "autoclose-enabled" + dbPath = mkdtemp() / "autoclose-disabled" dbOpts = defaultDbOptions(autoClose = false) readOpts = defaultReadOptions(autoClose = false) writeOpts = defaultWriteOptions(autoClose = false) diff --git a/tests/test_sstfilewriter.nim b/tests/test_sstfilewriter.nim index f21e197..187affe 100644 --- a/tests/test_sstfilewriter.nim +++ b/tests/test_sstfilewriter.nim @@ -86,3 +86,33 @@ suite "SstFileWriterRef Tests": check writer.isClosed() writer.close() check writer.isClosed() + + test "Test auto close enabled": + let + dbOpts = defaultDbOptions(autoClose = true) + writer = openSstFileWriter(sstFilePath, dbOpts).get() + + check: + dbOpts.isClosed() == false + writer.isClosed() == false + + writer.close() + + check: + dbOpts.isClosed() == true + writer.isClosed() == true + + test "Test auto close disabled": + let + dbOpts = defaultDbOptions(autoClose = false) + writer = openSstFileWriter(sstFilePath, dbOpts).get() + + check: + dbOpts.isClosed() == false + writer.isClosed() == false + + writer.close() + + check: + dbOpts.isClosed() == false + writer.isClosed() == true diff --git a/tests/test_transactiondb.nim b/tests/test_transactiondb.nim index c991c73..2ae0132 100644 --- a/tests/test_transactiondb.nim +++ b/tests/test_transactiondb.nim @@ -202,21 +202,96 @@ suite "TransactionDbRef Tests": tx2.close() check tx2.isClosed() - test "Test auto close": + test "Test auto close enabled": let - dbPath = mkdtemp() / "autoclose" - dbOpts = defaultDbOptions(autoClose = false) + dbPath = mkdtemp() / "autoclose-enabled" + dbOpts = defaultDbOptions(autoClose = true) txDbOpts = defaultTransactionDbOptions(autoClose = true) - db = openTransactionDb(dbPath, dbOpts, txDbOpts).get() + columnFamilies = + @[ + initColFamilyDescriptor(CF_DEFAULT, defaultColFamilyOptions(autoClose = true)) + ] + db = openTransactionDb(dbPath, dbOpts, txDbOpts, columnFamilies).get() check: dbOpts.isClosed() == false txDbOpts.isClosed() == false + columnFamilies[0].isClosed() == false + db.isClosed() == false + + db.close() + + check: + dbOpts.isClosed() == true + txDbOpts.isClosed() == true + columnFamilies[0].isClosed() == true + db.isClosed() == true + + test "Test auto close enabled": + let + dbPath = mkdtemp() / "autoclose-disabled" + dbOpts = defaultDbOptions(autoClose = false) + txDbOpts = defaultTransactionDbOptions(autoClose = false) + columnFamilies = + @[ + initColFamilyDescriptor( + CF_DEFAULT, defaultColFamilyOptions(autoClose = false) + ) + ] + db = openTransactionDb(dbPath, dbOpts, txDbOpts, columnFamilies).get() + + check: + dbOpts.isClosed() == false + txDbOpts.isClosed() == false + columnFamilies[0].isClosed() == false db.isClosed() == false db.close() check: dbOpts.isClosed() == false - txDbOpts.isClosed() == true + txDbOpts.isClosed() == false + columnFamilies[0].isClosed() == false db.isClosed() == true + + test "Test auto close tx enabled": + let + readOpts = defaultReadOptions(autoClose = true) + writeOpts = defaultWriteOptions(autoClose = true) + txOpts = defaultTransactionOptions(autoClose = true) + tx = db.beginTransaction(readOpts, writeOpts, txOpts) + + check: + readOpts.isClosed() == false + writeOpts.isClosed() == false + txOpts.isClosed() == false + tx.isClosed() == false + + tx.close() + + check: + readOpts.isClosed() == true + writeOpts.isClosed() == true + txOpts.isClosed() == true + tx.isClosed() == true + + test "Test auto close tx disabled": + let + readOpts = defaultReadOptions(autoClose = false) + writeOpts = defaultWriteOptions(autoClose = false) + txOpts = defaultTransactionOptions(autoClose = false) + tx = db.beginTransaction(readOpts, writeOpts, txOpts) + + check: + readOpts.isClosed() == false + writeOpts.isClosed() == false + txOpts.isClosed() == false + tx.isClosed() == false + + tx.close() + + check: + readOpts.isClosed() == false + writeOpts.isClosed() == false + txOpts.isClosed() == false + tx.isClosed() == true diff --git a/tests/transactions/test_txdbopts.nim b/tests/transactions/test_txdbopts.nim index 47574e7..5619395 100644 --- a/tests/transactions/test_txdbopts.nim +++ b/tests/transactions/test_txdbopts.nim @@ -13,7 +13,7 @@ import unittest2, ../../rocksdb/transactions/txdbopts suite "TransactionDbOptionsRef Tests": test "Test newTransactionDbOptions": - var txDbOpts = newTransactionDbOptions() + var txDbOpts = createTransactionDbOptions() check not txDbOpts.cPtr.isNil() diff --git a/tests/transactions/test_txopts.nim b/tests/transactions/test_txopts.nim index 3e853e8..9ea00f7 100644 --- a/tests/transactions/test_txopts.nim +++ b/tests/transactions/test_txopts.nim @@ -13,7 +13,7 @@ import unittest2, ../../rocksdb/transactions/txopts suite "TransactionOptionsRef Tests": test "Test newTransactionOptions": - var txOpts = newTransactionOptions() + var txOpts = createTransactionOptions() check not txOpts.cPtr.isNil()