125 lines
3.4 KiB
Nim
Raw Normal View History

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-07 16:48:47 +01:00
OnItem = proc(item: NodeEntry): 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
emptySignal: ?Future[void]
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-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
if isSome(this.emptySignal):
trace "List no longer empty.", name = this.name
this.emptySignal.get().complete()
this.emptySignal = Future[void].none
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
2025-02-07 16:48:47 +01:00
proc remove*(this: List, item: NodeEntry): Future[?!void] {.async.} =
if this.items.len < 1:
return failure(this.name & "List is empty.")
this.items.keepItIf(item.id != it.id)
without itemKey =? Key.init(this.name / $item.id), err:
return failure(err)
?await this.store.delete(itemKey)
this.onMetric(this.items.len.int64)
return success()
2025-02-07 14:51:03 +01:00
proc pop*(this: List): Future[?!NodeEntry] {.async.} =
if this.items.len < 1:
trace "List is empty. Waiting for new items...", name = this.name
this.emptySignal = some(newFuture[void]("list.emptySignal"))
await this.emptySignal.get().wait(1.hours)
if this.items.len < 1:
return failure(this.name & "List is empty.")
2025-02-07 14:51:03 +01:00
2025-02-07 15:35:40 +01:00
let item = this.items[0]
2025-02-07 16:48:47 +01:00
if err =? (await this.remove(item)).errorOption:
2025-02-07 14:51:03 +01:00
return failure(err)
return success(item)
proc len*(this: List): int =
this.items.len
2025-02-07 16:48:47 +01:00
proc iterateAll*(this: List, onItem: OnItem) {.async.} =
for item in this.items:
onItem(item)
await sleepAsync(1.millis)