2023-06-20 13:26:25 +00:00
|
|
|
# nimbus-eth1
|
|
|
|
# Copyright (c) 2021 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)
|
|
|
|
# * 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.
|
|
|
|
|
|
|
|
## Rocks DB store data iterator
|
|
|
|
## ============================
|
|
|
|
##
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/sequtils,
|
|
|
|
eth/common,
|
|
|
|
rocksdb,
|
|
|
|
../aristo_init_common,
|
|
|
|
./rdb_desc
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2023-08-22 18:44:54 +00:00
|
|
|
# Private helpers
|
2023-06-20 13:26:25 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func keyPfx(kData: cstring, kLen: csize_t): int =
|
2023-08-22 18:44:54 +00:00
|
|
|
if not kData.isNil and kLen == 1 + sizeof(uint64):
|
2023-06-20 13:26:25 +00:00
|
|
|
kData.toOpenArrayByte(0,0)[0].int
|
|
|
|
else:
|
|
|
|
-1
|
|
|
|
|
2023-08-18 19:46:55 +00:00
|
|
|
func keyXid(kData: cstring, kLen: csize_t): uint64 =
|
2023-08-22 18:44:54 +00:00
|
|
|
if not kData.isNil and kLen == 1 + sizeof(uint64):
|
2023-08-18 19:46:55 +00:00
|
|
|
return uint64.fromBytesBE kData.toOpenArrayByte(1,int(kLen)-1).toSeq
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
func valBlob(vData: cstring, vLen: csize_t): Blob =
|
|
|
|
if not vData.isNil and 0 < vLen:
|
|
|
|
return vData.toOpenArrayByte(0,int(vLen)-1).toSeq
|
|
|
|
|
2023-08-22 18:44:54 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private debugging helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc pp(kd: cstring, kl: csize_t): string =
|
|
|
|
if kd.isNil: "n/a" else: $kd.keyXid(kl)
|
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public iterators
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
iterator walk*(
|
|
|
|
rdb: RdbInst;
|
2023-08-18 19:46:55 +00:00
|
|
|
): tuple[n: int, pfx: StorageType, xid: uint64, data: Blob] =
|
2023-06-20 13:26:25 +00:00
|
|
|
## Walk over all key-value pairs of the database.
|
|
|
|
##
|
|
|
|
## Non-decodable entries are stepped over while the counter `n` of the
|
|
|
|
## yield record is still incremented.
|
|
|
|
let rit = rdb.store.db.rocksdb_create_iterator(rdb.store.readOptions)
|
|
|
|
defer: rit.rocksdb_iter_destroy()
|
|
|
|
|
|
|
|
rit.rocksdb_iter_seek_to_first()
|
|
|
|
var count = 0
|
|
|
|
|
|
|
|
while rit.rocksdb_iter_valid() != 0:
|
|
|
|
var kLen: csize_t
|
|
|
|
let kData = rit.rocksdb_iter_key(addr kLen)
|
|
|
|
|
|
|
|
let pfx = kData.keyPfx(kLen)
|
|
|
|
if 0 <= pfx:
|
2023-08-18 19:46:55 +00:00
|
|
|
if high(StorageType).ord < pfx:
|
2023-06-20 13:26:25 +00:00
|
|
|
break
|
|
|
|
|
2023-08-18 19:46:55 +00:00
|
|
|
let xid = kData.keyXid(kLen)
|
|
|
|
if 0 < xid:
|
2023-06-20 13:26:25 +00:00
|
|
|
var vLen: csize_t
|
|
|
|
let vData = rit.rocksdb_iter_value(addr vLen)
|
|
|
|
|
|
|
|
let val = vData.valBlob(vLen)
|
|
|
|
if 0 < val.len:
|
2023-08-18 19:46:55 +00:00
|
|
|
yield (count, pfx.StorageType, xid, val)
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
# Update Iterator (might overwrite kData/vdata)
|
|
|
|
rit.rocksdb_iter_next()
|
|
|
|
count.inc
|
|
|
|
# End while
|
|
|
|
|
2023-08-22 18:44:54 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
iterator walk*(
|
|
|
|
rdb: RdbInst;
|
2023-08-18 19:46:55 +00:00
|
|
|
pfx: StorageType;
|
|
|
|
): tuple[n: int, xid: uint64, data: Blob] =
|
2023-08-22 18:44:54 +00:00
|
|
|
## Walk over key-value pairs of the table referted to by the argument `pfx`
|
|
|
|
## whic must be different from `Oops` and `AdmPfx`.
|
2023-06-20 13:26:25 +00:00
|
|
|
##
|
|
|
|
## Non-decodable entries are stepped over while the counter `n` of the
|
|
|
|
## yield record is still incremented.
|
2023-08-22 18:44:54 +00:00
|
|
|
##
|
2023-06-20 13:26:25 +00:00
|
|
|
block walkBody:
|
2023-08-22 18:44:54 +00:00
|
|
|
if pfx in {Oops, AdmPfx}:
|
|
|
|
# Unsupported
|
|
|
|
break walkBody
|
|
|
|
|
|
|
|
let rit = rdb.store.db.rocksdb_create_iterator(rdb.store.readOptions)
|
|
|
|
defer: rit.rocksdb_iter_destroy()
|
|
|
|
|
|
|
|
var
|
|
|
|
count = 0
|
|
|
|
kLen: csize_t
|
|
|
|
kData: cstring
|
|
|
|
|
|
|
|
# Seek for `VertexID(1)` and subsequent entries if that fails. There should
|
|
|
|
# always be a `VertexID(1)` entry unless the sub-table is empty. There is
|
|
|
|
# no such control for the filter table in which case there is a blind guess
|
|
|
|
# (in case `rocksdb_iter_seek()` does not search `ge` for some reason.)
|
|
|
|
let keyOne = 1u64.toRdbKey pfx
|
|
|
|
|
|
|
|
# It is not clear what happens when the `key` does not exist. The guess
|
|
|
|
# is that the interation will proceed at the next key position.
|
|
|
|
#
|
|
|
|
# Comment from GO port at
|
|
|
|
# //github.com/DanielMorsing/rocksdb/blob/master/iterator.go:
|
|
|
|
#
|
|
|
|
# Seek moves the iterator the position of the key given or, if the key
|
|
|
|
# doesn't exist, the next key that does exist in the database. If the key
|
|
|
|
# doesn't exist, and there is no next key, the Iterator becomes invalid.
|
|
|
|
#
|
|
|
|
kData = cast[cstring](unsafeAddr keyOne[0])
|
|
|
|
kLen = sizeof(keyOne).csize_t
|
|
|
|
rit.rocksdb_iter_seek(kData, kLen)
|
2023-06-20 13:26:25 +00:00
|
|
|
if rit.rocksdb_iter_valid() == 0:
|
|
|
|
break walkBody
|
|
|
|
|
|
|
|
# Fetch sub-table data
|
|
|
|
while true:
|
2023-08-22 18:44:54 +00:00
|
|
|
kData = rit.rocksdb_iter_key(addr kLen)
|
|
|
|
if pfx.ord != kData.keyPfx kLen:
|
2023-06-20 13:26:25 +00:00
|
|
|
break walkBody # done
|
|
|
|
|
2023-08-18 19:46:55 +00:00
|
|
|
let xid = kData.keyXid(kLen)
|
2023-08-22 18:44:54 +00:00
|
|
|
if 0 < xid:
|
2023-06-20 13:26:25 +00:00
|
|
|
# Fetch value data
|
|
|
|
var vLen: csize_t
|
|
|
|
let vData = rit.rocksdb_iter_value(addr vLen)
|
|
|
|
|
|
|
|
let val = vData.valBlob(vLen)
|
|
|
|
if 0 < val.len:
|
2023-08-18 19:46:55 +00:00
|
|
|
yield (count, xid, val)
|
2023-06-20 13:26:25 +00:00
|
|
|
|
|
|
|
# Update Iterator
|
|
|
|
rit.rocksdb_iter_next()
|
|
|
|
if rit.rocksdb_iter_valid() == 0:
|
|
|
|
break walkBody
|
2023-08-22 18:44:54 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
count.inc
|
|
|
|
# End while
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|