<commit_before>package cz.encircled.joiner.reactive

import com.querydsl.core.types.ParamExpression
import com.querydsl.core.types.ParamNotSetException
import com.querydsl.core.types.dsl.Param
import cz.encircled.joiner.core.Joiner
import cz.encircled.joiner.query.JoinerQuery
import cz.encircled.joiner.reactive.composer.JoinerComposer
import org.hibernate.reactive.stage.Stage
import org.hibernate.reactive.stage.Stage.SessionFactory
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionStage
import javax.persistence.EntityManagerFactory

/**
 * Encapsulates Hibernate Reactive API and provides internal reactive API instead.
 *
 * Base class for other reactive implementations of Joiner
 */
abstract class GenericHibernateReactiveJoiner(val emf: EntityManagerFactory) {

    private val joiner = Joiner(emf.createEntityManager())

    private var sessionFactory: SessionFactory = emf.unwrap(SessionFactory::class.java)

    init {
        println("Creating GenericHibernateReactiveJoiner")
    }

    fun <T, C, P> executeComposed(c: JoinerComposer<T, C, P>): CompletionStage<C> {
        return sessionFactory().withTransaction { session, _ ->
            var curr = executeChainStep(false, session, c.steps[0])

            (1 until c.steps.size).forEach { i ->
                curr = curr.thenCompose { v ->
                    executeChainStep(v, session, c.steps[i])
                }
            }

            curr as CompletionStage<C>
        }
    }

    /**
     * @param value current result set
     * @param session DB session with open transaction
     * @param step chain step to be executed
     */
    protected fun executeChainStep(
        value: Any,
        session: Stage.Session,
        step: ExecutionStep<*>,
    ): CompletionStage<*> {
        return when (val stepResult = step.perform(value)) {
            // TODO this should probably be executed async and eventually returned in vertex thread
            is CompletableFuture<*> -> {
                try {
                    CompletableFuture.completedFuture(step.convertResult(stepResult.get() as List<Any>))
                } catch (e: Exception) {
                    CompletableFuture.failedStage(e.cause)
                }
            }

            is JoinerQuery<*, *> -> createQuery(session, stepResult).resultList.thenApply {
                step.convertResult(it as List<Any>)
            }

            is List<*> -> session.persistMultiple(stepResult).thenApply {
                step.convertResult(it as List<Any>)
            }
<commit_msg>Make compatible with java 8<commit_after>package cz.encircled.joiner.reactive

import com.querydsl.core.types.ParamExpression
import com.querydsl.core.types.ParamNotSetException
import com.querydsl.core.types.dsl.Param
import cz.encircled.joiner.core.Joiner
import cz.encircled.joiner.query.JoinerQuery
import cz.encircled.joiner.reactive.composer.JoinerComposer
import org.hibernate.reactive.stage.Stage
import org.hibernate.reactive.stage.Stage.SessionFactory
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionStage
import javax.persistence.EntityManagerFactory

/**
 * Encapsulates Hibernate Reactive API and provides internal reactive API instead.
 *
 * Base class for other reactive implementations of Joiner
 */
abstract class GenericHibernateReactiveJoiner(val emf: EntityManagerFactory) {

    private val joiner = Joiner(emf.createEntityManager())

    private var sessionFactory: SessionFactory = emf.unwrap(SessionFactory::class.java)

    init {
        println("Creating GenericHibernateReactiveJoiner")
    }

    fun <T, C, P> executeComposed(c: JoinerComposer<T, C, P>): CompletionStage<C> {
        return sessionFactory().withTransaction { session, _ ->
            var curr = executeChainStep(false, session, c.steps[0])

            (1 until c.steps.size).forEach { i ->
                curr = curr.thenCompose { v ->
                    executeChainStep(v, session, c.steps[i])
                }
            }

            curr as CompletionStage<C>
        }
    }

    /**
     * @param value current result set
     * @param session DB session with open transaction
     * @param step chain step to be executed
     */
    protected fun executeChainStep(
        value: Any,
        session: Stage.Session,
        step: ExecutionStep<*>,
    ): CompletionStage<*> {
        return when (val stepResult = step.perform(value)) {
            // TODO this should probably be executed async and eventually returned in vertex thread
            is CompletableFuture<*> -> {
                try {
                    CompletableFuture.completedFuture(step.convertResult(stepResult.get() as List<Any>))
                } catch (e: Exception) {
                    val c = CompletableFuture<Any>()
                    c.completeExceptionally(e.cause)
                    c
                }
            }

            is JoinerQuery<*, *> -> createQuery(session, stepResult).resultList.thenApply {
                step.convertResult(it as List<Any>)
            }

            is List<*> -> session.persistMultiple(stepResult).thenApply {
                step.convertResult(it as List<Any>)
            }

{'fraction_non_alphanumeric': 0.09240482112110197, 'fraction_numerical': 0.0009565716472163765, 'mean_word_length': 2.508724832214765, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3528212', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1379, 'n_words': 326}