Skip to content

Commit

Permalink
Commit library rewrite progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
bhartnett committed Feb 19, 2024
1 parent 55487c8 commit 656e521
Show file tree
Hide file tree
Showing 9 changed files with 244 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import
./cfopts

const DEFAULT_COLUMN_FAMILY* = "default"
const DEFAULT_COLUMN_FAMILY_NAME* = "default"

type
ColFamilyDescriptor* = object
Expand All @@ -24,14 +24,17 @@ proc initColFamilyDescriptor*(
options = defaultColFamilyOptions()): ColFamilyDescriptor =
ColFamilyDescriptor(name: name, options: options)

proc name*(descriptor: ColFamilyDescriptor): string =
template name*(descriptor: ColFamilyDescriptor): string =
descriptor.name

proc options*(descriptor: ColFamilyDescriptor): ColFamilyOptionsRef =
template options*(descriptor: ColFamilyDescriptor): ColFamilyOptionsRef =
descriptor.options

proc defaultColFamilyDescriptor*(): ColFamilyDescriptor =
initColFamilyDescriptor(DEFAULT_COLUMN_FAMILY)
template isDefault*(descriptor: ColFamilyDescriptor): bool =
descriptor.name() == DEFAULT_COLUMN_FAMILY_NAME

template defaultColFamilyDescriptor*(): ColFamilyDescriptor =
initColFamilyDescriptor(DEFAULT_COLUMN_FAMILY_NAME)

template close*(descriptor: var ColFamilyDescriptor) =
descriptor.options.close()
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,40 @@

import
../lib/librocksdb,
./descriptor
./cfdescriptor

type
ColFamilyHandlePtr* = ptr rocksdb_column_family_handle_t

ColFamilyHandleRef* = ref object
handlePtr: ColFamilyHandlePtr
cPtr: ColFamilyHandlePtr

proc newColFamilyHandle*(handlePtr: ColFamilyHandlePtr): ColFamilyHandleRef =
ColFamilyHandleRef(handlePtr: handlePtr)
proc newColFamilyHandle*(cPtr: ColFamilyHandlePtr): ColFamilyHandleRef =
ColFamilyHandleRef(cPtr: cPtr)

template isClosed(handle: ColFamilyHandleRef): bool =
handle.handlePtr.isNil()
handle.cPtr.isNil()

proc cPtr*(handle: ColFamilyHandleRef): ColFamilyHandlePtr =
doAssert not handle.isClosed()
handle.cPtr

proc getId*(handle: ColFamilyHandleRef): int =
doAssert not handle.isClosed()
rocksdb_column_family_handle_get_id(handle.handlePtr).int
rocksdb_column_family_handle_get_id(handle.cPtr).int

proc getName*(handle: ColFamilyHandleRef): string =
doAssert not handle.isClosed()
var nameLen: csize_t # do we need to use this?
$rocksdb_column_family_handle_get_name(handle.handlePtr, nameLen.addr)
$rocksdb_column_family_handle_get_name(handle.cPtr, nameLen.addr)

proc isDefault*(handle: ColFamilyHandleRef): bool =
handle.getName() == DEFAULT_COLUMN_FAMILY
template isDefault*(handle: ColFamilyHandleRef): bool =
handle.getName() == DEFAULT_COLUMN_FAMILY_NAME

# proc getDescriptor*(handle: ColFamilyHandleRef): ColumnFamilyDescriptor =
# doAssert not handle.isClosed()

proc close*(handle: var ColFamilyHandleRef) =
if not handle.isClosed():
rocksdb_column_family_handle_destroy(handle.handlePtr)
handle.handlePtr = nil
rocksdb_column_family_handle_destroy(handle.cPtr)
handle.cPtr = nil
22 changes: 13 additions & 9 deletions rocksdb/columnfamily/cfopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@ import
../lib/librocksdb

type
OptionsPtr = ptr rocksdb_options_t
ColFamilyOptionsPtr* = ptr rocksdb_options_t

ColFamilyOptionsRef* = ref object
optionsPtr: OptionsPtr
cPtr: ColFamilyOptionsPtr

proc newColFamilyOptions*(): ColFamilyOptionsRef =
ColFamilyOptionsRef(optionsPtr: rocksdb_options_create())
ColFamilyOptionsRef(cPtr: rocksdb_options_create())

template isClosed(dbOpts: ColFamilyOptionsRef): bool =
dbOpts.optionsPtr.isNil()
template isClosed(cfOpts: ColFamilyOptionsRef): bool =
cfOpts.cPtr.isNil()

proc cPtr*(cfOpts: ColFamilyOptionsRef): ColFamilyOptionsPtr =
doAssert not cfOpts.isClosed()
cfOpts.cPtr

