manifest: add index conversion functions

Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
Csaba Kiraly 2023-11-22 14:21:43 +01:00
parent 53dc6e20f4
commit 2f4b0f9d1d
No known key found for this signature in database
GPG Key ID: 0FE274EE8C95166E
1 changed files with 30 additions and 0 deletions

View File

@ -100,6 +100,36 @@ proc treeCid*(self: Manifest): Cid =
proc blocksCount*(self: Manifest): int =
divUp(self.datasetSize.int, self.blockSize.int)
proc indexToCoord(encoded: Manifest, idx: int): (int, int, int) {.inline.} =
let
column = (idx mod encoded.interleave)
step = (idx div encoded.interleave) div (encoded.ecK + encoded.ecM)
pos = (idx div encoded.interleave) mod (encoded.ecK + encoded.ecM)
(step, column, pos)
func indexToPos(encoded: Manifest, idx: int): int {.inline.} =
(idx div encoded.interleave) mod (encoded.ecK + encoded.ecM)
func isParity*(self: Manifest, idx: int): bool {.inline.} =
self.protected and self.indexToPos(idx) >= self.ecK
func oldIndex*(encoded: Manifest, idx: int): int =
(idx div (encoded.interleave * (encoded.ecK + encoded.ecM))) * (encoded.interleave * encoded.ecK) +
(idx mod (encoded.interleave * (encoded.ecK + encoded.ecM)))
proc isPadding*(self: Manifest, idx: int): bool =
var
mfest = self
i = idx
while mfest.protected:
let coord = mfest.indexToCoord(i)
if mfest.isParity(i):
return false
i = mfest.oldIndex(i)
mfest = mfest.originalManifest
result = (i >= mfest.blocksCount)
############################################################
# Operations on block list
############################################################