handle empty blocks (#70)

This commit is contained in:
Dmitriy Ryajov 2022-04-05 08:24:48 -06:00 committed by GitHub
parent 8ef8cfcd74
commit 70e4b2e5eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 4 deletions

View File

@ -22,7 +22,6 @@ import pkg/questionable
import pkg/questionable/results
import ./blockstore
import ../blocktype
import ../chunker
import ../errors
@ -50,28 +49,40 @@ method getBlock*(
## Get a block from the stores
##
trace "Getting block from cache", cid
if cid.isEmpty:
trace "Empty block, ignoring"
return cid.emptyBlock.success
return self.cache[cid].catch()
method hasBlock*(self: CacheStore, cid: Cid): bool =
## check if the block exists
##
self.cache.contains(cid)
trace "Checking for block presence in cache", cid
if cid.isEmpty:
trace "Empty block, ignoring"
return true
cid in self.cache
func putBlockSync(self: CacheStore, blk: Block): bool =
let blkSize = blk.data.len # in bytes
if blkSize > self.size:
trace "Block size is larger than cache size", blk = blkSize, cache = self.size
return false
while self.currentSize + blkSize > self.size:
try:
let removed = self.cache.removeLru()
self.currentSize -= removed.data.len
except EmptyLruCacheError:
except EmptyLruCacheError as exc:
# if the cache is empty, can't remove anything, so break and add item
# to the cache
trace "Exception puting block to cache", exc = exc.msg
break
self.cache[blk.cid] = blk
@ -83,6 +94,12 @@ method putBlock*(
blk: Block): Future[bool] {.async.} =
## Put a block to the blockstore
##
trace "Storing block in cache", cid = blk.cid
if blk.isEmpty:
trace "Empty block, ignoring"
return true
return self.putBlockSync(blk)
method delBlock*(
@ -91,6 +108,11 @@ method delBlock*(
## delete a block/s from the block store
##
trace "Deleting block from cache", cid
if cid.isEmpty:
trace "Empty block, ignoring"
return true
try:
let removed = self.cache.del(cid)
if removed.isSome:

View File

@ -22,7 +22,6 @@ import pkg/stew/io2
import ./cachestore
import ./blockstore
import ../blocktype
export blockstore
@ -44,6 +43,13 @@ method getBlock*(
## Get a block from the stores
##
if cid.isEmpty:
trace "Empty block, ignoring"
return cid.emptyBlock.success
if cid in self.cache:
return await self.cache.getBlock(cid)
if cid notin self:
return Block.failure("Couldn't find block in fs store")
@ -64,6 +70,10 @@ method putBlock*(
## Put a block to the blockstore
##
if blk.isEmpty:
trace "Empty block, ignoring"
return true
if blk.cid in self:
return true
@ -80,6 +90,9 @@ method putBlock*(
trace "Unable to store block", path, cid = blk.cid, error
return false
if await self.cache.putBlock(blk):
trace "Unable to store block in cache", cid = blk.cid
return true
method delBlock*(
@ -88,6 +101,10 @@ method delBlock*(
## Delete a block/s from the block store
##
if cid.isEmpty:
trace "Empty block, ignoring"
return true
let path = self.blockPath(cid)
if (
let res = io2.removeFile(path);
@ -96,12 +113,20 @@ method delBlock*(
trace "Unable to delete block", path, cid, error
return false
if await self.cache.delBlock(cid):
trace "Unable to store block in cache", cid
return true
method hasBlock*(self: FSStore, cid: Cid): bool =
## Check if the block exists in the blockstore
##
trace "Checking for block existence", cid
if cid.isEmpty:
trace "Empty block, ignoring"
return true
self.blockPath(cid).isFile()
proc new*(

View File

@ -54,6 +54,10 @@ method getBlock*(
method putBlock*(
self: NetworkStore,
blk: bt.Block): Future[bool] {.async.} =
## Store block locally and notify the
## network
##
trace "Puting block", cid = blk.cid
if not (await self.localStore.putBlock(blk)):