Skip to content

Commit

Permalink
Handle default option objects, close after use
Browse files Browse the repository at this point in the history
  • Loading branch information
mjfh committed Jun 13, 2024
1 parent 7d11708 commit 04b16a7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 28 deletions.
54 changes: 38 additions & 16 deletions rocksdb/rocksdb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type

proc listColumnFamilies*(
path: string;
dbOpts = defaultDbOptions();
dbOpts = DbOptionsRef(nil);
): RocksDBResult[seq[string]] =
## List exisiting column families on disk. This might be used to find out
## whether there were some columns missing with the version on disk.
Expand All @@ -78,12 +78,17 @@ proc listColumnFamilies*(
## above once rocksdb has been upgraded to the latest version, see comments
## at the end of ./columnfamily/cfhandle.nim.
##
let
useDbOpts = (if dbOpts.isNil: defaultDbOptions() else: dbOpts)
defer:
if dbOpts.isNil: useDbOpts.close()

var
lencf: csize_t
errors: cstring
let
cList = rocksdb_list_column_families(
dbOpts.cPtr,
useDbOpts.cPtr,
path.cstring,
addr lencf,
cast[cstringArray](errors.addr))
Expand All @@ -107,17 +112,22 @@ proc listColumnFamilies*(
ok cfs

proc openRocksDb*(
path: string,
dbOpts = defaultDbOptions(),
readOpts = defaultReadOptions(),
writeOpts = defaultWriteOptions(),
columnFamilies: openArray[ColFamilyDescriptor] = []): RocksDBResult[RocksDbReadWriteRef] =
path: string;
dbOpts = DbOptionsRef(nil);
readOpts = ReadOptionsRef(nil);
writeOpts = WriteOptionsRef(nil);
columnFamilies: openArray[ColFamilyDescriptor] = [];
): RocksDBResult[RocksDbReadWriteRef] =
## Open a RocksDB instance in read-write mode. If `columnFamilies` is empty
## then it will open the default column family. If `dbOpts`, `readOpts`, or
## `writeOpts` are not supplied then the default options will be used.
## By default, column families will be created if they don't yet exist.
## All existing column families must be specified if the database has
## previously created any column families.
let
useDbOpts = (if dbOpts.isNil: defaultDbOptions() else: dbOpts)
defer:
if dbOpts.isNil: useDbOpts.close()

var cfs = columnFamilies.toSeq()
if DEFAULT_COLUMN_FAMILY_NAME notin columnFamilies.mapIt(it.name()):
Expand All @@ -129,7 +139,7 @@ proc openRocksDb*(
cfHandles = newSeq[ColFamilyHandlePtr](cfs.len)
errors: cstring
let rocksDbPtr = rocksdb_open_column_families(
dbOpts.cPtr,
useDbOpts.cPtr,
path.cstring,
cfNames.len().cint,
cast[cstringArray](cfNames[0].addr),
Expand All @@ -138,7 +148,11 @@ proc openRocksDb*(
cast[cstringArray](errors.addr))
bailOnErrors(errors)

let db = RocksDbReadWriteRef(
let
dbOpts = useDbOpts # don't close on exit
readOpts = (if readOpts.isNil: defaultReadOptions() else: readOpts)
writeOpts = (if writeOpts.isNil: defaultWriteOptions() else: writeOpts)
db = RocksDbReadWriteRef(
lock: createLock(),
cPtr: rocksDbPtr,
path: path,
Expand All @@ -151,17 +165,22 @@ proc openRocksDb*(
ok(db)

proc openRocksDbReadOnly*(
path: string,
dbOpts = defaultDbOptions(),
readOpts = defaultReadOptions(),
columnFamilies: openArray[ColFamilyDescriptor] = [],
errorIfWalFileExists = false): RocksDBResult[RocksDbReadOnlyRef] =
path: string;
dbOpts = DbOptionsRef(nil);
readOpts = ReadOptionsRef(nil);
columnFamilies: openArray[ColFamilyDescriptor] = [];
errorIfWalFileExists = false;
): RocksDBResult[RocksDbReadOnlyRef] =
## Open a RocksDB instance in read-only mode. If `columnFamilies` is empty
## then it will open the default column family. If `dbOpts` or `readOpts` are
## not supplied then the default options will be used. By default, column
## families will be created if they don't yet exist. If the database already
## contains any column families, then all or a subset of the existing column
## families can be opened for reading.
let
useDbOpts = (if dbOpts.isNil: defaultDbOptions() else: dbOpts)
defer:
if dbOpts.isNil: useDbOpts.close()

var cfs = columnFamilies.toSeq()
if DEFAULT_COLUMN_FAMILY_NAME notin columnFamilies.mapIt(it.name()):
Expand All @@ -173,7 +192,7 @@ proc openRocksDbReadOnly*(
cfHandles = newSeq[ColFamilyHandlePtr](cfs.len)
errors: cstring
let rocksDbPtr = rocksdb_open_for_read_only_column_families(
dbOpts.cPtr,
useDbOpts.cPtr,
path.cstring,
cfNames.len().cint,
cast[cstringArray](cfNames[0].addr),
Expand All @@ -183,7 +202,10 @@ proc openRocksDbReadOnly*(
cast[cstringArray](errors.addr))
bailOnErrors(errors)

let db = RocksDbReadOnlyRef(
let
dbOpts = useDbOpts # don't close on exit
readOpts = (if readOpts.isNil: defaultReadOptions() else: readOpts)
db = RocksDbReadOnlyRef(
lock: createLock(),
cPtr: rocksDbPtr,
path: path,
Expand Down
6 changes: 4 additions & 2 deletions rocksdb/sstfilewriter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ type
dbOpts: DbOptionsRef

proc openSstFileWriter*(
filePath: string,
dbOpts = defaultDbOptions()): RocksDBResult[SstFileWriterRef] =
filePath: string;
dbOpts = DbOptionsRef(nil);
): RocksDBResult[SstFileWriterRef] =
## Creates a new `SstFileWriterRef` and opens the file at the given `filePath`.
let dbOpts = (if dbOpts.isNil: defaultDbOptions() else: dbOpts)
doAssert not dbOpts.isClosed()

let envOptsPtr = rocksdb_envoptions_create()
Expand Down
34 changes: 24 additions & 10 deletions rocksdb/transactiondb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,17 @@ type

proc openTransactionDb*(
path: string,
dbOpts = defaultDbOptions(),
txDbOpts = defaultTransactionDbOptions(),
columnFamilies: openArray[ColFamilyDescriptor] = []): RocksDBResult[TransactionDbRef] =
dbOpts = DbOptionsRef(nil);
txDbOpts = TransactionDbOptionsRef(nil);
columnFamilies: openArray[ColFamilyDescriptor] = [];
): RocksDBResult[TransactionDbRef] =
## Open a `TransactionDbRef` with the given options and column families.
## If no column families are provided the default column family will be used.
## If no options are provided the default options will be used.
let
useDbOpts = (if dbOpts.isNil: defaultDbOptions() else: dbOpts)
defer:
if dbOpts.isNil: useDbOpts.close()

var cfs = columnFamilies.toSeq()
if DEFAULT_COLUMN_FAMILY_NAME notin columnFamilies.mapIt(it.name()):
Expand All @@ -65,7 +70,7 @@ proc openTransactionDb*(
errors: cstring

let txDbPtr = rocksdb_transactiondb_open_column_families(
dbOpts.cPtr,
useDbOpts.cPtr,
txDbOpts.cPtr,
path.cstring,
cfNames.len().cint,
Expand All @@ -75,7 +80,10 @@ proc openTransactionDb*(
cast[cstringArray](errors.addr))
bailOnErrors(errors)

let db = TransactionDbRef(
let
dbOpts = useDbOpts # don't close on exit
txDbOpts = (if txDbOpts.isNil: defaultTransactionDbOptions() else: txDbOpts)
db = TransactionDbRef(
lock: createLock(),
cPtr: txDbPtr,
path: path,
Expand All @@ -89,15 +97,21 @@ proc isClosed*(db: TransactionDbRef): bool {.inline.} =
db.cPtr.isNil()

proc beginTransaction*(
db: TransactionDbRef,
readOpts = defaultReadOptions(),
writeOpts = defaultWriteOptions(),
txOpts = defaultTransactionOptions(),
columnFamily = DEFAULT_COLUMN_FAMILY_NAME): TransactionRef =
db: TransactionDbRef;
readOpts = ReadOptionsRef(nil);
writeOpts = WriteOptionsRef(nil);
txDbOpts = TransactionDbOptionsRef(nil);
txOpts = defaultTransactionOptions();
columnFamily = DEFAULT_COLUMN_FAMILY_NAME;
): TransactionRef =
## Begin a new transaction against the database. The transaction will default
## to using the specified column family. If no column family is specified
## then the default column family will be used.
doAssert not db.isClosed()
let
txDbOpts = (if txDbOpts.isNil: defaultTransactionDbOptions() else: txDbOpts)
readOpts = (if readOpts.isNil: defaultReadOptions() else: readOpts)
writeOpts = (if writeOpts.isNil: defaultWriteOptions() else: writeOpts)

let txPtr = rocksdb_transaction_begin(
db.cPtr,
Expand Down

0 comments on commit 04b16a7

Please sign in to comment.