Browse Source

MongoDB: merged all code into single dropwizard project. Added MongoConfiguration, use findAndModify() for update tests, limit returned fields to exclude duplicate "id" in database.
Models: removed unused hibernate auto id generation annotation, added JsonProperty annotations
Resources: removed parallelism TODO since most frameworks are using similar looping/async logic.

Yun Zhi Lin 10 years ago
parent
commit
6b6f375d4f
32 changed files with 195 additions and 588 deletions
  1. 0 25
      frameworks/Java/dropwizard-mongodb/benchmark_config.json
  2. 0 18
      frameworks/Java/dropwizard-mongodb/hello-world.yml
  3. 0 3
      frameworks/Java/dropwizard-mongodb/install.sh
  4. 0 95
      frameworks/Java/dropwizard-mongodb/pom.xml
  5. 0 8
      frameworks/Java/dropwizard-mongodb/setup.sh
  6. 0 23
      frameworks/Java/dropwizard-mongodb/source_code
  7. 0 48
      frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/HelloWorldService.java
  8. 0 12
      frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/config/HelloWorldConfiguration.java
  9. 0 20
      frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/db/FortuneDAO.java
  10. 0 20
      frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/db/MongoHealthCheck.java
  11. 0 23
      frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/db/WorldDAO.java
  12. 0 35
      frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/db/model/Fortune.java
  13. 0 41
      frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/db/model/World.java
  14. 0 45
      frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/resources/Helper.java
  15. 0 19
      frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/resources/JsonResource.java
  16. 0 26
      frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/resources/TextResource.java
  17. 0 55
      frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/resources/WorldResource.java
  18. 0 17
      frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/resources/api/HelloMessage.java
  19. 0 23
      frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/resources/views/FortuneView.java
  20. 24 3
      frameworks/Java/dropwizard/benchmark_config.json
  21. 6 1
      frameworks/Java/dropwizard/hello-world.yml
  22. 5 0
      frameworks/Java/dropwizard/pom.xml
  23. 10 3
      frameworks/Java/dropwizard/source_code
  24. 37 6
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/HelloWorldService.java
  25. 9 0
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/config/HelloWorldConfiguration.java
  26. 15 3
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/config/MongoConfiguration.java
  27. 0 0
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/MongoManaged.java
  28. 4 1
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/model/Fortune.java
  29. 10 3
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/model/World.java
  30. 9 8
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/MongoFortuneResource.java
  31. 65 0
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/MongoWorldResource.java
  32. 1 4
      frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/WorldResource.java

+ 0 - 25
frameworks/Java/dropwizard-mongodb/benchmark_config.json

@@ -1,25 +0,0 @@
-{
-  "framework": "dropwizard-mongodb",
-  "tests": [{
-    "default": {
-      "setup_file": "setup",
-      "json_url": "/json",
-      "db_url": "/db",
-      "query_url": "/db?queries=",
-      "port": 9000,
-      "approach": "Realistic",
-      "classification": "Fullstack",
-      "database": "MongoDB",
-      "framework": "dropwizard",
-      "language": "Java",
-      "orm": "Full",
-      "platform": "Jetty",
-      "webserver": "Jetty",
-      "os": "Linux",
-      "database_os": "Linux",
-      "display_name": "dropwizard-mongodb",
-      "notes": "mongodb implementation of dropwizard example",
-      "versus": ""
-    }
-  }]
-}

+ 0 - 18
frameworks/Java/dropwizard-mongodb/hello-world.yml

@@ -1,18 +0,0 @@
-server:
-  type: simple
-  applicationContextPath: /
-  connector:
-    type: http
-    port: 9090
-    useServerHeader: true
-
-  requestLog:
-    appenders: []
-
-logging:
-  appenders: []
-
-mongo:
-  host: 127.0.0.1
-  port: 27017
-  db: hello_world

+ 0 - 3
frameworks/Java/dropwizard-mongodb/install.sh

@@ -1,3 +0,0 @@
-#!/bin/bash
-
-fw_depends java7 maven

+ 0 - 95
frameworks/Java/dropwizard-mongodb/pom.xml

