2023-09-12 18:44:45 +00:00
|
|
|
# nimbus-eth1
|
2024-05-07 19:59:27 +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.
|
|
|
|
|
|
|
|
## Kvt DB -- Common functions
|
|
|
|
## ==========================
|
|
|
|
##
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2023-12-20 16:19:00 +00:00
|
|
|
std/tables, eth/common, results, ./kvt_desc/desc_backend, "."/[kvt_desc, kvt_layers]
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2024-05-07 19:59:27 +00:00
|
|
|
# Public functions, converters
|
2023-09-12 18:44:45 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-05-07 19:59:27 +00:00
|
|
|
proc getUbe*(
|
2023-09-12 18:44:45 +00:00
|
|
|
db: KvtDbRef, # Database
|
2023-09-15 15:23:53 +00:00
|
|
|
key: openArray[byte], # Key of database record
|
2023-09-12 18:44:45 +00:00
|
|
|
): Result[Blob, KvtError] =
|
|
|
|
## For the argument `key` return the associated value from the backend
|
|
|
|
## database if available.
|
|
|
|
##
|
|
|
|
let be = db.backend
|
|
|
|
if not be.isNil:
|
|
|
|
return be.getKvpFn key
|
|
|
|
err(GetNotFound)
|
|
|
|
|
2024-06-21 07:44:10 +00:00
|
|
|
proc getUbeLen*(
|
|
|
|
db: KvtDbRef, # Database
|
|
|
|
key: openArray[byte], # Key of database record
|
|
|
|
): Result[int, KvtError] =
|
|
|
|
## For the argument `key` return the associated value from the backend
|
|
|
|
## database if available.
|
|
|
|
##
|
|
|
|
let be = db.backend
|
|
|
|
if not be.isNil:
|
|
|
|
return be.lenKvpFn key
|
|
|
|
err(GetNotFound)
|
|
|
|
|
2024-05-07 19:59:27 +00:00
|
|
|
proc getBe*(
|
|
|
|
db: KvtDbRef, # Database
|
|
|
|
key: openArray[byte], # Key of database record
|
|
|
|
): Result[Blob, KvtError] =
|
|
|
|
## Get the vertex from the (filtered) backened if available.
|
2024-06-13 18:15:11 +00:00
|
|
|
if not db.balancer.isNil:
|
|
|
|
db.balancer.sTab.withValue(@key, w):
|
2024-05-07 19:59:27 +00:00
|
|
|
if w[].len == 0:
|
|
|
|
return err(GetNotFound)
|
|
|
|
return ok(w[])
|
|
|
|
db.getUbe key
|
|
|
|
|
2024-06-21 07:44:10 +00:00
|
|
|
proc getBeLen*(
|
|
|
|
db: KvtDbRef, # Database
|
|
|
|
key: openArray[byte], # Key of database record
|
|
|
|
): Result[int, KvtError] =
|
|
|
|
## Get the vertex from the (filtered) backened if available.
|
|
|
|
if not db.balancer.isNil:
|
|
|
|
db.balancer.sTab.withValue(@key, w):
|
|
|
|
if w[].len == 0:
|
|
|
|
return err(GetNotFound)
|
|
|
|
return ok(w[].len)
|
|
|
|
db.getUbeLen key
|
|
|
|
|
2024-05-07 19:59:27 +00:00
|
|
|
# ------------
|
2023-09-12 18:44:45 +00:00
|
|
|
|
|
|
|
proc put*(
|
|
|
|
db: KvtDbRef, # Database
|
2023-09-15 15:23:53 +00:00
|
|
|
key: openArray[byte], # Key of database record to store
|
|
|
|
data: openArray[byte], # Value of database record to store
|
2023-09-12 18:44:45 +00:00
|
|
|
): Result[void, KvtError] =
|
|
|
|
## For the argument `key` associated the argument `data` as value (which
|
|
|
|
## will be marked in the top layer cache.)
|
|
|
|
if key.len == 0:
|
|
|
|
return err(KeyInvalid)
|
|
|
|
if data.len == 0:
|
|
|
|
return err(DataInvalid)
|
|
|
|
|
2023-12-20 16:19:00 +00:00
|
|
|
db.layersPut(key, data)
|
2023-09-12 18:44:45 +00:00
|
|
|
ok()
|
|
|
|
|
|
|
|
proc del*(
|
|
|
|
db: KvtDbRef, # Database
|
2023-09-15 15:23:53 +00:00
|
|
|
key: openArray[byte], # Key of database record to delete
|
2023-09-12 18:44:45 +00:00
|
|
|
): Result[void, KvtError] =
|
|
|
|
## For the argument `key` delete the associated value (which will be marked
|
|
|
|
## in the top layer cache.)
|
|
|
|
if key.len == 0:
|
|
|
|
return err(KeyInvalid)
|
|
|
|
|
2023-12-20 16:19:00 +00:00
|
|
|
db.layersPut(key, EmptyBlob)
|
2023-09-12 18:44:45 +00:00
|
|
|
ok()
|
|
|
|
|
|
|
|
# ------------
|
|
|
|
|
|
|
|
proc get*(
|
|
|
|
db: KvtDbRef, # Database
|
2023-09-15 15:23:53 +00:00
|
|
|
key: openArray[byte], # Key of database record
|
2023-09-12 18:44:45 +00:00
|
|
|
): Result[Blob, KvtError] =
|
|
|
|
## For the argument `key` return the associated value preferably from the
|
|
|
|
## top layer, or the database otherwise.
|
|
|
|
##
|
|
|
|
if key.len == 0:
|
|
|
|
return err(KeyInvalid)
|
2023-12-19 12:39:23 +00:00
|
|
|
|
2024-05-24 09:27:17 +00:00
|
|
|
var data = db.layersGet(key).valueOr:
|
2024-05-07 19:59:27 +00:00
|
|
|
return db.getBe key
|
2023-12-19 12:39:23 +00:00
|
|
|
|
2024-05-24 09:27:17 +00:00
|
|
|
return ok(move(data))
|
2023-09-12 18:44:45 +00:00
|
|
|
|
2024-06-21 07:44:10 +00:00
|
|
|
proc len*(
|
|
|
|
db: KvtDbRef, # Database
|
|
|
|
key: openArray[byte], # Key of database record
|
|
|
|
): Result[int, KvtError] =
|
2024-07-12 13:12:25 +00:00
|
|
|
## For the argument `key` return the length of the associated value,
|
|
|
|
## preferably from the top layer, or the database otherwise.
|
2024-06-21 07:44:10 +00:00
|
|
|
##
|
|
|
|
if key.len == 0:
|
|
|
|
return err(KeyInvalid)
|
|
|
|
|
|
|
|
let len = db.layersLen(key).valueOr:
|
|
|
|
return db.getBeLen key
|
|
|
|
ok(len)
|
2023-12-19 12:39:23 +00:00
|
|
|
|
2023-10-27 21:36:51 +00:00
|
|
|
proc hasKey*(
|
2023-09-26 09:21:13 +00:00
|
|
|
db: KvtDbRef, # Database
|
|
|
|
key: openArray[byte], # Key of database record
|
|
|
|
): Result[bool, KvtError] =
|
|
|
|
## For the argument `key` return the associated value preferably from the
|
|
|
|
## top layer, or the database otherwise.
|
|
|
|
##
|
|
|
|
if key.len == 0:
|
|
|
|
return err(KeyInvalid)
|
2023-12-19 12:39:23 +00:00
|
|
|
|
2024-06-11 09:38:58 +00:00
|
|
|
if db.layersHasKey key:
|
2023-09-26 09:21:13 +00:00
|
|
|
return ok(true)
|
2023-12-19 12:39:23 +00:00
|
|
|
|
2024-05-07 19:59:27 +00:00
|
|
|
let rc = db.getBe key
|
2023-09-26 09:21:13 +00:00
|
|
|
if rc.isOk:
|
|
|
|
return ok(true)
|
|
|
|
if rc.error == GetNotFound:
|
|
|
|
return ok(false)
|
|
|
|
err(rc.error)
|
|
|
|
|
2023-09-12 18:44:45 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|