<commit_before>package org.jetbrains.jps.incremental.scala
package local

import data._
import java.io.File
import sbt.inc.{AnalysisFormats, FileBasedStore, AnalysisStore}
import org.jetbrains.jps.incremental.ModuleLevelBuilder.ExitCode

/**
 * @author <NAME>
 */
class LocalServer extends Server {
  private var cachedCompilerFactory: Option[CompilerFactory] = None

  def compile(sbtData: SbtData, compilerData: CompilerData, compilationData: CompilationData, client: Client): ExitCode = {
    val compilerFactory = compilerFactoryFrom(sbtData)

    client.progress("Instantiating compiler...")
    val compiler = compilerFactory.createCompiler(compilerData, client, LocalServer.createAnalysisStore)

    if (!client.isCanceled) {
      client.progress("Searching for changed files...")
      compiler.compile(compilationData, client)
    }

    ExitCode.OK
  }

  private def compilerFactoryFrom(sbtData: SbtData): CompilerFactory = cachedCompilerFactory.getOrElse {
    val factory = new CachingFactory(new CompilerFactoryImpl(sbtData), 5, 5)
    cachedCompilerFactory = Some(factory)
    factory
  }
}

object LocalServer {
  private def createAnalysisStore(cacheFile: File): AnalysisStore = {
    import sbinary.DefaultProtocol.{immutableMapFormat, immutableSetFormat, StringFormat, tuple2Format}
    import sbt.inc.AnalysisFormats._
    val store = FileBasedStore(cacheFile)(AnalysisFormats.analysisFormat, AnalysisFormats.setupFormat)
    AnalysisStore.sync(AnalysisStore.cached(store))
  }
}<commit_msg>LocalServer: wait for shared compilers<commit_after>package org.jetbrains.jps.incremental.scala
package local

import data._
import java.io.File
import sbt.inc.{AnalysisFormats, FileBasedStore, AnalysisStore}
import org.jetbrains.jps.incremental.ModuleLevelBuilder.ExitCode

/**
 * @author <NAME>
 */
class LocalServer extends Server {
  private var cachedCompilerFactory: Option[CompilerFactory] = None
  private val lock = new Object()

  def compile(sbtData: SbtData, compilerData: CompilerData, compilationData: CompilationData, client: Client): ExitCode = {
    val compiler = lock.synchronized {
      val compilerFactory = compilerFactoryFrom(sbtData)

      client.progress("Instantiating compiler...")
      compilerFactory.createCompiler(compilerData, client, LocalServer.createAnalysisStore)
    }

    if (!client.isCanceled) {
      client.progress("Waiting for a free compiler...")

      compiler.synchronized {
        client.progress("Searching for changed files...")
        compiler.compile(compilationData, client)
      }
    }

    ExitCode.OK
  }

  private def compilerFactoryFrom(sbtData: SbtData): CompilerFactory = cachedCompilerFactory.getOrElse {
    val factory = new CachingFactory(new CompilerFactoryImpl(sbtData), 5, 5)
    cachedCompilerFactory = Some(factory)
    factory
  }
}

object LocalServer {
  private def createAnalysisStore(cacheFile: File): AnalysisStore = {
    import sbinary.DefaultProtocol.{immutableMapFormat, immutableSetFormat, StringFormat, tuple2Format}
    import sbt.inc.AnalysisFormats._
    val store = FileBasedStore(cacheFile)(AnalysisFormats.analysisFormat, AnalysisFormats.setupFormat)
    AnalysisStore.sync(AnalysisStore.cached(store))
  }
}
{'fraction_non_alphanumeric': 0.09073958980733375, 'fraction_numerical': 0.0018645121193287756, 'mean_word_length': 4.906422018348624, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1465847', 'n_tokens_mistral': 920, 'n_tokens_neox': 889, 'n_words': 194}