Skip to content

Commit

Permalink
Format using nph (#54)
Browse files Browse the repository at this point in the history
* Format entire project using nph.

* Add nph lint to CI.
  • Loading branch information
bhartnett authored Jun 26, 2024
1 parent 45f7a92 commit a691d5b
Show file tree
Hide file tree
Showing 47 changed files with 5,753 additions and 3,076 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ on:
workflow_dispatch:

jobs:
lint:
name: "nph Lint"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 2 # In PR, has extra merge commit: ^1 = PR, ^2 = base

- name: Check nph formatting
# Pin nph to a specific version to avoid sudden style differences.
# Updating nph version should be accompanied with running the new
# version on the project directory.
run: |
VERSION="v0.5.1"
ARCHIVE="nph-linux_x64.tar.gz"
curl -L "https://github.com/arnetheduck/nph/releases/download/${VERSION}/${ARCHIVE}" -o ${ARCHIVE}
tar -xzf ${ARCHIVE}
./nph .
git diff --exit-code
build:
strategy:
fail-fast: false
Expand Down
6 changes: 4 additions & 2 deletions config.nims
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ when defined(rocksdb_static_linking):
switch("dynlibOverride", "lz4")
switch("dynlibOverride", "zstd")

--styleCheck:usages
--styleCheck:error
--styleCheck:
usages
--styleCheck:
error
32 changes: 23 additions & 9 deletions examples/simple_example.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ proc main() =
# snappy support (for example Fedora 28, certain Ubuntu versions)
# rocksdb_options_optimize_level_style_compaction(options, 0);
# create the DB if it's not already present
rocksdb_options_set_create_if_missing(options, 1);
rocksdb_options_set_create_if_missing(options, 1)

# open DB
var err: cstring # memory leak: example code does not free error string!
var # open DB
err: cstring # memory leak: example code does not free error string!
db = rocksdb_open(options, dbPath, cast[cstringArray](err.addr))
doAssert err.isNil, $err

Expand All @@ -32,15 +32,28 @@ proc main() =
var writeOptions = rocksdb_writeoptions_create()
let key = "key"
let put_value = "value"
rocksdb_put(db, writeOptions, key.cstring, key.len.csize_t, put_value.cstring,
put_value.len.csize_t, cast[cstringArray](err.addr))
rocksdb_put(
db,
writeOptions,
key.cstring,
key.len.csize_t,
put_value.cstring,
put_value.len.csize_t,
cast[cstringArray](err.addr),
)
doAssert err.isNil, $err

# Get value
var readOptions = rocksdb_readoptions_create()
var len: csize_t
let raw_value = rocksdb_get(db, readOptions, key.cstring, key.len.csize_t, addr len,
cast[cstringArray](err.addr)) # Important: rocksdb_get is not null-terminated
let raw_value = rocksdb_get(
db,
readOptions,
key.cstring,
key.len.csize_t,
addr len,
cast[cstringArray](err.addr),
) # Important: rocksdb_get is not null-terminated
doAssert err.isNil, $err

# Copy it to a regular Nim string (copyMem workaround because raw value is NOT null-terminated)
Expand All @@ -57,8 +70,9 @@ proc main() =

# If something is wrong, you might want to restore data from last backup
var restoreOptions = rocksdb_restore_options_create()
rocksdb_backup_engine_restore_db_from_latest_backup(be, dbPath, dbPath,
restoreOptions, cast[cstringArray](err.addr))
rocksdb_backup_engine_restore_db_from_latest_backup(
be, dbPath, dbPath, restoreOptions, cast[cstringArray](err.addr)
)
doAssert err.isNil, $err
rocksdb_restore_options_destroy(restore_options)

Expand Down
19 changes: 5 additions & 14 deletions rocksdb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,10 @@
# at your option. This file may not be copied, modified, or distributed except according to those terms.

import
./rocksdb/[backup,
columnfamily,
rocksdb,
rocksiterator,
sstfilewriter,
transactiondb,
writebatch]
./rocksdb/[
backup, columnfamily, rocksdb, rocksiterator, sstfilewriter, transactiondb,
writebatch,
]

export
backup,
columnfamily,
rocksdb,
rocksiterator,
sstfilewriter,
transactiondb,
writebatch
backup, columnfamily, rocksdb, rocksiterator, sstfilewriter, transactiondb, writebatch
27 changes: 16 additions & 11 deletions rocksdb.nimble
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
packageName = "rocksdb"
version = "0.4.0"
author = "Status Research & Development GmbH"
description = "A wrapper for Facebook's RocksDB, an embeddable, persistent key-value store for fast storage"
license = "Apache License 2.0 or GPLv2"
skipDirs = @["examples", "tests"]
mode = ScriptMode.Verbose
packageName = "rocksdb"
version = "0.4.0"
author = "Status Research & Development GmbH"
description =
"A wrapper for Facebook's RocksDB, an embeddable, persistent key-value store for fast storage"
license = "Apache License 2.0 or GPLv2"
skipDirs = @["examples", "tests"]
mode = ScriptMode.Verbose

### Dependencies
requires "nim >= 1.6",
"results",
"tempfile",
"unittest2"
requires "nim >= 1.6", "results", "tempfile", "unittest2"

# Format only works with nim version 2
task format, "Format nim code using nph":
# Using the latest nph commit for now because the latest tagged version
# doesn't work with the latest nim 2 version
exec "nimble install nph@#head"
exec "nph ."

task clean, "Remove temporary files":
exec "rm -rf build"
Expand Down
44 changes: 16 additions & 28 deletions rocksdb/backup.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,9 @@
{.push raises: [].}

import
./lib/librocksdb,
./internal/utils,
./options/backupopts,
./rocksdb,
./rocksresult
./lib/librocksdb, ./internal/utils, ./options/backupopts, ./rocksdb, ./rocksresult

export
backupopts,
rocksdb,
rocksresult
export backupopts, rocksdb, rocksresult

type
BackupEnginePtr* = ptr rocksdb_backup_engine_t
Expand All @@ -32,50 +25,44 @@ type
backupOpts: BackupEngineOptionsRef

proc openBackupEngine*(
path: string,
backupOpts = defaultBackupEngineOptions()): RocksDBResult[BackupEngineRef] =
path: string, backupOpts = defaultBackupEngineOptions()
): RocksDBResult[BackupEngineRef] =
## Create a new backup engine. The `path` parameter is the path of the backup
## directory. Note that the same directory should not be used for both backups
## and the database itself.

