<commit_before>package models

import shared.{AddressPool, IpAddressConfiguration}

import play.api.Configuration
import play.api.libs.json._
import util.{Config, IpAddress, IpAddressCalc}
import util.plugins.Callback
import org.squeryl.dsl.ast.LogicalBoolean

case class IpAddresses(
  asset_id: Long,
  gateway: Long,
  address: Long,
  netmask: Long,
  pool: String,
  id: Long = 0) extends IpAddressable
{
  override def asJson: String = {
    Json.stringify(JsObject(forJsonObject))
  }
  def toJsonObject() = JsObject(forJsonObject)
  def forJsonObject(): Seq[(String,JsValue)] = Seq(
    "ID" -> JsNumber(getId()),
    "ASSET_ID" -> JsNumber(getAssetId()),
    "ADDRESS" -> JsString(dottedAddress),
    "GATEWAY" -> JsString(dottedGateway),
    "NETMASK" -> JsString(dottedNetmask),
    "POOL" -> JsString(pool)
  )
}

object IpAddresses extends IpAddressStorage[IpAddresses] with IpAddressCacheManagement {
  import org.squeryl.PrimitiveTypeMode._

  override protected def createEventName: Option[String] = Some("ipAddresses_create")
  override protected def updateEventName: Option[String] = Some("ipAddresses_update")
  override protected def deleteEventName: Option[String] = Some("ipAddresses_delete")

  lazy val AddressConfig = IpAddressConfiguration(Config.get("ipAddresses"))

  val tableDef = table[IpAddresses]("ip_addresses")
  on(tableDef)(i => declare(
    i.id is(autoIncremented,primaryKey),
    i.address is(unique),
    i.gateway is(indexed),
    i.netmask is(indexed),
    i.pool is(indexed),
    columns(i.asset_id, i.address) are(indexed)
  ))

  def createForAsset(asset: Asset, count: Int, scope: Option[String]): Seq[IpAddresses] = {
    (0 until count).map { i =>
      createForAsset(asset, scope)
    }
  }

  override def getNextAvailableAddress(overrideStart: Option[String] = None)(implicit scope: Option[String]): Tuple3[Long,Long,Long] = {
    throw new UnsupportedOperationException("getNextAvailableAddress not supported")
  }

  def getNextAddress(iteration: Int)(implicit scope: Option[String]): Tuple3[Long,Long,Long] = {
    val network = getNetwork
    val startAt = getStartAddress
    val calc = IpAddressCalc(network, startAt)
    val gateway: Long = getGateway().getOrElse(calc.minAddressAsLong)
    val netmask: Long = calc.netmaskAsLong
    val currentMax: Option[Long] = iteration match {
      case norm if norm == 0 =>
        nextAddressFromCache(scope)
      case failed =>
        logger.info("Address cache dirty. Failed cache lookup so using DB")
        populateCacheIfNeeded(scope, true)
        getCurrentMaxAddress(calc.minAddressAsLong, calc.maxAddressAsLong)
    }
    val address: Long = calc.nextAvailableAsLong(currentMax)
    (gateway, address, netmask)
  }

  def createForAsset(asset: Asset, scope: Option[String]): IpAddresses = inTransaction {
    val assetId = asset.getId
    val cfg = getConfig()(scope)
    createWithRetry(10) { attempt =>
      val (gateway, address, netmask) = getNextAddress(attempt)(scope)
      logger.debug("trying to use address %s".format(IpAddress.toString(address)))
      val ipAddresses = IpAddresses(assetId, gateway, address, netmask, scope.getOrElse(""))
      super.create(ipAddresses)
    }
  }

  def deleteByAssetAndPool(asset: Asset, pool: Option[String]): Int = inTransaction {
    val rows = tableDef.where(i =>
      i.asset_id === asset.id and
      i.pool === pool.?
    ).toList
    rows.foldLeft(0) { case(sum, ipInfo) =>
      sum + delete(ipInfo)
    }
  }