@@ -1,95 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0"
-         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-    <modelVersion>4.0.0</modelVersion>
-
-    <groupId>com.danielt</groupId>
-    <artifactId>dropwizard-mongodb</artifactId>
-    <version>0.0.1-SNAPSHOT</version>
-
-    <properties>
-        <jdk.version>1.7</jdk.version>
-
-        <dropwizard.version>0.7.1</dropwizard.version>
-    </properties>
-
-    <dependencies>
-        <dependency>
-            <groupId>io.dropwizard</groupId>
-            <artifactId>dropwizard-core</artifactId>
-            <version>${dropwizard.version}</version>
-        </dependency>
-        <dependency>
-            <groupId>io.dropwizard</groupId>
-            <artifactId>dropwizard-views-mustache</artifactId>
-            <version>${dropwizard.version}</version>
-        </dependency>
-        <dependency>
-			<groupId>org.mongojack</groupId>
-			<artifactId>mongojack</artifactId>
-			<version>2.1.0</version>
-		</dependency>
-
-    </dependencies>
-
-    <build>
-        <plugins>
-            <plugin>
-                <groupId>org.apache.maven.plugins</groupId>
-                <artifactId>maven-compiler-plugin</artifactId>
-                <version>2.3.2</version>
-                <configuration>
-                    <source>1.7</source>
-                    <target>1.7</target>
-                </configuration>
-            </plugin>
-            <plugin>
-                <groupId>org.apache.maven.plugins</groupId>
-                <artifactId>maven-jar-plugin</artifactId>
-                <version>2.3.2</version>
-                <configuration>
-                    <archive>
-                        <manifest>
-                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
-                        </manifest>
-                    </archive>
-                </configuration>
-            </plugin>
-            <plugin>
-                <groupId>org.apache.maven.plugins</groupId>
-                <artifactId>maven-shade-plugin</artifactId>
-                <version>1.6</version>
-                <configuration>
-                    <createDependencyReducedPom>true</createDependencyReducedPom>
-                    <filters>
-                        <filter>
-                            <artifact>*:*</artifact>
-                            <excludes>
-                                <exclude>META-INF/*.SF</exclude>
-                                <exclude>META-INF/*.DSA</exclude>
-                                <exclude>META-INF/*.RSA</exclude>
-                            </excludes>
-                        </filter>
-                    </filters>
-                </configuration>
-                <executions>
-                    <execution>
-                        <phase>package</phase>
-                        <goals>
-                            <goal>shade</goal>
-                        </goals>
-                        <configuration>
-                            <transformers>
-                                <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
-                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
-                                    <mainClass>com.example.helloworld.HelloWorldService</mainClass>
-                                </transformer>
-                            </transformers>
-                        </configuration>
-                    </execution>
-                </executions>
-            </plugin>
-        </plugins>
-    </build>
-</project>

+ 0 - 8
frameworks/Java/dropwizard-mongodb/setup.sh

@@ -1,8 +0,0 @@
-#!/bin/bash
-
-# load java environment variables
-source $IROOT/java7.installed
-
-mvn clean package
-
-java -jar target/dropwizard-mongodb-0.0.1-SNAPSHOT.jar server hello-world.yml &

+ 0 - 23
frameworks/Java/dropwizard-mongodb/source_code

@@ -1,23 +0,0 @@
-./dropwizard-mongodb/src/main/java/com/example/helloworld/
-./dropwizard-mongodb/src/main/java/com/example/helloworld/config/HelloWorldConfiguration.java
-./dropwizard-mongodb/src/main/java/com/example/helloworld/config/MongoConfiguration.java
-./dropwizard-mongodb/src/main/java/com/example/helloworld/db
-./dropwizard-mongodb/src/main/java/com/example/helloworld/db/model
-./dropwizard-mongodb/src/main/java/com/example/helloworld/db/model/Fortune.java
-./dropwizard-mongodb/src/main/java/com/example/helloworld/db/model/World.java
-./dropwizard-mongodb/src/main/java/com/example/helloworld/db/FortuneDAO.java
-./dropwizard-mongodb/src/main/java/com/example/helloworld/db/WorldDAO.java
-./dropwizard-mongodb/src/main/java/com/example/helloworld/db/MongoHealthCheck.java
-./dropwizard-mongodb/src/main/java/com/example/helloworld/db/MongoManaged.java
-./dropwizard-mongodb/src/main/java/com/example/helloworld/resources
-./dropwizard-mongodb/src/main/java/com/example/helloworld/resources/api
-./dropwizard-mongodb/src/main/java/com/example/helloworld/resources/api/HelloMessage
-./dropwizard-mongodb/src/main/java/com/example/helloworld/resources/views
-./dropwizard-mongodb/src/main/java/com/example/helloworld/resources/views/FortuneView
-./dropwizard-mongodb/src/main/java/com/example/helloworld/resources/FortuneResource.java
-./dropwizard-mongodb/src/main/java/com/example/helloworld/resources/JsonResource.java
-./dropwizard-mongodb/src/main/java/com/example/helloworld/resources/TextResource.java
-./dropwizard-mongodb/src/main/java/com/example/helloworld/resources/WorldResource.java
-./dropwizard-mongodb/src/main/java/com/example/helloworld/resources/Helper.java
-./dropwizard-mongodb/src/main/java/com/example/helloworld/HelloWorldService.java
-./dropwizard/src/main/resources/fortunes.mustache

+ 0 - 48
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/HelloWorldService.java

@@ -1,48 +0,0 @@
-
-package com.example.helloworld;
-
-import com.example.helloworld.config.HelloWorldConfiguration;
-import com.example.helloworld.db.FortuneDAO;
-import com.example.helloworld.db.MongoHealthCheck;
-import com.example.helloworld.db.MongoManaged;
-import com.example.helloworld.db.WorldDAO;
-import com.example.helloworld.resources.FortuneResource;
-import com.example.helloworld.resources.JsonResource;
-import com.example.helloworld.resources.TextResource;
-import com.example.helloworld.resources.WorldResource;
-import com.mongodb.DB;
-import com.mongodb.MongoClient;
-import io.dropwizard.Application;
-import io.dropwizard.setup.Bootstrap;
-import io.dropwizard.setup.Environment;
-
-import java.net.UnknownHostException;
-
-public class HelloWorldService
-        extends Application<HelloWorldConfiguration> {
-
-    public static void main(String[] args) throws Exception {
-        new HelloWorldService().run(args);
-    }
-
-    @Override
-    public void run(HelloWorldConfiguration config, Environment environment) throws UnknownHostException {
-        MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
-        environment.lifecycle().manage(new MongoManaged(mongoClient));
-        environment.healthChecks().register("mongo", new MongoHealthCheck(mongoClient));
-
-        DB db = mongoClient.getDB(config.mongo.db);
-        WorldDAO worldDAO = new WorldDAO(db);
-        FortuneDAO fortuneDAO = new FortuneDAO(db);
-
-        environment.jersey().register(new JsonResource());
-        environment.jersey().register(new WorldResource(worldDAO));
-        environment.jersey().register(new FortuneResource(fortuneDAO));
-        environment.jersey().register(new TextResource());
-    }
-
-    @Override
-    public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
-
-    }
-}

+ 0 - 12
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/config/HelloWorldConfiguration.java

@@ -1,12 +0,0 @@
-
-package com.example.helloworld.config;
-
-import com.fasterxml.jackson.annotation.JsonProperty;
-import io.dropwizard.Configuration;
-
-public class HelloWorldConfiguration extends Configuration {
-    @JsonProperty
-    public MongoConfiguration mongo;
-
-
-}

+ 0 - 20
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/db/FortuneDAO.java

@@ -1,20 +0,0 @@
-package com.example.helloworld.db;
-
-import com.example.helloworld.db.model.Fortune;
-import com.mongodb.DB;
-import org.mongojack.JacksonDBCollection;
-
-import java.util.Collection;
-
-public class FortuneDAO {
-
-
-    private final JacksonDBCollection<Fortune, String> fortunes;
-    public FortuneDAO(DB db) {
-        fortunes = JacksonDBCollection.wrap(db.getCollection("fortunes"), Fortune.class, String.class);
-    }
-
-    public Collection<? extends Fortune> list() {
-        return null;
-    }
-}

+ 0 - 20
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/db/MongoHealthCheck.java

@@ -1,20 +0,0 @@
-package com.example.helloworld.db;
-
-import com.codahale.metrics.health.HealthCheck;
-import com.mongodb.Mongo;
-
-public class MongoHealthCheck extends HealthCheck {
- 
-    private Mongo mongo;
- 
-    public MongoHealthCheck(Mongo mongo) {
-        this.mongo = mongo;
-    }
- 
-    @Override
-    protected Result check() throws Exception {
-        mongo.getDatabaseNames();
-        return Result.healthy();
-    }
- 
-}

+ 0 - 23
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/db/WorldDAO.java

@@ -1,23 +0,0 @@
-package com.example.helloworld.db;
-
-import com.example.helloworld.db.model.World;
-import com.google.common.base.Optional;
-import com.mongodb.DB;
-import org.mongojack.JacksonDBCollection;
-
-public class WorldDAO {
-
-    private final JacksonDBCollection<World, String> worlds;
-
-    public WorldDAO(DB db) {
-        worlds = JacksonDBCollection.wrap(db.getCollection("world"), World.class, String.class);
-    }
-
-    public Optional<World> findById(long worldId) {
-        return Optional.fromNullable(worlds.findOneById(String.valueOf(worldId)));
-    }
-
-    public World update(World world) {
-        return worlds.insert(world).getSavedObject();
-    }
-}

+ 0 - 35
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/db/model/Fortune.java

@@ -1,35 +0,0 @@
-package com.example.helloworld.db.model;
-
-import javax.persistence.*;
-
-@Entity
-@Table(name = "Fortune")
-public class Fortune implements Comparable<Fortune> {
-
-    @Id
-    @GeneratedValue(strategy = GenerationType.AUTO)
-    private long id;
-
-    @Column(name = "message", nullable = false)
-    private String message;
-
-    @SuppressWarnings("unused")
-    public Fortune() {}
-
-    public Fortune(String message) {
-        this.message = message;
-    }
-
-    public long getId() {
-        return id;
-    }
-
-    public String getMessage() {
-        return message;
-    }
-
-    @Override
-    public int compareTo(Fortune o) {
-        return message.compareTo(o.message);
-    }
-}

+ 0 - 41
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/db/model/World.java

@@ -1,41 +0,0 @@
-
-package com.example.helloworld.db.model;
-
-import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
-import org.mongojack.Id;
-
-import javax.persistence.Column;
-import javax.persistence.Entity;
-import javax.persistence.Table;
-
-@Entity
-@Table(name = "World")
-@JsonIgnoreProperties(ignoreUnknown = true)
-public class World
-{
-  @Id
-  private long id;
-
-  @Column(name = "randomNumber", nullable = false)
-  private long randomNumber;
-
-  public long getId()
-  {
-    return id;
-  }
-
-  public void setId(long id)
-  {
-    this.id = id;
-  }
-
-  public long getRandomNumber()
-  {
-    return this.randomNumber;
-  }
-
-  public void setRandomNumber(long randomNumber)
-  {
-    this.randomNumber = randomNumber;
-  }
-}

+ 0 - 45
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/resources/Helper.java

@@ -1,45 +0,0 @@
-package com.example.helloworld.resources;
-
-import java.util.concurrent.ThreadLocalRandom;
-
-import com.google.common.base.Optional;
-
-/**
- * Provides utility methods for the benchmark tests.
- * Taken from undertow-edge project.
- */
-final class Helper {
-  private Helper() {
-    throw new AssertionError();
-  }
-
-  /**
-   * Returns the value of the "queries" request parameter, which is an integer
-   * bound between 1 and 500 with a default value of 1.
-   *
-   * @param exchange the current HTTP exchange
-   * @return the value of the "queries" request parameter
-   */
-  static int getQueries(Optional<String> queries) {
-    String value = queries.orNull();
-    if (value == null) {
-      return 1;
-    }
-    try {
-      int parsedValue = Integer.parseInt(value);
-      return Math.min(500, Math.max(1, parsedValue));
-    } catch (NumberFormatException e) {
-      return 1;
-    }
-  }
-
-  /**
-   * Returns a random integer that is a suitable value for both the {@code id}
-   * and {@code randomNumber} properties of a world object.
-   *
-   * @return a random world number
-   */
-  static int randomWorld() {
-    return 1 + ThreadLocalRandom.current().nextInt(10000);
-  }
-}

