2025-02-05 15:21:11 +01:00
|
|
|
import pkg/chronos
|
|
|
|
|
import pkg/chronicles
|
2025-02-05 14:05:18 +01:00
|
|
|
import pkg/metrics
|
2025-02-05 15:21:11 +01:00
|
|
|
import pkg/datastore
|
|
|
|
|
import pkg/datastore/typedds
|
2025-02-05 16:35:02 +01:00
|
|
|
import pkg/stew/byteutils
|
|
|
|
|
import pkg/stew/endians2
|
2025-02-05 15:21:11 +01:00
|
|
|
import pkg/questionable
|
|
|
|
|
import pkg/questionable/results
|
2025-02-07 14:51:03 +01:00
|
|
|
import pkg/stint
|
|
|
|
|
import pkg/codexdht
|
2025-02-05 15:21:11 +01:00
|
|
|
|
2025-02-06 15:32:39 +01:00
|
|
|
import std/sets
|
2025-02-05 16:35:02 +01:00
|
|
|
import std/strutils
|
2025-02-07 15:35:40 +01:00
|
|
|
import std/sequtils
|
2025-02-05 15:21:11 +01:00
|
|
|
import std/os
|
2025-02-05 16:06:04 +01:00
|
|
|
|
|
|
|
|
import ./nodeentry
|
2025-02-05 15:21:11 +01:00
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
|
topics = "list"
|
2025-02-05 14:05:18 +01:00
|
|
|
|
|
|
|
|
type
|
2025-02-05 16:06:04 +01:00
|
|
|
OnUpdateMetric = proc(value: int64): void {.gcsafe, raises: [].}
|
2025-02-05 15:21:11 +01:00
|
|
|
|
|
|
|
|
List* = ref object
|
|
|
|
|
name: string
|
|
|
|
|
store: TypedDatastore
|
2025-02-07 15:35:40 +01:00
|
|
|
items: seq[NodeEntry]
|
2025-02-05 14:05:18 +01:00
|
|
|
onMetric: OnUpdateMetric
|
2025-02-05 15:21:11 +01:00
|
|
|
|
2025-02-05 16:35:02 +01:00
|
|
|
proc encode(s: NodeEntry): seq[byte] =
|
2025-02-07 16:19:26 +01:00
|
|
|
s.toBytes()
|
2025-02-05 16:35:02 +01:00
|
|
|
|
|
|
|
|
proc decode(T: type NodeEntry, bytes: seq[byte]): ?!T =
|
2025-02-07 16:19:26 +01:00
|
|
|
if bytes.len < 1:
|
|
|
|
|
return success(NodeEntry(id: UInt256.fromHex("0"), lastVisit: 0.uint64))
|
|
|
|
|
return NodeEntry.fromBytes(bytes)
|
2025-02-05 16:35:02 +01:00
|
|
|
|
2025-02-05 16:06:04 +01:00
|
|
|
proc saveItem(this: List, item: NodeEntry): Future[?!void] {.async.} =
|
2025-02-07 14:51:03 +01:00
|
|
|
without itemKey =? Key.init(this.name / $item.id), err:
|
2025-02-05 15:59:48 +01:00
|
|
|
return failure(err)
|
2025-02-05 16:06:04 +01:00
|
|
|
?await this.store.put(itemKey, item)
|
2025-02-05 15:21:11 +01:00
|
|
|
return success()
|
|
|
|
|
|
2025-02-07 14:51:03 +01:00
|
|
|
proc removeItem(this: List, item: NodeEntry): Future[?!void] {.async.} =
|
|
|
|
|
without itemKey =? Key.init(this.name / $item.id), err:
|
|
|
|
|
return failure(err)
|
|
|
|
|
?await this.store.delete(itemKey)
|
|
|
|
|
return success()
|
|
|
|
|
|
2025-02-05 16:06:04 +01:00
|
|
|
proc load*(this: List): Future[?!void] {.async.} =
|
2025-02-05 15:59:48 +01:00
|
|
|
without queryKey =? Key.init(this.name), err:
|
|
|
|
|
return failure(err)
|
2025-02-05 16:06:04 +01:00
|
|
|
without iter =? (await query[NodeEntry](this.store, Query.init(queryKey))), err:
|
2025-02-05 15:59:48 +01:00
|
|
|
return failure(err)
|
2025-02-05 15:21:11 +01:00
|
|
|
|
2025-02-05 15:59:48 +01:00
|
|
|
while not iter.finished:
|
|
|
|
|
without item =? (await iter.next()), err:
|
|
|
|
|
return failure(err)
|
|
|
|
|
without value =? item.value, err:
|
|
|
|
|
return failure(err)
|
2025-02-07 16:19:26 +01:00
|
|
|
if value.id > 0 or value.lastVisit > 0:
|
2025-02-07 15:35:40 +01:00
|
|
|
this.items.add(value)
|
2025-02-05 15:21:11 +01:00
|
|
|
|
2025-02-06 15:32:39 +01:00
|
|
|
this.onMetric(this.items.len.int64)
|
2025-02-05 15:21:11 +01:00
|
|
|
info "Loaded list", name = this.name, items = this.items.len
|
|
|
|
|
return success()
|
|
|
|
|
|
|
|
|
|
proc new*(
|
2025-02-05 16:06:04 +01:00
|
|
|
_: type List, name: string, store: TypedDatastore, onMetric: OnUpdateMetric
|
2025-02-05 15:21:11 +01:00
|
|
|
): List =
|
2025-02-06 15:32:39 +01:00
|
|
|
List(name: name, store: store, onMetric: onMetric)
|
2025-02-05 14:05:18 +01:00
|
|
|
|
2025-02-07 15:35:40 +01:00
|
|
|
proc contains*(this: List, nodeId: NodeId): bool =
|
|
|
|
|
this.items.anyIt(it.id == nodeId)
|
|
|
|
|
|
|
|
|
|
proc contains*(this: List, item: NodeEntry): bool =
|
|
|
|
|
this.contains(item.id)
|
|
|
|
|
|
2025-02-05 16:06:04 +01:00
|
|
|
proc add*(this: List, item: NodeEntry): Future[?!void] {.async.} =
|
2025-02-07 15:35:40 +01:00
|
|
|
if this.contains(item):
|
|
|
|
|
return success()
|
|
|
|
|
|
|
|
|
|
this.items.add(item)
|
2025-02-05 14:05:18 +01:00
|
|
|
this.onMetric(this.items.len.int64)
|
2025-02-05 15:21:11 +01:00
|
|
|
|
2025-02-05 15:59:48 +01:00
|
|
|
if err =? (await this.saveItem(item)).errorOption:
|
|
|
|
|
return failure(err)
|
|
|
|
|
return success()
|
2025-02-07 14:51:03 +01:00
|
|
|
|
|
|
|
|
proc pop*(this: List): Future[?!NodeEntry] {.async.} =
|
|
|
|
|
if this.items.len < 1:
|
|
|
|
|
return failure(this.name & "List is empty.")
|
|
|
|
|
|
2025-02-07 15:35:40 +01:00
|
|
|
let item = this.items[0]
|
|
|
|
|
this.items.delete(0)
|
|
|
|
|
this.onMetric(this.items.len.int64)
|
|
|
|
|
|
2025-02-07 14:51:03 +01:00
|
|
|
if err =? (await this.removeItem(item)).errorOption:
|
|
|
|
|
return failure(err)
|
|
|
|
|
return success(item)
|
|
|
|
|
|
|
|
|
|
proc len*(this: List): int =
|
|
|
|
|
this.items.len
|