From 70e4b2e5eb863ee417977ada45ce1c38cae8b427 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 5 Apr 2022 08:24:48 -0600 Subject: [PATCH] handle empty blocks (#70) --- dagger/stores/cachestore.nim | 28 +++++++++++++++++++++++++--- dagger/stores/fsstore.nim | 27 ++++++++++++++++++++++++++- dagger/stores/networkstore.nim | 4 ++++ 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/dagger/stores/cachestore.nim b/dagger/stores/cachestore.nim index 8f736520..ba879398 100644 --- a/dagger/stores/cachestore.nim +++ b/dagger/stores/cachestore.nim @@ -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: diff --git a/dagger/stores/fsstore.nim b/dagger/stores/fsstore.nim index 2b125842..29027166 100644 --- a/dagger/stores/fsstore.nim +++ b/dagger/stores/fsstore.nim @@ -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*( diff --git a/dagger/stores/networkstore.nim b/dagger/stores/networkstore.nim index 0b361caa..0d2320e0 100644 --- a/dagger/stores/networkstore.nim +++ b/dagger/stores/networkstore.nim @@ -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)):