2021-02-26 00:23:22 +00:00
|
|
|
## 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.
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
# TODO: This is super inneficient and needs a rewrite, but it'll do for now
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
import pkg/chronicles
|
2021-08-30 19:25:20 +00:00
|
|
|
import pkg/questionable
|
|
|
|
import pkg/questionable/results
|
2022-01-10 15:32:56 +00:00
|
|
|
import pkg/chronos
|
|
|
|
import pkg/libp2p except shuffle
|
2021-08-30 19:25:20 +00:00
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
import ./blocktype
|
|
|
|
|
|
|
|
export blocktype
|
|
|
|
|
|
|
|
const
|
|
|
|
DefaultChunkSize*: int64 = 1024 * 256
|
|
|
|
|
|
|
|
type
|
|
|
|
# default reader type
|
2022-01-10 15:32:56 +00:00
|
|
|
ChunkBuffer* = ptr UncheckedArray[byte]
|
2021-08-30 19:25:20 +00:00
|
|
|
Reader* =
|
2022-01-10 15:32:56 +00:00
|
|
|
proc(data: ChunkBuffer, len: int): Future[int] {.gcsafe, raises: [Defect].}
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
ChunkerType* {.pure.} = enum
|
2022-01-10 15:32:56 +00:00
|
|
|
FixedChunker
|
2021-02-26 00:23:22 +00:00
|
|
|
RabinChunker
|
|
|
|
|
2022-01-12 17:32:27 +00:00
|
|
|
Chunker* = object
|
2021-02-26 00:23:22 +00:00
|
|
|
reader*: Reader
|
|
|
|
case kind*: ChunkerType:
|
2022-01-10 15:32:56 +00:00
|
|
|
of FixedChunker:
|
2021-02-26 00:23:22 +00:00
|
|
|
chunkSize*: Natural
|
|
|
|
pad*: bool # pad last block if less than size
|
|
|
|
of RabinChunker:
|
|
|
|
discard
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
FileChunker* = Chunker
|
|
|
|
LPStreamChunker* = Chunker
|
|
|
|
|
|
|
|
proc getBytes*(c: Chunker): Future[seq[byte]] {.async.} =
|
2021-02-26 00:23:22 +00:00
|
|
|
## returns a chunk of bytes from
|
|
|
|
## the instantiated chunker
|
|
|
|
##
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
var buff = newSeq[byte](c.chunkSize)
|
|
|
|
let read = await c.reader(cast[ChunkBuffer](addr buff[0]), buff.len)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
if read <= 0:
|
|
|
|
return @[]
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
if not c.pad and buff.len > read:
|
|
|
|
buff.setLen(read)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
return buff
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
func new*(
|
2021-02-26 00:23:22 +00:00
|
|
|
T: type Chunker,
|
2022-01-10 15:32:56 +00:00
|
|
|
kind = ChunkerType.FixedChunker,
|
2021-02-26 00:23:22 +00:00
|
|
|
reader: Reader,
|
|
|
|
chunkSize = DefaultChunkSize,
|
|
|
|
pad = false): T =
|
|
|
|
var chunker = Chunker(
|
|
|
|
kind: kind,
|
2022-01-10 15:32:56 +00:00
|
|
|
reader: reader)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
if kind == ChunkerType.FixedChunker:
|
2021-02-26 00:23:22 +00:00
|
|
|
chunker.pad = pad
|
|
|
|
chunker.chunkSize = chunkSize
|
|
|
|
|
|
|
|
return chunker
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
proc new*(
|
|
|
|
T: type LPStreamChunker,
|
|
|
|
stream: LPStream,
|
|
|
|
kind = ChunkerType.FixedChunker,
|
2021-02-26 00:23:22 +00:00
|
|
|
chunkSize = DefaultChunkSize,
|
2022-01-10 15:32:56 +00:00
|
|
|
pad = false): T =
|
|
|
|
## create the default File chunker
|
2021-02-26 00:23:22 +00:00
|
|
|
##
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
proc reader(data: ChunkBuffer, len: int): Future[int]
|
|
|
|
{.gcsafe, async, raises: [Defect].} =
|
|
|
|
var res = 0
|
|
|
|
try:
|
|
|
|
while res < len:
|
|
|
|
res += await stream.readOnce(addr data[res], len - res)
|
|
|
|
except LPStreamEOFError as exc:
|
|
|
|
trace "LPStreamChunker stream Eof", exc = exc.msg
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "CatchableError exception", exc = exc.msg
|
|
|
|
raise newException(Defect, exc.msg)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
return res
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
Chunker.new(
|
2022-01-10 15:32:56 +00:00
|
|
|
kind = ChunkerType.FixedChunker,
|
2021-02-26 00:23:22 +00:00
|
|
|
reader = reader,
|
|
|
|
pad = pad,
|
|
|
|
chunkSize = chunkSize)
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
proc new*(
|
|
|
|
T: type FileChunker,
|
2021-02-26 00:23:22 +00:00
|
|
|
file: File,
|
2022-01-10 15:32:56 +00:00
|
|
|
kind = ChunkerType.FixedChunker,
|
2021-02-26 00:23:22 +00:00
|
|
|
chunkSize = DefaultChunkSize,
|
2022-01-10 15:32:56 +00:00
|
|
|
pad = false): T =
|
2021-02-26 00:23:22 +00:00
|
|
|
## create the default File chunker
|
|
|
|
##
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
proc reader(data: ChunkBuffer, len: int): Future[int]
|
|
|
|
{.gcsafe, async, raises: [Defect].} =
|
|
|
|
var total = 0
|
2021-08-30 19:25:20 +00:00
|
|
|
try:
|
2022-01-10 15:32:56 +00:00
|
|
|
while total < len:
|
|
|
|
let res = file.readBuffer(addr data[total], len - total)
|
|
|
|
if res <= 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
total += res
|
2021-08-30 19:25:20 +00:00
|
|
|
except IOError as exc:
|
2022-01-10 15:32:56 +00:00
|
|
|
trace "Exception reading file", exc = exc.msg
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "CatchableError exception", exc = exc.msg
|
2021-08-30 19:25:20 +00:00
|
|
|
raise newException(Defect, exc.msg)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
return total
|
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
Chunker.new(
|
2022-01-10 15:32:56 +00:00
|
|
|
kind = ChunkerType.FixedChunker,
|
2021-02-26 00:23:22 +00:00
|
|
|
reader = reader,
|
|
|
|
pad = pad,
|
|
|
|
chunkSize = chunkSize)
|