-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for write batch with index.
- Loading branch information
Showing
7 changed files
with
439 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
# 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. | ||
|
||
## A `WriteBatchWIRef` holds a collection of updates to apply atomically to the database. | ||
## It depends on resources from an instance of `RocksDbRef' and therefore should be used | ||
## and closed before the `RocksDbRef` is closed. | ||
## | ||
## `WriteBatchWIRef` is similar to `WriteBatchRef` but with a binary searchable index | ||
## built for all the keys inserted which allows reading the data which has been writen | ||
## to the batch. | ||
|
||
{.push raises: [].} | ||
|
||
import ./lib/librocksdb, ./internal/[cftable, utils], ./options/dbopts, ./rocksresult | ||
|
||
export rocksresult | ||
|
||
type | ||
WriteBatchWIPtr* = ptr rocksdb_writebatch_wi_t | ||
|
||
WriteBatchWIRef* = ref object | ||
cPtr: WriteBatchWIPtr | ||
dbOpts: DbOptionsRef | ||
defaultCfHandle: ColFamilyHandleRef | ||
|
||
proc createWriteBatch*( | ||
reservedBytes: int, | ||
overwriteKey: bool, | ||
dbOpts: DbOptionsRef, | ||
defaultCfHandle: ColFamilyHandleRef, | ||
): WriteBatchWIRef = | ||
WriteBatchWIRef( | ||
cPtr: rocksdb_writebatch_wi_create(reservedBytes.csize_t, overwriteKey.uint8), | ||
dbOpts: dbOpts, | ||
defaultCfHandle: defaultCfHandle, | ||
) | ||
|
||
proc isClosed*(batch: WriteBatchWIRef): bool {.inline.} = | ||
## Returns `true` if the `WriteBatchWIRef` has been closed and `false` otherwise. | ||
batch.cPtr.isNil() | ||
|
||
proc cPtr*(batch: WriteBatchWIRef): WriteBatchWIPtr = | ||
## Get the underlying database pointer. | ||
doAssert not batch.isClosed() | ||
batch.cPtr | ||
|
||
proc clear*(batch: WriteBatchWIRef) = | ||
## Clears the write batch. | ||
doAssert not batch.isClosed() | ||
rocksdb_writebatch_wi_clear(batch.cPtr) | ||
|
||
proc count*(batch: WriteBatchWIRef): int = | ||
## Get the number of updates in the write batch. | ||
doAssert not batch.isClosed() | ||
rocksdb_writebatch_wi_count(batch.cPtr).int | ||
|
||
proc put*( | ||
batch: WriteBatchWIRef, key, val: openArray[byte], cfHandle = batch.defaultCfHandle | ||
): RocksDBResult[void] = | ||
## Add a put operation to the write batch. | ||
|
||
if key.len() == 0: | ||
return err("rocksdb: key is empty") | ||
|
||
rocksdb_writebatch_wi_put_cf( | ||
batch.cPtr, | ||
cfHandle.cPtr, | ||
cast[cstring](unsafeAddr key[0]), | ||
csize_t(key.len), | ||
cast[cstring](if val.len > 0: | ||
unsafeAddr val[0] | ||
else: | ||
nil | ||
), | ||
csize_t(val.len), | ||
) | ||
|
||
ok() | ||
|
||
proc delete*( | ||
batch: WriteBatchWIRef, key: openArray[byte], cfHandle = batch.defaultCfHandle | ||
): RocksDBResult[void] = | ||
## Add a delete operation to the write batch. | ||
|
||
if key.len() == 0: | ||
return err("rocksdb: key is empty") | ||
|
||
rocksdb_writebatch_wi_delete_cf( | ||
batch.cPtr, cfHandle.cPtr, cast[cstring](unsafeAddr key[0]), csize_t(key.len) | ||
) | ||
|
||
ok() | ||
|
||
proc get*( | ||
batch: WriteBatchWIRef, | ||
key: openArray[byte], | ||
onData: DataProc, | ||
cfHandle = batch.defaultCfHandle, | ||
): RocksDBResult[bool] = | ||
## Get the value for a given key from the batch using the provided | ||
## `onData` callback. | ||
|
||
if key.len() == 0: | ||
return err("rocksdb: key is empty") | ||
|
||
var | ||
len: csize_t | ||
errors: cstring | ||
let data = rocksdb_writebatch_wi_get_from_batch_cf( | ||
batch.cPtr, | ||
batch.dbOpts.cPtr, | ||
cfHandle.cPtr, | ||
cast[cstring](unsafeAddr key[0]), | ||
csize_t(key.len), | ||
len.addr, | ||
cast[cstringArray](errors.addr), | ||
) | ||
bailOnErrors(errors) | ||
|
||
if data.isNil(): | ||
doAssert len == 0 | ||
ok(false) | ||
else: | ||
onData(toOpenArrayByte(data, 0, len.int - 1)) | ||
rocksdb_free(data) | ||
ok(true) | ||
|
||
proc get*( | ||
batch: WriteBatchWIRef, key: openArray[byte], cfHandle = batch.defaultCfHandle | ||
): RocksDBResult[seq[byte]] = | ||
## Get the value for a given key from the batch. | ||
|
||
var dataRes: RocksDBResult[seq[byte]] | ||
proc onData(data: openArray[byte]) = | ||
dataRes.ok(@data) | ||
|
||
let res = batch.get(key, onData, cfHandle) | ||
if res.isOk(): | ||
return dataRes | ||
|
||
dataRes.err(res.error()) | ||
|
||
proc close*(batch: WriteBatchWIRef) = | ||
## Close the `WriteBatchWIRef`. | ||
if not batch.isClosed(): | ||
rocksdb_writebatch_wi_destroy(batch.cPtr) | ||
batch.cPtr = nil |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.