[node] support self.cache=nil in SQLiteStore
also fix a discrepancy where cli option `--cache-size` is documented as `0 disables the cache` in `codex/conf.nim`, but previously the value `0` would result in a cache being constructed with default parameter values for `CacheStore.new()` Closes #180
This commit is contained in:
parent
b7df2d151c
commit
2ecf750959
|
@ -102,11 +102,11 @@ proc new*(T: type CodexServer, config: CodexConf): T =
|
|||
.withTcpTransport({ServerFlags.ReuseAddr})
|
||||
.build()
|
||||
|
||||
let cache =
|
||||
var
|
||||
cache: CacheStore
|
||||
|
||||
if config.cacheSize > 0:
|
||||
CacheStore.new(cacheSize = config.cacheSize * MiB)
|
||||
else:
|
||||
CacheStore.new()
|
||||
cache = CacheStore.new(cacheSize = config.cacheSize * MiB)
|
||||
|
||||
let
|
||||
discoveryBootstrapNodes = config.bootstrapNodes
|
||||
|
|
|
@ -70,12 +70,16 @@ method getBlock*(
|
|||
## Save a copy to the cache if present in the database but not in the cache
|
||||
##
|
||||
|
||||
if not self.cache.isNil:
|
||||
trace "Getting block from cache or database", cid
|
||||
else:
|
||||
trace "Getting block from database", cid
|
||||
|
||||
if cid.isEmpty:
|
||||
trace "Empty block, ignoring"
|
||||
return success cid.emptyBlock.some
|
||||
|
||||
if not self.cache.isNil:
|
||||
without cachedBlkOpt =? await self.cache.getBlock(cid), error:
|
||||
trace "Unable to read block from cache", cid, error = error.msg
|
||||
|
||||
|
@ -96,6 +100,7 @@ method getBlock*(
|
|||
trace "Unable to construct block from data", cid, error = error.msg
|
||||
return failure error
|
||||
|
||||
if not self.cache.isNil:
|
||||
let
|
||||
putCachedRes = await self.cache.putBlock(blk)
|
||||
|
||||
|
@ -111,7 +116,10 @@ method putBlock*(
|
|||
## Save a copy to the cache
|
||||
##
|
||||
|
||||
if not self.cache.isNil:
|
||||
trace "Putting block into database and cache", cid = blk.cid
|
||||
else:
|
||||
trace "Putting block into database", cid = blk.cid
|
||||
|
||||
if blk.isEmpty:
|
||||
trace "Empty block, ignoring"
|
||||
|
@ -127,6 +135,7 @@ method putBlock*(
|
|||
trace "Unable to store block in database", key = blkKey.id, error = putRes.error.msg
|
||||
return failure putRes.error
|
||||
|
||||
if not self.cache.isNil:
|
||||
let
|
||||
putCachedRes = await self.cache.putBlock(blk)
|
||||
|
||||
|
@ -141,12 +150,16 @@ method delBlock*(
|
|||
## Delete a block from the database and cache
|
||||
##
|
||||
|
||||
if not self.cache.isNil:
|
||||
trace "Deleting block from cache and database", cid
|
||||
else:
|
||||
trace "Deleting block from database", cid
|
||||
|
||||
if cid.isEmpty:
|
||||
trace "Empty block, ignoring"
|
||||
return success()
|
||||
|
||||
if not self.cache.isNil:
|
||||
let
|
||||
delCachedRes = await self.cache.delBlock(cid)
|
||||
|
||||
|
|
|
@ -15,7 +15,8 @@ import pkg/codex/stores
|
|||
|
||||
import ../helpers
|
||||
|
||||
suite "SQLite Store":
|
||||
proc runSuite(cache: bool) =
|
||||
suite "SQLite Store " & (if cache: "(cache enabled)" else: "(cache disabled)"):
|
||||
randomize()
|
||||
|
||||
var
|
||||
|
@ -37,7 +38,12 @@ suite "SQLite Store":
|
|||
setup:
|
||||
removeDir(repoDir)
|
||||
require(not dirExists(repoDir))
|
||||
|
||||
if cache:
|
||||
store = SQLiteStore.new(repoDir)
|
||||
else:
|
||||
store = SQLiteStore.new(repoDir, nil)
|
||||
|
||||
newBlock = randomBlock()
|
||||
|
||||
teardown:
|
||||
|
@ -56,7 +62,7 @@ suite "SQLite Store":
|
|||
blkKey = blkKeyRes.get
|
||||
|
||||
var
|
||||
# bypass cache
|
||||
# bypass enabled cache
|
||||
containsRes = await store.datastore.contains(blkKey)
|
||||
|
||||
assert containsRes.isOk
|
||||
|
@ -67,7 +73,7 @@ suite "SQLite Store":
|
|||
|
||||
check: putRes.isOk
|
||||
|
||||
# bypass cache
|
||||
# bypass enabled cache
|
||||
containsRes = await store.datastore.contains(blkKey)
|
||||
|
||||
assert containsRes.isOk
|
||||
|
@ -88,7 +94,7 @@ suite "SQLite Store":
|
|||
assert kRes.isOk
|
||||
|
||||
let
|
||||
# bypass cache
|
||||
# bypass enabled cache
|
||||
pRes = await store.datastore.put(kRes.get, b.data)
|
||||
|
||||
assert pRes.isOk
|
||||
|
@ -99,7 +105,7 @@ suite "SQLite Store":
|
|||
assert blkKeyRes.isOk
|
||||
|
||||
var
|
||||
# bypass cache
|
||||
# bypass enabled cache
|
||||
putRes = await store.datastore.put(blkKeyRes.get, newBlock.data)
|
||||
|
||||
assert putRes.isOk
|
||||
|
@ -116,7 +122,7 @@ suite "SQLite Store":
|
|||
assert kRes.isOk
|
||||
|
||||
let
|
||||
# bypass cache
|
||||
# bypass enabled cache
|
||||
pRes = await store.datastore.put(kRes.get, b.data)
|
||||
|
||||
assert pRes.isOk
|
||||
|
@ -134,7 +140,7 @@ suite "SQLite Store":
|
|||
blkOpt.isSome
|
||||
blkOpt.get == newBlock
|
||||
|
||||
# get from cache
|
||||
# get from enabled cache
|
||||
getRes = await store.getBlock(newBlock.cid)
|
||||
|
||||
check: getRes.isOk
|
||||
|
@ -223,3 +229,6 @@ suite "SQLite Store":
|
|||
check:
|
||||
delRes.isOk
|
||||
not (await newBlock.cid in store)
|
||||
|
||||
runSuite(cache = true)
|
||||
runSuite(cache = false)
|
||||
|
|
Loading…
Reference in New Issue