add blockSize and size to manifest

This commit is contained in:
Dmitriy Ryajov 2022-03-14 18:11:09 -06:00
parent f414c695be
commit 8990171e6c
No known key found for this signature in database
GPG Key ID: DA8C680CE7C657A4
5 changed files with 20 additions and 7 deletions

View File

@ -16,6 +16,9 @@ import pkg/questionable/results
import ./errors
const
BlockSize* = 4096 # file chunk read size
type
Block* = object of RootObj
cid*: Cid

View File

@ -27,20 +27,20 @@ type
ManifestCoderType*[codec: static MultiCodec] = object
DagPBCoder* = ManifestCoderType[multiCodec("dag-pb")]
func encode*(_: DagPBCoder, b: Manifest): ?!seq[byte] =
func encode*(_: DagPBCoder, manifest: Manifest): ?!seq[byte] =
## Encode the manifest into a ``ManifestCodec``
## multicodec container (Dag-pb) for now
##
var pbNode = initProtoBuffer()
for c in b.blocks:
for c in manifest.blocks:
var pbLink = initProtoBuffer()
pbLink.write(1, c.data.buffer) # write Cid links
pbLink.finish()
pbNode.write(2, pbLink)
let cid = !b.rootHash
let cid = !manifest.rootHash
pbNode.write(1, cid.data.buffer) # set the rootHash Cid as the data field
pbNode.finish()

View File

@ -19,6 +19,7 @@ import pkg/chronicles
import ./types
import ./coders
import ../errors
import ../blocktype
export coders, Manifest
@ -30,16 +31,21 @@ const
func len*(self: Manifest): int =
self.blocks.len
func size*(self: Manifest): int =
self.blocks.len * self.blockSize
func `[]`*(self: Manifest, i: Natural): Cid =
self.blocks[i]
func `[]=`*(self: var Manifest, i: Natural, item: Cid) =
self.rootHash = Cid.none
self.blocks[i] = item
func `[]`*(self: Manifest, i: BackwardsIndex): Cid =
self.blocks[self.len - i.int]
func `[]=`*(self: var Manifest, i: BackwardsIndex, item: Cid) =
self.rootHash = Cid.none
self.blocks[self.len - i.int] = item
proc add*(self: var Manifest, cid: Cid) =
@ -61,6 +67,10 @@ template hashBytes(mh: MultiHash): seq[byte] =
mh.data.buffer[mh.dpos..(mh.dpos + mh.size - 1)]
proc makeRoot(self: var Manifest): ?!void =
## Create a tree hash root of the contained
## block hashes
##
var
stack: seq[MultiHash]
@ -119,7 +129,8 @@ proc init*(
blocks: openArray[Cid] = [],
version = CIDv1,
hcodec = multiCodec("sha2-256"),
codec = multiCodec("raw")): ?!T =
codec = multiCodec("raw"),
blockSize = BlockSize): ?!T =
## Create a manifest using array of `Cid`s
##
@ -131,4 +142,5 @@ proc init*(
version: version,
codec: codec,
hcodec: hcodec,
blockSize: blockSize
).success

View File

@ -40,6 +40,7 @@ template EmptyDigests*: untyped =
type
Manifest* = object of RootObj
rootHash*: ?Cid
blockSize*: int
blocks*: seq[Cid]
version*: CidVersion
hcodec*: MultiCodec

View File

@ -29,9 +29,6 @@ import ./blockexchange
logScope:
topics = "dagger node"
const
FileChunkSize* = 4096 # file chunk read size
type
DaggerError = object of CatchableError