2023-09-12 18:44:45 +00:00
|
|
|
# nimbus-eth1
|
2024-04-16 20:39:11 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-09-12 18:44:45 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed
|
|
|
|
# except according to those terms.
|
|
|
|
|
|
|
|
## Persistent constructor for Kvt DB
|
|
|
|
## ====================================
|
|
|
|
##
|
|
|
|
## This module automatically pulls in the persistent backend library at the
|
|
|
|
## linking stage (e.g. `rocksdb`) which can be avoided for pure memory DB
|
|
|
|
## applications by importing `./kvt_init/memory_only` (rather than
|
|
|
|
## `./kvt_init/persistent`.)
|
|
|
|
##
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2024-06-19 08:55:57 +00:00
|
|
|
rocksdb,
|
2023-09-12 18:44:45 +00:00
|
|
|
results,
|
2024-06-13 18:15:11 +00:00
|
|
|
../../aristo,
|
|
|
|
../../opts,
|
2023-09-12 18:44:45 +00:00
|
|
|
../kvt_desc,
|
2023-09-15 15:23:53 +00:00
|
|
|
"."/[rocks_db, memory_only]
|
2024-04-16 20:39:11 +00:00
|
|
|
|
2023-09-12 18:44:45 +00:00
|
|
|
export
|
|
|
|
RdbBackendRef,
|
|
|
|
memory_only
|
|
|
|
|
2024-06-13 18:15:11 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func toErr0(err: (KvtError,string)): KvtError =
|
|
|
|
err[0]
|
|
|
|
|
2023-09-12 18:44:45 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public database constuctors, destructor
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-06-13 18:15:11 +00:00
|
|
|
proc init*(
|
2023-09-15 15:23:53 +00:00
|
|
|
T: type KvtDbRef;
|
2024-06-13 18:15:11 +00:00
|
|
|
B: type RdbBackendRef;
|
2023-09-12 18:44:45 +00:00
|
|
|
basePath: string;
|
2024-06-19 08:55:57 +00:00
|
|
|
dbOpts: DbOptionsRef;
|
|
|
|
cfOpts: ColFamilyOptionsRef;
|
2023-09-12 18:44:45 +00:00
|
|
|
): Result[KvtDbRef,KvtError] =
|
2024-06-13 18:15:11 +00:00
|
|
|
## Generic constructor for `RocksDb` backend
|
|
|
|
##
|
|
|
|
ok KvtDbRef(
|
|
|
|
top: LayerRef.init(),
|
2024-06-19 08:55:57 +00:00
|
|
|
backend: ? rocksDbKvtBackend(basePath, dbOpts, cfOpts).mapErr toErr0)
|
2024-06-13 18:15:11 +00:00
|
|
|
|
|
|
|
proc init*(
|
|
|
|
T: type KvtDbRef;
|
|
|
|
B: type RdbBackendRef;
|
|
|
|
adb: AristoDbRef;
|
2024-06-19 08:55:57 +00:00
|
|
|
oCfs: openArray[ColFamilyReadWrite];
|
2024-06-13 18:15:11 +00:00
|
|
|
): Result[KvtDbRef,KvtError] =
|
|
|
|
## Constructor for `RocksDb` backend which piggybacks on the `Aristo`
|
|
|
|
## backend. The following changes will occur after successful instantiation:
|
|
|
|
##
|
|
|
|
## * When invoked, the function `kvt_tx.persistent()` will always return an
|
|
|
|
## error. If everything is all right (e.g. saving is possible), the error
|
|
|
|
## returned will be `TxPersistDelayed`. This indicates that the save
|
|
|
|
## request was queued, waiting for being picked up by an event handler.
|
|
|
|
##
|
|
|
|
## * There should be an invocation of `aristo_tx.persistent()` immediately
|
|
|
|
## follwing the `kvt_tx.persistent()` call (some `KVT` functions might
|
|
|
|
## return `RdbBeDelayedLocked` or similar errors while the save request
|
|
|
|
## is pending.) Once successful, the`aristo_tx.persistent()` function will
|
|
|
|
## also have commited the pending save request mentioned above.
|
|
|
|
##
|
|
|
|
## * The function `kvt_init/memory_only.finish()` does nothing.
|
|
|
|
##
|
|
|
|
## * The function `aristo_init/memory_only.finish()` will close both
|
|
|
|
## sessions, the one for `KVT` and the other for `Aristo`.
|
2023-09-12 18:44:45 +00:00
|
|
|
##
|
2024-06-13 18:15:11 +00:00
|
|
|
## * The functiond `kvt_delta.deltaUpdate()` and `tx_stow.tcStow()` should
|
|
|
|
## not be invoked directly (they will stop with an error most of the time,
|
|
|
|
## anyway.)
|
2024-04-16 20:39:11 +00:00
|
|
|
##
|
2024-06-13 18:15:11 +00:00
|
|
|
ok KvtDbRef(
|
|
|
|
top: LayerRef.init(),
|
2024-06-19 08:55:57 +00:00
|
|
|
backend: ? rocksDbKvtTriggeredBackend(adb, oCfs).mapErr toErr0)
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|