From: <ba...@us...> - 2010-07-13 15:35:47
|
Revision: 3437 http://obo.svn.sourceforge.net/obo/?rev=3437&view=rev Author: balhoff Date: 2010-07-13 15:35:40 +0000 (Tue, 13 Jul 2010) Log Message: ----------- Bug fixes to gene annotations query. Modified Paths: -------------- OBD-WS/trunk/src/org/phenoscape/obd/query/GeneAnnotationsQueryBuilder.java OBD-WS/trunk/src/org/phenoscape/obd/query/PhenoscapeDataStore.java OBD-WS/trunk/src/org/phenoscape/ws/resource/GeneAnnotationsResource.java Modified: OBD-WS/trunk/src/org/phenoscape/obd/query/GeneAnnotationsQueryBuilder.java =================================================================== --- OBD-WS/trunk/src/org/phenoscape/obd/query/GeneAnnotationsQueryBuilder.java 2010-07-13 02:17:56 UTC (rev 3436) +++ OBD-WS/trunk/src/org/phenoscape/obd/query/GeneAnnotationsQueryBuilder.java 2010-07-13 15:35:40 UTC (rev 3437) @@ -10,6 +10,7 @@ public class GeneAnnotationsQueryBuilder extends QueryBuilder { private final GeneAnnotationsQueryConfig config; + private final boolean totalOnly; private static final Map<SORT_COLUMN, String> COLUMNS = new HashMap<SORT_COLUMN, String>(); static { COLUMNS.put(SORT_COLUMN.GENE, "distinct_gene_annotation.gene_label"); @@ -18,25 +19,27 @@ COLUMNS.put(SORT_COLUMN.RELATED_ENTITY, "distinct_gene_annotation.related_entity_label"); } - public GeneAnnotationsQueryBuilder(GeneAnnotationsQueryConfig config) { + public GeneAnnotationsQueryBuilder(GeneAnnotationsQueryConfig config, boolean totalOnly) { this.config = config; + this.totalOnly = totalOnly; } @Override protected void fillStatement(PreparedStatement statement) throws SQLException { //TODO - // maybe first shouldn't be a string parameter? - statement.setString(1, COLUMNS.get(this.config.getSortColumn())); - statement.setInt(2, this.config.getLimit()); - statement.setInt(3, this.config.getIndex()); + if (!this.totalOnly) { + statement.setInt(1, this.config.getLimit()); + statement.setInt(2, this.config.getIndex()); + } } @Override protected String getQuery() { //TODO - return "SELECT distinct_gene_annotation.* FROM distinct_gene_annotation " + - "ORDER BY ? " + - "LIMIT ? OFFSET ?"; + final String select = this.totalOnly ? "SELECT count(distinct_gene_annotation.*) " : "SELECT distinct_gene_annotation.* "; + final String ending = this.totalOnly ? "" : "ORDER BY " + COLUMNS.get(this.config.getSortColumn()) + " " + "LIMIT ? OFFSET ?"; + return select + "FROM distinct_gene_annotation " + ending; + } } Modified: OBD-WS/trunk/src/org/phenoscape/obd/query/PhenoscapeDataStore.java =================================================================== --- OBD-WS/trunk/src/org/phenoscape/obd/query/PhenoscapeDataStore.java 2010-07-13 02:17:56 UTC (rev 3436) +++ OBD-WS/trunk/src/org/phenoscape/obd/query/PhenoscapeDataStore.java 2010-07-13 15:35:40 UTC (rev 3437) @@ -266,7 +266,7 @@ } public List<GeneAnnotation> getGeneAnnotations(GeneAnnotationsQueryConfig config) throws SQLException { - final QueryBuilder query = new GeneAnnotationsQueryBuilder(config); + final QueryBuilder query = new GeneAnnotationsQueryBuilder(config, false); return (new QueryExecutor<List<GeneAnnotation>>(this.dataSource, query) { @Override public List<GeneAnnotation> processResult(ResultSet result) throws SQLException { @@ -291,10 +291,10 @@ return annotation; } - public int getCountOfGeneAnnotations() throws SQLException { + public int getCountOfGeneAnnotations(GeneAnnotationsQueryConfig config) throws SQLException { // returning distinct count for now because we aren't preserving useful information about different // annotation statements in the data loader - final QueryBuilder query = new SimpleQuery("SELECT count(*) FROM distinct_gene_annotation"); + final QueryBuilder query = new GeneAnnotationsQueryBuilder(config, true); final QueryExecutor<Integer> queryExecutor = new QueryExecutor<Integer>(this.dataSource, query) { @Override public Integer processResult(ResultSet result) throws SQLException { Modified: OBD-WS/trunk/src/org/phenoscape/ws/resource/GeneAnnotationsResource.java =================================================================== --- OBD-WS/trunk/src/org/phenoscape/ws/resource/GeneAnnotationsResource.java 2010-07-13 02:17:56 UTC (rev 3436) +++ OBD-WS/trunk/src/org/phenoscape/ws/resource/GeneAnnotationsResource.java 2010-07-13 15:35:40 UTC (rev 3437) @@ -35,7 +35,7 @@ * The maximum number of annotations to pull out of the database in one query. * TODO: need to investigate ideal value for this to maximize performance with acceptable memory usage */ - private static final int QUERY_LIMIT = 500; + private static final int QUERY_LIMIT = 1000; private JSONObject query = new JSONObject(); private String sortColumn = "gene"; private int limit; @@ -70,15 +70,21 @@ public Representation getJSONRepresentation() { try { final Iterator<JSONObject> annotations = this.translateToJSON(this.queryForAnnotations()); - return new StreamableJSONRepresentation(annotations, "annotations"); + final int total = this.queryForAnnotationsCount(this.createInitialQueryConfig()); + final JSONObject otherValues = new JSONObject(); + otherValues.put("total", total); + return new StreamableJSONRepresentation(annotations, "annotations", otherValues); } catch (JSONException e) { - this.setStatus(Status.SERVER_ERROR_INTERNAL, "Error creating JSON object"); + this.log().error("Error creating JSON object", e); + this.setStatus(Status.SERVER_ERROR_INTERNAL, e); return null; } catch (SQLException e) { - this.setStatus(Status.SERVER_ERROR_INTERNAL, "Error querying database"); + this.log().error("Error querying database", e); + this.setStatus(Status.SERVER_ERROR_INTERNAL, e); return null; } catch (QueryException e) { - this.setStatus(Status.SERVER_ERROR_INTERNAL, "Error querying database"); + this.log().error("Error querying database", e); + this.setStatus(Status.SERVER_ERROR_INTERNAL, e); return null; } } @@ -89,13 +95,16 @@ final Iterator<String> annotations = this.translateToText(this.queryForAnnotations()); return new StreamableTextRepresentation(annotations, MediaType.TEXT_TSV); } catch (JSONException e) { - this.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, "Invalid annotation query"); + this.log().error("Invalid annotation query", e); + this.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, e); return null; } catch (SQLException e) { - this.setStatus(Status.SERVER_ERROR_INTERNAL, "Error querying database"); + this.log().error("Error querying database", e); + this.setStatus(Status.SERVER_ERROR_INTERNAL, e); return null; } catch (QueryException e) { - this.setStatus(Status.SERVER_ERROR_INTERNAL, "Error querying database"); + this.log().error("Error querying database", e); + this.setStatus(Status.SERVER_ERROR_INTERNAL, e); return null; } } @@ -217,17 +226,29 @@ @Override public void remove() {} private boolean needMore() { - return this.gotten < limit; + if (limit > 0) { + return this.gotten < limit; + } else { + return true; + } } }; } + private int queryForAnnotationsCount(GeneAnnotationsQueryConfig config) throws SQLException { + return this.getDataStore().getCountOfGeneAnnotations(config); + } + private GeneAnnotationsQueryConfig createInitialQueryConfig() throws JSONException, QueryException { final GeneAnnotationsQueryConfig config = new GeneAnnotationsQueryConfig(); config.setIndex(this.index); config.setSortColumn(COLUMNS.get(this.sortColumn)); config.setSortDescending(this.sortDescending); - config.setLimit(Math.min(this.limit, QUERY_LIMIT)); + if (this.limit > 0) { + config.setLimit(Math.min(this.limit, QUERY_LIMIT)); + } else { + config.setLimit(QUERY_LIMIT); + } if (this.query.has("gene")) { for (JSONObject gene : this.toIterable(this.query.getJSONArray("gene"))) { config.addGeneID(gene.getString("id")); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |