mirror of
https://github.com/logos-storage/nim-datastore.git
synced 2026-01-07 16:13:07 +00:00
make blank sharedds
This commit is contained in:
parent
da0eafe16e
commit
57450fe25a
@ -14,104 +14,68 @@ export key, query
|
|||||||
push: {.upraises: [].}
|
push: {.upraises: [].}
|
||||||
|
|
||||||
type
|
type
|
||||||
MountedStore* = object
|
|
||||||
store*: Datastore
|
|
||||||
key*: Key
|
|
||||||
|
|
||||||
MountedDatastore* = ref object of Datastore
|
SharedDatastore* = ref object of Datastore
|
||||||
stores*: Table[Key, MountedStore]
|
# stores*: Table[Key, SharedDatastore]
|
||||||
|
|
||||||
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))
|
|
||||||
|
|
||||||
method has*(
|
method has*(
|
||||||
self: MountedDatastore,
|
self: SharedDatastore,
|
||||||
key: Key): Future[?!bool] {.async.} =
|
key: Key
|
||||||
|
): Future[?!bool] {.async.} =
|
||||||
|
|
||||||
without mounted =? self.dispatch(key):
|
# without mounted =? self.dispatch(key):
|
||||||
return failure "No mounted datastore found"
|
# return failure "No mounted datastore found"
|
||||||
|
# return (await mounted.store.store.has(mounted.relative))
|
||||||
return (await mounted.store.store.has(mounted.relative))
|
return success(true)
|
||||||
|
|
||||||
method delete*(
|
method delete*(
|
||||||
self: MountedDatastore,
|
self: SharedDatastore,
|
||||||
key: Key): Future[?!void] {.async.} =
|
key: Key
|
||||||
|
): Future[?!void] {.async.} =
|
||||||
|
|
||||||
without mounted =? self.dispatch(key), error:
|
# without mounted =? self.dispatch(key), error:
|
||||||
return failure(error)
|
# return failure(error)
|
||||||
|
# return (await mounted.store.store.delete(mounted.relative))
|
||||||
return (await mounted.store.store.delete(mounted.relative))
|
return success()
|
||||||
|
|
||||||
method delete*(
|
method delete*(
|
||||||
self: MountedDatastore,
|
self: SharedDatastore,
|
||||||
keys: seq[Key]): Future[?!void] {.async.} =
|
keys: seq[Key]
|
||||||
|
): Future[?!void] {.async.} =
|
||||||
|
|
||||||
for key in keys:
|
# for key in keys:
|
||||||
if err =? (await self.delete(key)).errorOption:
|
# if err =? (await self.delete(key)).errorOption:
|
||||||
return failure err
|
# return failure err
|
||||||
|
|
||||||
return success()
|
return success()
|
||||||
|
|
||||||
method get*(
|
method get*(
|
||||||
self: MountedDatastore,
|
self: SharedDatastore,
|
||||||
key: Key): Future[?!seq[byte]] {.async.} =
|
key: Key
|
||||||
|
): Future[?!seq[byte]] {.async.} =
|
||||||
|
|
||||||
without mounted =? self.dispatch(key), error:
|
# without mounted =? self.dispatch(key), error:
|
||||||
return failure(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*(
|
method put*(
|
||||||
self: MountedDatastore,
|
self: SharedDatastore,
|
||||||
key: Key,
|
key: Key,
|
||||||
data: seq[byte]): Future[?!void] {.async.} =
|
data: seq[byte]
|
||||||
|
): Future[?!void] {.async.} =
|
||||||
|
|
||||||
without mounted =? self.dispatch(key), error:
|
# without mounted =? self.dispatch(key), error:
|
||||||
return failure(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*(
|
method put*(
|
||||||
self: MountedDatastore,
|
self: SharedDatastore,
|
||||||
batch: seq[BatchEntry]): Future[?!void] {.async.} =
|
batch: seq[BatchEntry]
|
||||||
|
): Future[?!void] {.async.} =
|
||||||
|
|
||||||
for entry in batch:
|
for entry in batch:
|
||||||
if err =? (await self.put(entry.key, entry.data)).errorOption:
|
if err =? (await self.put(entry.key, entry.data)).errorOption:
|
||||||
@ -119,19 +83,23 @@ method put*(
|
|||||||
|
|
||||||
return success()
|
return success()
|
||||||
|
|
||||||
method close*(self: MountedDatastore): Future[?!void] {.async.} =
|
method close*(
|
||||||
for s in self.stores.values:
|
self: SharedDatastore
|
||||||
discard await s.store.close()
|
): Future[?!void] {.async.} =
|
||||||
|
|
||||||
|
# for s in self.stores.values:
|
||||||
|
# discard await s.store.close()
|
||||||
|
|
||||||
# TODO: how to handle failed close?
|
# TODO: how to handle failed close?
|
||||||
return success()
|
return success()
|
||||||
|
|
||||||
func new*(
|
func new*[S: ref Datastore](
|
||||||
T: type MountedDatastore,
|
T: typedesc[SharedDatastore],
|
||||||
stores: Table[Key, Datastore] = initTable[Key, Datastore]()): ?!T =
|
storeTp: typedesc[S]
|
||||||
|
): ?!SharedDatastore =
|
||||||
|
|
||||||
var self = T()
|
var self = T()
|
||||||
for (k, v) in stores.pairs:
|
# for (k, v) in stores.pairs:
|
||||||
self.stores[?k.path] = MountedStore(store: v, key: k)
|
# self.stores[?k.path] = MountedStore(store: v, key: k)
|
||||||
|
|
||||||
success self
|
success self
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user