From 5dda9a565086cf54dd7b5b2041d668e9599b2b84 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 15 Nov 2022 11:57:56 -0600 Subject: [PATCH] wip: adding proper batching helper --- codex/utils/batchutils.nim | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 codex/utils/batchutils.nim diff --git a/codex/utils/batchutils.nim b/codex/utils/batchutils.nim new file mode 100644 index 00000000..4a68c074 --- /dev/null +++ b/codex/utils/batchutils.nim @@ -0,0 +1,24 @@ +## Nim-Codex +## Copyright (c) 2022 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 pkg/upraises +push: {.upraises: [].} + +import std/sequtils + +template batchIt*[T](elms: openArray[T], batchSize: int, body: untyped) = + let + batches = + (elms.len div batchSize) + + (if (elms.len mod batchSize) > 0: 1 else: 0) + + trace "Splitting requests into batches", elements = elms.len, batches = batches, size = batchSize + for it {.inject.} in elms.distribute(max(1, batches), spread = false): + block: + body