Skip to content

Commit

Permalink
Fix compile for nim v1.6.
Browse files Browse the repository at this point in the history
  • Loading branch information
bhartnett committed Jun 27, 2024
1 parent ed425d0 commit be1699a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
22 changes: 11 additions & 11 deletions rocksdb/internal/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,29 @@ proc createLock*(): Lock =
initLock(lock)
lock

template autoCloseNonNil*(opts: typed) =
if not opts.isNil and opts.autoClose:
opts.close()

template bailOnErrors*(
errors: cstring,
dbOpts: DbOptionsRef = nil,
readOpts: ReadOptionsRef = nil,
writeOpts: WriteOptionsRef = nil,
txDbOpts: TransactionDbOptionsRef = nil,
cfDescriptors: openArray[ColFamilyDescriptor] = [],
backupOpts: BackupEngineOptionsRef = nil,
cfDescriptors: openArray[ColFamilyDescriptor] = @[],
): auto =
if not errors.isNil:
if not dbOpts.isNil() and dbOpts.autoClose:
dbOpts.close()
if not readOpts.isNil() and dbOpts.autoClose:
readOpts.close()
if not writeOpts.isNil() and dbOpts.autoClose:
writeOpts.close()
if not txDbOpts.isNil() and dbOpts.autoClose:
txDbOpts.close()
autoCloseNonNil(dbOpts)
autoCloseNonNil(readOpts)
autoCloseNonNil(writeOpts)
autoCloseNonNil(txDbOpts)
autoCloseNonNil(backupOpts)

for cfDesc in cfDescriptors:
if cfDesc.autoClose:
cfDesc.close()
if not backupOpts.isNil() and backupOpts.autoClose:
backupOpts.close()

let res = err($(errors))
rocksdb_free(errors)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_rocksdb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,22 @@ suite "RocksDbRef Tests":
check db.isClosed()
db.close()
check db.isClosed()

test "Test auto close":
let
dbPath = mkdtemp() / "autoclose"
dbOpts = defaultDbOptions(autoClose = false)
readOpts = defaultReadOptions(autoClose = true)
db = openRocksDb(dbPath, dbOpts, readOpts).get()

check:
dbOpts.isClosed() == false
readOpts.isClosed() == false
db.isClosed() == false

db.close()

check:
dbOpts.isClosed() == false
readOpts.isClosed() == true
db.isClosed() == true
19 changes: 19 additions & 0 deletions tests/test_transactiondb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,22 @@ suite "TransactionDbRef Tests":
check tx2.isClosed()
tx2.close()
check tx2.isClosed()

test "Test auto close":
let
dbPath = mkdtemp() / "autoclose"
dbOpts = defaultDbOptions(autoClose = false)
txDbOpts = defaultTransactionDbOptions(autoClose = true)
db = openTransactionDb(dbPath, dbOpts, txDbOpts).get()

check:
dbOpts.isClosed() == false
txDbOpts.isClosed() == false
db.isClosed() == false

db.close()

check:
dbOpts.isClosed() == false
txDbOpts.isClosed() == true
db.isClosed() == true

0 comments on commit be1699a

Please sign in to comment.