rework contains to return a bool (#39)

This commit is contained in:
Dmitriy Ryajov 2022-12-02 16:25:44 -06:00 committed by GitHub
parent 9d49c8016e
commit 44c198b96a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 42 additions and 41 deletions

View File

@ -13,7 +13,7 @@ push: {.upraises: [].}
type
BatchEntry* = tuple[key: Key, data: seq[byte]]
method contains*(self: Datastore, key: Key): Future[?!bool] {.base, locks: "unknown".} =
method has*(self: Datastore, key: Key): Future[?!bool] {.base, locks: "unknown".} =
raiseAssert("Not implemented!")
method delete*(self: Datastore, key: Key): Future[?!void] {.base, locks: "unknown".} =
@ -39,3 +39,6 @@ method query*(
query: Query): Future[?!QueryIter] {.base, gcsafe.} =
raiseAssert("Not implemented!")
proc contains*(self: Datastore, key: Key): Future[bool] {.async.} =
return (await self.has(key)) |? false

View File

@ -64,7 +64,7 @@ proc path*(self: FSDatastore, key: Key): ?!string =
return success fullname
method contains*(self: FSDatastore, key: Key): Future[?!bool] {.async.} =
method has*(self: FSDatastore, key: Key): Future[?!bool] {.async.} =
return self.path(key).?fileExists()
method delete*(self: FSDatastore, key: Key): Future[?!void] {.async.} =

View File

@ -62,14 +62,14 @@ proc dispatch(
return success (store: mounted, relative: ?key.relative(mounted.key))
method contains*(
method has*(
self: MountedDatastore,
key: Key): Future[?!bool] {.async.} =
without mounted =? self.dispatch(key):
return failure "No mounted datastore found"
return (await mounted.store.store.contains(mounted.relative))
return (await mounted.store.store.has(mounted.relative))
method delete*(
self: MountedDatastore,
@ -111,7 +111,7 @@ method put*(
method put*(
self: MountedDatastore,
batch: seq[BatchEntry]): Future[?!void] {.async, locks: "unknown".} =
batch: seq[BatchEntry]): Future[?!void] {.async.} =
for entry in batch:
if err =? (await self.put(entry.key, entry.data)).errorOption:

View File

@ -29,17 +29,15 @@ proc `readOnly=`*(self: SQLiteDatastore): bool
proc timestamp*(t = epochTime()): int64 =
(t * 1_000_000).int64
method contains*(self: SQLiteDatastore, key: Key): Future[?!bool] {.async.} =
method has*(self: SQLiteDatastore, key: Key): Future[?!bool] {.async.} =
var
exists = false
proc onData(s: RawStmtPtr) =
exists = sqlite3_column_int64(s, ContainsStmtExistsCol.cint).bool
if (
let res = self.db.containsStmt.query((key.id), onData);
res.isErr):
return failure res.error.msg
if err =? self.db.containsStmt.query((key.id), onData).errorOption:
return failure err
return success exists

View File

@ -28,22 +28,22 @@ proc new*(
proc stores*(self: TieredDatastore): seq[Datastore] =
self.stores
method contains*(
method has*(
self: TieredDatastore,
key: Key): Future[?!bool] {.async, locks: "unknown".} =
key: Key): Future[?!bool] {.async.} =
for store in self.stores:
let
containsRes = await store.contains(key)
without res =? (await store.has(key)), err:
return failure(err)
if containsRes.isErr: return containsRes
if containsRes.get == true: return success true
if res:
return success true
return success false
method delete*(
self: TieredDatastore,
key: Key): Future[?!void] {.async, locks: "unknown".} =
key: Key): Future[?!void] {.async.} =
let
pending = await allFinished(self.stores.mapIt(it.delete(key)))
@ -55,7 +55,7 @@ method delete*(
method delete*(
self: TieredDatastore,
keys: seq[Key]): Future[?!void] {.async, locks: "unknown".} =
keys: seq[Key]): Future[?!void] {.async.} =
for key in keys:
let
@ -68,7 +68,7 @@ method delete*(
method get*(
self: TieredDatastore,
key: Key): Future[?!seq[byte]] {.async, locks: "unknown".} =
key: Key): Future[?!seq[byte]] {.async.} =
var
bytes: seq[byte]
@ -93,7 +93,7 @@ method get*(
method put*(
self: TieredDatastore,
key: Key,
data: seq[byte]): Future[?!void] {.async, locks: "unknown".} =
data: seq[byte]): Future[?!void] {.async.} =
let
pending = await allFinished(self.stores.mapIt(it.put(key, data)))
@ -105,7 +105,7 @@ method put*(
method put*(
self: TieredDatastore,
batch: seq[BatchEntry]): Future[?!void] {.async, locks: "unknown".} =
batch: seq[BatchEntry]): Future[?!void] {.async.} =
for entry in batch:
let
@ -118,6 +118,6 @@ method put*(
# method query*(
# self: TieredDatastore,
# query: ...): Future[?!(?...)] {.async, locks: "unknown".} =
# query: ...): Future[?!(?...)] {.async.} =
#
# return success ....some

View File

@ -31,7 +31,7 @@ proc basicStoreTests*(
test "contains":
check:
not (await ds.contains(key)).tryGet()
not await (key in ds)
test "put batch":
var
@ -43,7 +43,7 @@ proc basicStoreTests*(
(await ds.put(batch)).tryGet
for k in batch:
check: (await ds.contains(k.key)).tryGet
check: (await ds.has(k.key)).tryGet
test "delete batch":
var
@ -55,4 +55,4 @@ proc basicStoreTests*(
(await ds.delete(batch)).tryGet
for k in batch:
check: not (await ds.contains(k)).tryGet
check: not (await ds.has(k)).tryGet

View File

@ -73,8 +73,8 @@ suite "Test Read Only SQLiteDatastore":
test "contains":
check:
not (await readOnlyDb.contains(key)).tryGet()
not (await dsDb.contains(key)).tryGet()
not (await readOnlyDb.has(key)).tryGet()
not (await dsDb.has(key)).tryGet()
suite "Test Query":
var

View File

@ -18,7 +18,7 @@ suite "Datastore (base)":
expect Defect: discard ds.delete(key)
test "contains":
expect Defect: discard ds.contains(key)
expect Defect: discard ds.has(key)
test "get":
expect Defect: discard ds.get(key)

View File

@ -73,7 +73,7 @@ suite "Test Misc FSDatastore":
(await fs.put(key, bytes)).isErr
(await fs.get(key)).isErr
(await fs.delete(key)).isErr
(await fs.contains(key)).isErr
(await fs.has(key)).isErr
test "Test valid key (path) depth":
let
@ -84,7 +84,7 @@ suite "Test Misc FSDatastore":
(await fs.put(key, bytes)).isOk
(await fs.get(key)).isOk
(await fs.delete(key)).isOk
(await fs.contains(key)).isOk
(await fs.has(key)).isOk
test "Test key cannot write outside of root":
let
@ -95,7 +95,7 @@ suite "Test Misc FSDatastore":
(await fs.put(key, bytes)).isErr
(await fs.get(key)).isErr
(await fs.delete(key)).isErr
(await fs.contains(key)).isErr
(await fs.has(key)).isErr
test "Test key cannot convert to invalid path":
let
@ -112,7 +112,7 @@ suite "Test Misc FSDatastore":
(await fs.put(key, bytes)).isErr
(await fs.get(key)).isErr
(await fs.delete(key)).isErr
(await fs.contains(key)).isErr
(await fs.has(key)).isErr
suite "Test Query":
let

View File

@ -116,24 +116,24 @@ suite "TieredDatastore":
ds = TieredDatastore.new(ds1, ds2).tryGet
check:
not (await ds1.contains(key)).tryGet
not (await ds2.contains(key)).tryGet
not (await ds1.has(key)).tryGet
not (await ds2.has(key)).tryGet
(await ds.put(key, bytes)).tryGet
check:
(await ds.contains(key)).tryGet
(await ds1.contains(key)).tryGet
(await ds2.contains(key)).tryGet
(await ds.has(key)).tryGet
(await ds1.has(key)).tryGet
(await ds2.has(key)).tryGet
test "get":
var
ds = TieredDatastore.new(ds1, ds2).tryGet
check:
not (await ds1.contains(key)).tryGet
not (await ds2.contains(key)).tryGet
not (await ds.contains(key)).tryGet
not (await ds1.has(key)).tryGet
not (await ds2.has(key)).tryGet
not (await ds.has(key)).tryGet
(await ds.put(key, bytes)).tryGet
@ -149,7 +149,7 @@ suite "TieredDatastore":
ds = TieredDatastore.new(ds1, ds2).tryGet
check:
not (await ds1.contains(key)).tryGet
not (await ds1.has(key)).tryGet
(await ds2.get(key)).tryGet == bytes
(await ds.get(key)).tryGet == bytes
(await ds1.get(key)).tryGet == bytes