2022-05-24 05:24:15 +00:00
|
|
|
## Nim-Dagger
|
2022-03-17 13:56:46 +00:00
|
|
|
## Copyright (c) 2022 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-07-28 00:39:17 +00:00
|
|
|
import std/options
|
|
|
|
|
2022-03-21 18:09:59 +00:00
|
|
|
import pkg/upraises
|
|
|
|
|
|
|
|
push: {.upraises: [].}
|
2022-03-17 13:56:46 +00:00
|
|
|
|
|
|
|
import pkg/libp2p
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/chronicles
|
|
|
|
import pkg/stew/ptrops
|
|
|
|
|
2022-03-21 18:09:59 +00:00
|
|
|
import ../stores
|
|
|
|
import ../manifest
|
|
|
|
import ../blocktype
|
|
|
|
|
|
|
|
import ./seekablestream
|
2022-03-17 13:56:46 +00:00
|
|
|
|
2022-03-21 18:09:59 +00:00
|
|
|
export stores, blocktype, manifest, chronos
|
2022-03-17 13:56:46 +00:00
|
|
|
|
|
|
|
logScope:
|
2022-05-24 05:24:15 +00:00
|
|
|
topics = "dagger storestream"
|
2022-03-17 13:56:46 +00:00
|
|
|
|
|
|
|
type
|
2022-08-24 12:15:59 +00:00
|
|
|
# Make SeekableStream from a sequence of blocks stored in Manifest
|
|
|
|
# (only original file data - see StoreStream.size)
|
2022-03-21 18:09:59 +00:00
|
|
|
StoreStream* = ref object of SeekableStream
|
2022-08-24 12:15:59 +00:00
|
|
|
store*: BlockStore # Store where to lookup block contents
|
|
|
|
manifest*: Manifest # List of block CIDs
|
|
|
|
pad*: bool # Pad last block to manifest.blockSize?
|
2022-03-17 13:56:46 +00:00
|
|
|
|
2022-03-30 02:43:35 +00:00
|
|
|
proc new*(
|
2022-03-17 13:56:46 +00:00
|
|
|
T: type StoreStream,
|
|
|
|
store: BlockStore,
|
2022-08-24 12:15:59 +00:00
|
|
|
manifest: Manifest,
|
|
|
|
pad = true): T =
|
|
|
|
|
2022-03-17 13:56:46 +00:00
|
|
|
result = T(
|
|
|
|
store: store,
|
|
|
|
manifest: manifest,
|
2022-08-24 12:15:59 +00:00
|
|
|
pad: pad,
|
|
|
|
offset: 0)
|
2022-03-17 13:56:46 +00:00
|
|
|
|
|
|
|
result.initStream()
|
|
|
|
|
2022-06-14 15:19:35 +00:00
|
|
|
method `size`*(self: StoreStream): int =
|
2022-08-24 12:15:59 +00:00
|
|
|
bytes(self.manifest, self.pad)
|
2022-03-21 18:09:59 +00:00
|
|
|
|
2022-06-14 15:19:35 +00:00
|
|
|
proc `size=`*(self: StoreStream, size: int)
|
|
|
|
{.error: "Setting the size is forbidden".} =
|
|
|
|
discard
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
method atEof*(self: StoreStream): bool =
|
|
|
|
self.offset >= self.size
|
|
|
|
|
2022-03-17 13:56:46 +00:00
|
|
|
method readOnce*(
|
|
|
|
self: StoreStream,
|
|
|
|
pbytes: pointer,
|
|
|
|
nbytes: int): Future[int] {.async.} =
|
2022-07-28 00:39:17 +00:00
|
|
|
## Read `nbytes` from current position in the StoreStream into output buffer pointed by `pbytes`.
|
|
|
|
## Return how many bytes were actually read before EOF was encountered.
|
|
|
|
## Raise exception if we are already at EOF.
|
2022-03-17 13:56:46 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
trace "Reading from manifest", cid = self.manifest.cid.get(), blocks = self.manifest.len
|
2022-03-17 13:56:46 +00:00
|
|
|
if self.atEof:
|
|
|
|
raise newLPStreamEOFError()
|
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
# The loop iterates over blocks in the StoreStream,
|
|
|
|
# reading them and copying their data into outbuf
|
|
|
|
var read = 0 # Bytes read so far, and thus write offset in the outbuf
|
2022-03-30 02:43:35 +00:00
|
|
|
while read < nbytes and not self.atEof:
|
2022-07-28 00:39:17 +00:00
|
|
|
# Compute from the current stream position `self.offset` the block num/offset to read
|
|
|
|
# Compute how many bytes to read from this block
|
2022-03-17 13:56:46 +00:00
|
|
|
let
|
2022-07-28 00:39:17 +00:00
|
|
|
blockNum = self.offset div self.manifest.blockSize
|
|
|
|
blockOffset = self.offset mod self.manifest.blockSize
|
2022-08-24 12:15:59 +00:00
|
|
|
readBytes = min([self.size - self.offset, nbytes - read, self.manifest.blockSize - blockOffset])
|
2022-07-28 00:39:17 +00:00
|
|
|
|
|
|
|
# Read contents of block `blockNum`
|
2022-08-19 00:56:36 +00:00
|
|
|
without blk =? await self.store.getBlock(self.manifest[blockNum]), error:
|
2022-07-28 00:39:17 +00:00
|
|
|
raise newLPStreamReadError(error)
|
2022-08-19 00:56:36 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
trace "Reading bytes from store stream", blockNum, cid = blk.cid, bytes = readBytes, blockOffset
|
|
|
|
|
|
|
|
# Copy `readBytes` bytes starting at `blockOffset` from the block into the outbuf
|
2022-08-24 12:15:59 +00:00
|
|
|
if blk.isEmpty:
|
|
|
|
zeroMem(pbytes.offset(read), readBytes)
|
|
|
|
else:
|
|
|
|
copyMem(pbytes.offset(read), blk.data[blockOffset].addr, readBytes)
|
2022-05-24 05:24:15 +00:00
|
|
|
|
2022-07-28 00:39:17 +00:00
|
|
|
# Update current positions in the stream and outbuf
|
2022-03-17 13:56:46 +00:00
|
|
|
self.offset += readBytes
|
|
|
|
read += readBytes
|
|
|
|
|
|
|
|
return read
|
|
|
|
|
|
|
|
method closeImpl*(self: StoreStream) {.async.} =
|
2022-08-24 12:15:59 +00:00
|
|
|
trace "Closing StoreStream"
|
|
|
|
self.offset = self.size # set Eof
|
2022-03-17 13:56:46 +00:00
|
|
|
await procCall LPStream(self).closeImpl()
|