Get rid of copyAllScalarFields()
This commit is contained in:
parent
e5e70a7dd2
commit
2c7de91278
|
@ -153,37 +153,6 @@ proc cid*(self: Manifest): ?!Cid =
|
||||||
# Constructors
|
# Constructors
|
||||||
############################################################
|
############################################################
|
||||||
|
|
||||||
func copyAllScalarFields(
|
|
||||||
original: Manifest,
|
|
||||||
protected: bool
|
|
||||||
): Manifest =
|
|
||||||
## Sometimes we need to copy all but a few fields
|
|
||||||
## from a manifest to another one.
|
|
||||||
## It can be mplemented by copying all fields and then
|
|
||||||
## making a few edits, but it is inefficient to copy
|
|
||||||
## an entire `blocks` array only to drop it.
|
|
||||||
## So we made a helper that copies all scalar fields,
|
|
||||||
## i.e. all fields except for `blocks`.
|
|
||||||
##
|
|
||||||
|
|
||||||
var copy = Manifest(
|
|
||||||
rootHash: original.rootHash,
|
|
||||||
originalBytes: original.originalBytes,
|
|
||||||
blockSize: original.blockSize,
|
|
||||||
blocks: @[],
|
|
||||||
version: original.version,
|
|
||||||
hcodec: original.hcodec,
|
|
||||||
codec: original.codec,
|
|
||||||
protected: protected)
|
|
||||||
|
|
||||||
if copy.protected and original.protected:
|
|
||||||
copy.K = original.K
|
|
||||||
copy.M = original.M
|
|
||||||
copy.originalCid = original.originalCid
|
|
||||||
copy.originalLen = original.originalLen
|
|
||||||
|
|
||||||
return copy
|
|
||||||
|
|
||||||
proc new*(
|
proc new*(
|
||||||
T: type Manifest,
|
T: type Manifest,
|
||||||
blocks: openArray[Cid] = [],
|
blocks: openArray[Cid] = [],
|
||||||
|
@ -207,32 +176,42 @@ proc new*(
|
||||||
protected: protected).success
|
protected: protected).success
|
||||||
|
|
||||||
proc protect*(
|
proc protect*(
|
||||||
manifest: Manifest,
|
original: Manifest,
|
||||||
K, M: int): ?!Manifest =
|
K, M: int): ?!Manifest =
|
||||||
## Create an erasure protected dataset manifest from an unprotected one
|
## Create an erasure protected dataset manifest from an unprotected one
|
||||||
##
|
##
|
||||||
|
|
||||||
? manifest.verify()
|
? original.verify()
|
||||||
if manifest.protected:
|
if original.protected:
|
||||||
return failure newException(CodexError, "Trying to protect already protected manifest")
|
return failure newException(CodexError, "Trying to protect already protected manifest")
|
||||||
|
|
||||||
var self = copyAllScalarFields(manifest, protected = true)
|
var self = Manifest(
|
||||||
self.K = K
|
# copy of original fields
|
||||||
self.M = M
|
rootHash: original.rootHash,
|
||||||
self.originalCid = ? manifest.cid
|
originalBytes: original.originalBytes,
|
||||||
self.originalLen = manifest.len
|
blockSize: original.blockSize,
|
||||||
|
version: original.version,
|
||||||
|
hcodec: original.hcodec,
|
||||||
|
codec: original.codec,
|
||||||
|
# modified fields
|
||||||
|
protected: true,
|
||||||
|
blocks: @[],
|
||||||
|
K: K,
|
||||||
|
M: M,
|
||||||
|
originalCid: ? original.cid,
|
||||||
|
originalLen: original.len)
|
||||||
|
|
||||||
let encodedLen = self.rounded + (self.steps * M)
|
let encodedLen = self.rounded + (self.steps * M)
|
||||||
self.blocks = newSeq[Cid](encodedLen)
|
self.blocks = newSeq[Cid](encodedLen)
|
||||||
|
|
||||||
# copy original manifest blocks
|
# copy original manifest blocks
|
||||||
for i in 0..<self.rounded:
|
for i in 0..<self.rounded:
|
||||||
if i < manifest.len:
|
if i < original.len:
|
||||||
self.blocks[i] = manifest[i]
|
self.blocks[i] = original[i]
|
||||||
else:
|
else:
|
||||||
self.blocks[i] = EmptyCid[manifest.version]
|
self.blocks[i] = EmptyCid[original.version]
|
||||||
.catch
|
.catch
|
||||||
.get()[manifest.hcodec]
|
.get()[original.hcodec]
|
||||||
.catch
|
.catch
|
||||||
.get()
|
.get()
|
||||||
|
|
||||||
|
@ -240,17 +219,26 @@ proc protect*(
|
||||||
self.success
|
self.success
|
||||||
|
|
||||||
proc unprotect*(
|
proc unprotect*(
|
||||||
manifest: Manifest
|
original: Manifest
|
||||||
): ?!Manifest =
|
): ?!Manifest =
|
||||||
## Create an unprotected dataset manifest from an erasure protected one
|
## Create an unprotected dataset manifest from an erasure protected one
|
||||||
##
|
##
|
||||||
|
|
||||||
? manifest.verify()
|
? original.verify()
|
||||||
if not manifest.protected:
|
if not original.protected:
|
||||||
return failure newException(CodexError, "Trying to unprotect already non-protected manifest")
|
return failure newException(CodexError, "Trying to unprotect already non-protected manifest")
|
||||||
|
|
||||||
var self = copyAllScalarFields(manifest, protected = false)
|
var self = Manifest(
|
||||||
self.blocks = manifest.blocks[0..<manifest.originalLen]
|
# copy of original fields
|
||||||
|
rootHash: original.rootHash,
|
||||||
|
originalBytes: original.originalBytes,
|
||||||
|
blockSize: original.blockSize,
|
||||||
|
version: original.version,
|
||||||
|
hcodec: original.hcodec,
|
||||||
|
codec: original.codec,
|
||||||
|
# modified fields
|
||||||
|
protected: false,
|
||||||
|
blocks: original.blocks[0..<original.originalLen])
|
||||||
|
|
||||||
? self.verify()
|
? self.verify()
|
||||||
self.success
|
self.success
|
||||||
|
|
|
@ -27,6 +27,7 @@ const
|
||||||
|
|
||||||
type
|
type
|
||||||
Manifest* = ref object of RootObj
|
Manifest* = ref object of RootObj
|
||||||
|
# when adding/changing fields - make sure to modify ALL constructors in manifest.nim
|
||||||
rootHash*: ?Cid # Root (tree) hash of the contained data set
|
rootHash*: ?Cid # Root (tree) hash of the contained data set
|
||||||
originalBytes*: int # Exact size of the original (uploaded) file
|
originalBytes*: int # Exact size of the original (uploaded) file
|
||||||
blockSize*: int # Size of each contained block (might not be needed if blocks are len-prefixed)
|
blockSize*: int # Size of each contained block (might not be needed if blocks are len-prefixed)
|
||||||
|
|
Loading…
Reference in New Issue