proc setCreateMissingColumnFamilies*(cfOpts: var ColFamilyOptionsRef, flag: bool) =
doAssert not cfOpts.isClosed()
rocksdb_options_set_create_missing_column_families(cfOpts.optionsPtr, flag.uint8)
rocksdb_options_set_create_missing_column_families(cfOpts.cPtr, flag.uint8)

proc defaultColFamilyOptions*(): ColFamilyOptionsRef =
var opts = newColFamilyOptions()
Expand All @@ -36,10 +40,10 @@ proc defaultColFamilyOptions*(): ColFamilyOptionsRef =

proc getCreateMissingColumnFamilies*(cfOpts: ColFamilyOptionsRef): bool =
doAssert not cfOpts.isClosed()
rocksdb_options_get_create_missing_column_families(cfOpts.optionsPtr).bool
rocksdb_options_get_create_missing_column_families(cfOpts.cPtr).bool

proc close*(cfOpts: var ColFamilyOptionsRef) =
if not cfOpts.isClosed():
rocksdb_options_destroy(cfOpts.optionsPtr)
cfOpts.optionsPtr = nil
rocksdb_options_destroy(cfOpts.cPtr)
cfOpts.cPtr = nil

41 changes: 41 additions & 0 deletions rocksdb/internal/cftable.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Nim-RocksDB
# Copyright 2024 Status Research & Development GmbH
# Licensed under either of
#
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
# * GPL license, version 2.0, ([LICENSE-GPLv2](LICENSE-GPLv2) or https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html)
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.

{.push raises: [].}

import
std/tables,
../columnfamily/cfhandle

type
ColFamilyTableRef* = ref object
columnFamilies: TableRef[string, ColFamilyHandleRef]

proc newColFamilyTableRef*(): ColFamilyTableRef =
ColFamilyTableRef(columnFamilies: newTable[string, ColFamilyHandleRef]())

template isClosed(table: ColFamilyTableRef): bool =
table.columnFamilies.isNil()

proc put*(
table: var ColFamilyTableRef,
name: string,
handle: ColFamilyHandlePtr) =
doAssert not table.isClosed()
table.columnFamilies[name] = newColFamilyHandle(handle)

proc get*(table: ColFamilyTableRef, name: string): ColFamilyHandleRef =
doAssert not table.isClosed()
table.columnFamilies.getOrDefault(name)

proc close*(table: var ColFamilyTableRef) =
if not table.isClosed():
for _, v in table.columnFamilies.mpairs():
v.close()
table.columnFamilies = nil
Empty file removed rocksdb/internal/utils.nim
Empty file.
30 changes: 17 additions & 13 deletions rocksdb/options/dbopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,38 @@ import
../lib/librocksdb

type
OptionsPtr = ptr rocksdb_options_t
DbOptionsPtr* = ptr rocksdb_options_t

DbOptionsRef* = ref object
optionsPtr: OptionsPtr
cPtr: DbOptionsPtr

proc newDbOptions*(): DbOptionsRef =
DbOptionsRef(optionsPtr: rocksdb_options_create())
DbOptionsRef(cPtr: rocksdb_options_create())

template isClosed(dbOpts: DbOptionsRef): bool =
dbOpts.optionsPtr.isNil()
dbOpts.cPtr.isNil()

proc cPtr*(dbOpts: DbOptionsRef): DbOptionsPtr =
doAssert not dbOpts.isClosed()
dbOpts.cPtr

proc setIncreaseParallelism*(dbOpts: var DbOptionsRef, totalThreads: int) =
doAssert totalThreads > 0
doAssert not dbOpts.isClosed()
rocksdb_options_increase_parallelism(dbOpts.optionsPtr, totalThreads.cint)
rocksdb_options_increase_parallelism(dbOpts.cPtr, totalThreads.cint)

proc setCreateIfMissing*(dbOpts: var DbOptionsRef, flag: bool) =
doAssert not dbOpts.isClosed()
rocksdb_options_set_create_if_missing(dbOpts.optionsPtr, flag.uint8)
rocksdb_options_set_create_if_missing(dbOpts.cPtr, flag.uint8)

proc setMaxOpenFiles*(dbOpts: var DbOptionsRef, maxOpenFiles: int) =
doAssert maxOpenFiles > -1
doAssert not dbOpts.isClosed()
rocksdb_options_set_max_open_files(dbOpts.optionsPtr, maxOpenFiles.cint)
rocksdb_options_set_max_open_files(dbOpts.cPtr, maxOpenFiles.cint)

