Browse Source

fixed db update to be compliant with the requirements

weltermann17 11 years ago
parent
commit
dbe8045565
2 changed files with 19 additions and 13 deletions
  1. 1 1
      plain/sbt
  2. 18 12
      plain/src/main/scala/com/ibm/techempower/Db.scala

+ 1 - 1
plain/sbt

@@ -1 +1 @@
-java -Xmx3g -Xms2g -Xmn1g -Xss8M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=384M -Dsbt.global.base=project/.sbtboot -Dsbt.boot.directory=project/.boot -Dsbt.ivy.home=project/.ivy -jar `dirname $0`/sbt-launch.jar "$@"
+java -server -Xnoclassgc -XX:MaxPermSize=1g -XX:ReservedCodeCacheSize=384m -Xmx8g -Xss8m -Xmn4g -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=384M -Dsbt.global.base=project/.sbtboot -Dsbt.boot.directory=project/.boot -Dsbt.ivy.home=project/.ivy -jar `dirname $0`/sbt-launch.jar "$@"

+ 18 - 12
plain/src/main/scala/com/ibm/techempower/Db.scala

@@ -4,6 +4,7 @@ import java.util.concurrent.ThreadLocalRandom.{ current => random }
 
 import scala.language.implicitConversions
 import scala.language.postfixOps
+import scala.collection.mutable.MutableList
 
 import com.ibm.plain.rest.{ Form, Resource }
 import com.ibm.plain.json.{ Json => J }
@@ -33,27 +34,28 @@ sealed abstract class DbResource
     extends Resource {
 
   @inline protected[this] final def get(form: Option[Form]): J = {
-    var list: List[J] = Nil
+    val output = new MutableList[J]
     val q = form match { case None => 1 case Some(f) => queries(f) }
-    withConnection(datasource) {
-      implicit connection => for (i <- 1 to q) { for (j <- selectsql << next <<! asJson) { list = j :: list } }
+    withConnection(datasource) { implicit connection =>
+      for (i <- 1 to q) { for (j <- selectsql << next <<! asJson) { output += j } }
     }
-    form match { case None => list.head case _ => J(list) }
+    form match { case None => output.head case _ => J(output.toList) }
   }
 
   @inline protected[this] final def update(form: Form): J = {
-    var list: List[J] = Nil
+    val input = new MutableList[World]
+    val output = new MutableList[J]
     val q = queries(form)
-    withConnection(datasource) {
-      implicit connection =>
-        for (i <- 1 to q) {
-          val id = next
+    withConnection(datasource) { implicit connection =>
+      for (i <- 1 to q) { for (j <- selectsql << next <<! asTuple) { input += j } }
+      input.foreach {
+        case (id, _) =>
           val randomNumber = next
           updatesql << randomNumber << id <<!!;
-          list = asJson(id, randomNumber) :: list
-        }
+          output += asJson(id, randomNumber)
+      }
     }
-    J(list)
+    J(output.toList)
   }
 
   @inline private[this] final def queries(form: Form) = try {
@@ -70,8 +72,12 @@ sealed abstract class DbResource
 
   @inline private[this] final def asJson(id: Int, randomNumber: Int) = J(Map("id" -> id, "randomNumber" -> randomNumber))
 
+  @inline private[this] final def asTuple = (r: RichResultSet) => (r.nextInt.get, r.nextInt.get)
+
   @inline private[this] final def next = random.nextInt(1, 10001)
 
+  private[this] final type World = (Int, Int)
+
   private[this] final val selectsql = "select id, randomNumber from World where id = ?"
 
   private[this] final val updatesql = "update World set randomNumber = ? where id = ?"