2022-05-19 19:56:03 +00:00
|
|
|
## Nim-Codex
|
2022-01-10 15:32:56 +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-03-18 22:17:51 +00:00
|
|
|
import pkg/upraises
|
|
|
|
|
|
|
|
push: {.upraises: [].}
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
import std/os
|
|
|
|
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/chronicles
|
|
|
|
import pkg/libp2p
|
|
|
|
import pkg/questionable
|
|
|
|
import pkg/questionable/results
|
|
|
|
import pkg/stew/io2
|
|
|
|
|
2022-03-02 16:30:42 +00:00
|
|
|
import ./cachestore
|
2022-01-10 15:32:56 +00:00
|
|
|
import ./blockstore
|
|
|
|
|
|
|
|
export blockstore
|
|
|
|
|
|
|
|
logScope:
|
2022-05-19 19:56:03 +00:00
|
|
|
topics = "codex fsstore"
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
FSStore* = ref object of BlockStore
|
|
|
|
cache: BlockStore
|
|
|
|
repoDir: string
|
|
|
|
postfixLen*: int
|
|
|
|
|
|
|
|
template blockPath*(self: FSStore, cid: Cid): string =
|
|
|
|
self.repoDir / ($cid)[^self.postfixLen..^1] / $cid
|
|
|
|
|
|
|
|
method getBlock*(
|
|
|
|
self: FSStore,
|
|
|
|
cid: Cid): Future[?!Block] {.async.} =
|
|
|
|
## Get a block from the stores
|
|
|
|
##
|
|
|
|
|
2022-04-05 14:24:48 +00:00
|
|
|
if cid.isEmpty:
|
|
|
|
trace "Empty block, ignoring"
|
|
|
|
return cid.emptyBlock.success
|
|
|
|
|
|
|
|
if cid in self.cache:
|
|
|
|
return await self.cache.getBlock(cid)
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
if cid notin self:
|
|
|
|
return Block.failure("Couldn't find block in fs store")
|
|
|
|
|
|
|
|
var data: seq[byte]
|
|
|
|
let path = self.blockPath(cid)
|
|
|
|
if (
|
|
|
|
let res = io2.readFile(path, data);
|
|
|
|
res.isErr):
|
|
|
|
let error = io2.ioErrorMsg(res.error)
|
|
|
|
trace "Cannot read file from fs store", path , error
|
|
|
|
return Block.failure("Cannot read file from fs store")
|
|
|
|
|
2022-03-18 19:50:53 +00:00
|
|
|
return Block.new(cid, data)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
method putBlock*(
|
|
|
|
self: FSStore,
|
|
|
|
blk: Block): Future[bool] {.async.} =
|
|
|
|
## Put a block to the blockstore
|
|
|
|
##
|
|
|
|
|
2022-04-05 14:24:48 +00:00
|
|
|
if blk.isEmpty:
|
|
|
|
trace "Empty block, ignoring"
|
|
|
|
return true
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
if blk.cid in self:
|
|
|
|
return true
|
|
|
|
|
|
|
|
# if directory exists it wont fail
|
|
|
|
if io2.createPath(self.blockPath(blk.cid).parentDir).isErr:
|
|
|
|
trace "Unable to create block prefix dir", dir = self.blockPath(blk.cid).parentDir
|
|
|
|
return false
|
|
|
|
|
|
|
|
let path = self.blockPath(blk.cid)
|
|
|
|
if (
|
|
|
|
let res = io2.writeFile(path, blk.data);
|
|
|
|
res.isErr):
|
|
|
|
let error = io2.ioErrorMsg(res.error)
|
|
|
|
trace "Unable to store block", path, cid = blk.cid, error
|
|
|
|
return false
|
|
|
|
|
2022-05-12 20:02:30 +00:00
|
|
|
if not (await self.cache.putBlock(blk)):
|
2022-04-05 14:24:48 +00:00
|
|
|
trace "Unable to store block in cache", cid = blk.cid
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
return true
|
|
|
|
|
|
|
|
method delBlock*(
|
|
|
|
self: FSStore,
|
2022-06-28 16:10:05 +00:00
|
|
|
cid: Cid): Future[?!void] {.async.} =
|
|
|
|
## Delete a block from the blockstore
|
2022-01-10 15:32:56 +00:00
|
|
|
##
|
|
|
|
|
2022-06-28 16:10:05 +00:00
|
|
|
trace "Deleting block from filestore", cid
|
2022-04-05 14:24:48 +00:00
|
|
|
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
|
|
|
|
path = self.blockPath(cid)
|
|
|
|
res = io2.removeFile(path)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-06-28 16:10:05 +00:00
|
|
|
if res.isErr:
|
|
|
|
let errmsg = io2.ioErrorMsg(res.error)
|
|
|
|
trace "Unable to delete block", path, cid, errmsg
|
|
|
|
return errmsg.failure
|
2022-04-05 14:24:48 +00:00
|
|
|
|
2022-06-28 16:10:05 +00:00
|
|
|
return await self.cache.delBlock(cid)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
method hasBlock*(self: FSStore, cid: Cid): bool =
|
|
|
|
## Check if the block exists in the blockstore
|
|
|
|
##
|
|
|
|
|
2022-04-05 14:24:48 +00:00
|
|
|
trace "Checking for block existence", cid
|
|
|
|
if cid.isEmpty:
|
|
|
|
trace "Empty block, ignoring"
|
|
|
|
return true
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
self.blockPath(cid).isFile()
|
|
|
|
|
2022-05-12 20:02:30 +00:00
|
|
|
method listBlocks*(self: FSStore, onBlock: OnBlock) {.async.} =
|
2022-05-19 02:29:15 +00:00
|
|
|
debug "Listing all blocks in store"
|
2022-05-12 20:02:30 +00:00
|
|
|
for (pkind, folderPath) in self.repoDir.walkDir():
|
2022-04-13 16:32:35 +00:00
|
|
|
if pkind != pcDir: continue
|
|
|
|
let baseName = basename(folderPath)
|
2022-05-12 20:02:30 +00:00
|
|
|
if baseName.len != self.postfixLen: continue
|
2022-04-13 16:32:35 +00:00
|
|
|
|
|
|
|
for (fkind, filePath) in folderPath.walkDir(false):
|
|
|
|
if fkind != pcFile: continue
|
|
|
|
let cid = Cid.init(basename(filePath))
|
|
|
|
if cid.isOk:
|
2022-05-12 20:02:30 +00:00
|
|
|
# getting a weird `Error: unhandled exception: index 1 not in 0 .. 0 [IndexError]`
|
|
|
|
# compilation error if using different syntax/construct bellow
|
|
|
|
try:
|
|
|
|
await onBlock(cid.get())
|
2022-05-19 02:29:15 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
trace "Cancelling list blocks"
|
|
|
|
raise exc
|
2022-05-12 20:02:30 +00:00
|
|
|
except CatchableError as exc:
|
|
|
|
trace "Couldn't get block", cid = $(cid.get())
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
proc new*(
|
|
|
|
T: type FSStore,
|
|
|
|
repoDir: string,
|
|
|
|
postfixLen = 2,
|
2022-03-02 16:30:42 +00:00
|
|
|
cache: BlockStore = CacheStore.new()): T =
|
2022-01-10 15:32:56 +00:00
|
|
|
T(
|
|
|
|
postfixLen: postfixLen,
|
|
|
|
repoDir: repoDir,
|
|
|
|
cache: cache)
|