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
|
|
|
|
|
2022-08-19 00:56:36 +00:00
|
|
|
method getBlock*(self: FSStore, cid: Cid): Future[?!Block] {.async.} =
|
|
|
|
## Get a block from the cache or filestore.
|
|
|
|
## Save a copy to the cache if present in the filestore but not in the cache
|
2022-01-10 15:32:56 +00:00
|
|
|
##
|
|
|
|
|
2022-08-19 00:56:36 +00:00
|
|
|
if not self.cache.isNil:
|
|
|
|
trace "Getting block from cache or filestore", cid
|
|
|
|
else:
|
|
|
|
trace "Getting block from filestore", cid
|
|
|
|
|
2022-04-05 14:24:48 +00:00
|
|
|
if cid.isEmpty:
|
|
|
|
trace "Empty block, ignoring"
|
2022-08-19 00:56:36 +00:00
|
|
|
return success cid.emptyBlock
|
|
|
|
|
|
|
|
if not self.cache.isNil:
|
|
|
|
let
|
|
|
|
cachedBlockRes = await self.cache.getBlock(cid)
|
2022-04-05 14:24:48 +00:00
|
|
|
|
2022-08-19 00:56:36 +00:00
|
|
|
if not cachedBlockRes.isErr:
|
|
|
|
return success cachedBlockRes.get
|
|
|
|
else:
|
|
|
|
trace "Unable to read block from cache", cid, error = cachedBlockRes.error.msg
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
# Read file contents
|
2022-08-19 00:56:36 +00:00
|
|
|
var
|
|
|
|
data: seq[byte]
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
let
|
|
|
|
path = self.blockPath(cid)
|
|
|
|
res = io2.readFile(path, data)
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
if res.isErr:
|
|
|
|
if not isFile(path): # May be, check instead that "res.error == ERROR_FILE_NOT_FOUND" ?
|
2022-08-19 00:56:36 +00:00
|
|
|
return failure (ref BlockNotFoundError)(msg: "Block not in filestore")
|
2022-07-28 00:39:17 +00:00
|
|
|
else:
|
2022-08-19 00:56:36 +00:00
|
|
|
let
|
|
|
|
error = io2.ioErrorMsg(res.error)
|
2022-07-28 00:39:17 +00:00
|
|
|
|
2022-08-19 00:56:36 +00:00
|
|
|
trace "Error requesting block from filestore", path, error
|
|
|
|
return failure "Error requesting block from filestore: " & error
|
|
|
|
|
|
|
|
without blk =? Block.new(cid, data), error:
|
|
|
|
trace "Unable to construct block from data", cid, error = error.msg
|
|
|
|
return failure error
|
|
|
|
|
|
|
|
if not self.cache.isNil:
|
|
|
|
let
|
|
|
|
putCachedRes = await self.cache.putBlock(blk)
|
2022-07-28 00:39:17 +00:00
|
|
|
|
2022-08-19 00:56:36 +00:00
|
|
|
if putCachedRes.isErr:
|
|
|
|
trace "Unable to store block in cache", cid, error = putCachedRes.error.msg
|
|
|
|
|
|
|
|
return success blk
|
2022-07-28 00:39:17 +00:00
|
|
|
|
|
|
|
method putBlock*(self: FSStore, blk: Block): Future[?!void] {.async.} =
|
2022-08-19 00:56:36 +00:00
|
|
|
## Write a block's contents to a file with name based on blk.cid.
|
|
|
|
## Save a copy to the cache
|
2022-01-10 15:32:56 +00:00
|
|
|
##
|
|
|
|
|
2022-08-19 00:56:36 +00:00
|
|
|
if not self.cache.isNil:
|
|
|
|
trace "Putting block into filestore and cache", cid = blk.cid
|
|
|
|
else:
|
|
|
|
trace "Putting block into filestore", cid = blk.cid
|
|
|
|
|
2022-04-05 14:24:48 +00:00
|
|
|
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
|
|
|
let path = self.blockPath(blk.cid)
|
|
|
|
if isFile(path):
|
|
|
|
return success()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
# If directory exists createPath wont fail
|
|
|
|
let dir = path.parentDir
|
|
|
|
if io2.createPath(dir).isErr:
|
|
|
|
trace "Unable to create block prefix dir", dir
|
|
|
|
return failure("Unable to create block prefix dir")
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
let res = io2.writeFile(path, blk.data)
|
|
|
|
if res.isErr:
|
2022-01-10 15:32:56 +00:00
|
|
|
let error = io2.ioErrorMsg(res.error)
|
|
|
|
trace "Unable to store block", path, cid = blk.cid, error
|
2022-07-28 00:39:17 +00:00
|
|
|
return failure("Unable to store block")
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-08-19 00:56:36 +00:00
|
|
|
if not self.cache.isNil:
|
|
|
|
let
|
|
|
|
putCachedRes = await self.cache.putBlock(blk)
|
|
|
|
|
|
|
|
if putCachedRes.isErr:
|
|
|
|
trace "Unable to store block in cache", cid = blk.cid, error = putCachedRes.error.msg
|
2022-04-05 14:24:48 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
return success()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
method delBlock*(self: FSStore, cid: Cid): Future[?!void] {.async.} =
|
2022-08-19 00:56:36 +00:00
|
|
|
## Delete a block from the cache and filestore
|
2022-01-10 15:32:56 +00:00
|
|
|
##
|
|
|
|
|
2022-08-19 00:56:36 +00:00
|
|
|
if not self.cache.isNil:
|
|
|
|
trace "Deleting block from cache and filestore", cid
|
|
|
|
else:
|
|
|
|
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-08-19 00:56:36 +00:00
|
|
|
if not self.cache.isNil:
|
|
|
|
let
|
|
|
|
delCachedRes = await self.cache.delBlock(cid)
|
|
|
|
|
|
|
|
if delCachedRes.isErr:
|
|
|
|
trace "Unable to delete block from cache", cid, error = delCachedRes.error.msg
|
|
|
|
|
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:
|
2022-07-28 00:39:17 +00:00
|
|
|
let error = io2.ioErrorMsg(res.error)
|
|
|
|
trace "Unable to delete block", path, cid, error
|
|
|
|
return error.failure
|
2022-04-05 14:24:48 +00:00
|
|
|
|
2022-08-19 00:56:36 +00:00
|
|
|
return success()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
method hasBlock*(self: FSStore, cid: Cid): Future[?!bool] {.async.} =
|
2022-08-19 00:56:36 +00:00
|
|
|
## Check if a block exists in the filestore
|
2022-01-10 15:32:56 +00:00
|
|
|
##
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
trace "Checking filestore for block existence", 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
|
2022-04-05 14:24:48 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
return self.blockPath(cid).isFile().success
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
method listBlocks*(self: FSStore, onBlock: OnBlock): Future[?!void] {.async.} =
|
2022-08-19 00:56:36 +00:00
|
|
|
## Process list of all blocks in the filestore via callback.
|
|
|
|
## This is an intensive operation
|
2022-07-28 00:39:17 +00:00
|
|
|
##
|
|
|
|
|
|
|
|
trace "Listing all blocks in filestore"
|
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
|
2022-07-28 00:39:17 +00:00
|
|
|
if len(folderPath.basename) != self.postfixLen: continue
|
2022-04-13 16:32:35 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
for (fkind, filename) in folderPath.walkDir(relative = true):
|
2022-04-13 16:32:35 +00:00
|
|
|
if fkind != pcFile: continue
|
2022-07-28 00:39:17 +00:00
|
|
|
let cid = Cid.init(filename)
|
2022-07-22 23:38:49 +00:00
|
|
|
if cid.isOk: await onBlock(cid.get())
|
2022-05-12 20:02:30 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
return success()
|
|
|
|
|
2022-07-22 23:38:49 +00:00
|
|
|
method close*(self: FSStore): Future[void] {.async.} =
|
|
|
|
## Close the underlying cache
|
|
|
|
##
|
|
|
|
|
|
|
|
if not self.cache.isNil: await self.cache.close
|
|
|
|
|
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)
|