<commit_before>import java.sql.Connection
import java.sql.DriverManager
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import scala.concurrent.ExecutionContext.Implicits.global
import scala.io.Source
import scala.util.Try

class AdderServlet extends HttpServlet {

  private val c: Connection = {
    Class.forName(sys.env("DB_DRIVER"))
    val c: Connection =
      DriverManager.getConnection( sys.env("DB_URL")
                                 , sys.env("DB_USER")
                                 , sys.env("DB_PASS")
                                 )
    c.setAutoCommit(false)
    c
  }

  private val updater: Thread =
    new Thread {
      override def run(): Unit = {
        while (!isInterrupted) {
          Try(Service.unsafeRunSync(Adder.update, c, 10000))
          Thread.sleep(250)
        }
      }
    }

  override def init(): Unit = {
    Service.unsafeRunSync(Adder.init, c, 1000)
    updater.start
  }

  override def destroy(): Unit = {
    updater.interrupt
  }

  override def doGet( req: HttpServletRequest
                    , res: HttpServletResponse
                    ) {

    res.setContentType("text/plain")
    res.setCharacterEncoding("UTF-8")

    Service.unsafeRunSync(Adder.getSum, c, 1000) match {
      case Right((_, result)) =>
        res.getWriter.write(s"${result}\n")
      case Left(message) =>
        res.setStatus(500)
        res.getWriter.write(message)
    }
  }

  override def doPost( req: HttpServletRequest
                     , res: HttpServletResponse
                     ) {

    res.setStatus(201)
    res.setHeader("Location", "/")

    val reqBody: String =
      Source.fromInputStream(req.getInputStream).mkString
    val s: Service[Unit] = Adder.add(reqBody.toInt)
    Service.unsafeRunSync(s, c, 1000)
  }
}
<commit_msg>Decouple command and query servlets<commit_after>import java.sql.Connection
import java.sql.DriverManager
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import scala.concurrent.ExecutionContext.Implicits.global
import scala.io.Source
import scala.util.Try

trait CommandServlet extends HttpServlet {

  def c: Connection

  override def doPost( req: HttpServletRequest
                     , res: HttpServletResponse
                     ) {

    res.setStatus(201)
    res.setHeader("Location", "/")

    val reqBody: String =
      Source.fromInputStream(req.getInputStream).mkString
    val s: Service[Unit] = Adder.add(reqBody.toInt)
    Service.unsafeRunSync(s, c, 1000)
  }
}

trait QueryServlet extends HttpServlet {

  def c: Connection

  private val updater: Thread =
    new Thread {
      override def run(): Unit = {
        while (!isInterrupted) {
          Try(Service.unsafeRunSync(Adder.update, c, 10000))
          Thread.sleep(250)
        }
      }
    }

  override def init(): Unit = {
    updater.start
    super.init()
  }

  override def destroy(): Unit = {
    updater.interrupt
    super.destroy()
  }

  override def doGet( req: HttpServletRequest
                    , res: HttpServletResponse
                    ) {

    res.setContentType("text/plain")
    res.setCharacterEncoding("UTF-8")

    Service.unsafeRunSync(Adder.getSum, c, 1000) match {
      case Right((_, result)) =>
        res.getWriter.write(s"${result}\n")
      case Left(message) =>
        res.setStatus(500)
        res.getWriter.write(message)
    }
  }
}

class AdderServlet extends CommandServlet with QueryServlet {

  val c: Connection = {
    Class.forName(sys.env("DB_DRIVER"))
    val c: Connection =
      DriverManager.getConnection( sys.env("DB_URL")
                                 , sys.env("DB_USER")
                                 , sys.env("DB_PASS")
                                 )
    c.setAutoCommit(false)
    c
  }

  override def init(): Unit = {
    Service.unsafeRunSync(Adder.init, c, 1000)
    super.init()
  }

  override def destroy(): Unit = {
    super.destroy()
  }
}

{'fraction_non_alphanumeric': 0.1071779744346116, 'fraction_numerical': 0.01327433628318584, 'mean_word_length': 2.2814516129032256, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6712688', 'n_tokens_mistral': 1254, 'n_tokens_neox': 1185, 'n_words': 239}