nim-datastore/datastore/sharedds.nim

106 lines
2.1 KiB
Nim
Raw Normal View History

2023-08-24 15:51:25 -07:00
import std/tables
import pkg/chronos
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 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.put(mounted.relative, data))
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 15:51:25 -07:00
for entry in batch:
if err =? (await self.put(entry.key, entry.data)).errorOption:
return failure err
return success()
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
var self = T()
2023-08-24 15:57:15 -07:00
# for (k, v) in stores.pairs:
# self.stores[?k.path] = MountedStore(store: v, key: k)
2023-08-24 15:51:25 -07:00
success self