126 lines
3.2 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
2025-02-05 15:21:11 +01:00
2025-02-06 15:32:39 +01:00
import std/sets
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
2025-02-10 15:34:41 +01:00
import ./types
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-10 16:02:47 +01:00
OnItem = proc(item: Nid): void {.gcsafe, raises: [].}
2025-02-05 15:21:11 +01:00
List* = ref object
name: string
store: TypedDatastore
2025-02-10 16:02:47 +01:00
items: HashSet[Nid]
2025-02-05 14:05:18 +01:00
onMetric: OnUpdateMetric
emptySignal: ?Future[void]
2025-02-05 15:21:11 +01:00
2025-02-10 16:02:47 +01:00
proc encode(s: Nid): seq[byte] =
2025-02-07 16:19:26 +01:00
s.toBytes()
2025-02-05 16:35:02 +01:00
2025-02-10 16:02:47 +01:00
proc decode(T: type Nid, bytes: seq[byte]): ?!T =
2025-02-07 16:19:26 +01:00
if bytes.len < 1:
2025-02-10 16:02:47 +01:00
return success(Nid.fromStr("0"))
return Nid.fromBytes(bytes)
2025-02-05 16:35:02 +01:00
2025-02-10 16:02:47 +01:00
proc saveItem(this: List, item: Nid): Future[?!void] {.async.} =
without itemKey =? Key.init(this.name / $item), 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-10 15:34:41 +01:00
let id = Nid.fromStr("0")
let bytes = newSeq[byte]()
2025-02-10 16:02:47 +01:00
let ne = Nid.fromBytes(bytes)
2025-02-10 15:34:41 +01:00
2025-02-05 15:59:48 +01:00
without queryKey =? Key.init(this.name), err:
return failure(err)
2025-02-10 16:02:47 +01:00
without iter =? (await query[Nid](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-10 16:02:47 +01:00
if value > 0:
this.items.incl(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-10 16:02:47 +01:00
proc contains*(this: List, nid: Nid): bool =
this.items.anyIt(it == nid)
2025-02-07 15:35:40 +01:00
2025-02-10 16:02:47 +01:00
proc add*(this: List, nid: Nid): Future[?!void] {.async.} =
if this.contains(nid):
2025-02-07 15:35:40 +01:00
return success()
2025-02-10 16:02:47 +01:00
this.items.incl(nid)
2025-02-05 14:05:18 +01:00
this.onMetric(this.items.len.int64)
2025-02-05 15:21:11 +01:00
2025-02-10 16:02:47 +01:00
if err =? (await this.saveItem(nid)).errorOption:
return failure(err)
2025-02-10 15:34:41 +01:00
if s =? this.emptySignal:
trace "List no longer empty.", name = this.name
2025-02-10 15:34:41 +01:00
s.complete()
this.emptySignal = Future[void].none
2025-02-05 15:59:48 +01:00
return success()
2025-02-07 14:51:03 +01:00
2025-02-10 16:02:47 +01:00
proc remove*(this: List, nid: Nid): Future[?!void] {.async.} =
2025-02-07 16:48:47 +01:00
if this.items.len < 1:
return failure(this.name & "List is empty.")
2025-02-10 16:02:47 +01:00
this.items.excl(nid)
without itemKey =? Key.init(this.name / $nid), err:
2025-02-07 16:48:47 +01:00
return failure(err)
?await this.store.delete(itemKey)
this.onMetric(this.items.len.int64)
return success()
2025-02-10 16:02:47 +01:00
proc pop*(this: List): Future[?!Nid] {.async.} =
2025-02-07 14:51:03 +01:00
if this.items.len < 1:
trace "List is empty. Waiting for new items...", name = this.name
2025-02-10 15:34:41 +01:00
let signal = newFuture[void]("list.emptySignal")
this.emptySignal = some(signal)
await signal.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-10 16:02:47 +01:00
let item = this.items.pop()
2025-02-07 15:35:40 +01:00
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)