+ 0 - 19
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/resources/JsonResource.java

@@ -1,19 +0,0 @@
-
-package com.example.helloworld.resources;
-
-import com.example.helloworld.resources.api.HelloMessage;
-
-import javax.ws.rs.GET;
-import javax.ws.rs.Path;
-import javax.ws.rs.Produces;
-import javax.ws.rs.core.MediaType;
-
-@Path("/json")
-@Produces(MediaType.APPLICATION_JSON)
-public class JsonResource {
-
-    @GET
-    public HelloMessage sayHello() {
-        return new HelloMessage("Hello, World!");
-    }
-}

+ 0 - 26
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/resources/TextResource.java

@@ -1,26 +0,0 @@
-package com.example.helloworld.resources;
-
-import javax.ws.rs.GET;
-import javax.ws.rs.Path;
-import javax.ws.rs.Produces;
-import javax.ws.rs.core.MediaType;
-
-@Path("/plaintext")
-@Produces(MediaType.TEXT_PLAIN)
-public class TextResource {
-	private static final String MESSAGE = "Hello, World!";
-	private static final byte[] buffer;
-
-	static {
-		try {
-			buffer = MESSAGE.getBytes("US-ASCII");
-		} catch (Exception e) {
-			throw new RuntimeException(e);
-		}
-	}
-
-	@GET
-	public byte[] sayHello() {
-		return buffer;
-	}
-}

