2022-05-11 10:50:05 -05:00
|
|
|
import std/options
|
|
|
|
|
import std/os
|
|
|
|
|
|
2022-09-19 17:14:06 -06:00
|
|
|
import pkg/asynctest
|
2022-06-29 11:04:35 -05:00
|
|
|
import pkg/chronos
|
2022-05-11 10:50:05 -05:00
|
|
|
import pkg/stew/results
|
2022-09-19 17:23:19 -06:00
|
|
|
import pkg/stew/byteutils
|
2022-05-11 10:50:05 -05:00
|
|
|
|
2022-09-19 17:14:06 -06:00
|
|
|
import pkg/datastore/fsds
|
|
|
|
|
import pkg/datastore/sql
|
|
|
|
|
import pkg/datastore/tieredds
|
2022-05-11 10:50:05 -05:00
|
|
|
|
2022-09-20 20:18:33 -04:00
|
|
|
import ./dscommontests
|
2022-09-19 17:23:19 -06:00
|
|
|
|
2022-09-21 20:13:50 -04:00
|
|
|
suite "Test Basic Tired Datastore":
|
2022-09-19 17:23:19 -06:00
|
|
|
let
|
|
|
|
|
bytes = "some bytes".toBytes
|
|
|
|
|
otherBytes = "some other bytes".toBytes
|
|
|
|
|
key = Key.init("a:b/c/d:e").get
|
|
|
|
|
root = "tests" / "test_data"
|
2022-11-22 15:23:23 -06:00
|
|
|
path = currentSourcePath() # get this file's name
|
2022-09-19 17:23:19 -06:00
|
|
|
rootAbs = path.parentDir / root
|
|
|
|
|
|
|
|
|
|
var
|
|
|
|
|
ds1: SQLiteDatastore
|
|
|
|
|
ds2: FSDatastore
|
|
|
|
|
tiredDs: TieredDatastore
|
|
|
|
|
|
|
|
|
|
setupAll:
|
|
|
|
|
removeDir(rootAbs)
|
|
|
|
|
require(not dirExists(rootAbs))
|
|
|
|
|
createDir(rootAbs)
|
|
|
|
|
|
|
|
|
|
ds1 = SQLiteDatastore.new(Memory).tryGet
|
|
|
|
|
ds2 = FSDatastore.new(rootAbs, depth = 5).tryGet
|
|
|
|
|
tiredDs = TieredDatastore.new(@[ds1, ds2]).tryGet
|
|
|
|
|
|
|
|
|
|
teardownAll:
|
|
|
|
|
removeDir(rootAbs)
|
|
|
|
|
require(not dirExists(rootAbs))
|
|
|
|
|
|
|
|
|
|
basicStoreTests(tiredDs, key, bytes, otherBytes)
|
|
|
|
|
|
2022-05-11 10:50:05 -05:00
|
|
|
suite "TieredDatastore":
|
2022-06-29 11:04:35 -05:00
|
|
|
# assumes tests/test_all is run from project root, e.g. with `nimble test`
|
|
|
|
|
let
|
|
|
|
|
bytes = @[1.byte, 2.byte, 3.byte]
|
|
|
|
|
key = Key.init("a:b/c/d:e").get
|
|
|
|
|
root = "tests" / "test_data"
|
2022-11-22 15:23:23 -06:00
|
|
|
path = currentSourcePath() # get this file's name
|
2022-09-19 17:14:06 -06:00
|
|
|
rootAbs = path.parentDir / root
|
2022-05-11 10:50:05 -05:00
|
|
|
|
2022-06-29 11:04:35 -05:00
|
|
|
var
|
|
|
|
|
ds1: SQLiteDatastore
|
2022-09-19 17:14:06 -06:00
|
|
|
ds2: FSDatastore
|
2022-05-11 10:50:05 -05:00
|
|
|
|
2022-06-29 11:04:35 -05:00
|
|
|
setup:
|
2022-05-11 10:50:05 -05:00
|
|
|
removeDir(rootAbs)
|
|
|
|
|
require(not dirExists(rootAbs))
|
2022-08-03 20:54:50 -05:00
|
|
|
createDir(rootAbs)
|
2022-09-19 17:14:06 -06:00
|
|
|
ds1 = SQLiteDatastore.new(Memory).get
|
|
|
|
|
ds2 = FSDatastore.new(rootAbs, depth = 5).get
|
2022-05-11 10:50:05 -05:00
|
|
|
|
|
|
|
|
teardown:
|
2022-09-19 17:14:06 -06:00
|
|
|
if not ds1.isNil:
|
|
|
|
|
discard await ds1.close
|
|
|
|
|
|
2022-06-29 11:04:35 -05:00
|
|
|
ds1 = nil
|
2022-09-19 17:14:06 -06:00
|
|
|
|
2022-05-11 10:50:05 -05:00
|
|
|
removeDir(rootAbs)
|
|
|
|
|
require(not dirExists(rootAbs))
|
|
|
|
|
|
2022-09-19 15:54:19 -06:00
|
|
|
test "new":
|
2022-05-11 10:50:05 -05:00
|
|
|
check:
|
|
|
|
|
TieredDatastore.new().isErr
|
|
|
|
|
TieredDatastore.new([]).isErr
|
|
|
|
|
TieredDatastore.new(@[]).isErr
|
|
|
|
|
TieredDatastore.new(ds1, ds2).isOk
|
|
|
|
|
TieredDatastore.new([ds1, ds2]).isOk
|
|
|
|
|
TieredDatastore.new(@[ds1, ds2]).isOk
|
|
|
|
|
|
2022-09-19 15:54:19 -06:00
|
|
|
test "accessors":
|
2022-05-11 10:50:05 -05:00
|
|
|
let
|
|
|
|
|
stores = @[ds1, ds2]
|
|
|
|
|
|
|
|
|
|
check:
|
2022-09-19 17:14:06 -06:00
|
|
|
TieredDatastore.new(ds1, ds2).tryGet.stores == stores
|
|
|
|
|
TieredDatastore.new([ds1, ds2]).tryGet.stores == stores
|
|
|
|
|
TieredDatastore.new(@[ds1, ds2]).tryGet.stores == stores
|
2022-05-11 10:50:05 -05:00
|
|
|
|
2022-09-19 15:54:19 -06:00
|
|
|
test "put":
|
2022-05-11 10:50:05 -05:00
|
|
|
let
|
|
|
|
|
ds = TieredDatastore.new(ds1, ds2).get
|
2022-06-29 11:04:35 -05:00
|
|
|
putRes = await ds.put(key, bytes)
|
2022-05-11 10:50:05 -05:00
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
putRes.isOk
|
2022-09-19 17:14:06 -06:00
|
|
|
(await ds1.get(key)).tryGet == bytes
|
|
|
|
|
(await ds2.get(key)).tryGet == bytes
|
2022-05-11 10:50:05 -05:00
|
|
|
|
2022-09-19 15:54:19 -06:00
|
|
|
test "delete":
|
2022-05-11 10:50:05 -05:00
|
|
|
let
|
|
|
|
|
ds = TieredDatastore.new(ds1, ds2).get
|
|
|
|
|
|
2022-09-19 17:14:06 -06:00
|
|
|
(await ds.put(key, bytes)).tryGet
|
|
|
|
|
(await ds.delete(key)).tryGet
|
2022-05-11 10:50:05 -05:00
|
|
|
|
2022-11-22 15:23:23 -06:00
|
|
|
expect DatastoreKeyNotFound:
|
|
|
|
|
discard (await ds1.get(key)).tryGet
|
2022-05-11 10:50:05 -05:00
|
|
|
|
2022-09-19 17:14:06 -06:00
|
|
|
expect DatastoreKeyNotFound:
|
|
|
|
|
discard (await ds2.get(key)).tryGet
|
2022-05-11 10:50:05 -05:00
|
|
|
|
2022-09-19 17:14:06 -06:00
|
|
|
test "contains":
|
2022-05-11 10:50:05 -05:00
|
|
|
let
|
2022-09-19 17:14:06 -06:00
|
|
|
ds = TieredDatastore.new(ds1, ds2).tryGet
|
2022-05-11 10:50:05 -05:00
|
|
|
|
2022-09-19 17:14:06 -06:00
|
|
|
check:
|
2022-12-02 16:25:44 -06:00
|
|
|
not (await ds1.has(key)).tryGet
|
|
|
|
|
not (await ds2.has(key)).tryGet
|
2022-05-11 10:50:05 -05:00
|
|
|
|
2022-09-19 17:14:06 -06:00
|
|
|
(await ds.put(key, bytes)).tryGet
|
2022-05-11 10:50:05 -05:00
|
|
|
|
|
|
|
|
check:
|
2022-12-02 16:25:44 -06:00
|
|
|
(await ds.has(key)).tryGet
|
|
|
|
|
(await ds1.has(key)).tryGet
|
|
|
|
|
(await ds2.has(key)).tryGet
|
2022-05-11 10:50:05 -05:00
|
|
|
|
2022-09-19 15:54:19 -06:00
|
|
|
test "get":
|
2022-05-11 10:50:05 -05:00
|
|
|
var
|
2022-09-19 17:14:06 -06:00
|
|
|
ds = TieredDatastore.new(ds1, ds2).tryGet
|
2022-05-11 10:50:05 -05:00
|
|
|
|
2022-09-19 17:14:06 -06:00
|
|
|
check:
|
2022-12-02 16:25:44 -06:00
|
|
|
not (await ds1.has(key)).tryGet
|
|
|
|
|
not (await ds2.has(key)).tryGet
|
|
|
|
|
not (await ds.has(key)).tryGet
|
2022-05-11 10:50:05 -05:00
|
|
|
|
2022-09-19 17:14:06 -06:00
|
|
|
(await ds.put(key, bytes)).tryGet
|
2022-05-11 10:50:05 -05:00
|
|
|
|
|
|
|
|
check:
|
2022-09-19 17:14:06 -06:00
|
|
|
(await ds.get(key)).tryGet == bytes
|
|
|
|
|
(await ds1.get(key)).tryGet == bytes
|
|
|
|
|
(await ds2.get(key)).tryGet == bytes
|
2022-05-11 10:50:05 -05:00
|
|
|
|
2022-09-19 17:14:06 -06:00
|
|
|
(await ds1.close()).tryGet
|
|
|
|
|
ds1 = nil
|
2022-05-11 10:50:05 -05:00
|
|
|
|
2022-09-19 17:14:06 -06:00
|
|
|
ds1 = SQLiteDatastore.new(Memory).tryGet
|
|
|
|
|
ds = TieredDatastore.new(ds1, ds2).tryGet
|
2022-05-11 10:50:05 -05:00
|
|
|
|
|
|
|
|
check:
|
2022-12-02 16:25:44 -06:00
|
|
|
not (await ds1.has(key)).tryGet
|
2022-09-19 17:14:06 -06:00
|
|
|
(await ds2.get(key)).tryGet == bytes
|
|
|
|
|
(await ds.get(key)).tryGet == bytes
|
|
|
|
|
(await ds1.get(key)).tryGet == bytes
|