Browse Source

Changed the model to anorm and fixed the db action

The model now uses anorm, and the db returns proper json an uses futures
Skamander 12 years ago
parent
commit
4a5b5d5694

+ 4 - 1
play-scala/.gitignore

@@ -1,7 +1,10 @@
 logs
 project/project
 project/target
+public
 target
+test
 tmp
 .history
-dist
+dist
+conf/evolutions

+ 2 - 2
play-scala/README.md

@@ -4,12 +4,12 @@ This is the Play portion of a [benchmarking test suite](../) comparing a variety
 
 ### JSON Encoding Test
 
-* [JSON test source](app/controllers/Application.java)
+* [JSON test source](app/controllers/Application.scala)
 
 ### Data-Store/Database Mapping Test
 
 * [Database test controller](app/controllers/Application.scala)
-* [Database test model](app/models/World.java)
+* [Database test model](app/models/World.scala)
 
 ## Infrastructure Software Versions
 The tests were run with:

+ 17 - 23
play-scala/app/controllers/Application.scala

@@ -1,40 +1,34 @@
 package controllers
 
-import play._
-import play.api.libs.concurrent._
+import play.api._
 import play.api.mvc._
-import play.libs.Json
-import org.codehaus.jackson.node.ObjectNode
-import views.html._
-import models._
-import java.util._
+import play.api.libs.json.Json
+import play.api.libs.concurrent._
 import java.util.concurrent.ThreadLocalRandom
 import scala.concurrent._
+import models._
 
 object Application extends Controller {
+  
   private val TEST_DATABASE_ROWS = 10000
 
   def json() = Action {
-    val result = Json.newObject()
-    result.put("message", "Hello World!")
-    Ok(result.toString)
+    Ok(Json.obj("message" -> "Hello World!"))   
   }
 
   def db(queries: Int) = Action {
     import play.api.libs.concurrent.Execution.Implicits._
 
-    val random = ThreadLocalRandom.current()
-
     Async {
-      Future {
-        (1 to queries) map {
-          _ =>
-            World.find.byId(random.nextInt(TEST_DATABASE_ROWS) + 1)
-        }
-      } map {
-        worlds =>
-          Ok(Json.toJson(worlds).toString())
-      }
+      val random = ThreadLocalRandom.current()
+
+      val worlds = Future.sequence( for {
+        _ <- (1 to queries)
+      } yield Future(World.findById(random.nextInt(TEST_DATABASE_ROWS) + 1)))
+
+      worlds map {
+        w => Ok(Json.toJson(w))  
+      } 
     }
-  }
-}
+  }  
+}

+ 0 - 24
play-scala/app/models/World.java

@@ -1,24 +0,0 @@
-package models;
-
-import java.util.*;
-import javax.persistence.*;
-
-import play.db.ebean.*;
-import play.db.ebean.Model.Finder;
-import play.data.format.*;
-import play.data.validation.*;
-
-@Entity
-public class World extends Model {
-
-  @Id
-  public Long id;
-
-  @Column(name = "randomNumber")
-  public Long randomNumber;
-
-  public static Finder<Long,World> find = new Finder<Long,World>(
-    Long.class, World.class
-  );
-
-}

+ 45 - 0
play-scala/app/models/World.scala

@@ -0,0 +1,45 @@
+package models
+
+import play.api.db._
+import play.api.Play.current
+import anorm._
+import anorm.SqlParser._
+import play.api.libs.json._
+import play.api.libs.functional.syntax._
+
+case class World(id: Pk[Long], randomNumber: Long)
+
+object World {
+    /**
+    * Convert a World to Json object
+    */
+    implicit val toJson = new Writes[World] {
+        def writes(w: World): JsValue = {
+            Json.obj(
+                "id" -> w.id.get,
+                "randomNumber" -> w.randomNumber
+            )
+        }
+    }
+
+  /**
+   * Parse a World from a ResultSet
+   */
+    val simpleRowParser = {
+        get[Pk[Long]]("world.id") ~
+        get[Long]("world.randomNumber") map {
+            case id~randomNumber => World(id, randomNumber)
+        }
+    }
+
+  /**
+   * Retrieve a World by id.
+   */
+    def findById(id: Long): World = {
+        DB.withConnection { implicit connection =>
+            SQL("SELECT * FROM world WHERE id = {id}").on(
+                "id" -> id
+            ).as(World.simpleRowParser.single)
+        }
+    }
+}

+ 4 - 12
play-scala/conf/application.conf

@@ -22,9 +22,9 @@ application.langs="en"
 # You can declare as many datasources as you want.
 # By convention, the default datasource is named `default`
 #
-# db.default.driver=org.h2.Driver
-# db.default.url="jdbc:h2:mem:play"
-# db.default.user=sa
+#db.default.driver=org.h2.Driver
+#db.default.url="jdbc:h2:mem:play"
+#db.default.user=sa
 # db.default.password=
 #
 # You can expose this datasource via JNDI if needed (Useful for JPA)
@@ -52,13 +52,6 @@ db.default.minConnectionsPerPartition=5
 # You can disable evolutions if needed
 # evolutionplugin=disabled
 
-# Ebean configuration
-# ~~~~~
-# You can declare as many Ebean servers as you want.
-# By convention, the default server is named `default`
-#
-ebean.default="models.*"
-
 # Logger
 # ~~~~~
 # You can also configure logback (http://logback.qos.ch/), by providing a logger.xml file in the conf directory .
@@ -70,5 +63,4 @@ logger.root=ERROR
 logger.play=ERROR
 
 # Logger provided to your application:
-logger.application=ERROR
-
+logger.application=ERROR

+ 1 - 1
play-scala/conf/routes

@@ -7,4 +7,4 @@ GET     /json                           controllers.Application.json
 GET     /db                             controllers.Application.db(queries: Int ?= 1)
 
 # Map static resources from the /public folder to the /assets URL path
-GET     /assets/*file               controllers.Assets.at(path="/public", file)
+GET     /assets/*file                   controllers.Assets.at(path="/public", file)

+ 9 - 13
play-scala/project/Build.scala

@@ -4,20 +4,16 @@ import PlayProject._
 
 object ApplicationBuild extends Build {
 
-    val appName         = "play-scala"
-    val appVersion      = "1.0-SNAPSHOT"
+  val appName         = "play-scala"
+  val appVersion      = "1.0-SNAPSHOT"
 
-    val appDependencies = Seq(
-      // Add your project dependencies here,
-      javaCore,
-      javaJdbc,
-      javaEbean,
-      "mysql" % "mysql-connector-java" % "5.1.22"
+  val appDependencies = Seq(
+    jdbc,
+    anorm,
+    "mysql" % "mysql-connector-java" % "5.1.22"
+  )
 
-    )
-
-    val main = play.Project(appName, appVersion, appDependencies).settings(
-      // Add your own project settings here
-    )
+  val main = play.Project(appName, appVersion, appDependencies).settings(
+  )
 
 }