Skip to content

Commit 2bd3343

Browse files
authored
Merge pull request #390 from modelix/bugfix/objects-put-transactions
fix(model-server): use one large transaction when putting many values
2 parents 3f42ecd + 1b00161 commit 2bd3343

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

model-server/src/main/kotlin/org/modelix/model/server/handlers/ModelReplicationServer.kt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -225,26 +225,26 @@ class ModelReplicationServer(val repositoriesManager: RepositoriesManager) {
225225
}
226226
route("objects") {
227227
put {
228-
var writtenEntries = 0
229-
withContext(Dispatchers.IO) {
230-
var isKey = true
231-
var key = ""
232-
call.receiveStream().bufferedReader().lineSequence().forEach { line ->
233-
if (isKey) {
234-
key = line
235-
} else {
236-
val value = line
228+
val writtenEntries = withContext(Dispatchers.IO) {
229+
val entries = call.receiveStream().bufferedReader().use { reader ->
230+
reader.lineSequence().windowed(2, 2).map {
231+
val key = it[0]
232+
val value = it[1]
233+
237234
require(HashUtil.isSha256(key)) {
238235
"This API cannot be used to store other entries than serialized objects." +
239236
" The key is expected to be a SHA256 hash over the value: $key -> $value"
240237
}
241238
val expectedKey = HashUtil.sha256(value)
242239
require(expectedKey == key) { "Hash mismatch. Expected $expectedKey, but $key was provided. Value: $value" }
243-
storeClient.put(key, value, true)
244-
writtenEntries++
245-
}
246-
isKey = !isKey
240+
241+
key to value
242+
}.toMap()
247243
}
244+
245+
storeClient.putAll(entries, true)
246+
247+
entries.size
248248
}
249249
call.respondText("$writtenEntries objects received")
250250
}

0 commit comments

Comments
 (0)