<commit_before>package io.mediachain.util

import cats.data.Xor
import gremlin.scala._
import io.mediachain.core.GraphError
import io.mediachain.core.GraphError.TransactionFailed

object GremlinUtils {

  def withTransaction[T](graph: Graph)(f: => T): Xor[GraphError, T] = {
    val isNested = graph.tx.isOpen
    if (!isNested) graph.tx.open()

    val result: Xor[GraphError, T] =
      Xor.catchNonFatal(f).leftMap(TransactionFailed)

    if (!isNested) {
      result match {
        case Xor.Left(_) => graph.tx.rollback()
        case _ => graph.tx.commit()
      }
    }

    result
  }


  def withTransactionXor[T](graph: Graph)(f: => Xor[GraphError, T])
  : Xor[GraphError, T] = {
    val isNested = graph.tx.isOpen
    if (!isNested) graph.tx.open()

    val result: Xor[GraphError, T] =
      Xor.catchNonFatal(f)
        .leftMap(TransactionFailed)
        .flatMap(res => res)

    if (!isNested) {
      result match {
        case Xor.Left(er) => graph.tx.rollback()
        case _ => graph.tx.commit()
      }
    }

    result
  }
}<commit_msg>use raw orient db transactions<commit_after>package io.mediachain.util

import cats.data.Xor
import gremlin.scala._
import io.mediachain.core.GraphError
import io.mediachain.core.GraphError.TransactionFailed
import org.apache.tinkerpop.gremlin.orientdb.OrientGraph

object GremlinUtils {
  def beginTx(graph: Graph): Unit = graph match {
    case orientGraph: OrientGraph =>
      orientGraph.getRawDatabase.begin()
    case _ => graph.tx.open()
  }

  def commitTx(graph: Graph): Unit = graph match {
    case orientGraph: OrientGraph =>
      orientGraph.getRawDatabase.commit()
    case _ => graph.tx.commit()
  }

  def rollbackTx(graph: Graph): Unit = graph match {
    case orientGraph: OrientGraph =>
      orientGraph.getRawDatabase.rollback()
    case _ => graph.tx.rollback()
  }

  def withTransaction[T](graph: Graph)(f: => T): Xor[GraphError, T] = {

    beginTx(graph)

    val result: Xor[GraphError, T] =
      Xor.catchNonFatal(f).leftMap(TransactionFailed)

    result match {
      case Xor.Left(_) => rollbackTx(graph)
      case _ => commitTx(graph)
    }

    result
  }


  def withTransactionXor[T](graph: Graph)(f: => Xor[GraphError, T])
  : Xor[GraphError, T] = {
    beginTx(graph)

    val result: Xor[GraphError, T] =
      Xor.catchNonFatal(f)
        .leftMap(TransactionFailed)
        .flatMap(res => res)

    result match {
      case Xor.Left(er) => rollbackTx(graph)
      case _ => commitTx(graph)
    }

    result
  }
}
{'fraction_non_alphanumeric': 0.1281237604125347, 'fraction_numerical': 0.0, 'mean_word_length': 2.9716535433070868, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '1085929', 'n_tokens_mistral': 883, 'n_tokens_neox': 846, 'n_words': 168}