Skip to content

Commit

Permalink
Implement autoclose on missed types.
Browse files Browse the repository at this point in the history
  • Loading branch information
bhartnett committed Jun 28, 2024
1 parent 03313d8 commit a3c7266
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 19 deletions.
3 changes: 2 additions & 1 deletion rocksdb/backup.nim
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ proc openBackupEngine*(
let backupEnginePtr = rocksdb_backup_engine_open(
backupOpts.cPtr, path.cstring, cast[cstringArray](errors.addr)
)
bailOnErrors(errors, backupOpts = backupOpts)

bailOnErrorsAutoCloseOpts(errors, backupOpts = backupOpts)

let engine =
BackupEngineRef(cPtr: backupEnginePtr, path: path, backupOpts: backupOpts)
Expand Down
24 changes: 22 additions & 2 deletions rocksdb/columnfamily/cfopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import ../lib/librocksdb, ../options/tableopts

type
SlicetransformPtr* = ptr rocksdb_slicetransform_t

SlicetransformRef* = ref object
cPtr: SlicetransformPtr
autoClose*: bool # if true then close will be called when the parent is closed

ColFamilyOptionsPtr* = ptr rocksdb_options_t

Expand All @@ -23,6 +25,8 @@ 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

Compression* {.pure.} = enum
Expand All @@ -36,8 +40,11 @@ type
xpressCompression = rocksdb_xpress_compression
zstdCompression = rocksdb_zstd_compression

proc createFixedPrefix*(value: int): SlicetransformRef =
SlicetransformRef(cPtr: rocksdb_slicetransform_create_fixed_prefix(value.csize_t))
proc createFixedPrefix*(value: int, autoClose = false): SlicetransformRef =
SlicetransformRef(
cPtr: rocksdb_slicetransform_create_fixed_prefix(value.csize_t),
autoClose: autoClose,
)

proc isClosed*(s: SlicetransformRef): bool {.inline.} =
s.cPtr.isNil()
Expand Down Expand Up @@ -66,6 +73,11 @@ proc close*(cfOpts: ColFamilyOptionsRef) =
rocksdb_options_destroy(cfOpts.cPtr)
cfOpts.cPtr = nil

if not cfOpts.sliceTransform.isNil() and cfOpts.sliceTransform.autoClose:
cfOpts.sliceTransform.close()
if not cfOpts.tableOpts.isNil() and cfOpts.tableOpts.autoClose:
cfOpts.tableOpts.close()

template opt(nname, ntyp, ctyp: untyped) =
proc `nname=`*(cfOpts: ColFamilyOptionsRef, value: ntyp) =
doAssert not cfOpts.isClosed
Expand Down Expand Up @@ -125,13 +137,21 @@ 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

rocksdb_options_set_prefix_extractor(cfOpts.cPtr, value.cPtr)
cfOpts.sliceTransform = value

proc `blockBasedTableFactory=`*(
cfOpts: ColFamilyOptionsRef, tableOpts: TableOptionsRef
) =
doAssert not cfOpts.isClosed()
doAssert cfOpts.tableOpts.isNil()
# don't allow overwriting an existing tableOpts which could leak memory

rocksdb_options_set_block_based_table_factory(cfOpts.cPtr, tableOpts.cPtr)
cfOpts.tableOpts = tableOpts

# https://github.com/facebook/rocksdb/wiki/MemTable
proc setHashSkipListRep*(
Expand Down
8 changes: 7 additions & 1 deletion rocksdb/internal/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ template autoCloseNonNil*(opts: typed) =
if not opts.isNil and opts.autoClose:
opts.close()

template bailOnErrors*(
template bailOnErrorsAutoCloseOpts*(
errors: cstring,
dbOpts: DbOptionsRef = nil,
readOpts: ReadOptionsRef = nil,
Expand All @@ -48,3 +48,9 @@ template bailOnErrors*(
let res = err($(errors))
rocksdb_free(errors)
return res

template bailOnErrors*(errors: cstring): auto =
if not errors.isNil:
let res = err($(errors))
rocksdb_free(errors)
return res
5 changes: 3 additions & 2 deletions rocksdb/options/cache.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ type

CacheRef* = ref object
cPtr*: CachePtr
autoClose*: bool # if true then close will be called when the parent type is closed

proc cacheCreateLRU*(size: int): CacheRef =
CacheRef(cPtr: rocksdb_cache_create_lru(size.csize_t))
proc cacheCreateLRU*(size: int, autoClose = false): CacheRef =
CacheRef(cPtr: rocksdb_cache_create_lru(size.csize_t), autoClose: autoClose)

proc close*(cache: CacheRef) =
if cache.cPtr != nil:
Expand Down
8 changes: 8 additions & 0 deletions rocksdb/options/dbopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type

DbOptionsRef* = ref object
cPtr: DbOptionsPtr
cache: CacheRef
autoClose*: bool # if true then close will be called when the database is closed

proc newDbOptions*(autoClose = false): DbOptionsRef =
Expand Down Expand Up @@ -94,7 +95,11 @@ opt avoidUnnecessaryBlockingIo, bool, uint8

proc `rowCache=`*(dbOpts: DbOptionsRef, cache: CacheRef) =
doAssert not dbOpts.isClosed()
doAssert dbOpts.cache.isNil()
# don't allow overwriting an existing cache which could leak memory

rocksdb_options_set_row_cache(dbOpts.cPtr, cache.cPtr)
dbOpts.cache = cache

proc defaultDbOptions*(autoClose = false): DbOptionsRef =
let opts: DbOptionsRef = newDbOptions(autoClose)
Expand All @@ -118,3 +123,6 @@ proc close*(dbOpts: DbOptionsRef) =
if not dbOpts.isClosed():
rocksdb_options_destroy(dbOpts.cPtr)
dbOpts.cPtr = nil

if not dbOpts.cache.isNil() and dbOpts.cache.autoClose:
dbOpts.cache.close()
39 changes: 31 additions & 8 deletions rocksdb/options/tableopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ type

TableOptionsRef* = ref object
cPtr*: TableOptionsPtr
cache: CacheRef
filterPolicy: FilterPolicyRef
autoClose*: bool # if true then close will be called when the parent type is closed

FilterPolicyPtr* = ptr rocksdb_filterpolicy_t

FilterPolicyRef* = ref object
cPtr*: FilterPolicyPtr
autoClose*: bool # if true then close will be called when the parent type is closed

IndexType* {.pure.} = enum
binarySearch = rocksdb_block_based_table_index_type_binary_search
Expand All @@ -22,14 +26,17 @@ type
binarySearchAndHash =
rocksdb_block_based_table_data_block_index_type_binary_search_and_hash

proc createRibbon*(bitsPerKey: float): FilterPolicyRef =
FilterPolicyRef(cPtr: rocksdb_filterpolicy_create_ribbon(bitsPerKey))
proc createRibbon*(bitsPerKey: float, autoClose = false): FilterPolicyRef =
FilterPolicyRef(
cPtr: rocksdb_filterpolicy_create_ribbon(bitsPerKey), autoClose: autoClose
)

proc createRibbonHybrid*(
bitsPerKey: float, bloomBeforeLevel: int = 0
bitsPerKey: float, bloomBeforeLevel: int = 0, autoClose = false
): FilterPolicyRef =
FilterPolicyRef(
cPtr: rocksdb_filterpolicy_create_ribbon_hybrid(bitsPerKey, bloomBeforeLevel.cint)
cPtr: rocksdb_filterpolicy_create_ribbon_hybrid(bitsPerKey, bloomBeforeLevel.cint),
autoClose: autoClose,
)

proc isClosed*(policy: FilterPolicyRef): bool =
Expand All @@ -40,8 +47,8 @@ proc close*(policy: FilterPolicyRef) =
rocksdb_filterpolicy_destroy(policy.cPtr)
policy.cPtr = nil

proc createTableOptions*(): TableOptionsRef =
TableOptionsRef(cPtr: rocksdb_block_based_options_create())
proc createTableOptions*(autoClose = false): TableOptionsRef =
TableOptionsRef(cPtr: rocksdb_block_based_options_create(), autoClose: autoClose)

proc isClosed*(opts: TableOptionsRef): bool =
isNil(opts.cPtr)
Expand All @@ -51,6 +58,11 @@ proc close*(opts: TableOptionsRef) =
rocksdb_block_based_options_destroy(opts.cPtr)
opts.cPtr = nil

if not opts.cache.isNil() and opts.cache.autoClose:
opts.cache.close()
if not opts.filterPolicy.isNil() and opts.filterPolicy.autoClose:
opts.filterPolicy.close()

template opt(nname, ntyp, ctyp: untyped) =
proc `nname=`*(opts: TableOptionsRef, value: ntyp) =
doAssert not opts.isClosed
Expand All @@ -76,15 +88,26 @@ opt wholeKeyFiltering, bool, uint8
opt formatVersion, int, cint

proc `blockCache=`*(opts: TableOptionsRef, cache: CacheRef) =
doAssert not opts.isClosed()
doAssert opts.cache.isNil()
# don't allow overwriting an existing cache which could leak memory

rocksdb_block_based_options_set_block_cache(opts.cPtr, cache.cPtr)
opts.cache = cache

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

rocksdb_block_based_options_set_filter_policy(opts.cPtr, policy.cPtr)
opts.filterPolicy = policy

proc defaultTableOptions*(): TableOptionsRef =
proc defaultTableOptions*(autoClose = false): TableOptionsRef =
# https://github.com/facebook/rocksdb/wiki/Setup-Options-and-Basic-Tuning#other-general-options
let opts = createTableOptions()
let opts = createTableOptions(autoClose)
opts.blockSize = 16 * 1024
opts.cacheIndexAndFilterBlocks = true
opts.pinL0FilterAndIndexBlocksInCache = true

opts
6 changes: 3 additions & 3 deletions rocksdb/rocksdb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ proc listColumnFamilies*(path: string): RocksDBResult[seq[string]] =
cfList = rocksdb_list_column_families(
dbOpts.cPtr, path.cstring, addr cfLen, cast[cstringArray](errors.addr)
)
bailOnErrors(errors, dbOpts)
bailOnErrorsAutoCloseOpts(errors, dbOpts)

if cfList.isNil or cfLen == 0:
return ok(newSeqOfCap[string](0))
Expand Down Expand Up @@ -127,7 +127,7 @@ proc openRocksDb*(
cfHandles[0].addr,
cast[cstringArray](errors.addr),
)
bailOnErrors(errors, dbOpts, readOpts, writeOpts, cfDescriptors = cfs)
bailOnErrorsAutoCloseOpts(errors, dbOpts, readOpts, writeOpts, cfDescriptors = cfs)

let
cfTable = newColFamilyTable(cfNames.mapIt($it), cfHandles)
Expand Down Expand Up @@ -181,7 +181,7 @@ proc openRocksDbReadOnly*(
errorIfWalFileExists.uint8,
cast[cstringArray](errors.addr),
)
bailOnErrors(errors, dbOpts, readOpts, cfDescriptors = cfs)
bailOnErrorsAutoCloseOpts(errors, dbOpts, readOpts, cfDescriptors = cfs)

let
cfTable = newColFamilyTable(cfNames.mapIt($it), cfHandles)
Expand Down
2 changes: 1 addition & 1 deletion rocksdb/sstfilewriter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ proc openSstFileWriter*(
rocksdb_sstfilewriter_open(
writer.cPtr, filePath.cstring, cast[cstringArray](errors.addr)
)
bailOnErrors(errors, dbOpts)
bailOnErrorsAutoCloseOpts(errors, dbOpts)

ok(writer)

Expand Down
2 changes: 1 addition & 1 deletion rocksdb/transactiondb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ proc openTransactionDb*(
cfHandles[0].addr,
cast[cstringArray](errors.addr),
)
bailOnErrors(errors, dbOpts, txDbOpts = txDbOpts, cfDescriptors = cfs)
bailOnErrorsAutoCloseOpts(errors, dbOpts, txDbOpts = txDbOpts, cfDescriptors = cfs)

let
cfTable = newColFamilyTable(cfNames.mapIt($it), cfHandles)
Expand Down

0 comments on commit a3c7266

Please sign in to comment.