<commit_before>package org.vitrivr.adampro.rpc

import java.util.concurrent.TimeUnit

import io.grpc.internal.DnsNameResolverProvider
import io.grpc.netty.NettyChannelBuilder
import io.grpc.stub.StreamObserver
import io.grpc.{ManagedChannel, ManagedChannelBuilder}
import org.vitrivr.adampro.grpc.grpc.AdamDefinitionGrpc.{AdamDefinitionBlockingStub, AdamDefinitionStub}
import org.vitrivr.adampro.grpc.grpc.AdamSearchGrpc.{AdamSearchBlockingStub, AdamSearchStub}
import org.vitrivr.adampro.grpc.grpc.AdaptScanMethodsMessage.IndexCollection.NEW_INDEXES
import org.vitrivr.adampro.grpc.grpc.AdaptScanMethodsMessage.QueryCollection.RANDOM_QUERIES
import org.vitrivr.adampro.grpc.grpc.DistanceMessage.DistanceType
import org.vitrivr.adampro.grpc.grpc.RepartitionMessage.PartitionOptions
import org.vitrivr.adampro.grpc.grpc._
import org.vitrivr.adampro.rpc.datastructures.{RPCAttributeDefinition, RPCQueryObject, RPCQueryResults}
import org.vitrivr.adampro.utils.Logging

import scala.util.{Failure, Success, Try}

/**
  * adamtwo
  *
  * <NAME>
  * March 2016
  */
class RPCClient(channel: ManagedChannel,
                private[adampro] val definerBlocking: AdamDefinitionBlockingStub,
                private[adampro] val definer: AdamDefinitionStub,
                private[adampro] val searcherBlocking: AdamSearchBlockingStub,
                private[adampro] val searcher: AdamSearchStub) extends Logging {
  /**
    *
    * @param desc description
    * @param op   operation
    * @return
    */
  private def execute[T](desc: String)(op: => Try[T]): Try[T] = {
    try {
      log.debug("starting " + desc)
      val t1 = System.currentTimeMillis
      val res = op
      val t2 = System.currentTimeMillis
      log.debug("performed " + desc + " in " + (t2 - t1) + " msecs")
      res
    } catch {
      case e: Exception =>
        log.error("error in " + desc, e)
        Failure(e)
    }
  }


  /**
    * Create an entity.
    *
    * @param entityname name of entity
    * @param attributes attributes of new entity
    * @return
    */
  def entityCreate(entityname: String, attributes: Seq[RPCAttributeDefinition]): Try[String] = {
    execute("create entity operation") {
      val attributeMessages = attributes.map { attribute =>
        val adm = AttributeDefinitionMessage(attribute.name, getAttributeType(attribute.datatype), attribute.pk, params = attribute.params)

        //add handler information if available
        if (attribute.storagehandlername.isDefined) {
          adm.withHandler(attribute.storagehandlername.get)
        }

        adm
      }

      val res = definerBlocking.createEntity(CreateEntityMessage(entityname, attributeMessages))
      if (res.code == AckMessage.Code.OK) {
        return Success(res.message)
      } else {
        return Failure(new Exception(res.message))
      }
    }
  }

  /**
    * Check if entity exists.
    *
    * @param entityname name of entity
    * @return
    */
  def entityExists(entityname: String): Try[Boolean] = {
    execute("entity exists operation") {
      val res = definerBlocking.existsEntity(EntityNameMessage(entityname))
      if (res.ack.get.code == AckMessage.Code.OK) {
        return Success(res.exists)
      } else {
        return Failure(new Exception(res.ack.get.message))
      }
    }
  }<commit_msg>handler information was not passed to adampro<commit_after>package org.vitrivr.adampro.rpc

import java.util.concurrent.TimeUnit

import io.grpc.internal.DnsNameResolverProvider
import io.grpc.netty.NettyChannelBuilder
import io.grpc.stub.StreamObserver
import io.grpc.{ManagedChannel, ManagedChannelBuilder}
import org.vitrivr.adampro.grpc.grpc.AdamDefinitionGrpc.{AdamDefinitionBlockingStub, AdamDefinitionStub}
import org.vitrivr.adampro.grpc.grpc.AdamSearchGrpc.{AdamSearchBlockingStub, AdamSearchStub}
import org.vitrivr.adampro.grpc.grpc.AdaptScanMethodsMessage.IndexCollection.NEW_INDEXES
import org.vitrivr.adampro.grpc.grpc.AdaptScanMethodsMessage.QueryCollection.RANDOM_QUERIES
import org.vitrivr.adampro.grpc.grpc.DistanceMessage.DistanceType
import org.vitrivr.adampro.grpc.grpc.RepartitionMessage.PartitionOptions
import org.vitrivr.adampro.grpc.grpc._
import org.vitrivr.adampro.rpc.datastructures.{RPCAttributeDefinition, RPCQueryObject, RPCQueryResults}
import org.vitrivr.adampro.utils.Logging

import scala.util.{Failure, Success, Try}

/**
  * adamtwo
  *
  * <NAME>
  * March 2016
  */
class RPCClient(channel: ManagedChannel,
                private[adampro] val definerBlocking: AdamDefinitionBlockingStub,
                private[adampro] val definer: AdamDefinitionStub,
                private[adampro] val searcherBlocking: AdamSearchBlockingStub,
                private[adampro] val searcher: AdamSearchStub) extends Logging {
  /**
    *
    * @param desc description
    * @param op   operation
    * @return
    */
  private def execute[T](desc: String)(op: => Try[T]): Try[T] = {
    try {
      log.debug("starting " + desc)
      val t1 = System.currentTimeMillis
      val res = op
      val t2 = System.currentTimeMillis
      log.debug("performed " + desc + " in " + (t2 - t1) + " msecs")
      res
    } catch {
      case e: Exception =>
        log.error("error in " + desc, e)
        Failure(e)
    }
  }


  /**
    * Create an entity.
    *
    * @param entityname name of entity
    * @param attributes attributes of new entity
    * @return
    */
  def entityCreate(entityname: String, attributes: Seq[RPCAttributeDefinition]): Try[String] = {
    execute("create entity operation") {
      val attributeMessages = attributes.map { attribute =>
        var adm = AttributeDefinitionMessage(attribute.name, getAttributeType(attribute.datatype), attribute.pk, params = attribute.params)

        //add handler information if available
        if (attribute.storagehandlername.isDefined) {
          adm = adm.withHandler(attribute.storagehandlername.get)
        }

        adm
      }

      val res = definerBlocking.createEntity(CreateEntityMessage(entityname, attributeMessages))
      if (res.code == AckMessage.Code.OK) {
        return Success(res.message)
      } else {
        return Failure(new Exception(res.message))
      }
    }
  }

  /**
    * Check if entity exists.
    *
    * @param entityname name of entity
    * @return
    */
  def entityExists(entityname: String): Try[Boolean] = {
    execute("entity exists operation") {
      val res = definerBlocking.existsEntity(EntityNameMessage(entityname))
      if (res.ack.get.code == AckMessage.Code.OK) {
        return Success(res.exists)
      } else {
        return Failure(new Exception(res.ack.get.message))
      }
    }
  }
{'fraction_non_alphanumeric': 0.1028837488735356, 'fraction_numerical': 0.0024031240612796636, 'mean_word_length': 3.2333121424030513, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '5936030', 'n_tokens_mistral': 2065, 'n_tokens_neox': 2034, 'n_words': 389}