+ 0 - 55
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/resources/WorldResource.java

@@ -1,55 +0,0 @@
-package com.example.helloworld.resources;
-
-import com.example.helloworld.db.WorldDAO;
-import com.example.helloworld.db.model.World;
-import com.google.common.base.Optional;
-
-import javax.ws.rs.GET;
-import javax.ws.rs.Path;
-import javax.ws.rs.Produces;
-import javax.ws.rs.QueryParam;
-import javax.ws.rs.core.MediaType;
-
-@Path("/db")
-@Produces(MediaType.APPLICATION_JSON)
-public class WorldResource {
-    private final WorldDAO worldDAO;
-
-    public WorldResource(WorldDAO worldDAO) {
-        this.worldDAO = worldDAO;
-
-    }
-
-    @GET
-    public Object dbTest(@QueryParam("queries") Optional<String> queries) {
-        int totalQueries = Helper.getQueries(queries);
-        final World[] worlds = new World[totalQueries];
-
-        for (int i = 0; i < totalQueries; i++) {
-            final long worldId = Helper.randomWorld();
-            worlds[i] = worldDAO.findById(worldId).orNull();
-        }
-        if (!queries.isPresent()) {
-            return worlds[0];
-        } else {
-            return worlds;
-        }
-    }
-
-    @GET
-    @Path("/update")
-    public World[] updateTest(@QueryParam("queries") Optional<String> queries) {
-        int totalQueries = Helper.getQueries(queries);
-        final World[] worlds = new World[totalQueries];
-
-        for (int i = 0; i < totalQueries; i++) {
-            final long worldId = Helper.randomWorld();
-
-            final World world = worldDAO.findById(worldId).orNull();
-            world.setRandomNumber(Helper.randomWorld());
-            worlds[i] = worldDAO.update(world);
-        }
-
-        return worlds;
-    }
-}