proc setCreateMissingColumnFamilies*(dbOpts: var DbOptionsRef, flag: bool) =
doAssert not dbOpts.isClosed()
rocksdb_options_set_create_missing_column_families(dbOpts.optionsPtr, flag.uint8)
rocksdb_options_set_create_missing_column_families(dbOpts.cPtr, flag.uint8)

proc defaultDbOptions*(): DbOptionsRef =
var opts = newDbOptions()
Expand All @@ -60,18 +64,18 @@ proc defaultDbOptions*(): DbOptionsRef =

proc getCreateIfMissing*(dbOpts: DbOptionsRef): bool =
doAssert not dbOpts.isClosed()
rocksdb_options_get_create_if_missing(dbOpts.optionsPtr).bool
rocksdb_options_get_create_if_missing(dbOpts.cPtr).bool

proc getMaxOpenFiles*(dbOpts: DbOptionsRef): int =
doAssert not dbOpts.isClosed()
rocksdb_options_get_max_open_files(dbOpts.optionsPtr).int
rocksdb_options_get_max_open_files(dbOpts.cPtr).int

proc getCreateMissingColumnFamilies*(dbOpts: DbOptionsRef): bool =
doAssert not dbOpts.isClosed()
rocksdb_options_get_create_missing_column_families(dbOpts.optionsPtr).bool
rocksdb_options_get_create_missing_column_families(dbOpts.cPtr).bool

proc close*(dbOpts: var DbOptionsRef) =
if not dbOpts.isClosed():
rocksdb_options_destroy(dbOpts.optionsPtr)
dbOpts.optionsPtr = nil
rocksdb_options_destroy(dbOpts.cPtr)
dbOpts.cPtr = nil

24 changes: 14 additions & 10 deletions rocksdb/options/readopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,29 @@ import
../lib/librocksdb

type
ReadOptionsPtr = ptr rocksdb_readoptions_t
ReadOptionsPtr* = ptr rocksdb_readoptions_t

ReadOptionsRef* = ref object
readOptsPtr: ReadOptionsPtr
cPtr: ReadOptionsPtr

proc newReadOptions*(): ReadOptionsRef =
ReadOptionsRef(readOptsPtr: rocksdb_readoptions_create())

template defaultReadOptions*(): ReadOptionsRef =
newReadOptions()
ReadOptionsRef(cPtr: rocksdb_readoptions_create())

template isClosed(readOpts: ReadOptionsRef): bool =
readOpts.readOptsPtr.isNil()
readOpts.cPtr.isNil()

proc cPtr*(readOpts: ReadOptionsRef): ReadOptionsPtr =
doAssert not readOpts.isClosed()
readOpts.cPtr

# TODO: Add setters and getters for read options properties.
# Currently we are using the default settings.

template defaultReadOptions*(): ReadOptionsRef =
newReadOptions()
# TODO: set prefered defaults

proc close*(readOpts: var ReadOptionsRef) =
if not readOpts.isClosed():
rocksdb_readoptions_destroy(readOpts.readOptsPtr)
readOpts.readOptsPtr = nil
rocksdb_readoptions_destroy(readOpts.cPtr)
readOpts.cPtr = nil

24 changes: 14 additions & 10 deletions rocksdb/options/writeopts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,29 @@ import
../lib/librocksdb

type
WriteOptionsPtr = ptr rocksdb_writeoptions_t
WriteOptionsPtr* = ptr rocksdb_writeoptions_t

WriteOptionsRef* = ref object
writeOptsPtr: WriteOptionsPtr
cPtr: WriteOptionsPtr

proc newWriteOptions*(): WriteOptionsRef =
WriteOptionsRef(writeOptsPtr: rocksdb_writeoptions_create())

template defaultWriteOptions*(): WriteOptionsRef =
newWriteOptions()
WriteOptionsRef(cPtr: rocksdb_writeoptions_create())

template isClosed(writeOpts: WriteOptionsRef): bool =
writeOpts.writeOptsPtr.isNil()
writeOpts.cPtr.isNil()

proc cPtr*(writeOpts: WriteOptionsRef): WriteOptionsPtr =
doAssert not writeOpts.isClosed()
writeOpts.cPtr

# TODO: Add setters and getters for write options properties.
# Currently we are using the default settings.

template defaultWriteOptions*(): WriteOptionsRef =
newWriteOptions()
# TODO: set prefered defaults

proc close*(writeOpts: var WriteOptionsRef) =
if not writeOpts.isClosed():
rocksdb_writeoptions_destroy(writeOpts.writeOptsPtr)
writeOpts.writeOptsPtr = nil
rocksdb_writeoptions_destroy(writeOpts.cPtr)
writeOpts.cPtr = nil

Loading

0 comments on commit 656e521

Please sign in to comment.