Skip to content

Commit 8bd838e

Browse files
feat: batching sending of authors
1 parent 896a692 commit 8bd838e

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/main/kotlin/app/hashers/RepoHasher.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import app.utils.EmptyRepoException
1616
import app.utils.FileHelper.toPath
1717
import app.utils.HashingException
1818
import app.utils.RepoHelper
19+
import app.utils.batch
1920
import org.eclipse.jgit.api.Git
2021
import java.io.File
2122
import java.io.IOException
@@ -145,7 +146,9 @@ class RepoHasher(private val api: Api,
145146
private fun postAuthorsToServer(authors: HashSet<Author>,
146147
serverRepo: Repo) {
147148
authors.forEach { author -> author.repo = serverRepo }
148-
api.postAuthors(authors.toList()).onErrorThrow()
149+
for (authorsBatch in authors.asSequence().batch(1000)) {
150+
api.postAuthors(authorsBatch).onErrorThrow()
151+
}
149152
}
150153

151154
private fun initServerRepo(localRepo: LocalRepo,

src/main/kotlin/app/utils/Misc.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2018 Sourcerer Inc. All Rights Reserved.
2+
// Author: Anatoly Kislov ([email protected])
3+
4+
package app.utils
5+
6+
// TODO(anatoly): Replace with chunked from new Kotlin version.
7+
// SO #34498368 @ Jayson Minard.
8+
fun <T> Sequence<T>.batch(n: Int): Sequence<List<T>> {
9+
return BatchingSequence(this, n)
10+
}
11+
12+
class BatchingSequence<T>(val source: Sequence<T>,
13+
val batchSize: Int) : Sequence<List<T>> {
14+
override fun iterator(): Iterator<List<T>> = object :
15+
AbstractIterator<List<T>>() {
16+
val iterate = if (batchSize > 0) source.iterator() else
17+
emptyList<T>().iterator()
18+
override fun computeNext() {
19+
if (iterate.hasNext()) setNext(iterate.asSequence()
20+
.take(batchSize).toList())
21+
else done()
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)