+ 0 - 17
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/resources/api/HelloMessage.java

@@ -1,17 +0,0 @@
-package com.example.helloworld.resources.api;
-
-import com.fasterxml.jackson.annotation.JsonProperty;
-
-public final class HelloMessage {
-
-    @JsonProperty
-    private final String message;
-
-    public HelloMessage(String m) {
-        message = m;
-    }
-
-    public String getMessage() {
-        return message;
-    }
-}

+ 0 - 23
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/resources/views/FortuneView.java

@@ -1,23 +0,0 @@
-package com.example.helloworld.resources.views;
-
-import com.example.helloworld.db.model.Fortune;
-import io.dropwizard.views.View;
-
-import java.nio.charset.StandardCharsets;
-import java.util.List;
-
-public class FortuneView extends View {
-
-    private final List<Fortune> fortunes;
-
-    public FortuneView(List<Fortune> fortunes) {
-        super("/fortunes.mustache", StandardCharsets.UTF_8);
-
-        this.fortunes = fortunes;
-    }
-
-    @SuppressWarnings("unused")
-    public List<Fortune> getFortunes() {
-        return fortunes;
-    }
-}

+ 24 - 3
frameworks/Java/dropwizard/benchmark_config.json

@@ -9,7 +9,7 @@
       "fortune_url": "/fortunes",
       "update_url": "/db/update?queries=",
       "plaintext_url": "/plaintext",
-      "port": 9000,
+      "port": 9090,
       "approach": "Realistic",
       "classification": "Fullstack",
       "database": "MySQL",
@@ -21,8 +21,29 @@
       "os": "Linux",
       "database_os": "Linux",
       "display_name": "dropwizard",
-      "notes": "",
+      "notes": "dropwizard with mysql using hibernate",
+      "versus": ""
+    },
+    "mongodb": {
+      "setup_file": "setup",
+      "db_url": "/mongo/db",
+      "query_url": "/mongo/db?queries=",
+      "fortune_url": "/mongo/fortunes",
+      "update_url": "/mongo/db/update?queries=",
+      "port": 9090,
+      "approach": "Realistic",
+      "classification": "Fullstack",
+      "database": "MongoDB",
+      "framework": "dropwizard",
+      "language": "Java",
+      "orm": "Full",
+      "platform": "Jetty",
+      "webserver": "Jetty",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "dropwizard-mongodb",
+      "notes": "dropwizard with mongodb using mongojack",
       "versus": ""
     }
   }]
-}
+}

+ 6 - 1
frameworks/Java/dropwizard/hello-world.yml

@@ -3,7 +3,7 @@ server:
   applicationContextPath: /
   connector:
     type: http
-    port: 9080
+    port: 9090
     useServerHeader: true
 
   requestLog:
@@ -43,3 +43,8 @@ database:
 
   properties:
     hibernate.dialect: org.hibernate.dialect.MySQLDialect
