make blank sharedds

This commit is contained in:
Jaremy Creechley 2023-08-24 15:57:15 -07:00
parent 3937d570d0
commit 3243038838
No known key found for this signature in database
GPG Key ID: 4E66FB67B21D3300

View File

@ -14,104 +14,68 @@ export key, query
push: {.upraises: [].}
type
MountedStore* = object
store*: Datastore
key*: Key
MountedDatastore* = ref object of Datastore
stores*: Table[Key, MountedStore]
method mount*(self: MountedDatastore, key: Key, store: Datastore): ?!void {.base.} =
## Mount a store on a namespace - namespaces are only `/`
##
if key in self.stores:
return failure("Key already has store mounted!")
self.stores.add(key, MountedStore(store: store, key: key))
return success()
func findStore*(self: MountedDatastore, key: Key): ?!MountedStore =
## Find a store mounted under a particular key
##
for (k, v) in self.stores.pairs:
var
mounted = key
while mounted.len > 0:
if ?k.path == ?mounted.path:
return success v
if mounted.parent.isErr:
break
mounted = mounted.parent.get
failure newException(DatastoreKeyNotFound, "No datastore found for key")
proc dispatch(
self: MountedDatastore,
key: Key): ?!tuple[store: MountedStore, relative: Key] =
## Helper to retrieve the store and corresponding relative key
##
let
mounted = ?self.findStore(key)
return success (store: mounted, relative: ?key.relative(mounted.key))
SharedDatastore* = ref object of Datastore
# stores*: Table[Key, SharedDatastore]
method has*(
self: MountedDatastore,
key: Key): Future[?!bool] {.async.} =
self: SharedDatastore,
key: Key
): Future[?!bool] {.async.} =
without mounted =? self.dispatch(key):
return failure "No mounted datastore found"
return (await mounted.store.store.has(mounted.relative))
# without mounted =? self.dispatch(key):
# return failure "No mounted datastore found"
# return (await mounted.store.store.has(mounted.relative))
return success(true)
method delete*(
self: MountedDatastore,
key: Key): Future[?!void] {.async.} =
self: SharedDatastore,
key: Key
): Future[?!void] {.async.} =
without mounted =? self.dispatch(key), error:
return failure(error)
return (await mounted.store.store.delete(mounted.relative))
# without mounted =? self.dispatch(key), error:
# return failure(error)
# return (await mounted.store.store.delete(mounted.relative))
return success()
method delete*(
self: MountedDatastore,
keys: seq[Key]): Future[?!void] {.async.} =
self: SharedDatastore,
keys: seq[Key]
): Future[?!void] {.async.} =
for key in keys:
if err =? (await self.delete(key)).errorOption:
return failure err
# for key in keys:
# if err =? (await self.delete(key)).errorOption:
# return failure err
return success()
method get*(
self: MountedDatastore,
key: Key): Future[?!seq[byte]] {.async.} =
self: SharedDatastore,
key: Key
): Future[?!seq[byte]] {.async.} =
without mounted =? self.dispatch(key), error:
return failure(error)
# without mounted =? self.dispatch(key), error:
# return failure(error)
return await mounted.store.store.get(mounted.relative)
# return await mounted.store.store.get(mounted.relative)
return success(newSeq[byte]())
method put*(
self: MountedDatastore,
self: SharedDatastore,
key: Key,
data: seq[byte]): Future[?!void] {.async.} =
data: seq[byte]
): Future[?!void] {.async.} =
without mounted =? self.dispatch(key), error:
return failure(error)
# without mounted =? self.dispatch(key), error:
# return failure(error)
return (await mounted.store.store.put(mounted.relative, data))
# return (await mounted.store.store.put(mounted.relative, data))
return success()
method put*(
self: MountedDatastore,
batch: seq[BatchEntry]): Future[?!void] {.async.} =
self: SharedDatastore,
batch: seq[BatchEntry]
): Future[?!void] {.async.} =
for entry in batch:
if err =? (await self.put(entry.key, entry.data)).errorOption:
@ -119,19 +83,23 @@ method put*(
return success()
method close*(self: MountedDatastore): Future[?!void] {.async.} =
for s in self.stores.values:
discard await s.store.close()
method close*(
self: SharedDatastore
): Future[?!void] {.async.} =
# for s in self.stores.values:
# discard await s.store.close()
# TODO: how to handle failed close?
return success()
func new*(
T: type MountedDatastore,
stores: Table[Key, Datastore] = initTable[Key, Datastore]()): ?!T =
func new*[S: ref Datastore](
T: typedesc[SharedDatastore],
storeTp: typedesc[S]
): ?!SharedDatastore =
var self = T()
for (k, v) in stores.pairs:
self.stores[?k.path] = MountedStore(store: v, key: k)
# for (k, v) in stores.pairs:
# self.stores[?k.path] = MountedStore(store: v, key: k)
success self