Skip to content

Commit 9d9540c

Browse files
committed
Optimize DELETE events with batch; add big JSON for test
1 parent 64b6dae commit 9d9540c

File tree

2 files changed

+325048
-25
lines changed

2 files changed

+325048
-25
lines changed

src/jvmMain/kotlin/Application.kt

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -563,22 +563,21 @@ fun Application.main() {
563563
}
564564
}
565565

566-
delete("/${EVENT_ENTITY_API_NAME}") { // TODO batches to speed up
566+
delete("/${EVENT_ENTITY_API_NAME}") {
567567
val roles = call.principal<WithRoles>()?.roles!!
568568
if (!roles.isAdmin) {
569569
call.respond(HttpStatusCode.Unauthorized, "Only user with Admin role can delete events")
570570
return@delete
571571
}
572572
var connEMD: Connection? = null
573-
var deletedCount = 0
574573
try {
575574
// Here, we only care about reference to event, other event data is optional and is ignored, if passed
576575
val events = call.receive<Array<EventReprForDelete>>()
577576
connEMD = newEMDConnection(config, this.context)
578577
connEMD!!.autoCommit = false
579578
val storageMap = getStorageMap(connEMD!!)
579+
val fileData = mutableMapOf<Pair<Byte, String>, Int>()
580580
events.forEach { event ->
581-
println("Deleting event: $event")
582581
val storage_name = event.reference.storage_name
583582
val storage_id = storageMap.str_to_id[storage_name]
584583
if (storage_id == null) {
@@ -589,35 +588,44 @@ fun Application.main() {
589588
return@delete
590589
}
591590
val file_path = event.reference.file_path
592-
val file_guid: Int
593-
val res = connEMD!!.createStatement().executeQuery(
594-
"""SELECT file_guid FROM file_ WHERE storage_id = $storage_id AND file_path = '$file_path'"""
595-
)
596-
if (res.next()) {
597-
file_guid = res.getInt("file_guid")
598-
println("File GUID = $file_guid")
599-
} else { // no such file
600-
call.respond(
601-
HttpStatusCode.NotFound,
602-
"Error: file_guid not found for event ${event.str()}"
591+
if (Pair(storage_id, file_path) !in fileData) {
592+
val res = connEMD!!.createStatement().executeQuery(
593+
"""SELECT file_guid FROM file_ WHERE storage_id = $storage_id AND file_path = '$file_path'"""
603594
)
604-
return@delete
595+
if (res.next()) {
596+
val file_guid: Int = res.getInt("file_guid")
597+
println("File GUID ($storage_id, $file_path) = $file_guid")
598+
fileData[Pair(storage_id, file_path)] = file_guid
599+
} else { // no such file
600+
call.respond(
601+
HttpStatusCode.NotFound,
602+
"Error: file_guid not found for event ${event.str()}"
603+
)
604+
return@delete
605+
}
605606
}
607+
}
608+
val stmt = connEMD!!.createStatement()
609+
events.forEach { event ->
610+
println("Deleting event: $event")
611+
val storage_id = storageMap.str_to_id[event.reference.storage_name]
612+
val file_guid = fileData[Pair(storage_id, event.reference.file_path)]
606613
val query = """
607614
DELETE FROM ${page.db_table_name}
608615
WHERE (("file_guid" = $file_guid AND "event_number" = ${event.reference.event_number}));
609616
""".trimIndent()
610617
println(query)
611-
val intRes = connEMD!!.createStatement().executeUpdate(query)
612-
if (intRes == 1) {
613-
deletedCount++
614-
} else {
615-
call.respond(
616-
HttpStatusCode.NotFound,
617-
"Error: event (${event.str()}) not found"
618-
)
619-
return@delete
620-
}
618+
stmt.addBatch(query)
619+
}
620+
val res = stmt.executeBatch()
621+
val deletedCount = res.sum()
622+
if (deletedCount != events.size) {
623+
call.respond(
624+
HttpStatusCode.NotFound,
625+
"Error: could not find some of the events to delete, aborting transaction.\n" +
626+
"The first missing event was ${events[res.indexOfFirst { it == 0 }].str()}"
627+
)
628+
return@delete
621629
}
622630
connEMD!!.commit()
623631
call.respond(HttpStatusCode.OK, "Success: $deletedCount event(s) were deleted")

0 commit comments

Comments
 (0)