+
+mongo:
+  host: 127.0.0.1
+  port: 27017
+  db: hello_world

+ 5 - 0
frameworks/Java/dropwizard/pom.xml

@@ -34,6 +34,11 @@
             <artifactId>mysql-connector-java</artifactId>
             <version>${mysql-connector-java.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.mongojack</groupId>
+            <artifactId>mongojack</artifactId>
+            <version>2.1.0</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 10 - 3
frameworks/Java/dropwizard/source_code

@@ -1,12 +1,17 @@
 ./dropwizard/src/main/java/com/example/helloworld/
 ./dropwizard/src/main/java/com/example/helloworld/config
-./dropwizard/src/main/java/com/example/helloworld/config/HelloWorldConfiguration
+./dropwizard/src/main/java/com/example/helloworld/config/HelloWorldConfiguration.java
+./dropwizard-mongodb/src/main/java/com/example/helloworld/config/MongoConfiguration.java
 ./dropwizard/src/main/java/com/example/helloworld/db
 ./dropwizard/src/main/java/com/example/helloworld/db/model
-./dropwizard/src/main/java/com/example/helloworld/db/model/Fortune
-./dropwizard/src/main/java/com/example/helloworld/db/model/World
+./dropwizard/src/main/java/com/example/helloworld/db/model/Fortune.java
+./dropwizard/src/main/java/com/example/helloworld/db/model/World.java
 ./dropwizard/src/main/java/com/example/helloworld/db/FortuneDAO.java
 ./dropwizard/src/main/java/com/example/helloworld/db/WorldDAO.java
+./dropwizard/src/main/java/com/example/helloworld/db/mongo
+./dropwizard/src/main/java/com/example/helloworld/db/mongo/MongoManaged.java
+./dropwizard/src/main/java/com/example/helloworld/db/mongo/MongoWorldDAO.java
+./dropwizard/src/main/java/com/example/helloworld/db/mongo/MongoFortuneDAO.java
 ./dropwizard/src/main/java/com/example/helloworld/resources
 ./dropwizard/src/main/java/com/example/helloworld/resources/api
 ./dropwizard/src/main/java/com/example/helloworld/resources/api/HelloMessage
@@ -16,6 +21,8 @@
 ./dropwizard/src/main/java/com/example/helloworld/resources/JsonResource.java
 ./dropwizard/src/main/java/com/example/helloworld/resources/TextResource.java
 ./dropwizard/src/main/java/com/example/helloworld/resources/WorldResource.java
+./dropwizard/src/main/java/com/example/helloworld/resources/MongoFortuneResource.java
+./dropwizard/src/main/java/com/example/helloworld/resources/MongoWorldResource.java
 ./dropwizard/src/main/java/com/example/helloworld/resources/Helper.java
 ./dropwizard/src/main/java/com/example/helloworld/HelloWorldService.java
 ./dropwizard/src/main/resources/fortunes.mustache

+ 37 - 6
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/HelloWorldService.java

@@ -1,20 +1,27 @@
 package com.example.helloworld;
 
 import com.example.helloworld.config.HelloWorldConfiguration;
+import com.example.helloworld.config.MongoConfiguration;
 import com.example.helloworld.db.FortuneDAO;
+import com.example.helloworld.db.MongoManaged;
+import com.example.helloworld.db.WorldDAO;
 import com.example.helloworld.db.model.Fortune;
 import com.example.helloworld.db.model.World;
-import com.example.helloworld.db.WorldDAO;
-import com.example.helloworld.resources.FortuneResource;
-import com.example.helloworld.resources.JsonResource;
-import com.example.helloworld.resources.TextResource;
-import com.example.helloworld.resources.WorldResource;
+import com.example.helloworld.resources.*;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.mongodb.DB;
+import com.mongodb.MongoClient;
 import io.dropwizard.Application;
 import io.dropwizard.db.DataSourceFactory;
 import io.dropwizard.hibernate.HibernateBundle;
+import io.dropwizard.jackson.Jackson;
 import io.dropwizard.setup.Bootstrap;
 import io.dropwizard.setup.Environment;
 import io.dropwizard.views.ViewBundle;
+import org.mongojack.JacksonDBCollection;
+import org.mongojack.internal.MongoJackModule;
+
+import java.net.UnknownHostException;
 
 public class HelloWorldService extends Application<HelloWorldConfiguration> {
 
@@ -36,10 +43,34 @@ public class HelloWorldService extends Application<HelloWorldConfiguration> {
     }
 
     @Override
-    public void run(HelloWorldConfiguration config, Environment environment) {
+    public void run(HelloWorldConfiguration config, Environment environment) throws UnknownHostException {
         environment.jersey().register(new JsonResource()); // Test type 1: JSON serialization
         environment.jersey().register(new WorldResource(new WorldDAO(hibernate.getSessionFactory()))); // Test types 2, 3 & 5: Single database query, Multiple database queries & Database updates
         environment.jersey().register(new FortuneResource(new FortuneDAO(hibernate.getSessionFactory()))); // Test type 4: Fortunes
         environment.jersey().register(new TextResource()); // Test type 6: Plaintext
+
+        setUpMongo(config.getMongo(), environment);
+    }
+
+    private void setUpMongo(MongoConfiguration config, Environment environment) throws UnknownHostException {
+        final MongoClient mongoClient = new MongoClient(config.getHost(), config.getPort());
+        environment.lifecycle().manage(new MongoManaged(mongoClient));
+
+        final DB db = mongoClient.getDB(config.getDb());
+        final ObjectMapper mongoJackMapper = MongoJackModule.configure(Jackson.newObjectMapper());
+        final JacksonDBCollection<World, Long> worlds = JacksonDBCollection.wrap(
+                db.getCollection("World"),
+                World.class,
+                Long.class,
+                mongoJackMapper);
+
+        final JacksonDBCollection<Fortune, Long> fortunes = JacksonDBCollection.wrap(
+                db.getCollection("Fortune"),
+                Fortune.class,
+                Long.class,
+                mongoJackMapper);
+
+        environment.jersey().register(new MongoWorldResource(worlds));
+        environment.jersey().register(new MongoFortuneResource(fortunes));
     }
 }

+ 9 - 0
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/config/HelloWorldConfiguration.java

@@ -14,7 +14,16 @@ public class HelloWorldConfiguration extends Configuration {
     @JsonProperty
     private DataSourceFactory database = new DataSourceFactory();
 
+    @Valid
+    @NotNull
+    @JsonProperty
+    private MongoConfiguration mongo = new MongoConfiguration();
+
     public DataSourceFactory getDatabaseConfiguration() {
         return database;
     }
+
+    public MongoConfiguration getMongo() {
+        return mongo;
+    }
 }

+ 15 - 3
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/config/MongoConfiguration.java → frameworks/Java/dropwizard/src/main/java/com/example/helloworld/config/MongoConfiguration.java

@@ -9,14 +9,26 @@ import javax.validation.constraints.Min;
 public class MongoConfiguration {
     @JsonProperty
     @NotEmpty
-    public String host;
+    private String host;
 
     @JsonProperty
     @Min(1)
     @Max(65535)
-    public int port;
+    private int port;
 
     @JsonProperty
     @NotEmpty
-    public String db;
+    private String db;
+
+    public String getHost() {
+        return host;
+    }
+
+    public int getPort() {
+        return port;
+    }
+
+    public String getDb() {
+        return db;
+    }
 }

+ 0 - 0
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/db/MongoManaged.java → frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/MongoManaged.java


+ 4 - 1
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/model/Fortune.java

@@ -1,5 +1,7 @@
 package com.example.helloworld.db.model;
 
+import com.fasterxml.jackson.annotation.JsonProperty;
+
 import javax.persistence.*;
 
 @Entity
@@ -7,9 +9,10 @@ import javax.persistence.*;
 public class Fortune implements Comparable<Fortune> {
 
     @Id
-    @GeneratedValue(strategy = GenerationType.AUTO)
+    @JsonProperty
     private long id;
 
+    @JsonProperty
     @Column(name = "message", nullable = false)
     private String message;
 

+ 10 - 3
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/model/World.java

@@ -1,19 +1,26 @@
 package com.example.helloworld.db.model;
 
-import javax.persistence.*;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
 
 @Entity
 @Table(name = "World")
 public class World {
 
     @Id
-    @GeneratedValue(strategy = GenerationType.AUTO)
+    @JsonProperty
     private long id;
 
+    @JsonProperty
     @Column(name = "randomNumber", nullable = false)
     private long randomNumber;
 
-    public World() {}
+    public World() {
+    }
 
     public long getId() {
         return id;

+ 9 - 8
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/resources/FortuneResource.java → frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/MongoFortuneResource.java

@@ -1,10 +1,11 @@
 package com.example.helloworld.resources;
 
-import com.example.helloworld.db.FortuneDAO;
 import com.example.helloworld.db.model.Fortune;
 import com.example.helloworld.resources.views.FortuneView;
 import com.google.common.collect.Lists;
-import com.mongodb.DB;
+import org.mongojack.DBProjection;
+import org.mongojack.DBQuery;
+import org.mongojack.JacksonDBCollection;
 
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
@@ -13,21 +14,21 @@ import javax.ws.rs.core.MediaType;
 import java.util.Collections;
 import java.util.List;
 
-@Path("/fortunes")
+@Path("/mongo/fortunes")
 @Produces(MediaType.TEXT_HTML + ";charset=UTF-8")
-public class FortuneResource {
+public class MongoFortuneResource {
 
-    private final FortuneDAO fortuneDAO;
+    private final JacksonDBCollection<Fortune, Long> fortuneCollection;
 
-    public FortuneResource(FortuneDAO fortuneDAO) {
-        this.fortuneDAO = fortuneDAO;
+    public MongoFortuneResource(JacksonDBCollection<Fortune, Long> fortuneCollection) {
+        this.fortuneCollection = fortuneCollection;
     }
 
     @GET
     public FortuneView dbTest() {
         final List<Fortune> fortunes = Lists.newArrayListWithExpectedSize(32);
 
-        fortunes.addAll(fortuneDAO.list());
+        fortunes.addAll(fortuneCollection.find(DBQuery.empty(), DBProjection.include("_id", "message")).toArray());
         fortunes.add(new Fortune("Additional fortune added at request time."));
 
         Collections.sort(fortunes);

+ 65 - 0
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/MongoWorldResource.java

@@ -0,0 +1,65 @@
+package com.example.helloworld.resources;
+
+import com.example.helloworld.db.model.World;
+import com.google.common.base.Optional;
+import org.mongojack.DBProjection;
+import org.mongojack.DBQuery;
+import org.mongojack.DBUpdate;
+import org.mongojack.JacksonDBCollection;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.MediaType;
+
+import static com.example.helloworld.resources.Helper.getQueries;
+
+@Path("/mongo/db")
+@Produces(MediaType.APPLICATION_JSON)
+public class MongoWorldResource {
+    private final JacksonDBCollection<World, Long> worldsCollection;
+
+    public MongoWorldResource(JacksonDBCollection<World, Long> worldsCollection) {
+        this.worldsCollection = worldsCollection;
+    }
+
+    @GET
+    public Object dbTest(@QueryParam("queries") Optional<String> queries) {
+        int totalQueries = getQueries(queries);
+        final World[] worlds = new World[totalQueries];
+
+        for (int i = 0; i < totalQueries; i++) {
+            worlds[i] = worldsCollection.findOneById(
+                    (long) Helper.randomWorld(),
+                    DBProjection.include("_id", "randomNumber")
+            );
+        }
+        if (!queries.isPresent()) {
+            return worlds[0];
+        } else {
+            return worlds;
+        }
+    }
+
+    @GET
+    @Path("/update")
+    public World[] updateTest(@QueryParam("queries") Optional<String> queries) {
+        int totalQueries = getQueries(queries);
+        final World[] worlds = new World[totalQueries];
+
+        for (int i = 0; i < totalQueries; i++) {
+            // use findAndModify() similar to nodejs-mongodb-raw
+            worlds[i] = worldsCollection.findAndModify(
+                    DBQuery.is("_id", (long) Helper.randomWorld()),
+                    DBProjection.include("_id", "randomNumber"),
+                    null,
+                    false,
+                    DBUpdate.set("randomNumber", (long) Helper.randomWorld()),
+                    true,
+                    false
+            );
+        }
+        return worlds;
+    }
+}

+ 1 - 4
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/WorldResource.java

@@ -23,10 +23,9 @@ public class WorldResource {
     @GET
     @UnitOfWork
     public Object dbTest(@QueryParam("queries") Optional<String> queries) {
-    	int totalQueries = Helper.getQueries(queries);
+        int totalQueries = Helper.getQueries(queries);
         final World[] worlds = new World[totalQueries];
 
-        // TODO: Is parallelising this cheating?
         for (int i = 0; i < totalQueries; i++) {
             final long worldId = Helper.randomWorld();
             worlds[i] = worldDAO.findById(worldId).orNull();
@@ -45,7 +44,6 @@ public class WorldResource {
         int totalQueries = Helper.getQueries(queries);
         final World[] worlds = new World[totalQueries];
 
-        // TODO: Is parallelising this cheating?
         for (int i = 0; i < totalQueries; i++) {
             final long worldId = Helper.randomWorld();
 
@@ -53,7 +51,6 @@ public class WorldResource {
             world.setRandomNumber(Helper.randomWorld());
             worlds[i] = worldDAO.update(world);
         }
-
         return worlds;
     }
 }