<commit_before>/*
 * Copyright (C) 2010 <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.openmole.core.batch.replication

import com.db4o.ObjectContainer
import com.db4o.ObjectServer
import com.db4o.ObjectSet
import com.db4o.config.ClientServerConfiguration
import com.db4o.cs.Db4oClientServer
import com.db4o.defragment.Defragment
import com.db4o.defragment.DefragmentConfig
import com.db4o.ta.TransparentPersistenceSupport
import com.google.common.cache.CacheBuilder
import java.io.File
import org.openmole.misc.replication.DBServerInfo
import org.openmole.misc.replication.Replica
import org.openmole.misc.tools.io.FileUtil._
import org.openmole.misc.tools.service.LockRepository
import org.openmole.misc.tools.service.Logger
import java.util.regex.Pattern
import org.openmole.core.batch.control._
import org.openmole.core.batch.environment._
import org.openmole.core.batch.storage._
import org.openmole.core.batch.environment.BatchEnvironment._
import org.openmole.misc.tools.service.TimeCache
import org.openmole.misc.updater.Updater
import org.openmole.misc.workspace.ConfigurationLocation
import org.openmole.misc.exception.InternalProcessingError
import org.openmole.misc.hashservice.HashService._
import org.openmole.misc.workspace.Workspace
import scala.collection.JavaConversions._
import scala.collection.immutable.TreeMap
import scala.collection.immutable.TreeSet
import scala.collection.mutable.HashMap
import scala.collection.mutable.HashSet
import scala.collection.mutable.ListBuffer
import java.net.URI
import java.util.concurrent.Callable
import java.util.concurrent.TimeUnit

object ReplicaCatalog extends Logger {

  val NoAccessCleanTime = new ConfigurationLocation("ReplicaCatalog", "NoAccessCleanTime")
  val InCatalogCacheTime = new ConfigurationLocation("ReplicaCatalog", "InCatalogCacheTime")
  val ReplicaCacheTime = new ConfigurationLocation("ReplicaCatalog", "ReplicaCacheTime")

  Workspace += (NoAccessCleanTime, "P30D")
  Workspace += (InCatalogCacheTime, "PT2M")
  Workspace += (ReplicaCacheTime, "PT30M")

  lazy val replicationPattern = Pattern.compile("(\\p{XDigit}*)_.*")
  lazy val inCatalogCache = new TimeCache[Map[String, Set[String]]]

  type ReplicaCacheKey = (String, String, String, String)
  val replicaCache = CacheBuilder.newBuilder.asInstanceOf[CacheBuilder[ReplicaCacheKey, Replica]].
    expireAfterWrite(Workspace.preferenceAsDuration(ReplicaCacheTime).toSeconds, TimeUnit.SECONDS).build[ReplicaCacheKey, Replica]

  def openClient = {
    val dbInfoFile = DBServerInfo.dbInfoFile(DBServerInfo.base)
    val info = DBServerInfo.load(dbInfoFile)

    val configuration = Db4oClientServer.newClientConfiguration
    configuration.common.add(new TransparentPersistenceSupport)
    configuration.common.objectClass(classOf[Replica]).cascadeOnDelete(true)
    configuration.prefetchObjectCount(1000)
    configuration.common.bTreeNodeSize(256)
    configuration.common.objectClass(classOf[Replica]).objectField("_hash").indexed(true)
    configuration.common.objectClass(classOf[Replica]).objectField("_storage").indexed(true)
    configuration.common.objectClass(classOf[Replica]).objectField("_path").indexed(true)
    configuration.common.objectClass(classOf[Replica]).objectField("_hash").indexed(true)
    configuration.common.objectClass(classOf[Replica]).objectField("_environment").indexed(true)

    Db4oClientServer.openClient(configuration, "localhost", info.port, info.user, info.password)
  }

  def withClient[T](f: ObjectContainer ⇒ T) = {
    val client = openClient
    try f(client)
    finally client.close
  }

  /*private var _dbInfo: Option[(DBServerInfo, Long)] = None

   def dbInfo = synchronized {
   val dbInfoFile = DBServerInfo.dbInfoFile(DBServerInfo.base)

   if (!dbInfoFile.exists) throw new InternalProcessingError("Database server not launched, file " + dbInfoFile + " doesn't exists.")<commit_msg>Set timeout for db4o client socket.<commit_after>/*
 * Copyright (C) 2010 <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.openmole.core.batch.replication

import com.db4o.ObjectContainer
import com.db4o.ObjectServer
import com.db4o.ObjectSet
import com.db4o.config.ClientServerConfiguration
import com.db4o.cs.Db4oClientServer
import com.db4o.defragment.Defragment
import com.db4o.defragment.DefragmentConfig
import com.db4o.ta.TransparentPersistenceSupport
import com.google.common.cache.CacheBuilder
import java.io.File
import org.openmole.misc.replication.DBServerInfo
import org.openmole.misc.replication.Replica
import org.openmole.misc.tools.io.FileUtil._
import org.openmole.misc.tools.service.LockRepository
import org.openmole.misc.tools.service.Logger
import java.util.regex.Pattern
import org.openmole.core.batch.control._
import org.openmole.core.batch.environment._
import org.openmole.core.batch.storage._
import org.openmole.core.batch.environment.BatchEnvironment._
import org.openmole.misc.tools.service.TimeCache
import org.openmole.misc.updater.Updater
import org.openmole.misc.workspace.ConfigurationLocation
import org.openmole.misc.exception.InternalProcessingError
import org.openmole.misc.hashservice.HashService._
import org.openmole.misc.workspace.Workspace
import scala.collection.JavaConversions._
import scala.collection.immutable.TreeMap
import scala.collection.immutable.TreeSet
import scala.collection.mutable.HashMap
import scala.collection.mutable.HashSet
import scala.collection.mutable.ListBuffer
import java.net.URI
import java.util.concurrent.Callable
import java.util.concurrent.TimeUnit

object ReplicaCatalog extends Logger {

  val NoAccessCleanTime = new ConfigurationLocation("ReplicaCatalog", "NoAccessCleanTime")
  val InCatalogCacheTime = new ConfigurationLocation("ReplicaCatalog", "InCatalogCacheTime")
  val ReplicaCacheTime = new ConfigurationLocation("ReplicaCatalog", "ReplicaCacheTime")
  val SocketTimeout = new ConfigurationLocation("ReplicaCatalog", "SocketTimeout")

  Workspace += (NoAccessCleanTime, "P30D")
  Workspace += (InCatalogCacheTime, "PT2M")
  Workspace += (ReplicaCacheTime, "PT30M")
  Workspace += (SocketTimeout, "PT2M")

  lazy val replicationPattern = Pattern.compile("(\\p{XDigit}*)_.*")
  lazy val inCatalogCache = new TimeCache[Map[String, Set[String]]]

  type ReplicaCacheKey = (String, String, String, String)
  val replicaCache = CacheBuilder.newBuilder.asInstanceOf[CacheBuilder[ReplicaCacheKey, Replica]].
    expireAfterWrite(Workspace.preferenceAsDuration(ReplicaCacheTime).toSeconds, TimeUnit.SECONDS).build[ReplicaCacheKey, Replica]

  def openClient = {
    val dbInfoFile = DBServerInfo.dbInfoFile(DBServerInfo.base)
    val info = DBServerInfo.load(dbInfoFile)

    val configuration = Db4oClientServer.newClientConfiguration
    configuration.common.add(new TransparentPersistenceSupport)
    configuration.common.objectClass(classOf[Replica]).cascadeOnDelete(true)
    configuration.prefetchObjectCount(1000)
    configuration.common.bTreeNodeSize(256)
    configuration.common.objectClass(classOf[Replica]).objectField("_hash").indexed(true)
    configuration.common.objectClass(classOf[Replica]).objectField("_storage").indexed(true)
    configuration.common.objectClass(classOf[Replica]).objectField("_path").indexed(true)
    configuration.common.objectClass(classOf[Replica]).objectField("_hash").indexed(true)
    configuration.common.objectClass(classOf[Replica]).objectField("_environment").indexed(true)
    configuration.timeoutClientSocket(Workspace.preferenceAsDuration(SocketTimeout).toMilliSeconds.toInt)

    Db4oClientServer.openClient(configuration, "localhost", info.port, info.user, info.password)
  }

  def withClient[T](f: ObjectContainer ⇒ T) = {
    val client = openClient
    try f(client)
    finally client.close
  }

  /*private var _dbInfo: Option[(DBServerInfo, Long)] = None

   def dbInfo = synchronized {
   val dbInfoFile = DBServerInfo.dbInfoFile(DBServerInfo.base)

   if (!dbInfoFile.exists) throw new InternalProcessingError("Database server not launched, file " + dbInfoFile + " doesn't exists.")
{'fraction_non_alphanumeric': 0.09612903225806452, 'fraction_numerical': 0.006236559139784947, 'mean_word_length': 5.779154518950437, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1225906', 'n_tokens_mistral': 2745, 'n_tokens_neox': 2765, 'n_words': 597}