98 lines
2.5 KiB
Nim
Raw Normal View History

2025-02-11 14:02:30 +01:00
import std/os
2025-02-05 15:21:11 +01:00
import pkg/chronos
import pkg/chronicles
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-11 14:02:30 +01:00
import ./utils/datastoreutils
2025-02-05 15:21:11 +01:00
logScope:
topics = "list"
2025-02-05 14:05:18 +01:00
2025-02-11 14:29:41 +01:00
type List* = ref object of RootObj
name: string
store: TypedDatastore
items: HashSet[Nid]
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-11 14:02:30 +01:00
method load*(this: List): Future[?!void] {.async, base.} =
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
info "Loaded list", name = this.name, items = this.items.len
return success()
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-11 14:02:30 +01:00
method add*(this: List, nid: Nid): Future[?!void] {.async, base.} =
2025-02-10 16:02:47 +01:00
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 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-11 14:02:30 +01:00
method remove*(this: List, nid: Nid): Future[?!void] {.async, base.} =
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)
return success()
2025-02-11 14:29:41 +01:00
method len*(this: List): int {.base, gcsafe, raises: [].} =
2025-02-11 14:02:30 +01:00
this.items.len
2025-02-07 14:51:03 +01:00
2025-02-11 14:29:41 +01:00
proc new*(_: type List, name: string, store: TypedDatastore): List =
2025-02-11 14:02:30 +01:00
List(name: name, store: store)
2025-02-07 15:35:40 +01:00
2025-02-11 14:02:30 +01:00
proc createList*(dataDir: string, name: string): ?!List =
without store =? createTypedDatastore(dataDir / name), err:
2025-02-07 14:51:03 +01:00
return failure(err)
2025-02-11 14:02:30 +01:00
success(List.new(name, store))