  def findAssetsByAddress(page: PageParams, addys: Seq[String], finder: AssetFinder): Page[AssetView] = {
    def whereClause(assetRow: Asset, addressRow: IpAddresses) = {
      where(
        (assetRow.id === addressRow.asset_id) and
        generateFindQuery(addressRow, addys.head) and
        finder.asLogicalBoolean(assetRow)
      )
    }
    inTransaction { log {
      val results = from(Asset.tableDef, tableDef)((assetRow, addressRow) =>
        whereClause(assetRow, addressRow)
        select(assetRow)
      ).page(page.offset, page.size).toList
      val totalCount = from(Asset.tableDef, tableDef)((assetRow, addressRow) =>
        whereClause(assetRow, addressRow)<commit_msg>adding solr reindexing hooks for ip address create/delete<commit_after>package models

import shared.{AddressPool, IpAddressConfiguration}

import play.api.Configuration
import play.api.libs.json._
import util.{Config, IpAddress, IpAddressCalc}
import util.plugins.Callback
import org.squeryl.dsl.ast.LogicalBoolean

case class IpAddresses(
  asset_id: Long,
  gateway: Long,
  address: Long,
  netmask: Long,
  pool: String,
  id: Long = 0) extends IpAddressable
{
  override def asJson: String = {
    Json.stringify(JsObject(forJsonObject))
  }
  def toJsonObject() = JsObject(forJsonObject)
  def forJsonObject(): Seq[(String,JsValue)] = Seq(
    "ID" -> JsNumber(getId()),
    "ASSET_ID" -> JsNumber(getAssetId()),
    "ADDRESS" -> JsString(dottedAddress),
    "GATEWAY" -> JsString(dottedGateway),
    "NETMASK" -> JsString(dottedNetmask),
    "POOL" -> JsString(pool)
  )
}

object IpAddresses extends IpAddressStorage[IpAddresses] with IpAddressCacheManagement {
  import org.squeryl.PrimitiveTypeMode._

  override protected def createEventName: Option[String] = Some("ipAddresses_create")
  override protected def updateEventName: Option[String] = Some("ipAddresses_update")
  override protected def deleteEventName: Option[String] = Some("ipAddresses_delete")

  lazy val AddressConfig = IpAddressConfiguration(Config.get("ipAddresses"))

  val tableDef = table[IpAddresses]("ip_addresses")
  on(tableDef)(i => declare(
    i.id is(autoIncremented,primaryKey),
    i.address is(unique),
    i.gateway is(indexed),
    i.netmask is(indexed),
    i.pool is(indexed),
    columns(i.asset_id, i.address) are(indexed)
  ))

  def createForAsset(asset: Asset, count: Int, scope: Option[String]): Seq[IpAddresses] = {
    (0 until count).map { i =>
      createForAsset(asset, scope)
    }
  }

  override def getNextAvailableAddress(overrideStart: Option[String] = None)(implicit scope: Option[String]): Tuple3[Long,Long,Long] = {
    throw new UnsupportedOperationException("getNextAvailableAddress not supported")
  }

  def getNextAddress(iteration: Int)(implicit scope: Option[String]): Tuple3[Long,Long,Long] = {
    val network = getNetwork
    val startAt = getStartAddress
    val calc = IpAddressCalc(network, startAt)
    val gateway: Long = getGateway().getOrElse(calc.minAddressAsLong)
    val netmask: Long = calc.netmaskAsLong
    val currentMax: Option[Long] = iteration match {
      case norm if norm == 0 =>
        nextAddressFromCache(scope)
      case failed =>
        logger.info("Address cache dirty. Failed cache lookup so using DB")
        populateCacheIfNeeded(scope, true)
        getCurrentMaxAddress(calc.minAddressAsLong, calc.maxAddressAsLong)
    }
    val address: Long = calc.nextAvailableAsLong(currentMax)
    (gateway, address, netmask)
  }

  def createForAsset(asset: Asset, scope: Option[String]): IpAddresses = inTransaction {
    val assetId = asset.getId
    val cfg = getConfig()(scope)
    val ipAddresses = createWithRetry(10) { attempt =>
      val (gateway, address, netmask) = getNextAddress(attempt)(scope)
      logger.debug("trying to use address %s".format(IpAddress.toString(address)))
      val ipAddresses = IpAddresses(assetId, gateway, address, netmask, scope.getOrElse(""))
      super.create(ipAddresses)
    }
    Solr.updateAsset(asset)
    ipAddresses
  }

  def deleteByAssetAndPool(asset: Asset, pool: Option[String]): Int = inTransaction {
    val rows = tableDef.where(i =>
      i.asset_id === asset.id and
      i.pool === pool.?
    ).toList
    val res = rows.foldLeft(0) { case(sum, ipInfo) =>
      sum + delete(ipInfo)
    }
    Solr.updateAsset(asset)
    res
  }

  def findAssetsByAddress(page: PageParams, addys: Seq[String], finder: AssetFinder): Page[AssetView] = {
    def whereClause(assetRow: Asset, addressRow: IpAddresses) = {
      where(
        (assetRow.id === addressRow.asset_id) and
        generateFindQuery(addressRow, addys.head) and
        finder.asLogicalBoolean(assetRow)
      )
    }
    inTransaction { log {
      val results = from(Asset.tableDef, tableDef)((assetRow, addressRow) =>
        whereClause(assetRow, addressRow)
        select(assetRow)
      ).page(page.offset, page.size).toList
      val totalCount = from(Asset.tableDef, tableDef)((assetRow, addressRow) =>
        whereClause(assetRow, addressRow)
{'fraction_non_alphanumeric': 0.11609033168666197, 'fraction_numerical': 0.0018819101387908728, 'mean_word_length': 4.088569718731299, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, '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': '7540415', 'n_tokens_mistral': 2719, 'n_tokens_neox': 2673, 'n_words': 584}