mirror of
https://github.com/codex-storage/nim-codex.git
synced 2025-01-18 17:02:26 +00:00
handle empty blocks (#70)
This commit is contained in:
parent
8ef8cfcd74
commit
70e4b2e5eb
@ -22,7 +22,6 @@ import pkg/questionable
|
|||||||
import pkg/questionable/results
|
import pkg/questionable/results
|
||||||
|
|
||||||
import ./blockstore
|
import ./blockstore
|
||||||
import ../blocktype
|
|
||||||
import ../chunker
|
import ../chunker
|
||||||
import ../errors
|
import ../errors
|
||||||
|
|
||||||
@ -50,28 +49,40 @@ method getBlock*(
|
|||||||
## Get a block from the stores
|
## 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()
|
return self.cache[cid].catch()
|
||||||
|
|
||||||
method hasBlock*(self: CacheStore, cid: Cid): bool =
|
method hasBlock*(self: CacheStore, cid: Cid): bool =
|
||||||
## check if the block exists
|
## 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 =
|
func putBlockSync(self: CacheStore, blk: Block): bool =
|
||||||
|
|
||||||
let blkSize = blk.data.len # in bytes
|
let blkSize = blk.data.len # in bytes
|
||||||
|
|
||||||
if blkSize > self.size:
|
if blkSize > self.size:
|
||||||
|
trace "Block size is larger than cache size", blk = blkSize, cache = self.size
|
||||||
return false
|
return false
|
||||||
|
|
||||||
while self.currentSize + blkSize > self.size:
|
while self.currentSize + blkSize > self.size:
|
||||||
try:
|
try:
|
||||||
let removed = self.cache.removeLru()
|
let removed = self.cache.removeLru()
|
||||||
self.currentSize -= removed.data.len
|
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
|
# if the cache is empty, can't remove anything, so break and add item
|
||||||
# to the cache
|
# to the cache
|
||||||
|
trace "Exception puting block to cache", exc = exc.msg
|
||||||
break
|
break
|
||||||
|
|
||||||
self.cache[blk.cid] = blk
|
self.cache[blk.cid] = blk
|
||||||
@ -83,6 +94,12 @@ method putBlock*(
|
|||||||
blk: Block): Future[bool] {.async.} =
|
blk: Block): Future[bool] {.async.} =
|
||||||
## Put a block to the blockstore
|
## 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)
|
return self.putBlockSync(blk)
|
||||||
|
|
||||||
method delBlock*(
|
method delBlock*(
|
||||||
@ -91,6 +108,11 @@ method delBlock*(
|
|||||||
## delete a block/s from the block store
|
## delete a block/s from the block store
|
||||||
##
|
##
|
||||||
|
|
||||||
|
trace "Deleting block from cache", cid
|
||||||
|
if cid.isEmpty:
|
||||||
|
trace "Empty block, ignoring"
|
||||||
|
return true
|
||||||
|
|
||||||
try:
|
try:
|
||||||
let removed = self.cache.del(cid)
|
let removed = self.cache.del(cid)
|
||||||
if removed.isSome:
|
if removed.isSome:
|
||||||
|
@ -22,7 +22,6 @@ import pkg/stew/io2
|
|||||||
|
|
||||||
import ./cachestore
|
import ./cachestore
|
||||||
import ./blockstore
|
import ./blockstore
|
||||||
import ../blocktype
|
|
||||||
|
|
||||||
export blockstore
|
export blockstore
|
||||||
|
|
||||||
@ -44,6 +43,13 @@ method getBlock*(
|
|||||||
## Get a block from the stores
|
## 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:
|
if cid notin self:
|
||||||
return Block.failure("Couldn't find block in fs store")
|
return Block.failure("Couldn't find block in fs store")
|
||||||
|
|
||||||
@ -64,6 +70,10 @@ method putBlock*(
|
|||||||
## Put a block to the blockstore
|
## Put a block to the blockstore
|
||||||
##
|
##
|
||||||
|
|
||||||
|
if blk.isEmpty:
|
||||||
|
trace "Empty block, ignoring"
|
||||||
|
return true
|
||||||
|
|
||||||
if blk.cid in self:
|
if blk.cid in self:
|
||||||
return true
|
return true
|
||||||
|
|
||||||
@ -80,6 +90,9 @@ method putBlock*(
|
|||||||
trace "Unable to store block", path, cid = blk.cid, error
|
trace "Unable to store block", path, cid = blk.cid, error
|
||||||
return false
|
return false
|
||||||
|
|
||||||
|
if await self.cache.putBlock(blk):
|
||||||
|
trace "Unable to store block in cache", cid = blk.cid
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
|
||||||
method delBlock*(
|
method delBlock*(
|
||||||
@ -88,6 +101,10 @@ method delBlock*(
|
|||||||
## Delete a block/s from the block store
|
## Delete a block/s from the block store
|
||||||
##
|
##
|
||||||
|
|
||||||
|
if cid.isEmpty:
|
||||||
|
trace "Empty block, ignoring"
|
||||||
|
return true
|
||||||
|
|
||||||
let path = self.blockPath(cid)
|
let path = self.blockPath(cid)
|
||||||
if (
|
if (
|
||||||
let res = io2.removeFile(path);
|
let res = io2.removeFile(path);
|
||||||
@ -96,12 +113,20 @@ method delBlock*(
|
|||||||
trace "Unable to delete block", path, cid, error
|
trace "Unable to delete block", path, cid, error
|
||||||
return false
|
return false
|
||||||
|
|
||||||
|
if await self.cache.delBlock(cid):
|
||||||
|
trace "Unable to store block in cache", cid
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
|
||||||
method hasBlock*(self: FSStore, cid: Cid): bool =
|
method hasBlock*(self: FSStore, cid: Cid): bool =
|
||||||
## Check if the block exists in the blockstore
|
## 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()
|
self.blockPath(cid).isFile()
|
||||||
|
|
||||||
proc new*(
|
proc new*(
|
||||||
|
@ -54,6 +54,10 @@ method getBlock*(
|
|||||||
method putBlock*(
|
method putBlock*(
|
||||||
self: NetworkStore,
|
self: NetworkStore,
|
||||||
blk: bt.Block): Future[bool] {.async.} =
|
blk: bt.Block): Future[bool] {.async.} =
|
||||||
|
## Store block locally and notify the
|
||||||
|
## network
|
||||||
|
##
|
||||||
|
|
||||||
trace "Puting block", cid = blk.cid
|
trace "Puting block", cid = blk.cid
|
||||||
|
|
||||||
if not (await self.localStore.putBlock(blk)):
|
if not (await self.localStore.putBlock(blk)):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user