var errors: cstring
let backupEnginePtr = rocksdb_backup_engine_open(
backupOpts.cPtr,
path.cstring,
cast[cstringArray](errors.addr))
backupOpts.cPtr, path.cstring, cast[cstringArray](errors.addr)
)
bailOnErrors(errors)

let engine = BackupEngineRef(
cPtr: backupEnginePtr,
path: path,
backupOpts: backupOpts)
let engine =
BackupEngineRef(cPtr: backupEnginePtr, path: path, backupOpts: backupOpts)
ok(engine)

proc isClosed*(backupEngine: BackupEngineRef): bool {.inline.} =
## Returns `true` if the `BackupEngineRef` has been closed.
backupEngine.cPtr.isNil()

proc createNewBackup*(
backupEngine: BackupEngineRef,
db: RocksDbRef): RocksDBResult[void] =
backupEngine: BackupEngineRef, db: RocksDbRef
): RocksDBResult[void] =
## Create a new backup of the database.
doAssert not backupEngine.isClosed()
doAssert not db.isClosed()

var errors: cstring
rocksdb_backup_engine_create_new_backup(
backupEngine.cPtr,
db.cPtr,
cast[cstringArray](errors.addr))
backupEngine.cPtr, db.cPtr, cast[cstringArray](errors.addr)
)
bailOnErrors(errors)

ok()

proc restoreDbFromLatestBackup*(
backupEngine: BackupEngineRef,
dbDir: string,
walDir = dbDir,
keepLogFiles = false): RocksDBResult[void] =
backupEngine: BackupEngineRef, dbDir: string, walDir = dbDir, keepLogFiles = false
): RocksDBResult[void] =
## Restore the database from the latest backup.
doAssert not backupEngine.isClosed()

Expand All @@ -88,7 +75,8 @@ proc restoreDbFromLatestBackup*(
dbDir.cstring,
walDir.cstring,
restoreOptions,
cast[cstringArray](errors.addr))
cast[cstringArray](errors.addr),
)
bailOnErrors(errors)

rocksdb_restore_options_destroy(restoreOptions)
Expand Down
39 changes: 19 additions & 20 deletions rocksdb/columnfamily.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

{.push raises: [].}

import
./rocksdb
import ./rocksdb

export rocksdb

Expand All @@ -34,8 +33,8 @@ type
name: string

proc withColFamily*(
db: RocksDbReadOnlyRef,
name: string): RocksDBResult[ColFamilyReadOnly] =
db: RocksDbReadOnlyRef, name: string
): RocksDBResult[ColFamilyReadOnly] =
## Creates a new `ColFamilyReadOnly` from the given `RocksDbReadOnlyRef` and
## column family name.

