<commit_before>//
// Codex - a multi-language code indexer and grokker
// http://github.com/samskivert/codex

package codex.data

import java.io.File
import org.squeryl.PrimitiveTypeMode._
import org.squeryl.{KeyedEntity, Schema}
import samscala.nexus.{Entity, Handle, entity}
import scala.collection.mutable.{Map => MMap}

import codex._
import codex.extract.ProjectModel

/** Provides project-related services. */
class Projects extends Entity {
  import ProjectsDB._
  import ProjectsUtil._

  /** Returns (creating if necessary) the project that contains the supplied comp unit. */
  def forPath (path :String) :Option[Handle[Project]] = _byPath collectFirst {
    case (root, p) if (path startsWith(root + File.separator)) => p
  } orElse (findRoot(new File(path)) flatMap ProjectModel.forRoot map createProject)

  /** Returns (creating if necessary) the project at the specified root. */
  def forRoot (root :File) :Option[Handle[Project]] =
    (_byPath get root.getPath) orElse (ProjectModel.forRoot(root) map createProject)

  /** Returns (creating if necessary) the project for the specified dependency. */
  def forDep (depend :Depend) :Option[Handle[Project]] =
    (_byFqId get depend.toFqId) orElse (ProjectModel.forDepend(depend) map createProject)

  /** Returns the project for the supplied fqId or `None` if no such project is known. */
  def forId (fqId :FqId) :Option[Handle[Project]] = _byFqId get fqId

  /** Returns (fqId, rootPath) for all known projects. */
  def ids :Iterable[(FqId,String)] = using(_session) {
    projects map(p => (p.fqId, p.rootPath))
  }

  /** Deletes the project with the specified `fqId`. */
  def delete (fqId :FqId) {
    _byFqId remove(fqId) match {
      case None => log.warning("Can't delete unknown project", "fqId", fqId)
      case Some(ph) =>
        val (id, path) = ph request(p => (p.id, p.rootPath))
        _byPath -= path
        using(_session) { projects delete(id) }
        ph invoke(_.delete())
    }
  }

  // a backdoor to give projects easy access to their entity handle
  private[data] def handle (fqId :FqId) = _byFqId(fqId)

  private def createProject (pm :ProjectModel) = using(_session) {
    // if we already have a project with this fqId, we either need to usurp it, or take a back seat
    // to it; in the former case, we mutate the existing project's version and take the "stock"
    // version for ourselves, in the latter case we just register ourselves with a mutated version
    val stockFqId = FqId(pm.groupId, pm.artifactId, pm.version)
    val fqId = _byFqId get(stockFqId) match {
      case None => stockFqId
      case Some(ph) =>
        // if we're local and the other project is remote; delete the other project
        val (otherRemote, otherRoot) = ph request(p => (p.isRemote, p.rootPath))
        if (otherRemote && !pm.isRemote) {
          log.info(s"Usurping remote $stockFqId with local", "usurper", pm.root)
          delete(stockFqId)
          stockFqId
        }
        // else we're both local, so tack a conflict resolving suffix onto our version<commit_msg>Be sure to use project with longest matching prefix.<commit_after>//
// Codex - a multi-language code indexer and grokker
// http://github.com/samskivert/codex

package codex.data

import java.io.File
import org.squeryl.PrimitiveTypeMode._
import org.squeryl.{KeyedEntity, Schema}
import samscala.nexus.{Entity, Handle, entity}
import scala.collection.mutable.{Map => MMap}

import codex._
import codex.extract.ProjectModel

/** Provides project-related services. */
class Projects extends Entity {
  import ProjectsDB._
  import ProjectsUtil._

  /** Returns (creating if necessary) the project that contains the supplied comp unit. */
  def forPath (path :String) :Option[Handle[Project]] = bestPath(path) orElse (
    findRoot(new File(path)) flatMap ProjectModel.forRoot map createProject)

  /** Returns (creating if necessary) the project at the specified root. */
  def forRoot (root :File) :Option[Handle[Project]] =
    (_byPath get root.getPath) orElse (ProjectModel.forRoot(root) map createProject)

  /** Returns (creating if necessary) the project for the specified dependency. */
  def forDep (depend :Depend) :Option[Handle[Project]] =
    (_byFqId get depend.toFqId) orElse (ProjectModel.forDepend(depend) map createProject)

  /** Returns the project for the supplied fqId or `None` if no such project is known. */
  def forId (fqId :FqId) :Option[Handle[Project]] = _byFqId get fqId

  /** Returns (fqId, rootPath) for all known projects. */
  def ids :Iterable[(FqId,String)] = using(_session) {
    projects map(p => (p.fqId, p.rootPath))
  }

  /** Deletes the project with the specified `fqId`. */
  def delete (fqId :FqId) {
    _byFqId remove(fqId) match {
      case None => log.warning("Can't delete unknown project", "fqId", fqId)
      case Some(ph) =>
        val (id, path) = ph request(p => (p.id, p.rootPath))
        _byPath -= path
        using(_session) { projects delete(id) }
        ph invoke(_.delete())
    }
  }

  // a backdoor to give projects easy access to their entity handle
  private[data] def handle (fqId :FqId) = _byFqId(fqId)

  private def bestPath (path :String) = (("", None :Option[Handle[Project]]) /: _byPath) {
    case ((broot, bp), (root, p)) =>
      if (path.startsWith(root + File.separator) && (root.length > broot.length)) (root, Some(p))
      else (broot, bp)
  } _2

  private def createProject (pm :ProjectModel) = using(_session) {
    // if we already have a project with this fqId, we either need to usurp it, or take a back seat
    // to it; in the former case, we mutate the existing project's version and take the "stock"
    // version for ourselves, in the latter case we just register ourselves with a mutated version
    val stockFqId = FqId(pm.groupId, pm.artifactId, pm.version)
    val fqId = _byFqId get(stockFqId) match {
      case None => stockFqId
      case Some(ph) =>
        // if we're local and the other project is remote; delete the other project
        val (otherRemote, otherRoot) = ph request(p => (p.isRemote, p.rootPath))
        if (otherRemote && !pm.isRemote) {
          log.info(s"Usurping remote $stockFqId with local", "usurper", pm.root)
          delete(stockFqId)
          stockFqId
        }
        // else we're both local, so tack a conflict resolving suffix onto our version
{'fraction_non_alphanumeric': 0.11261190513585675, 'fraction_numerical': 0.00015705983979896342, 'mean_word_length': 3.40387275242047, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, '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': '1676868', 'n_tokens_mistral': 2022, 'n_tokens_neox': 1972, 'n_words': 678}