From 8b4162cec5f2ffef2d5cb1aae3d6a42c5b681512 Mon Sep 17 00:00:00 2001 From: web3-developer <51288821+web3-developer@users.noreply.github.com> Date: Thu, 27 Jun 2024 16:55:20 +0800 Subject: [PATCH] Add autoClose flag to each opt type. --- rocksdb/internal/utils.nim | 8 ++++---- rocksdb/options/dbopts.nim | 9 +++++---- rocksdb/options/readopts.nim | 9 +++++---- rocksdb/options/writeopts.nim | 9 +++++---- rocksdb/rocksdb.nim | 20 ++++++++++++-------- rocksdb/transactiondb.nim | 16 +++++++++------- rocksdb/transactions/transaction.nim | 12 ++++++++---- rocksdb/transactions/txdbopts.nim | 13 +++++++++---- rocksdb/transactions/txopts.nim | 11 +++++++---- 9 files changed, 64 insertions(+), 43 deletions(-) diff --git a/rocksdb/internal/utils.nim b/rocksdb/internal/utils.nim index 096828d..1fba356 100644 --- a/rocksdb/internal/utils.nim +++ b/rocksdb/internal/utils.nim @@ -32,13 +32,13 @@ template bailOnErrors*( txDbOpts: TransactionDbOptionsRef = nil, ): auto = if not errors.isNil: - if not dbOpts.isNil(): + if not dbOpts.isNil() and dbOpts.autoClose: dbOpts.close() - if not readOpts.isNil(): + if not readOpts.isNil() and dbOpts.autoClose: readOpts.close() - if not writeOpts.isNil(): + if not writeOpts.isNil() and dbOpts.autoClose: writeOpts.close() - if not txDbOpts.isNil(): + if not txDbOpts.isNil() and dbOpts.autoClose: txDbOpts.close() let res = err($(errors)) diff --git a/rocksdb/options/dbopts.nim b/rocksdb/options/dbopts.nim index 28ba3bb..e3f9c0f 100644 --- a/rocksdb/options/dbopts.nim +++ b/rocksdb/options/dbopts.nim @@ -18,9 +18,10 @@ type DbOptionsRef* = ref object cPtr: DbOptionsPtr + autoClose*: bool # if true then close will be called when the database is closed -proc newDbOptions*(): DbOptionsRef = - DbOptionsRef(cPtr: rocksdb_options_create()) +proc newDbOptions*(autoClose = false): DbOptionsRef = + DbOptionsRef(cPtr: rocksdb_options_create(), autoClose: autoClose) proc isClosed*(dbOpts: DbOptionsRef): bool {.inline.} = dbOpts.cPtr.isNil() @@ -95,8 +96,8 @@ proc `rowCache=`*(dbOpts: DbOptionsRef, cache: CacheRef) = doAssert not dbOpts.isClosed() rocksdb_options_set_row_cache(dbOpts.cPtr, cache.cPtr) -proc defaultDbOptions*(): DbOptionsRef = - let opts: DbOptionsRef = newDbOptions() +proc defaultDbOptions*(autoClose = false): DbOptionsRef = + let opts: DbOptionsRef = newDbOptions(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 0e719c8..fc15960 100644 --- a/rocksdb/options/readopts.nim +++ b/rocksdb/options/readopts.nim @@ -16,9 +16,10 @@ type ReadOptionsRef* = ref object cPtr: ReadOptionsPtr + autoClose*: bool # if true then close will be called when the database is closed -proc newReadOptions*(): ReadOptionsRef = - ReadOptionsRef(cPtr: rocksdb_readoptions_create()) +proc newReadOptions*(autoClose = false): ReadOptionsRef = + ReadOptionsRef(cPtr: rocksdb_readoptions_create(), autoClose: autoClose) proc isClosed*(readOpts: ReadOptionsRef): bool {.inline.} = readOpts.cPtr.isNil() @@ -29,8 +30,8 @@ proc cPtr*(readOpts: ReadOptionsRef): ReadOptionsPtr = # TODO: Add setters and getters for read options properties. -proc defaultReadOptions*(): ReadOptionsRef {.inline.} = - newReadOptions() +proc defaultReadOptions*(autoClose = false): ReadOptionsRef {.inline.} = + newReadOptions(autoClose) # TODO: set prefered defaults proc close*(readOpts: ReadOptionsRef) = diff --git a/rocksdb/options/writeopts.nim b/rocksdb/options/writeopts.nim index 9648f06..3702ed3 100644 --- a/rocksdb/options/writeopts.nim +++ b/rocksdb/options/writeopts.nim @@ -16,9 +16,10 @@ type WriteOptionsRef* = ref object cPtr: WriteOptionsPtr + autoClose*: bool # if true then close will be called when the database is closed -proc newWriteOptions*(): WriteOptionsRef = - WriteOptionsRef(cPtr: rocksdb_writeoptions_create()) +proc newWriteOptions*(autoClose = false): WriteOptionsRef = + WriteOptionsRef(cPtr: rocksdb_writeoptions_create(), autoClose: autoClose) proc isClosed*(writeOpts: WriteOptionsRef): bool {.inline.} = writeOpts.cPtr.isNil() @@ -29,8 +30,8 @@ proc cPtr*(writeOpts: WriteOptionsRef): WriteOptionsPtr = # TODO: Add setters and getters for write options properties. -proc defaultWriteOptions*(): WriteOptionsRef {.inline.} = - newWriteOptions() +proc defaultWriteOptions*(autoClose = false): WriteOptionsRef {.inline.} = + newWriteOptions(autoClose) # TODO: set prefered defaults proc close*(writeOpts: WriteOptionsRef) = diff --git a/rocksdb/rocksdb.nim b/rocksdb/rocksdb.nim index 55a1818..903c2e0 100644 --- a/rocksdb/rocksdb.nim +++ b/rocksdb/rocksdb.nim @@ -94,9 +94,9 @@ proc listColumnFamilies*( proc openRocksDb*( path: string, - dbOpts = defaultDbOptions(), - readOpts = defaultReadOptions(), - writeOpts = defaultWriteOptions(), + dbOpts = defaultDbOptions(autoClose = true), + readOpts = defaultReadOptions(autoClose = true), + writeOpts = defaultWriteOptions(autoClose = true), columnFamilies: openArray[ColFamilyDescriptor] = [], ): RocksDBResult[RocksDbReadWriteRef] = ## Open a RocksDB instance in read-write mode. If `columnFamilies` is empty @@ -143,8 +143,8 @@ proc openRocksDb*( proc openRocksDbReadOnly*( path: string, - dbOpts = defaultDbOptions(), - readOpts = defaultReadOptions(), + dbOpts = defaultDbOptions(autoClose = true), + readOpts = defaultReadOptions(autoClose = true), columnFamilies: openArray[ColFamilyDescriptor] = [], errorIfWalFileExists = false, ): RocksDBResult[RocksDbReadOnlyRef] = @@ -399,11 +399,15 @@ proc close*(db: RocksDbRef) = db.cPtr = nil # opts should be closed after the database is closed - db.dbOpts.close() - db.readOpts.close() + if db.dbOpts.autoClose: + db.dbOpts.close() + if db.readOpts.autoClose: + db.readOpts.close() if db of RocksDbReadWriteRef: let db = RocksDbReadWriteRef(db) - db.writeOpts.close() + if db.writeOpts.autoClose: + db.writeOpts.close() + rocksdb_ingestexternalfileoptions_destroy(db.ingestOptsPtr) db.ingestOptsPtr = nil diff --git a/rocksdb/transactiondb.nim b/rocksdb/transactiondb.nim index dabfbd0..654fdc7 100644 --- a/rocksdb/transactiondb.nim +++ b/rocksdb/transactiondb.nim @@ -41,8 +41,8 @@ type proc openTransactionDb*( path: string, - dbOpts = defaultDbOptions(), - txDbOpts = defaultTransactionDbOptions(), + dbOpts = defaultDbOptions(autoClose = true), + txDbOpts = defaultTransactionDbOptions(autoClose = true), columnFamilies: openArray[ColFamilyDescriptor] = [], ): RocksDBResult[TransactionDbRef] = ## Open a `TransactionDbRef` with the given options and column families. @@ -99,9 +99,9 @@ proc isClosed*(db: TransactionDbRef): bool {.inline.} = proc beginTransaction*( db: TransactionDbRef, - readOpts = defaultReadOptions(), - writeOpts = defaultWriteOptions(), - txOpts = defaultTransactionOptions(), + readOpts = defaultReadOptions(autoClose = true), + writeOpts = defaultWriteOptions(autoClose = true), + txOpts = defaultTransactionOptions(autoClose = true), cfHandle = db.defaultCfHandle, ): TransactionRef = ## Begin a new transaction against the database. The transaction will default @@ -127,5 +127,7 @@ proc close*(db: TransactionDbRef) = db.cPtr = nil # opts should be closed after the database is closed - db.dbOpts.close() - db.txDbOpts.close() + if db.dbOpts.autoClose: + db.dbOpts.close() + if db.txDbOpts.autoClose: + db.txDbOpts.close() diff --git a/rocksdb/transactions/transaction.nim b/rocksdb/transactions/transaction.nim index dd86c27..f268c9a 100644 --- a/rocksdb/transactions/transaction.nim +++ b/rocksdb/transactions/transaction.nim @@ -175,9 +175,13 @@ proc rollback*(tx: TransactionRef): RocksDBResult[void] = proc close*(tx: TransactionRef) = ## Close the `TransactionRef`. if not tx.isClosed(): - tx.readOpts.close() - tx.writeOpts.close() - tx.txOpts.close() - rocksdb_transaction_destroy(tx.cPtr) tx.cPtr = nil + + # opts should be closed after the transaction is closed + if tx.readOpts.autoClose: + tx.readOpts.close() + if tx.writeOpts.autoClose: + tx.writeOpts.close() + if tx.txOpts.autoClose: + tx.txOpts.close() diff --git a/rocksdb/transactions/txdbopts.nim b/rocksdb/transactions/txdbopts.nim index 6cb0fea..db019cb 100644 --- a/rocksdb/transactions/txdbopts.nim +++ b/rocksdb/transactions/txdbopts.nim @@ -16,9 +16,12 @@ type TransactionDbOptionsRef* = ref object cPtr: TransactionDbOptionsPtr + autoClose*: bool # if true then close will be called when the database is closed -proc newTransactionDbOptions*(): TransactionDbOptionsRef = - TransactionDbOptionsRef(cPtr: rocksdb_transactiondb_options_create()) +proc newTransactionDbOptions*(autoClose = false): TransactionDbOptionsRef = + TransactionDbOptionsRef( + cPtr: rocksdb_transactiondb_options_create(), autoClose: autoClose + ) proc isClosed*(txDbOpts: TransactionDbOptionsRef): bool {.inline.} = txDbOpts.cPtr.isNil() @@ -29,8 +32,10 @@ proc cPtr*(txDbOpts: TransactionDbOptionsRef): TransactionDbOptionsPtr = # TODO: Add setters and getters for backup options properties. -proc defaultTransactionDbOptions*(): TransactionDbOptionsRef {.inline.} = - newTransactionDbOptions() +proc defaultTransactionDbOptions*( + autoClose = false +): TransactionDbOptionsRef {.inline.} = + newTransactionDbOptions(autoClose) # TODO: set prefered defaults proc close*(txDbOpts: TransactionDbOptionsRef) = diff --git a/rocksdb/transactions/txopts.nim b/rocksdb/transactions/txopts.nim index 395be16..e31d7c7 100644 --- a/rocksdb/transactions/txopts.nim +++ b/rocksdb/transactions/txopts.nim @@ -16,9 +16,12 @@ type TransactionOptionsRef* = ref object cPtr: TransactionOptionsPtr + autoClose*: bool # if true then close will be called when the transaction is closed -proc newTransactionOptions*(): TransactionOptionsRef = - TransactionOptionsRef(cPtr: rocksdb_transaction_options_create()) +proc newTransactionOptions*(autoClose = false): TransactionOptionsRef = + TransactionOptionsRef( + cPtr: rocksdb_transaction_options_create(), autoClose: autoClose + ) proc isClosed*(txOpts: TransactionOptionsRef): bool {.inline.} = txOpts.cPtr.isNil() @@ -29,8 +32,8 @@ proc cPtr*(txOpts: TransactionOptionsRef): TransactionOptionsPtr = # TODO: Add setters and getters for backup options properties. -proc defaultTransactionOptions*(): TransactionOptionsRef {.inline.} = - newTransactionOptions() +proc defaultTransactionOptions*(autoClose = false): TransactionOptionsRef {.inline.} = + newTransactionOptions(autoClose) # TODO: set prefered defaults proc close*(txOpts: TransactionOptionsRef) =