Skip to content

Commit

Permalink
Add autoClose flag to each opt type.
Browse files Browse the repository at this point in the history
  • Loading branch information
bhartnett committed Jun 27, 2024
1 parent 8d5417c commit 8b4162c
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 43 deletions.
8 changes: 4 additions & 4 deletions rocksdb/internal/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
9 changes: 5 additions & 4 deletions rocksdb/options/dbopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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())
Expand Down
9 changes: 5 additions & 4 deletions rocksdb/options/readopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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) =
Expand Down
9 changes: 5 additions & 4 deletions rocksdb/options/writeopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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) =
Expand Down
20 changes: 12 additions & 8 deletions rocksdb/rocksdb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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] =
Expand Down Expand Up @@ -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
16 changes: 9 additions & 7 deletions rocksdb/transactiondb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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()
12 changes: 8 additions & 4 deletions rocksdb/transactions/transaction.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()
13 changes: 9 additions & 4 deletions rocksdb/transactions/txdbopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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) =
Expand Down
11 changes: 7 additions & 4 deletions rocksdb/transactions/txopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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) =
Expand Down

0 comments on commit 8b4162c

Please sign in to comment.