diff --git a/modules/common/app/edu/berkeley/ground/common/dao/core/NodeVersionDao.java b/modules/common/app/edu/berkeley/ground/common/dao/core/NodeVersionDao.java index 9e0ac9aa..f0690e37 100644 --- a/modules/common/app/edu/berkeley/ground/common/dao/core/NodeVersionDao.java +++ b/modules/common/app/edu/berkeley/ground/common/dao/core/NodeVersionDao.java @@ -23,6 +23,8 @@ public interface NodeVersionDao extends RichVersionDao { @Override NodeVersion retrieveFromDatabase(long id) throws GroundException; + List retrieveAdjacentLineageEdgeVersion(long startId) throws GroundException; + @Override default Class getType() { return NodeVersion.class; diff --git a/modules/postgres/app/edu/berkeley/ground/postgres/controllers/NodeController.java b/modules/postgres/app/edu/berkeley/ground/postgres/controllers/NodeController.java index fa6c2150..36985fbc 100644 --- a/modules/postgres/app/edu/berkeley/ground/postgres/controllers/NodeController.java +++ b/modules/postgres/app/edu/berkeley/ground/postgres/controllers/NodeController.java @@ -149,4 +149,22 @@ public final CompletionStage getHistory(String sourceKey) { .thenApply(Results::ok) .exceptionally(e -> GroundUtils.handleException(e, request())); } + + public final CompletionStage getAdjacentLineage(Long id) { + return CompletableFuture.supplyAsync( + () -> { + try { + return this.cache.getOrElse( + "node_version_adj_lineage." + id, + () -> Json.toJson(this.postgresNodeVersionDao.retrieveAdjacentLineageEdgeVersion(id)), + Integer.parseInt(System.getProperty("ground.cache.expire.secs"))); + } catch (Exception e) { + throw new CompletionException(e); + } + }, + PostgresUtils.getDbSourceHttpContext(actorSystem)) + .thenApply(Results::ok) + .exceptionally(e -> GroundUtils.handleException(e, request())); + } + } diff --git a/modules/postgres/app/edu/berkeley/ground/postgres/dao/SqlConstants.java b/modules/postgres/app/edu/berkeley/ground/postgres/dao/SqlConstants.java index de777205..d36a2995 100644 --- a/modules/postgres/app/edu/berkeley/ground/postgres/dao/SqlConstants.java +++ b/modules/postgres/app/edu/berkeley/ground/postgres/dao/SqlConstants.java @@ -52,6 +52,7 @@ public class SqlConstants { /* Node-specific statements */ public static final String INSERT_NODE_VERSION = "INSERT INTO node_version (id, node_id) VALUES (%d, %d);"; + public static final String SELECT_NODE_VERSION_ADJACENT_LINEAGE = "SELECT * FROM lineage_edge_version WHERE from_rich_version_id = %d;"; /* Rich Version-specific statements */ public static final String INSERT_RICH_VERSION_WITH_REFERENCE = "INSERT INTO rich_version (id, structure_version_id, reference) VALUES (%d, %d, " diff --git a/modules/postgres/app/edu/berkeley/ground/postgres/dao/core/PostgresNodeVersionDao.java b/modules/postgres/app/edu/berkeley/ground/postgres/dao/core/PostgresNodeVersionDao.java index 0c8c3036..da144d33 100644 --- a/modules/postgres/app/edu/berkeley/ground/postgres/dao/core/PostgresNodeVersionDao.java +++ b/modules/postgres/app/edu/berkeley/ground/postgres/dao/core/PostgresNodeVersionDao.java @@ -10,6 +10,7 @@ import edu.berkeley.ground.postgres.dao.SqlConstants; import edu.berkeley.ground.postgres.util.PostgresStatements; import edu.berkeley.ground.postgres.util.PostgresUtils; +import java.util.ArrayList; import java.util.List; import play.db.Database; import play.libs.Json; @@ -58,7 +59,7 @@ public PostgresStatements delete(long id) { @Override public NodeVersion retrieveFromDatabase(long id) throws GroundException { String sql = String.format(SqlConstants.SELECT_STAR_BY_ID, "node_version", id); - JsonNode json = Json.parse(PostgresUtils.executeQueryToJson(dbSource, sql)); + JsonNode json = Json.parse(PostgresUtils.executeQueryToJson(this.dbSource, sql)); if (json.size() == 0) { throw new GroundException(ExceptionType.VERSION_NOT_FOUND, this.getType().getSimpleName(), String.format("%d", id)); @@ -69,4 +70,15 @@ public NodeVersion retrieveFromDatabase(long id) throws GroundException { return new NodeVersion(id, richVersion, nodeVersion); } + + @Override + public List retrieveAdjacentLineageEdgeVersion(long startId) throws GroundException { + String sql = String.format(SqlConstants.SELECT_NODE_VERSION_ADJACENT_LINEAGE, startId); + JsonNode json = Json.parse(PostgresUtils.executeQueryToJson(this.dbSource, sql)); + + List result = new ArrayList<>(); + json.forEach(x -> result.add(x.get("id").asLong())); + + return result; + } } diff --git a/modules/postgres/conf/routes b/modules/postgres/conf/routes index 1f94267f..ef6c7246 100644 --- a/modules/postgres/conf/routes +++ b/modules/postgres/conf/routes @@ -3,56 +3,57 @@ # ~~~~ # An example controller showing a sample home page -GET / edu.berkeley.ground.postgres.controllers.HomeController.index +GET / edu.berkeley.ground.postgres.controllers.HomeController.index # Map static resources from the /public folder to the /assets URL path -GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset) +GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset) # edge endpoints -POST /edges edu.berkeley.ground.postgres.controllers.EdgeController.addEdge() -GET /edges/:sourceKey edu.berkeley.ground.postgres.controllers.EdgeController.getEdge(sourceKey: String) -POST /versions/edges edu.berkeley.ground.postgres.controllers.EdgeController.addEdgeVersion() -GET /versions/edges/:id edu.berkeley.ground.postgres.controllers.EdgeController.getEdgeVersion(id: Long) -GET /edges/:sourceKey/latest edu.berkeley.ground.postgres.controllers.EdgeController.getLatest(sourceKey: String) -GET /edges/:sourceKey/history edu.berkeley.ground.postgres.controllers.EdgeController.getHistory(sourceKey: String) +POST /edges edu.berkeley.ground.postgres.controllers.EdgeController.addEdge() +GET /edges/:sourceKey edu.berkeley.ground.postgres.controllers.EdgeController.getEdge(sourceKey: String) +POST /versions/edges edu.berkeley.ground.postgres.controllers.EdgeController.addEdgeVersion() +GET /versions/edges/:id edu.berkeley.ground.postgres.controllers.EdgeController.getEdgeVersion(id: Long) +GET /edges/:sourceKey/latest edu.berkeley.ground.postgres.controllers.EdgeController.getLatest(sourceKey: String) +GET /edges/:sourceKey/history edu.berkeley.ground.postgres.controllers.EdgeController.getHistory(sourceKey: String) # graph endpoints -POST /graphs edu.berkeley.ground.postgres.controllers.GraphController.addGraph() -GET /graphs/:sourceKey edu.berkeley.ground.postgres.controllers.GraphController.getGraph(sourceKey: String) -POST /versions/graphs edu.berkeley.ground.postgres.controllers.GraphController.addGraphVersion() -GET /versions/graphs/:id edu.berkeley.ground.postgres.controllers.GraphController.getGraphVersion(id: Long) -GET /graphs/:sourceKey/latest edu.berkeley.ground.postgres.controllers.GraphController.getLatest(sourceKey: String) -GET /graphs/:sourceKey/history edu.berkeley.ground.postgres.controllers.GraphController.getHistory(sourceKey: String) +POST /graphs edu.berkeley.ground.postgres.controllers.GraphController.addGraph() +GET /graphs/:sourceKey edu.berkeley.ground.postgres.controllers.GraphController.getGraph(sourceKey: String) +POST /versions/graphs edu.berkeley.ground.postgres.controllers.GraphController.addGraphVersion() +GET /versions/graphs/:id edu.berkeley.ground.postgres.controllers.GraphController.getGraphVersion(id: Long) +GET /graphs/:sourceKey/latest edu.berkeley.ground.postgres.controllers.GraphController.getLatest(sourceKey: String) +GET /graphs/:sourceKey/history edu.berkeley.ground.postgres.controllers.GraphController.getHistory(sourceKey: String) # lineage edge endpoints -POST /lineage_edges edu.berkeley.ground.postgres.controllers.LineageEdgeController.createLineageEdge() -GET /lineage_edges/:sourceKey edu.berkeley.ground.postgres.controllers.LineageEdgeController.getLineageEdge(sourceKey: String) -POST /versions/lineage_edges edu.berkeley.ground.postgres.controllers.LineageEdgeController.createLineageEdgeVersion() -GET /versions/lineage_edges/:id edu.berkeley.ground.postgres.controllers.LineageEdgeController.getLineageEdgeVersion(id: Long) -GET /lineage_edges/:sourceKey/latest edu.berkeley.ground.postgres.controllers.LineageEdgeController.getLatest(sourceKey: String) -GET /lineage_edges/:sourceKey/history edu.berkeley.ground.postgres.controllers.LineageEdgeController.getHistory(sourceKey: String) +POST /lineage_edges edu.berkeley.ground.postgres.controllers.LineageEdgeController.createLineageEdge() +GET /lineage_edges/:sourceKey edu.berkeley.ground.postgres.controllers.LineageEdgeController.getLineageEdge(sourceKey: String) +POST /versions/lineage_edges edu.berkeley.ground.postgres.controllers.LineageEdgeController.createLineageEdgeVersion() +GET /versions/lineage_edges/:id edu.berkeley.ground.postgres.controllers.LineageEdgeController.getLineageEdgeVersion(id: Long) +GET /lineage_edges/:sourceKey/latest edu.berkeley.ground.postgres.controllers.LineageEdgeController.getLatest(sourceKey: String) +GET /lineage_edges/:sourceKey/history edu.berkeley.ground.postgres.controllers.LineageEdgeController.getHistory(sourceKey: String) # lineage graph endpoints -POST /lineage_graphs edu.berkeley.ground.postgres.controllers.LineageGraphController.createLineageGraph() -GET /lineage_graphs/:sourceKey edu.berkeley.ground.postgres.controllers.LineageGraphController.getLineageGraph(sourceKey: String) -POST /versions/lineage_graphs edu.berkeley.ground.postgres.controllers.LineageGraphController.createLineageGraphVersion() -GET /versions/lineage_graphs/:id edu.berkeley.ground.postgres.controllers.LineageGraphController.getLineageGraphVersion(id: Long) -GET /lineage_graphs/:sourceKey/latest edu.berkeley.ground.postgres.controllers.LineageGraphController.getLatest(sourceKey: String) -GET /lineage_graphs/:sourceKey/history edu.berkeley.ground.postgres.controllers.LineageGraphController.getHistory(sourceKey: String) +POST /lineage_graphs edu.berkeley.ground.postgres.controllers.LineageGraphController.createLineageGraph() +GET /lineage_graphs/:sourceKey edu.berkeley.ground.postgres.controllers.LineageGraphController.getLineageGraph(sourceKey: String) +POST /versions/lineage_graphs edu.berkeley.ground.postgres.controllers.LineageGraphController.createLineageGraphVersion() +GET /versions/lineage_graphs/:id edu.berkeley.ground.postgres.controllers.LineageGraphController.getLineageGraphVersion(id: Long) +GET /lineage_graphs/:sourceKey/latest edu.berkeley.ground.postgres.controllers.LineageGraphController.getLatest(sourceKey: String) +GET /lineage_graphs/:sourceKey/history edu.berkeley.ground.postgres.controllers.LineageGraphController.getHistory(sourceKey: String) # node endpoints -POST /nodes edu.berkeley.ground.postgres.controllers.NodeController.addNode() -GET /nodes/:sourceKey edu.berkeley.ground.postgres.controllers.NodeController.getNode(sourceKey: String) -POST /versions/nodes edu.berkeley.ground.postgres.controllers.NodeController.addNodeVersion() -GET /versions/nodes/:id edu.berkeley.ground.postgres.controllers.NodeController.getNodeVersion(id: Long) -GET /nodes/:sourceKey/latest edu.berkeley.ground.postgres.controllers.NodeController.getLatest(sourceKey: String) -GET /nodes/:sourceKey/history edu.berkeley.ground.postgres.controllers.NodeController.getHistory(sourceKey: String) +POST /nodes edu.berkeley.ground.postgres.controllers.NodeController.addNode() +GET /nodes/:sourceKey edu.berkeley.ground.postgres.controllers.NodeController.getNode(sourceKey: String) +POST /versions/nodes edu.berkeley.ground.postgres.controllers.NodeController.addNodeVersion() +GET /versions/nodes/:id edu.berkeley.ground.postgres.controllers.NodeController.getNodeVersion(id: Long) +GET /nodes/:sourceKey/latest edu.berkeley.ground.postgres.controllers.NodeController.getLatest(sourceKey: String) +GET /nodes/:sourceKey/history edu.berkeley.ground.postgres.controllers.NodeController.getHistory(sourceKey: String) +GET /versions/nodes/adjacent/lineage/:id edu.berkeley.ground.postgres.controllers.NodeController.getAdjacentLineage(id: Long) # structure endpoints -POST /structures edu.berkeley.ground.postgres.controllers.StructureController.addStructure() -GET /structures/:sourceKey edu.berkeley.ground.postgres.controllers.StructureController.getStructure(sourceKey: String) -POST /versions/structures edu.berkeley.ground.postgres.controllers.StructureController.addStructureVersion() -GET /versions/structures/:id edu.berkeley.ground.postgres.controllers.StructureController.getStructureVersion(id: Long) -GET /structures/:sourceKey/latest edu.berkeley.ground.postgres.controllers.StructureController.getLatest(sourceKey: String) -GET /structures/:sourceKey/history edu.berkeley.ground.postgres.controllers.StructureController.getHistory(sourceKey: String) +POST /structures edu.berkeley.ground.postgres.controllers.StructureController.addStructure() +GET /structures/:sourceKey edu.berkeley.ground.postgres.controllers.StructureController.getStructure(sourceKey: String) +POST /versions/structures edu.berkeley.ground.postgres.controllers.StructureController.addStructureVersion() +GET /versions/structures/:id edu.berkeley.ground.postgres.controllers.StructureController.getStructureVersion(id: Long) +GET /structures/:sourceKey/latest edu.berkeley.ground.postgres.controllers.StructureController.getLatest(sourceKey: String) +GET /structures/:sourceKey/history edu.berkeley.ground.postgres.controllers.StructureController.getHistory(sourceKey: String)