From e965f5e0de628b647129aae176d203b3096aecad Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Mon, 21 Mar 2022 12:09:59 -0600 Subject: [PATCH] Seekable stream (#58) * cleanup manifest * adding a "seekable" base stream --- dagger/streams.nim | 4 ++++ dagger/streams/seekablestream.nim | 27 +++++++++++++++++++++++++++ dagger/{ => streams}/storestream.nim | 22 +++++++++++++--------- tests/dagger/teststorestream.nim | 5 +---- 4 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 dagger/streams.nim create mode 100644 dagger/streams/seekablestream.nim rename dagger/{ => streams}/storestream.nim (87%) diff --git a/dagger/streams.nim b/dagger/streams.nim new file mode 100644 index 00000000..e110adbd --- /dev/null +++ b/dagger/streams.nim @@ -0,0 +1,4 @@ +import ./streams/seekablestream +import ./streams/storestream + +export seekablestream, storestream diff --git a/dagger/streams/seekablestream.nim b/dagger/streams/seekablestream.nim new file mode 100644 index 00000000..2c1ec1b8 --- /dev/null +++ b/dagger/streams/seekablestream.nim @@ -0,0 +1,27 @@ +## 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. + +import pkg/libp2p +import pkg/chronos +import pkg/chronicles + +export libp2p, chronos, chronicles + +logScope: + topics = "dagger seekablestream" + +type + SeekableStream* = ref object of LPStream + offset*: int + +method size*(self: SeekableStream): int {.base.} = + raiseAssert("method unimplemented") + +proc setPos*(self: SeekableStream, pos: int) = + self.offset = pos diff --git a/dagger/storestream.nim b/dagger/streams/storestream.nim similarity index 87% rename from dagger/storestream.nim rename to dagger/streams/storestream.nim index 82e1a062..28b6d944 100644 --- a/dagger/storestream.nim +++ b/dagger/streams/storestream.nim @@ -7,18 +7,22 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -{.push raises: [Defect].} +import pkg/upraises + +push: {.upraises: [].} import pkg/libp2p import pkg/chronos import pkg/chronicles import pkg/stew/ptrops -import ./stores -import ./manifest -import ./blocktype +import ../stores +import ../manifest +import ../blocktype -export stores +import ./seekablestream + +export stores, blocktype, manifest, chronos logScope: topics = "dagger storestream" @@ -28,11 +32,10 @@ type Sequential, Grid - StoreStream* = ref object of LPStream + StoreStream* = ref object of SeekableStream store*: BlockStore manifest*: Manifest pattern*: ReadPattern - offset*: int proc init*( T: type StoreStream, @@ -47,6 +50,9 @@ proc init*( result.initStream() +method size*(self: StoreStream): int = + self.manifest.len * self.manifest.blockSize + method readOnce*( self: StoreStream, pbytes: pointer, @@ -61,8 +67,6 @@ method readOnce*( while read < nbytes and self.atEof.not: let pos = self.offset div self.manifest.blockSize - - let blk = (await self.store.getBlock(self.manifest[pos])).tryGet() blockOffset = if self.offset >= self.manifest.blockSize: self.offset mod self.manifest.blockSize diff --git a/tests/dagger/teststorestream.nim b/tests/dagger/teststorestream.nim index ded5d658..415a9846 100644 --- a/tests/dagger/teststorestream.nim +++ b/tests/dagger/teststorestream.nim @@ -5,10 +5,9 @@ import pkg/questionable/results import ./helpers -import pkg/dagger/storestream +import pkg/dagger/streams import pkg/dagger/stores import pkg/dagger/manifest -import pkg/dagger/chunker import pkg/dagger/rng suite "StoreStream": @@ -85,7 +84,6 @@ suite "StoreStream": buf = newSeq[byte](5) await stream.readExactly(addr buf[0], 5) - check buf == [byte 0, 1, 2, 3, 4] test "Read exact bytes outside of block boundary": @@ -93,5 +91,4 @@ suite "StoreStream": buf = newSeq[byte](15) await stream.readExactly(addr buf[0], 15) - check buf == [byte 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]