2022-05-19 19:56:03 +00:00
|
|
|
## Nim-Codex
|
2022-03-02 16:30:42 +00:00
|
|
|
## Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
2022-04-13 16:32:35 +00:00
|
|
|
import std/sequtils
|
2022-03-18 22:17:51 +00:00
|
|
|
import pkg/upraises
|
|
|
|
|
|
|
|
push: {.upraises: [].}
|
|
|
|
|
2022-03-02 16:30:42 +00:00
|
|
|
import std/options
|
|
|
|
|
|
|
|
import pkg/chronicles
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/libp2p
|
|
|
|
import pkg/lrucache
|
|
|
|
import pkg/questionable
|
|
|
|
import pkg/questionable/results
|
|
|
|
|
|
|
|
import ./blockstore
|
|
|
|
import ../chunker
|
|
|
|
import ../errors
|
|
|
|
|
|
|
|
export blockstore
|
|
|
|
|
|
|
|
logScope:
|
2022-05-19 19:56:03 +00:00
|
|
|
topics = "codex cachestore"
|
2022-03-02 16:30:42 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
CacheStore* = ref object of BlockStore
|
|
|
|
currentSize*: Natural # in bytes
|
|
|
|
size*: Positive # in bytes
|
|
|
|
cache: LruCache[Cid, Block]
|
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
InvalidBlockSize* = object of CodexError
|
2022-03-02 16:30:42 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
MiB* = 1024 * 1024 # bytes, 1 mebibyte = 1,048,576 bytes
|
|
|
|
DefaultCacheSizeMiB* = 100
|
|
|
|
DefaultCacheSize* = DefaultCacheSizeMiB * MiB # bytes
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
method getBlock*(self: CacheStore, cid: Cid): Future[?! (? Block)] {.async.} =
|
2022-03-02 16:30:42 +00:00
|
|
|
## Get a block from the stores
|
|
|
|
##
|
|
|
|
|
2022-04-05 14:24:48 +00:00
|
|
|
trace "Getting block from cache", cid
|
|
|
|
if cid.isEmpty:
|
|
|
|
trace "Empty block, ignoring"
|
2022-07-28 00:39:17 +00:00
|
|
|
return cid.emptyBlock.some.success
|
2022-04-05 14:24:48 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
if cid notin self.cache:
|
|
|
|
return Block.none.success
|
2022-03-02 16:30:42 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
try:
|
|
|
|
let blk = self.cache[cid]
|
|
|
|
return blk.some.success
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "Exception requesting block", cid, exc = exc.msg
|
|
|
|
return failure(exc)
|
|
|
|
|
|
|
|
method hasBlock*(self: CacheStore, cid: Cid): Future[?!bool] {.async.} =
|
|
|
|
## Check if the block exists in the blockstore
|
2022-03-02 16:30:42 +00:00
|
|
|
##
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
trace "Checking CacheStore for block presence", cid
|
2022-04-05 14:24:48 +00:00
|
|
|
if cid.isEmpty:
|
|
|
|
trace "Empty block, ignoring"
|
2022-07-28 00:39:17 +00:00
|
|
|
return true.success
|
|
|
|
|
|
|
|
return (cid in self.cache).success
|
2022-04-05 14:24:48 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
method listBlocks*(s: CacheStore, onBlock: OnBlock): Future[?!void] {.async.} =
|
|
|
|
## Get the list of blocks in the BlockStore. This is an intensive operation
|
|
|
|
##
|
2022-03-02 16:30:42 +00:00
|
|
|
|
2022-05-12 20:02:30 +00:00
|
|
|
for cid in toSeq(s.cache.keys):
|
|
|
|
await onBlock(cid)
|
2022-04-13 16:32:35 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
return success()
|
|
|
|
|
2022-03-02 16:30:42 +00:00
|
|
|
func putBlockSync(self: CacheStore, blk: Block): bool =
|
|
|
|
|
|
|
|
let blkSize = blk.data.len # in bytes
|
|
|
|
|
|
|
|
if blkSize > self.size:
|
2022-04-05 14:24:48 +00:00
|
|
|
trace "Block size is larger than cache size", blk = blkSize, cache = self.size
|
2022-03-02 16:30:42 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
while self.currentSize + blkSize > self.size:
|
|
|
|
try:
|
|
|
|
let removed = self.cache.removeLru()
|
|
|
|
self.currentSize -= removed.data.len
|
2022-04-05 14:24:48 +00:00
|
|
|
except EmptyLruCacheError as exc:
|
2022-03-02 16:30:42 +00:00
|
|
|
# if the cache is empty, can't remove anything, so break and add item
|
|
|
|
# to the cache
|
2022-04-05 14:24:48 +00:00
|
|
|
trace "Exception puting block to cache", exc = exc.msg
|
2022-03-02 16:30:42 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
self.cache[blk.cid] = blk
|
|
|
|
self.currentSize += blkSize
|
|
|
|
return true
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
method putBlock*(self: CacheStore, blk: Block): Future[?!void] {.async.} =
|
2022-03-02 16:30:42 +00:00
|
|
|
## Put a block to the blockstore
|
|
|
|
##
|
2022-04-05 14:24:48 +00:00
|
|
|
|
|
|
|
trace "Storing block in cache", cid = blk.cid
|
|
|
|
if blk.isEmpty:
|
|
|
|
trace "Empty block, ignoring"
|
2022-07-28 00:39:17 +00:00
|
|
|
return success()
|
2022-04-05 14:24:48 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
discard self.putBlockSync(blk)
|
|
|
|
return success()
|
2022-03-02 16:30:42 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
method delBlock*(self: CacheStore, cid: Cid): Future[?!void] {.async.} =
|
2022-06-28 16:10:05 +00:00
|
|
|
## Delete a block from the blockstore
|
2022-03-02 16:30:42 +00:00
|
|
|
##
|
|
|
|
|
2022-04-05 14:24:48 +00:00
|
|
|
trace "Deleting block from cache", cid
|
|
|
|
if cid.isEmpty:
|
|
|
|
trace "Empty block, ignoring"
|
2022-06-28 16:10:05 +00:00
|
|
|
return success()
|
2022-04-05 14:24:48 +00:00
|
|
|
|
2022-06-28 16:10:05 +00:00
|
|
|
let removed = self.cache.del(cid)
|
|
|
|
if removed.isSome:
|
|
|
|
self.currentSize -= removed.get.data.len
|
|
|
|
|
|
|
|
return success()
|
2022-03-02 16:30:42 +00:00
|
|
|
|
2022-07-22 23:38:49 +00:00
|
|
|
method close*(self: CacheStore): Future[void] {.async.} =
|
|
|
|
## Close the blockstore, a no-op for this implementation
|
|
|
|
##
|
|
|
|
|
|
|
|
discard
|
|
|
|
|
2022-03-02 16:30:42 +00:00
|
|
|
func new*(
|
|
|
|
_: type CacheStore,
|
|
|
|
blocks: openArray[Block] = [],
|
|
|
|
cacheSize: Positive = DefaultCacheSize, # in bytes
|
|
|
|
chunkSize: Positive = DefaultChunkSize # in bytes
|
|
|
|
): CacheStore {.raises: [Defect, ValueError].} =
|
|
|
|
|
|
|
|
if cacheSize < chunkSize:
|
|
|
|
raise newException(ValueError, "cacheSize cannot be less than chunkSize")
|
|
|
|
|
|
|
|
var currentSize = 0
|
|
|
|
let
|
|
|
|
size = cacheSize div chunkSize
|
|
|
|
cache = newLruCache[Cid, Block](size)
|
|
|
|
store = CacheStore(
|
|
|
|
cache: cache,
|
|
|
|
currentSize: currentSize,
|
2022-03-17 13:56:46 +00:00
|
|
|
size: cacheSize)
|
2022-03-02 16:30:42 +00:00
|
|
|
|
|
|
|
for blk in blocks:
|
|
|
|
discard store.putBlockSync(blk)
|
|
|
|
|
|
|
|
return store
|