introduce method close in the base API and refactor accordingly

This commit is contained in:
Michael Bradley, Jr 2022-09-09 00:39:16 -05:00
parent b0241a7c57
commit 2546740ed0
No known key found for this signature in database
GPG Key ID: D0307DBCF21A9A58
7 changed files with 33 additions and 20 deletions

View File

@ -13,28 +13,31 @@ push: {.upraises: [].}
type
Datastore* = ref object of RootObj
method close*(self: Datastore): Future[void] {.base, locks: "unknown".} =
raiseAssert("Not implemented!")
method contains*(
self: Datastore,
key: Key): Future[?!bool] {.async, base, locks: "unknown".} =
key: Key): Future[?!bool] {.base, locks: "unknown".} =
raiseAssert("Not implemented!")
method delete*(
self: Datastore,
key: Key): Future[?!void] {.async, base, locks: "unknown".} =
key: Key): Future[?!void] {.base, locks: "unknown".} =
raiseAssert("Not implemented!")
method get*(
self: Datastore,
key: Key): Future[?!(?seq[byte])] {.async, base, locks: "unknown".} =
key: Key): Future[?!(?seq[byte])] {.base, locks: "unknown".} =
raiseAssert("Not implemented!")
method put*(
self: Datastore,
key: Key,
data: seq[byte]): Future[?!void] {.async, base, locks: "unknown".} =
data: seq[byte]): Future[?!void] {.base, locks: "unknown".} =
raiseAssert("Not implemented!")

View File

@ -59,6 +59,9 @@ proc path*(
self.root / joinPath(segments) & objExt
method close*(self: FileSystemDatastore) {.async, locks: "unknown".} =
discard
method contains*(
self: FileSystemDatastore,
key: Key): Future[?!bool] {.async, locks: "unknown".} =

View File

@ -15,6 +15,9 @@ type
proc new*(T: type NullDatastore): T =
T()
method close*(self: NullDatastore) {.async, locks: "unknown".} =
discard
method contains*(
self: NullDatastore,
key: Key): Future[?!bool] {.async, locks: "unknown".} =

View File

@ -284,7 +284,10 @@ proc dbPath*(self: SQLiteDatastore): string =
proc env*(self: SQLiteDatastore): SQLite =
self.env
proc close*(self: SQLiteDatastore) =
proc timestamp*(t = epochTime()): int64 =
(t * 1_000_000).int64
method close*(self: SQLiteDatastore) {.async, locks: "unknown".} =
self.containsStmt.dispose
self.getStmt.dispose
@ -295,9 +298,6 @@ proc close*(self: SQLiteDatastore) =
self.env.dispose
self[] = SQLiteDatastore()[]
proc timestamp*(t = epochTime()): int64 =
(t * 1_000_000).int64
method contains*(
self: SQLiteDatastore,
key: Key): Future[?!bool] {.async, locks: "unknown".} =

View File

@ -28,6 +28,10 @@ proc new*(
proc stores*(self: TieredDatastore): seq[Datastore] =
self.stores
method close*(self: TieredDatastore) {.async, locks: "unknown".} =
for store in self.stores:
await store.close
method contains*(
self: TieredDatastore,
key: Key): Future[?!bool] {.async, locks: "unknown".} =

View File

@ -26,7 +26,7 @@ suite "SQLiteDatastore":
createDir(basePathAbs)
teardown:
if not ds.isNil: ds.close
if not ds.isNil: await ds.close
ds = nil
removeDir(basePathAbs)
require(not dirExists(basePathAbs))
@ -49,7 +49,7 @@ suite "SQLiteDatastore":
dsRes.isOk
fileExists(dbPathAbs)
dsRes.get.close
await dsRes.get.close
removeDir(basePathAbs)
assert not dirExists(basePathAbs)
createDir(basePathAbs)
@ -60,7 +60,7 @@ suite "SQLiteDatastore":
dsRes.isOk
fileExists(dbPathAbs)
dsRes.get.close
await dsRes.get.close
# for `readOnly = true` to succeed the database file must already exist, so
# the existing file (per previous step) is not deleted prior to the next
@ -70,7 +70,7 @@ suite "SQLiteDatastore":
check: dsRes.isOk
dsRes.get.close
await dsRes.get.close
removeDir(basePathAbs)
assert not dirExists(basePathAbs)
createDir(basePathAbs)
@ -79,7 +79,7 @@ suite "SQLiteDatastore":
check: dsRes.isOk
dsRes.get.close
await dsRes.get.close
dsRes = SQLiteDatastore.new(memory, readOnly = true)
@ -95,7 +95,7 @@ suite "SQLiteDatastore":
asyncTest "helpers":
ds = SQLiteDatastore.new(basePath).get
ds.close
await ds.close
check:
ds.env.isNil
@ -107,7 +107,7 @@ suite "SQLiteDatastore":
# for `readOnly = true` to succeed the database file must already exist
ds = SQLiteDatastore.new(basePathAbs, filename).get
ds.close
await ds.close
ds = SQLiteDatastore.new(basePathAbs, filename, readOnly = true).get
var
@ -117,7 +117,7 @@ suite "SQLiteDatastore":
check: putRes.isErr
ds.close
await ds.close
removeDir(basePathAbs)
assert not dirExists(basePathAbs)
createDir(basePathAbs)
@ -211,7 +211,7 @@ suite "SQLiteDatastore":
# for `readOnly = true` to succeed the database file must already exist
ds = SQLiteDatastore.new(basePathAbs, filename).get
ds.close
await ds.close
ds = SQLiteDatastore.new(basePathAbs, filename, readOnly = true).get
var
@ -219,7 +219,7 @@ suite "SQLiteDatastore":
check: delRes.isErr
ds.close
await ds.close
removeDir(basePathAbs)
assert not dirExists(basePathAbs)
createDir(basePathAbs)

View File

@ -30,7 +30,7 @@ suite "TieredDatastore":
ds2 = FileSystemDatastore.new(rootAbs).get
teardown:
if not ds1.isNil: ds1.close
if not ds1.isNil: await ds1.close
ds1 = nil
removeDir(rootAbs)
require(not dirExists(rootAbs))
@ -132,7 +132,7 @@ suite "TieredDatastore":
(await ds1.get(key)).get.get == bytes
(await ds2.get(key)).get.get == bytes
ds1.close
await ds1.close
ds1 = SQLiteDatastore.new(memory).get
ds = TieredDatastore.new(ds1, ds2).get