2022-03-17 13:56:46 +00:00
|
|
|
## Nim-Dagger
|
|
|
|
## 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-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:
|
|
|
|
topics = "dagger storestream"
|
|
|
|
|
|
|
|
type
|
2022-03-21 18:09:59 +00:00
|
|
|
StoreStream* = ref object of SeekableStream
|
2022-03-17 13:56:46 +00:00
|
|
|
store*: BlockStore
|
|
|
|
manifest*: Manifest
|
|
|
|
|
2022-03-30 02:43:35 +00:00
|
|
|
proc new*(
|
2022-03-17 13:56:46 +00:00
|
|
|
T: type StoreStream,
|
|
|
|
store: BlockStore,
|
2022-03-30 02:43:35 +00:00
|
|
|
manifest: Manifest): T =
|
2022-03-17 13:56:46 +00:00
|
|
|
result = T(
|
|
|
|
store: store,
|
|
|
|
manifest: manifest,
|
|
|
|
offset: 0)
|
|
|
|
|
|
|
|
result.initStream()
|
|
|
|
|
2022-03-21 18:09:59 +00:00
|
|
|
method size*(self: StoreStream): int =
|
|
|
|
self.manifest.len * self.manifest.blockSize
|
|
|
|
|
2022-03-17 13:56:46 +00:00
|
|
|
method readOnce*(
|
|
|
|
self: StoreStream,
|
|
|
|
pbytes: pointer,
|
|
|
|
nbytes: int): Future[int] {.async.} =
|
|
|
|
|
|
|
|
if self.atEof:
|
|
|
|
raise newLPStreamEOFError()
|
|
|
|
|
|
|
|
var
|
|
|
|
read = 0
|
|
|
|
|
2022-03-30 02:43:35 +00:00
|
|
|
while read < nbytes and not self.atEof:
|
2022-03-17 13:56:46 +00:00
|
|
|
let
|
|
|
|
pos = self.offset div self.manifest.blockSize
|
|
|
|
blk = (await self.store.getBlock(self.manifest[pos])).tryGet()
|
2022-03-30 02:43:35 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
blockOffset =
|
|
|
|
if self.offset >= self.manifest.blockSize:
|
2022-03-17 13:56:46 +00:00
|
|
|
self.offset mod self.manifest.blockSize
|
|
|
|
else:
|
|
|
|
self.offset
|
|
|
|
|
2022-03-30 02:43:35 +00:00
|
|
|
readBytes =
|
|
|
|
if (nbytes - read) >= (self.manifest.blockSize - blockOffset):
|
2022-03-17 13:56:46 +00:00
|
|
|
self.manifest.blockSize - blockOffset
|
|
|
|
else:
|
|
|
|
min(nbytes - read, self.manifest.blockSize)
|
|
|
|
|
|
|
|
copyMem(pbytes.offset(read), unsafeAddr blk.data[blockOffset], readBytes)
|
|
|
|
self.offset += readBytes
|
|
|
|
read += readBytes
|
|
|
|
|
|
|
|
return read
|
|
|
|
|
|
|
|
method atEof*(self: StoreStream): bool =
|
|
|
|
self.offset >= self.manifest.len * self.manifest.blockSize
|
|
|
|
|
|
|
|
method closeImpl*(self: StoreStream) {.async.} =
|
|
|
|
try:
|
2022-03-30 02:43:35 +00:00
|
|
|
trace "Closing StoreStream"
|
2022-03-17 13:56:46 +00:00
|
|
|
self.offset = self.manifest.len * self.manifest.blockSize # set Eof
|
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
|
|
|
except CatchableError as exc:
|
2022-03-30 02:43:35 +00:00
|
|
|
trace "Error closing StoreStream", msg = exc.msg
|
2022-03-17 13:56:46 +00:00
|
|
|
|
|
|
|
await procCall LPStream(self).closeImpl()
|