Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created Cassandra Connector Module for Play Framework #118

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ lazy val root = (project in file(".")).enablePlugins(PlayJava)
commonSettings,
name := "ground"
)
.aggregate(common, postgres)
.aggregate(common, postgres, cassandra)


lazy val common = (project in file("modules/common"))
Expand Down Expand Up @@ -48,6 +48,20 @@ lazy val postgres = (project in file("modules/postgres"))

).dependsOn(common)

lazy val cassandra = (project in file("modules/cassandra"))
.enablePlugins(PlayJava, JavaAppPackaging)
.settings(
commonSettings,
name := "ground-cassandra",
libraryDependencies += javaJdbc,
libraryDependencies += cache,
libraryDependencies += "com.datastax.cassandra" % "cassandra-driver-core" % "3.0.0",
libraryDependencies += "commons-beanutils" % "commons-beanutils-core" % "1.8.3",
jacoco.settings,
parallelExecution in jacoco.Config := false,
Keys.fork in jacoco.Config := true,
jacoco.reportFormats in jacoco.Config := Seq(XMLReport(encoding = "utf-8"))
).dependsOn(common)

jacoco.settings
parallelExecution in jacoco.Config := false
Expand Down
12 changes: 12 additions & 0 deletions modules/cassandra/app/Module.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import com.google.inject.AbstractModule;
import edu.berkeley.ground.cassandra.start.ApplicationStart;
import java.time.Clock;

