2023-09-12 18:44:45 +00:00
|
|
|
# nimbus-eth1
|
2024-03-14 22:17:43 +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.
|
|
|
|
|
|
|
|
## Non persistent constructors for Kvt DB
|
|
|
|
## ======================================
|
|
|
|
##
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
|
|
|
../kvt_desc,
|
|
|
|
../kvt_desc/desc_backend,
|
|
|
|
"."/[init_common, memory_db]
|
|
|
|
|
|
|
|
type
|
|
|
|
VoidBackendRef* = ref object of TypedBackendRef
|
2023-09-15 15:23:53 +00:00
|
|
|
## Dummy descriptor type, used as `nil` reference
|
|
|
|
|
|
|
|
MemOnlyBackend* = VoidBackendRef|MemBackendRef
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
export
|
|
|
|
BackendType,
|
|
|
|
MemBackendRef
|
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public helpers
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
2024-06-13 18:15:11 +00:00
|
|
|
func kind*(
|
2023-09-15 15:23:53 +00:00
|
|
|
be: BackendRef;
|
|
|
|
): BackendType =
|
|
|
|
## Retrieves the backend type symbol for a `be` backend database argument
|
|
|
|
## where `BackendVoid` is returned for the`nil` backend.
|
|
|
|
if be.isNil:
|
|
|
|
BackendVoid
|
|
|
|
else:
|
|
|
|
be.TypedBackendRef.beKind
|
|
|
|
|
2023-09-12 18:44:45 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public database constuctors, destructor
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
proc init*(
|
|
|
|
T: type KvtDbRef; # Target type
|
|
|
|
B: type MemOnlyBackend; # Backend type
|
|
|
|
): T =
|
|
|
|
## Memory backend constructor.
|
2023-09-12 18:44:45 +00:00
|
|
|
##
|
2023-09-15 15:23:53 +00:00
|
|
|
when B is VoidBackendRef:
|
2024-05-07 19:59:27 +00:00
|
|
|
KvtDbRef(top: LayerRef.init())
|
2023-09-12 18:44:45 +00:00
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
elif B is MemBackendRef:
|
2024-05-07 19:59:27 +00:00
|
|
|
KvtDbRef(top: LayerRef.init(), backend: memoryBackend())
|
2023-09-12 18:44:45 +00:00
|
|
|
|
2023-09-15 15:23:53 +00:00
|
|
|
proc init*(
|
|
|
|
T: type KvtDbRef; # Target type
|
|
|
|
): T =
|
|
|
|
## Shortcut for `KvtDbRef.init(VoidBackendRef)`
|
|
|
|
KvtDbRef.init VoidBackendRef
|
|
|
|
|
2023-09-12 18:44:45 +00:00
|
|
|
|
2024-06-14 11:19:48 +00:00
|
|
|
proc finish*(db: KvtDbRef; eradicate = false) =
|
|
|
|
## Backend destructor. The argument `eradicate` indicates that a full
|
|
|
|
## database deletion is requested. If set `false` the outcome might differ
|
|
|
|
## depending on the type of backend (e.g. the `BackendMemory` backend will
|
|
|
|
## always eradicate on close.)
|
2023-09-12 18:44:45 +00:00
|
|
|
##
|
2024-03-15 14:20:00 +00:00
|
|
|
if not db.backend.isNil:
|
2024-06-14 11:19:48 +00:00
|
|
|
db.backend.closeFn eradicate
|
2024-03-15 14:20:00 +00:00
|
|
|
discard db.getCentre.forgetOthers()
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|