mirror of
https://github.com/logos-storage/logos-storage-nim.git
synced 2026-01-02 21:43:11 +00:00
Remove makeRandomDataset in favor of direct composition with makeRandomBlocks and makeDataset. Part of https://github.com/codex-storage/nim-codex/issues/974 Signed-off-by: Chrysostomos Nanakos <chris@include.gr>
46 lines
1.2 KiB
Nim
46 lines
1.2 KiB
Nim
import std/random
|
|
|
|
import pkg/chronos
|
|
import pkg/codex/blocktype as bt
|
|
import pkg/codex/merkletree
|
|
import pkg/codex/manifest
|
|
import pkg/codex/rng
|
|
|
|
import ./randomchunker
|
|
|
|
type TestDataset* = tuple[blocks: seq[Block], tree: CodexTree, manifest: Manifest]
|
|
|
|
proc makeRandomBlock*(size: NBytes): Block =
|
|
let bytes = newSeqWith(size.int, rand(uint8))
|
|
Block.new(bytes).tryGet()
|
|
|
|
proc makeRandomBlocks*(
|
|
datasetSize: int, blockSize: NBytes
|
|
): Future[seq[Block]] {.async.} =
|
|
var chunker =
|
|
RandomChunker.new(Rng.instance(), size = datasetSize, chunkSize = blockSize)
|
|
|
|
while true:
|
|
let chunk = await chunker.getBytes()
|
|
if chunk.len <= 0:
|
|
break
|
|
|
|
result.add(Block.new(chunk).tryGet())
|
|
|
|
proc makeDataset*(blocks: seq[Block]): ?!TestDataset =
|
|
if blocks.len == 0:
|
|
return failure("Blocks list was empty")
|
|
|
|
let
|
|
datasetSize = blocks.mapIt(it.data.len).foldl(a + b)
|
|
blockSize = blocks.mapIt(it.data.len).foldl(max(a, b))
|
|
tree = ?CodexTree.init(blocks.mapIt(it.cid))
|
|
treeCid = ?tree.rootCid
|
|
manifest = Manifest.new(
|
|
treeCid = treeCid,
|
|
blockSize = NBytes(blockSize),
|
|
datasetSize = NBytes(datasetSize),
|
|
)
|
|
|
|
return success((blocks, tree, manifest))
|