Expand All @@ -46,8 +45,8 @@ proc withColFamily*(
ok(ColFamilyReadOnly(db: db, name: name))

proc withColFamily*(
db: RocksDbReadWriteRef,
name: string): RocksDBResult[ColFamilyReadWrite] =
db: RocksDbReadWriteRef, name: string
): RocksDBResult[ColFamilyReadWrite] =
## Create a new `ColFamilyReadWrite` from the given `RocksDbReadWriteRef` and
## column family name.

Expand All @@ -66,39 +65,39 @@ proc name*(cf: ColFamilyReadOnly | ColFamilyReadWrite): string {.inline.} =
cf.name

proc get*(
cf: ColFamilyReadOnly | ColFamilyReadWrite,
key: openArray[byte],
onData: DataProc): RocksDBResult[bool] {.inline.} =
cf: ColFamilyReadOnly | ColFamilyReadWrite, key: openArray[byte], onData: DataProc
): RocksDBResult[bool] {.inline.} =
## Gets the value of the given key from the column family using the `onData`
## callback.
cf.db.get(key, onData, cf.name)

proc get*(
cf: ColFamilyReadOnly | ColFamilyReadWrite,
key: openArray[byte]): RocksDBResult[seq[byte]] {.inline.} =
cf: ColFamilyReadOnly | ColFamilyReadWrite, key: openArray[byte]
): RocksDBResult[seq[byte]] {.inline.} =
## Gets the value of the given key from the column family.
cf.db.get(key, cf.name)

proc put*(
cf: ColFamilyReadWrite,
key, val: openArray[byte]): RocksDBResult[void] {.inline.} =
cf: ColFamilyReadWrite, key, val: openArray[byte]
): RocksDBResult[void] {.inline.} =
## Puts a value for the given key into the column family.
cf.db.put(key, val, cf.name)

proc keyExists*(
cf: ColFamilyReadOnly | ColFamilyReadWrite,
key: openArray[byte]): RocksDBResult[bool] {.inline.} =
cf: ColFamilyReadOnly | ColFamilyReadWrite, key: openArray[byte]
): RocksDBResult[bool] {.inline.} =
## Checks if the given key exists in the column family.
cf.db.keyExists(key, cf.name)

proc delete*(
cf: ColFamilyReadWrite,
key: openArray[byte]): RocksDBResult[void] {.inline.} =
cf: ColFamilyReadWrite, key: openArray[byte]
): RocksDBResult[void] {.inline.} =
## Deletes the given key from the column family.
cf.db.delete(key, cf.name)

proc openIterator*(
cf: ColFamilyReadOnly | ColFamilyReadWrite): RocksDBResult[RocksIteratorRef] {.inline.} =
cf: ColFamilyReadOnly | ColFamilyReadWrite
): RocksDBResult[RocksIteratorRef] {.inline.} =
## Opens an `RocksIteratorRef` for the given column family.
cf.db.openIterator(cf.name)

Expand All @@ -107,7 +106,7 @@ proc openWriteBatch*(cf: ColFamilyReadWrite): WriteBatchRef {.inline.} =
cf.db.openWriteBatch(cf.name)

proc write*(
cf: ColFamilyReadWrite,
updates: WriteBatchRef): RocksDBResult[void] {.inline.} =
cf: ColFamilyReadWrite, updates: WriteBatchRef
): RocksDBResult[void] {.inline.} =
## Writes the updates in the `WriteBatchRef` to the column family.
cf.db.write(updates)
15 changes: 6 additions & 9 deletions rocksdb/columnfamily/cfdescriptor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,17 @@

{.push raises: [].}

import
../internal/utils,
./cfopts
import ../internal/utils, ./cfopts

export cfopts

type
ColFamilyDescriptor* = object
name: string
options: ColFamilyOptionsRef
type ColFamilyDescriptor* = object
name: string
options: ColFamilyOptionsRef

proc initColFamilyDescriptor*(
name: string,
options = defaultColFamilyOptions()): ColFamilyDescriptor =
name: string, options = defaultColFamilyOptions()
): ColFamilyDescriptor =
ColFamilyDescriptor(name: name, options: options)

proc name*(descriptor: ColFamilyDescriptor): string {.inline.} =
Expand Down
3 changes: 1 addition & 2 deletions rocksdb/columnfamily/cfhandle.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

{.push raises: [].}

import
../lib/librocksdb
import ../lib/librocksdb

type
ColFamilyHandlePtr* = ptr rocksdb_column_family_handle_t
Expand Down
Loading

0 comments on commit a691d5b

Please sign in to comment.