2023-12-01 16:20:52 +00:00
|
|
|
# Fluffy
|
2024-02-28 17:31:45 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2022-06-20 14:52:48 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2022-06-20 14:52:48 +00:00
|
|
|
|
|
|
|
import
|
2022-07-20 10:46:42 +00:00
|
|
|
std/[options, os],
|
|
|
|
strutils,
|
2022-06-20 14:52:48 +00:00
|
|
|
eth/db/kvstore,
|
|
|
|
eth/db/kvstore_sqlite3,
|
2023-11-21 15:16:15 +00:00
|
|
|
stint,
|
|
|
|
./content_db_custom_sql_functions
|
2022-06-20 14:52:48 +00:00
|
|
|
|
|
|
|
export kvstore_sqlite3
|
|
|
|
|
|
|
|
type
|
2024-02-28 17:31:45 +00:00
|
|
|
ContentData =
|
|
|
|
tuple[contentId: array[32, byte], contentKey: seq[byte], content: seq[byte]]
|
2022-06-20 14:52:48 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
ContentDataDist* =
|
|
|
|
tuple[
|
|
|
|
contentId: array[32, byte],
|
|
|
|
contentKey: seq[byte],
|
|
|
|
content: seq[byte],
|
|
|
|
distance: array[32, byte],
|
|
|
|
]
|
2022-06-20 14:52:48 +00:00
|
|
|
|
|
|
|
SeedDb* = ref object
|
|
|
|
store: SqStoreRef
|
|
|
|
putStmt: SqliteStmt[(array[32, byte], seq[byte], seq[byte]), void]
|
|
|
|
getStmt: SqliteStmt[array[32, byte], ContentData]
|
2024-02-28 17:31:45 +00:00
|
|
|
getInRangeStmt:
|
|
|
|
SqliteStmt[(array[32, byte], array[32, byte], int64, int64), ContentDataDist]
|
2022-06-20 14:52:48 +00:00
|
|
|
|
|
|
|
template expectDb(x: auto): untyped =
|
|
|
|
# There's no meaningful error handling implemented for a corrupt database or
|
|
|
|
# full disk - this requires manual intervention, so we'll panic for now
|
|
|
|
x.expect("working database (disk broken/full?)")
|
|
|
|
|
2022-07-20 10:46:42 +00:00
|
|
|
proc getDbBasePathAndName*(path: string): Option[(string, string)] =
|
|
|
|
let (basePath, name) = splitPath(path)
|
|
|
|
if len(basePath) > 0 and len(name) > 0 and name.endsWith(".sqlite3"):
|
|
|
|
let nameAndExt = rsplit(name, ".", 1)
|
|
|
|
|
|
|
|
if len(nameAndExt) < 2 and len(nameAndExt[0]) == 0:
|
|
|
|
return none((string, string))
|
|
|
|
|
|
|
|
return some((basePath, nameAndExt[0]))
|
|
|
|
else:
|
|
|
|
return none((string, string))
|
|
|
|
|
2022-06-20 14:52:48 +00:00
|
|
|
proc new*(T: type SeedDb, path: string, name: string, inMemory = false): SeedDb =
|
|
|
|
let db =
|
|
|
|
if inMemory:
|
|
|
|
SqStoreRef.init("", "seed-db-test", inMemory = true).expect(
|
2024-02-28 17:31:45 +00:00
|
|
|
"working database (out of memory?)"
|
|
|
|
)
|
2022-06-20 14:52:48 +00:00
|
|
|
else:
|
|
|
|
SqStoreRef.init(path, name).expectDb()
|
|
|
|
|
|
|
|
if not db.readOnly:
|
2024-02-28 17:31:45 +00:00
|
|
|
let createSql =
|
|
|
|
"""
|
2022-06-20 14:52:48 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS seed_data (
|
|
|
|
contentid BLOB PRIMARY KEY,
|
|
|
|
contentkey BLOB,
|
|
|
|
content BLOB
|
|
|
|
);"""
|
|
|
|
|
|
|
|
db.exec(createSql).expectDb()
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
let putStmt = db.prepareStmt(
|
|
|
|
"INSERT OR REPLACE INTO seed_data (contentid, contentkey, content) VALUES (?, ?, ?);",
|
|
|
|
(array[32, byte], seq[byte], seq[byte]),
|
|
|
|
void,
|
|
|
|
)[]
|
2022-06-20 14:52:48 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
let getStmt = db.prepareStmt(
|
|
|
|
"SELECT contentid, contentkey, content FROM seed_data WHERE contentid = ?;",
|
|
|
|
array[32, byte],
|
|
|
|
ContentData,
|
|
|
|
)[]
|
2022-06-20 14:52:48 +00:00
|
|
|
|
2023-11-21 15:16:15 +00:00
|
|
|
db.createCustomFunction("xorDistance", 2, xorDistance).expect(
|
2024-02-28 17:31:45 +00:00
|
|
|
"Custom function xorDistance creation OK"
|
|
|
|
)
|
2022-06-20 14:52:48 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
let getInRangeStmt = db.prepareStmt(
|
|
|
|
"""
|
2022-06-20 14:52:48 +00:00
|
|
|
SELECT contentid, contentkey, content, xorDistance(?, contentid) as distance
|
|
|
|
FROM seed_data
|
|
|
|
WHERE distance <= ?
|
2022-07-20 10:46:42 +00:00
|
|
|
LIMIT ?
|
|
|
|
OFFSET ?;
|
2022-06-20 14:52:48 +00:00
|
|
|
""",
|
2024-02-28 17:31:45 +00:00
|
|
|
(array[32, byte], array[32, byte], int64, int64),
|
|
|
|
ContentDataDist,
|
|
|
|
)[]
|
|
|
|
|
|
|
|
SeedDb(store: db, putStmt: putStmt, getStmt: getStmt, getInRangeStmt: getInRangeStmt)
|
2022-06-20 14:52:48 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
proc put*(
|
|
|
|
db: SeedDb, contentId: array[32, byte], contentKey: seq[byte], content: seq[byte]
|
|
|
|
): void =
|
2022-06-20 14:52:48 +00:00
|
|
|
db.putStmt.exec((contentId, contentKey, content)).expectDb()
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
proc put*(
|
|
|
|
db: SeedDb, contentId: UInt256, contentKey: seq[byte], content: seq[byte]
|
|
|
|
): void =
|
2023-09-13 02:32:38 +00:00
|
|
|
db.put(contentId.toBytesBE(), contentKey, content)
|
2022-06-20 14:52:48 +00:00
|
|
|
|
|
|
|
proc get*(db: SeedDb, contentId: array[32, byte]): Option[ContentData] =
|
|
|
|
var res = none[ContentData]()
|
2024-02-28 17:31:45 +00:00
|
|
|
discard db.getStmt
|
|
|
|
.exec(
|
|
|
|
contentId,
|
|
|
|
proc(v: ContentData) =
|
|
|
|
res = some(v)
|
|
|
|
,
|
|
|
|
)
|
|
|
|
.expectDb()
|
2022-06-20 14:52:48 +00:00
|
|
|
return res
|
|
|
|
|
|
|
|
proc get*(db: SeedDb, contentId: UInt256): Option[ContentData] =
|
2023-09-13 02:32:38 +00:00
|
|
|
db.get(contentId.toBytesBE())
|
2022-06-20 14:52:48 +00:00
|
|
|
|
|
|
|
proc getContentInRange*(
|
2024-02-28 17:31:45 +00:00
|
|
|
db: SeedDb, nodeId: UInt256, nodeRadius: UInt256, max: int64, offset: int64
|
|
|
|
): seq[ContentDataDist] =
|
2022-07-20 10:46:42 +00:00
|
|
|
## Return `max` amount of content in `nodeId` range, starting from `offset` position
|
|
|
|
## i.e using `offset` 0 will return `max` closest items, using `offset` `10` will
|
|
|
|
## will retrun `max` closest items except first 10
|
2022-06-20 14:52:48 +00:00
|
|
|
|
|
|
|
var res: seq[ContentDataDist] = @[]
|
|
|
|
var cd: ContentDataDist
|
2024-02-28 17:31:45 +00:00
|
|
|
for e in db.getInRangeStmt.exec(
|
|
|
|
(nodeId.toBytesBE(), nodeRadius.toBytesBE(), max, offset), cd
|
|
|
|
):
|
2022-06-20 14:52:48 +00:00
|
|
|
res.add(cd)
|
|
|
|
return res
|
|
|
|
|
2022-07-20 10:46:42 +00:00
|
|
|
proc getContentInRange*(
|
2024-02-28 17:31:45 +00:00
|
|
|
db: SeedDb, nodeId: UInt256, nodeRadius: UInt256, max: int64
|
|
|
|
): seq[ContentDataDist] =
|
2022-07-20 10:46:42 +00:00
|
|
|
## Return `max` amount of content in `nodeId` range, starting from closest content
|
|
|
|
return db.getContentInRange(nodeId, nodeRadius, max, 0)
|
|
|
|
|
2022-06-20 14:52:48 +00:00
|
|
|
proc close*(db: SeedDb) =
|
|
|
|
db.store.close()
|