## Nim-Codex ## Copyright (c) 2024 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. import std/sequtils import pkg/taskpools import pkg/taskpools/flowvars import pkg/chronos import pkg/chronos/threadsync import pkg/questionable/results import ./backend import ../errors import ../logutils logScope: topics = "codex asyncerasure" const CompletitionTimeout = 1.seconds # Maximum await time for completition after receiving a signal CompletitionRetryDelay = 10.millis type EncoderBackendPtr = ptr EncoderBackend DecoderBackendPtr = ptr DecoderBackend # Args objects are missing seq[seq[byte]] field, to avoid unnecessary data copy EncodeTaskArgs = object signal: ThreadSignalPtr backend: EncoderBackendPtr blockSize: int ecM: int DecodeTaskArgs = object signal: ThreadSignalPtr backend: DecoderBackendPtr blockSize: int ecK: int SharedArrayHolder*[T] = object data: ptr UncheckedArray[T] size: int EncodeTaskResult = Result[SharedArrayHolder[byte], cstring] DecodeTaskResult = Result[SharedArrayHolder[byte], cstring] proc encodeTask(args: EncodeTaskArgs, data: seq[seq[byte]]): EncodeTaskResult = var data = data.unsafeAddr parity = newSeqWith[seq[byte]](args.ecM, newSeq[byte](args.blockSize)) try: let res = args.backend[].encode(data[], parity) if res.isOk: let resDataSize = parity.len * args.blockSize resData = cast[ptr UncheckedArray[byte]](allocShared0(resDataSize)) arrHolder = SharedArrayHolder[byte]( data: resData, size: resDataSize ) for i in 0..