<commit_before>package models

import java.util.UUID

import com.datastax.driver.core.Row
import com.websudos.phantom.dsl._
import helpers.ExtCrypto._
import helpers._
import models.cassandra._

import scala.concurrent.Future
import scala.concurrent.duration._
import scala.language.postfixOps

/**
 * @author <EMAIL>
 */
case class ExpirableLink(id: String, target_id: String)

trait ExpirableLinkCanonicalNamed extends CanonicalNamed {

  override val basicName = "expirable_links"
}

sealed class ExpirableLinkTable
  extends NamedCassandraTable[ExpirableLinkTable, ExpirableLink]
  with ExpirableLinkCanonicalNamed {

  object id
    extends StringColumn(this)
    with PartitionKey[String]

  object target_id
    extends StringColumn(this)

  override def fromRow(r: Row): ExpirableLink =
    ExpirableLink(id(r), target_id(r))
}

object ExpirableLink
  extends ExpirableLinkCanonicalNamed
  with ExceptionDefining {

  case class NotFound(id: String)
    extends BaseException(error_code("not.found"))

}

class ExpirableLinks(
  implicit
  val basicPlayApi: BasicPlayApi,
  val contactPoint: KeySpaceBuilder
)
  extends ExpirableLinkTable
  with ExtCQL[ExpirableLinkTable, ExpirableLink]
  with BasicPlayComponents
  with CassandraComponents
  with BootingProcess
  with Logging {

  onStart(create.ifNotExists.future())

  def find(id: String): Future[ExpirableLink] =
    CQL {
      select.where(_.id eqs id)
    }.one().map {
      case None       => throw ExpirableLink.NotFound(id)
      case Some(link) => link
    }

  def save(
    target_id: String,
    ttl: FiniteDuration = 24 hours
  ): Future[ExpirableLink] = {
    val id = Crypto.sha2(s"$target_id--${UUID.randomUUID()}", 512)
    CQL {
      insert.value(_.id, id)
        .value(_.target_id, target_id)
        .ttl(ttl.toSeconds)
    }.future().map {
      _ => ExpirableLink(id, target_id)
    }
  }

  def remove(id: String): Future[ResultSet] =
    CQL {
      delete.where(_.id eqs id)
    }.future()
}<commit_msg>Made length of expirable link configurable<commit_after>package models

import java.util.UUID

import com.datastax.driver.core.Row
import com.websudos.phantom.dsl._
import helpers.ExtCrypto._
import helpers._
import models.cassandra._

import scala.concurrent.Future
import scala.concurrent.duration._
import scala.language.postfixOps

/**
 * @author <EMAIL>
 */
case class ExpirableLink(id: String, target_id: String)

trait ExpirableLinkCanonicalNamed extends CanonicalNamed {

  override val basicName = "expirable_links"
}

sealed class ExpirableLinkTable
  extends NamedCassandraTable[ExpirableLinkTable, ExpirableLink]
  with ExpirableLinkCanonicalNamed {

  object id
    extends StringColumn(this)
    with PartitionKey[String]

  object target_id
    extends StringColumn(this)

  override def fromRow(r: Row): ExpirableLink =
    ExpirableLink(id(r), target_id(r))
}

object ExpirableLink
  extends ExpirableLinkCanonicalNamed
  with ExceptionDefining {

  case class NotFound(id: String)
    extends BaseException(error_code("not.found"))

}

class ExpirableLinks(
  implicit
  val basicPlayApi: BasicPlayApi,
  val contactPoint: KeySpaceBuilder
)
  extends ExpirableLinkTable
  with ExtCQL[ExpirableLinkTable, ExpirableLink]
  with BasicPlayComponents
  with CassandraComponents
  with BootingProcess
  with Logging {

  onStart(create.ifNotExists.future())

  def find(id: String): Future[ExpirableLink] =
    CQL {
      select.where(_.id eqs id)
    }.one().map {
      case None       => throw ExpirableLink.NotFound(id)
      case Some(link) => link
    }

  def save(
    target_id: String,
    length: Int = 256,
    ttl: FiniteDuration = 24 hours
  ): Future[ExpirableLink] = {
    val ln = if (length < 8) 8 else length
    val id = Crypto.sha2(s"$target_id--${UUID.randomUUID()}", ln)
    CQL {
      insert.value(_.id, id)
        .value(_.target_id, target_id)
        .ttl(ttl.toSeconds)
    }.future().map {
      _ => ExpirableLink(id, target_id)
    }
  }

  def remove(id: String): Future[ResultSet] =
    CQL {
      delete.where(_.id eqs id)
    }.future()
}
{'fraction_non_alphanumeric': 0.09671890303623898, 'fraction_numerical': 0.0034280117531831538, 'mean_word_length': 3.794600938967136, 'pattern_counts': {'":': 0, '<': 6, '<?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': '3128926', 'n_tokens_mistral': 1351, 'n_tokens_neox': 1362, 'n_words': 302}