All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.huanshankeji.vertx.sqlclient.PreparedQueryBatchExecution.kt Maven / Gradle / Ivy

package com.huanshankeji.vertx.sqlclient

import com.huanshankeji.Untested
import com.huanshankeji.collections.lexicographicOrderComparable
import io.vertx.sqlclient.PreparedQuery
import io.vertx.sqlclient.Tuple

/**
 * There can easily be deadlocks when batch-updating data concurrently in multiple event loops
 * if they are overlapped and unsorted.
 */
@Untested
fun > PreparedQuery.sortDataAndExecuteBatch(
    dataList: List, selector: (DataT) -> SelectorResultT, toTuple: DataT.() -> Tuple
) =
    executeBatch(dataList.sortedBy(selector).map(toTuple))


@Untested
inline fun > PreparedQuery.sortTuplesAndExecuteBatch(
    batch: List, crossinline selector: (Tuple) -> SelectorResultT
) =
    executeBatch(batch.sortedBy(selector))


@Untested
inline fun > PreparedQuery.sortTuplesAndExecuteBatch(
    batch: List, selectorPosition: Int
) =
    sortTuplesAndExecuteBatch(batch) { it.get(SelectorResultT::class.java, selectorPosition) }

@Untested
fun  PreparedQuery.sortTuplesAndExecuteBatch(
    batch: List, selectorPositions: List
) =
    sortTuplesAndExecuteBatch(batch) { tuple ->
        val listSelector = selectorPositions.map { i -> tuple.getValue(i) }
        @Suppress("UNCHECKED_CAST")
        (listSelector as List>).lexicographicOrderComparable()
    }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy