Files
nim-rocksdb/rocksdb/writebatch.nim
T

99 lines
2.9 KiB
Nim

# 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 `WriteBatchRef` 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.
{.push raises: [].}
import ./lib/librocksdb, ./internal/[cftable, utils], ./rocksresult
export rocksresult
type
WriteBatchPtr* = ptr rocksdb_writebatch_t
WriteBatchRef* = ref object
cPtr: WriteBatchPtr
defaultCfHandle: ColFamilyHandleRef
proc createWriteBatch*(defaultCfHandle: ColFamilyHandleRef): WriteBatchRef =
WriteBatchRef(cPtr: rocksdb_writebatch_create(), defaultCfHandle: defaultCfHandle)
template isClosed*(batch: WriteBatchRef): bool =
## Returns `true` if the `WriteBatchRef` has been closed and `false` otherwise.
batch.cPtr.isNil()
proc cPtr*(batch: WriteBatchRef): WriteBatchPtr =
## Get the underlying database pointer.
doAssert not batch.isClosed()
batch.cPtr
proc clear*(batch: WriteBatchRef) =
## Clears the write batch.
doAssert not batch.isClosed()
rocksdb_writebatch_clear(batch.cPtr)
proc count*(batch: WriteBatchRef): int =
## Get the number of updates in the write batch.
doAssert not batch.isClosed()
rocksdb_writebatch_count(batch.cPtr).int
proc put*(
batch: WriteBatchRef, key, val: openArray[byte], cfHandle = batch.defaultCfHandle
): RocksDBResult[void] =
## Add a put operation to the write batch.
rocksdb_writebatch_put_cf(
batch.cPtr,
cfHandle.cPtr,
cast[cstring](key.unsafeAddrOrNil()),
csize_t(key.len),
cast[cstring](val.unsafeAddrOrNil()),
csize_t(val.len),
)
ok()
proc delete*(
batch: WriteBatchRef, key: openArray[byte], cfHandle = batch.defaultCfHandle
): RocksDBResult[void] =
## Add a delete operation to the write batch.
rocksdb_writebatch_delete_cf(
batch.cPtr, cfHandle.cPtr, cast[cstring](key.unsafeAddrOrNil()), csize_t(key.len)
)
ok()
proc deleteRange*(
batch: WriteBatchRef,
startKey, endKey: openArray[byte],
cfHandle = batch.defaultCfHandle,
): RocksDBResult[void] =
## Add a delete range operation to the write batch.
rocksdb_writebatch_delete_range_cf(
batch.cPtr,
cfHandle.cPtr,
cast[cstring](startKey.unsafeAddrOrNil()),
csize_t(startKey.len),
cast[cstring](endKey.unsafeAddrOrNil()),
csize_t(endKey.len),
)
ok()
proc close*(batch: WriteBatchRef) =
## Close the `WriteBatchRef`.
if not batch.isClosed():
rocksdb_writebatch_destroy(batch.cPtr)
batch.cPtr = nil