public class Module extends AbstractModule {

@Override
public void configure() {
bind(Clock.class).toInstance(Clock.systemDefaultZone());
bind(ApplicationStart.class).asEagerSingleton();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package edu.berkeley.ground.cassandra.controllers;

import akka.actor.ActorSystem;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.berkeley.ground.common.exception.GroundException;
import edu.berkeley.ground.common.model.core.Edge;
import edu.berkeley.ground.common.model.core.EdgeVersion;
import edu.berkeley.ground.common.util.IdGenerator;
import edu.berkeley.ground.cassandra.dao.core.CassandraEdgeDao;
import edu.berkeley.ground.cassandra.dao.core.CassandraEdgeVersionDao;
import edu.berkeley.ground.cassandra.util.GroundUtils;
import edu.berkeley.ground.cassandra.util.CassandraDatabase;
import edu.berkeley.ground.cassandra.util.CassandraUtils;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import javax.inject.Inject;
import play.cache.CacheApi;
import play.libs.Json;
import play.mvc.BodyParser;
import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.Results;

public class EdgeController extends Controller {

private CacheApi cache;
private ActorSystem actorSystem;

private CassandraEdgeDao cassandraEdgeDao;
private CassandraEdgeVersionDao cassandraEdgeVersionDao;

@Inject
final void injectUtils(final CacheApi cache, final CassandraDatabase dbSource, final ActorSystem actorSystem, final IdGenerator idGenerator) {
this.actorSystem = actorSystem;
this.cache = cache;

this.cassandraEdgeDao = new CassandraEdgeDao(dbSource, idGenerator);
this.cassandraEdgeVersionDao = new CassandraEdgeVersionDao(dbSource, idGenerator);
}

public final CompletionStage<Result> getEdge(final String sourceKey) {
return CompletableFuture.supplyAsync(
() -> {
try {
return this.cache.getOrElse(
"edges",
() -> Json.toJson(this.cassandraEdgeDao.retrieveFromDatabase(sourceKey)),
Integer.parseInt(System.getProperty("ground.cache.expire.secs")));
} catch (Exception e) {
throw new CompletionException(e);
}
},
CassandraUtils.getDbSourceHttpContext(this.actorSystem))
.thenApply(Results::ok)
.exceptionally(e -> GroundUtils.handleException(e, request()));
}

@BodyParser.Of(BodyParser.Json.class)
public final CompletionStage<Result> addEdge() {
return CompletableFuture.supplyAsync(
() -> {
JsonNode json = request().body().asJson();
Edge edge = Json.fromJson(json, Edge.class);

try {
edge = this.cassandraEdgeDao.create(edge);
} catch (GroundException e) {
throw new CompletionException(e);
}

return Json.toJson(edge);
},
CassandraUtils.getDbSourceHttpContext(this.actorSystem))
.thenApply(Results::created)
.exceptionally(e -> GroundUtils.handleException(e, request()));
}

public final CompletionStage<Result> getEdgeVersion(Long id) {
return CompletableFuture.supplyAsync(
() -> {
try {
return this.cache.getOrElse(
"edge_versions",
() -> Json.toJson(this.cassandraEdgeVersionDao.retrieveFromDatabase(id)),
Integer.parseInt(System.getProperty("ground.cache.expire.secs")));
} catch (Exception e) {
throw new CompletionException(e);
}
},
CassandraUtils.getDbSourceHttpContext(actorSystem))
.thenApply(Results::ok)
.exceptionally(e -> GroundUtils.handleException(e, request()));
}

@BodyParser.Of(BodyParser.Json.class)
public final CompletionStage<Result> addEdgeVersion() {
return CompletableFuture.supplyAsync(
() -> {
JsonNode json = request().body().asJson();
List<Long> parentIds = GroundUtils.getListFromJson(json, "parentIds");

((ObjectNode) json).remove("parentIds");
EdgeVersion edgeVersion = Json.fromJson(json, EdgeVersion.class);

try {
edgeVersion = this.cassandraEdgeVersionDao.create(edgeVersion, parentIds);
} catch (GroundException e) {
throw new CompletionException(e);
}

return Json.toJson(edgeVersion);
},
CassandraUtils.getDbSourceHttpContext(actorSystem))
.thenApply(Results::created)
.exceptionally(e -> GroundUtils.handleException(e, request()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package edu.berkeley.ground.cassandra.controllers;

import akka.actor.ActorSystem;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.berkeley.ground.common.exception.GroundException;
import edu.berkeley.ground.common.model.core.Graph;
import edu.berkeley.ground.common.model.core.GraphVersion;
import edu.berkeley.ground.common.util.IdGenerator;
import edu.berkeley.ground.cassandra.dao.core.CassandraGraphDao;
import edu.berkeley.ground.cassandra.dao.core.CassandraGraphVersionDao;
import edu.berkeley.ground.cassandra.util.GroundUtils;
import edu.berkeley.ground.cassandra.util.CassandraDatabase;
import edu.berkeley.ground.cassandra.util.CassandraUtils;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import javax.inject.Inject;
import play.cache.CacheApi;
import play.libs.Json;
import play.mvc.BodyParser;
import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.Results;

public class GraphController extends Controller {

private CacheApi cache;
private ActorSystem actorSystem;

private CassandraGraphDao cassandraGraphDao;
private CassandraGraphVersionDao cassandraGraphVersionDao;

@Inject
final void injectUtils(final CacheApi cache, final CassandraDatabase dbSource, final ActorSystem actorSystem, final IdGenerator idGenerator) {
this.actorSystem = actorSystem;
this.cache = cache;

this.cassandraGraphDao = new CassandraGraphDao(dbSource, idGenerator);

this.cassandraGraphVersionDao = new CassandraGraphVersionDao(dbSource, idGenerator);
}

public final CompletionStage<Result> getGraph(String sourceKey) {
return CompletableFuture.supplyAsync(
() -> {
try {
return this.cache.getOrElse(
"graphs",
() -> Json.toJson(this.cassandraGraphDao.retrieveFromDatabase(sourceKey)),
Integer.parseInt(System.getProperty("ground.cache.expire.secs")));
} catch (Exception e) {
throw new CompletionException(e);
}
},
CassandraUtils.getDbSourceHttpContext(this.actorSystem))
.thenApply(Results::ok)
.exceptionally(e -> GroundUtils.handleException(e, request()));
}

public final CompletionStage<Result> getGraphVersion(Long id) {
return CompletableFuture.supplyAsync(
() -> {
try {
return this.cache.getOrElse("graph_versions",
() -> Json.toJson(this.cassandraGraphVersionDao.retrieveFromDatabase(id)),
Integer.parseInt(System.getProperty("ground.cache.expire.secs")));
} catch (Exception e) {
throw new CompletionException(e);
}
},
CassandraUtils.getDbSourceHttpContext(this.actorSystem))
.thenApply(Results::ok)
.exceptionally(e -> GroundUtils.handleException(e, request()));
}

@BodyParser.Of(BodyParser.Json.class)
public final CompletionStage<Result> addGraph() {
return CompletableFuture.supplyAsync(
() -> {
JsonNode json = request().body().asJson();
Graph graph = Json.fromJson(json, Graph.class);

try {
graph = this.cassandraGraphDao.create(graph);
} catch (GroundException e) {
throw new CompletionException(e);
}

return Json.toJson(graph);
},
CassandraUtils.getDbSourceHttpContext(actorSystem))
.thenApply(Results::created)
.exceptionally(e -> GroundUtils.handleException(e, request()));
}

@BodyParser.Of(BodyParser.Json.class)
public final CompletionStage<Result> addGraphVersion() {
return CompletableFuture.supplyAsync(
() -> {
JsonNode json = request().body().asJson();
List<Long> parentIds = GroundUtils.getListFromJson(json, "parentIds");
((ObjectNode) json).remove("parentIds");
GraphVersion graphVersion = Json.fromJson(json, GraphVersion.class);

try {
graphVersion = this.cassandraGraphVersionDao.create(graphVersion, parentIds);
} catch (GroundException e) {
throw new CompletionException(e);
}
return Json.toJson(graphVersion);
},
CassandraUtils.getDbSourceHttpContext(actorSystem))
.thenApply(Results::ok)
.exceptionally(e -> GroundUtils.handleException(e, request()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package edu.berkeley.ground.cassandra.controllers;

import play.mvc.*;
import views.html.*;

/** This controller contains an action to handle HTTP requests to the application's home page. */
public class HomeController extends Controller {

/**
* An action that renders an HTML page with a welcome message. The configuration in the <code>
* routes</code> file means that this method will be called when the application receives a <code>
* GET</code> request with a path of <code>/</code>.
*/
public Result index() {
return ok(index.render("Your new application is ready."));
}
}
Loading