<commit_before>package io.vamp.core.persistence.store

import com.typesafe.scalalogging.Logger
import io.vamp.core.model.artifact._
import io.vamp.core.persistence.notification.{ArtifactNotFound, PersistenceNotificationProvider, PersistenceOperationFailure, UnsupportedPersistenceRequest}
import io.vamp.core.persistence.slick.components.Components
import io.vamp.core.persistence.slick.model._
import io.vamp.core.persistence.store.jdbc._
import org.slf4j.LoggerFactory

import scala.concurrent.ExecutionContext
import scala.slick.jdbc.JdbcBackend
import scala.slick.jdbc.JdbcBackend._

case class JdbcStoreInfo(`type`: String, url: String, database: DatabaseInfo)

case class DatabaseInfo(name: String, version: String, schemaVersion : String)

/**
 * JDBC storage of artifacts
 */
class JdbcStoreProvider(executionContext: ExecutionContext) extends StoreProvider with PersistenceNotificationProvider {

  val db: Database = Database.forConfig("vamp.core.persistence.jdbc.provider")
  implicit val sess = db.createSession()

  override val store: Store = new JdbcStore()

  private class JdbcStore(implicit val sess: JdbcBackend.Session) extends Store with ScaleStore with PortStore
  with DeploymentStore with BlueprintStore with BreedStore
  with RoutingStore with FilterStore
  with EnvironmentVariableStore
  with SlaStore with EscalationStore with ParameterStore {

    import io.vamp.core.persistence.slick.components.Components.instance._

    private val logger = Logger(LoggerFactory.getLogger(classOf[JdbcStoreProvider]))

    Components.instance.upgradeSchema

    def info = JdbcStoreInfo(
      "jdbc",
      sess.conn.getMetaData.getURL,
      DatabaseInfo(sess.conn.getMetaData.getDatabaseProductName, sess.conn.getMetaData.getDatabaseProductVersion, Components.instance.schemaInfo(sess))
    )

    def create(artifact: Artifact, ignoreIfExists: Boolean): Artifact = {
      read(artifact.name, artifact.getClass) match {
        case None => createArtifact(artifact)
        case Some(storedArtifact) if !ignoreIfExists => update(artifact, create = false)
        case Some(storedArtifact) if ignoreIfExists => storedArtifact
      }
    }

    def read(name: String, ofType: Class[_ <: Artifact]): Option[Artifact] = {
      findArtifact(name, ofType)
    }

    def update(artifact: Artifact, create: Boolean): Artifact = {
      read(artifact.name, artifact.getClass) match {
        case None =>
          if (create) this.createArtifact(artifact)
          else throw exception(ArtifactNotFound(artifact.name, artifact.getClass))
        case Some(existingArtifact) => updateArtifact(artifact)
      }
    }

    def delete(name: String, ofType: Class[_ <: Artifact]): Artifact = {
      findArtifact(name, ofType) match {
        case Some(artifact) =>
          deleteArtifact(artifact)
          artifact
        case None =>
          throw exception(ArtifactNotFound(name, ofType))
      }
    }

    def all(ofType: Class[_ <: Artifact]): List[_ <: Artifact] = {
      ofType match {
        case _ if ofType == classOf[Deployment] => Deployments.fetchAll.map(a => read(a.name, ofType).get)
        case _ if ofType == classOf[DefaultBlueprint] || ofType == classOf[Blueprint] => DefaultBlueprints.fetchAll.map(a => read(a.name, classOf[DefaultBlueprint]).get)
        case _ if ofType == classOf[GenericEscalation] || ofType == classOf[Escalation] ||<commit_msg>logging statement<commit_after>package io.vamp.core.persistence.store

import com.typesafe.scalalogging.Logger
import io.vamp.core.model.artifact._
import io.vamp.core.persistence.notification.{ArtifactNotFound, PersistenceNotificationProvider, PersistenceOperationFailure, UnsupportedPersistenceRequest}
import io.vamp.core.persistence.slick.components.Components
import io.vamp.core.persistence.slick.model._
import io.vamp.core.persistence.store.jdbc._
import org.slf4j.LoggerFactory

import scala.concurrent.ExecutionContext
import scala.slick.jdbc.JdbcBackend
import scala.slick.jdbc.JdbcBackend._

case class JdbcStoreInfo(`type`: String, url: String, database: DatabaseInfo)

case class DatabaseInfo(name: String, version: String, schemaVersion : String)

/**
 * JDBC storage of artifacts
 */
class JdbcStoreProvider(executionContext: ExecutionContext) extends StoreProvider with PersistenceNotificationProvider {

  val db: Database = Database.forConfig("vamp.core.persistence.jdbc.provider")
  implicit val sess = db.createSession()

  override val store: Store = new JdbcStore()

  private class JdbcStore(implicit val sess: JdbcBackend.Session) extends Store with ScaleStore with PortStore
  with DeploymentStore with BlueprintStore with BreedStore
  with RoutingStore with FilterStore
  with EnvironmentVariableStore
  with SlaStore with EscalationStore with ParameterStore {

    import io.vamp.core.persistence.slick.components.Components.instance._

    private val logger = Logger(LoggerFactory.getLogger(classOf[JdbcStoreProvider]))

    Components.instance.upgradeSchema

    def info = JdbcStoreInfo(
      "jdbc",
      sess.conn.getMetaData.getURL,
      DatabaseInfo(sess.conn.getMetaData.getDatabaseProductName, sess.conn.getMetaData.getDatabaseProductVersion, Components.instance.schemaInfo(sess))
    )

    def create(artifact: Artifact, ignoreIfExists: Boolean): Artifact = {
      logger.debug(s"create [$ignoreIfExists] $artifact")
      read(artifact.name, artifact.getClass) match {
        case None => createArtifact(artifact)
        case Some(storedArtifact) if !ignoreIfExists => update(artifact, create = false)
        case Some(storedArtifact) if ignoreIfExists => storedArtifact
      }
    }

    def read(name: String, ofType: Class[_ <: Artifact]): Option[Artifact] = {
      findArtifact(name, ofType)
    }

    def update(artifact: Artifact, create: Boolean): Artifact = {
      logger.debug(s"update [$create] $artifact")
      read(artifact.name, artifact.getClass) match {
        case None =>
          if (create) this.createArtifact(artifact)
          else throw exception(ArtifactNotFound(artifact.name, artifact.getClass))
        case Some(existingArtifact) => updateArtifact(artifact)
      }
    }

    def delete(name: String, ofType: Class[_ <: Artifact]): Artifact = {
      findArtifact(name, ofType) match {
        case Some(artifact) =>
          deleteArtifact(artifact)
          artifact
        case None =>
          throw exception(ArtifactNotFound(name, ofType))
      }
    }

    def all(ofType: Class[_ <: Artifact]): List[_ <: Artifact] = {
      ofType match {
        case _ if ofType == classOf[Deployment] => Deployments.fetchAll.map(a => read(a.name, ofType).get)
        case _ if ofType == classOf[DefaultBlueprint] || ofType == classOf[Blueprint] => DefaultBlueprints.fetchAll.map(a => read(a.name, classOf[DefaultBlueprint]).get)
        case _ if ofType == classOf[GenericEscalation] || ofType == classOf[Escalation] ||
{'fraction_non_alphanumeric': 0.09915917657291969, 'fraction_numerical': 0.00028993911278631486, 'mean_word_length': 4.202865761689291, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1894293', 'n_tokens_mistral': 2020, 'n_tokens_neox': 1957, 'n_words': 435}