nim-datastore/datastore/sharedds.nim

101 lines
2.0 KiB
Nim
Raw Normal View History

2023-08-24 15:51:25 -07:00
import std/tables
import pkg/chronos
2023-08-24 17:33:32 -07:00
import pkg/chronos/threadsync
2023-08-24 15:51:25 -07:00
import pkg/questionable
import pkg/questionable/results
import pkg/upraises
import ./key
import ./query
import ./datastore
export key, query
push: {.upraises: [].}
type
2023-08-24 15:57:15 -07:00
SharedDatastore* = ref object of Datastore
# stores*: Table[Key, SharedDatastore]
2023-08-24 15:51:25 -07:00
method has*(
2023-08-24 15:57:15 -07:00
self: SharedDatastore,
key: Key
): Future[?!bool] {.async.} =
2023-08-24 15:51:25 -07:00
2023-08-24 15:57:15 -07:00
# without mounted =? self.dispatch(key):
# return failure "No mounted datastore found"
# return (await mounted.store.store.has(mounted.relative))
return success(true)
2023-08-24 15:51:25 -07:00
method delete*(
2023-08-24 15:57:15 -07:00
self: SharedDatastore,
key: Key
): Future[?!void] {.async.} =
2023-08-24 15:51:25 -07:00
2023-08-24 15:57:15 -07:00
# without mounted =? self.dispatch(key), error:
# return failure(error)
# return (await mounted.store.store.delete(mounted.relative))
return success()
2023-08-24 15:51:25 -07:00
method delete*(
2023-08-24 15:57:15 -07:00
self: SharedDatastore,
keys: seq[Key]
): Future[?!void] {.async.} =
2023-08-24 15:51:25 -07:00
2023-08-24 15:57:15 -07:00
# for key in keys:
# if err =? (await self.delete(key)).errorOption:
# return failure err
2023-08-24 15:51:25 -07:00
return success()
method get*(
2023-08-24 15:57:15 -07:00
self: SharedDatastore,
key: Key
): Future[?!seq[byte]] {.async.} =
2023-08-24 15:51:25 -07:00
2023-08-24 15:57:15 -07:00
# without mounted =? self.dispatch(key), error:
# return failure(error)
2023-08-24 15:51:25 -07:00
2023-08-24 15:57:15 -07:00
# return await mounted.store.store.get(mounted.relative)
return success(newSeq[byte]())
2023-08-24 15:51:25 -07:00
method put*(
2023-08-24 15:57:15 -07:00
self: SharedDatastore,
2023-08-24 15:51:25 -07:00
key: Key,
2023-08-24 15:57:15 -07:00
data: seq[byte]
): Future[?!void] {.async.} =
2023-08-24 15:51:25 -07:00
2023-08-24 17:33:32 -07:00
let signal = ThreadSignalPtr.new()
if signal.isErr:
return failure("error creating signal")
else:
await wait(signal.get())
return success()
2023-08-24 15:51:25 -07:00
method put*(
2023-08-24 15:57:15 -07:00
self: SharedDatastore,
batch: seq[BatchEntry]
): Future[?!void] {.async.} =
2023-08-24 17:33:32 -07:00
raiseAssert("Not implemented!")
2023-08-24 15:51:25 -07:00
2023-08-24 15:57:15 -07:00
method close*(
self: SharedDatastore
): Future[?!void] {.async.} =
# for s in self.stores.values:
# discard await s.store.close()
2023-08-24 15:51:25 -07:00
# TODO: how to handle failed close?
return success()
2023-08-24 15:57:15 -07:00
func new*[S: ref Datastore](
T: typedesc[SharedDatastore],
storeTp: typedesc[S]
): ?!SharedDatastore =
2023-08-24 15:51:25 -07:00
2023-08-24 17:33:32 -07:00
var self = SharedDatastore()
2023-08-24 15:51:25 -07:00
success self