From 271f7d7523ec399b11e7a8599ba104cf718c4bf9 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Fri, 27 Aug 2021 11:03:21 -0600 Subject: [PATCH] add block stream abstraction --- dagger/blockstream.nim | 29 +++++++++++++++++++++++++++++ dagger/chunkedblockstream.nim | 28 ++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 dagger/blockstream.nim create mode 100644 dagger/chunkedblockstream.nim diff --git a/dagger/blockstream.nim b/dagger/blockstream.nim new file mode 100644 index 00000000..96f0eac2 --- /dev/null +++ b/dagger/blockstream.nim @@ -0,0 +1,29 @@ +## Nim-Dagger +## 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. + +{.push raises: [Defect].} + +import pkg/questionable +import pkg/questionable/results +import ./blocktype + +export blocktype + +type + BlockStreamRef* = ref object of RootObj + +method nextBlock*(b: BlockStreamRef): ?!Block {.base.} = + doAssert(false, "Not implemented!") + +iterator items*(b: BlockStreamRef): Block = + while true: + without blk =? b.nextBlock(): + break + + yield blk diff --git a/dagger/chunkedblockstream.nim b/dagger/chunkedblockstream.nim new file mode 100644 index 00000000..49b6b6f5 --- /dev/null +++ b/dagger/chunkedblockstream.nim @@ -0,0 +1,28 @@ +## Nim-Dagger +## 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. + +{.push raises: [Defect].} + +import pkg/questionable +import pkg/questionable/results + +import ./blockstream +import ./chunker + +type + ChunkedBlockStreamRef* = ref object of BlockStreamRef + chunker*: Chunker + +method nextBlock*(c: ChunkedBlockStreamRef): ?!Block = + let data: seq[byte] = c.chunker.getBytes() + if data.len > 0: + return success Block.new(data) + +proc new*(T: type ChunkedBlockStreamRef, chunker: Chunker): T = + T(chunker: chunker)