From: <tho...@us...> - 2011-06-20 14:42:01
|
Revision: 4742 http://bigdata.svn.sourceforge.net/bigdata/?rev=4742&view=rev Author: thompsonbry Date: 2011-06-20 14:41:55 +0000 (Mon, 20 Jun 2011) Log Message: ----------- Rewrote the QueryType class to parse the query and examine the type of the ASTQuery node. This fixes the bug, but now we are parsing the query yet one more time so this introduces additional per-query overhead which still needs to be resolved. See https://sourceforge.net/apps/trac/bigdata/ticket/336 Modified Paths: -------------- branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/QueryType.java branches/QUADS_QUERY_BRANCH/bigdata-sails/src/test/com/bigdata/rdf/sail/TestQueryType.java Modified: branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/QueryType.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/QueryType.java 2011-06-20 14:14:54 UTC (rev 4741) +++ branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/QueryType.java 2011-06-20 14:41:55 UTC (rev 4742) @@ -1,90 +1,27 @@ package com.bigdata.rdf.sail; -import java.util.Arrays; +import org.openrdf.query.parser.sparql.ast.ASTAskQuery; +import org.openrdf.query.parser.sparql.ast.ASTConstructQuery; +import org.openrdf.query.parser.sparql.ast.ASTDescribeQuery; +import org.openrdf.query.parser.sparql.ast.ASTQuery; +import org.openrdf.query.parser.sparql.ast.ASTQueryContainer; +import org.openrdf.query.parser.sparql.ast.ASTSelectQuery; +import org.openrdf.query.parser.sparql.ast.ParseException; +import org.openrdf.query.parser.sparql.ast.SyntaxTreeBuilder; +import org.openrdf.query.parser.sparql.ast.TokenMgrError; /** * Helper class to figure out the type of a query. */ public enum QueryType { - ASK(0), DESCRIBE(1), CONSTRUCT(2), SELECT(3); + ASK, DESCRIBE, CONSTRUCT, SELECT; - private final int order; - - private QueryType(final int order) { + private QueryType() { - this.order = order; - } - private static QueryType getQueryType(final int order) { - switch (order) { - case 0: - return ASK; - case 1: - return DESCRIBE; - case 2: - return CONSTRUCT; - case 3: - return SELECT; - default: - throw new IllegalArgumentException("order=" + order); - } - } - /** - * Used to note the offset at which a keyword was found. - */ - static private class P implements Comparable<QueryType.P> { - - final int offset; - - final QueryType queryType; - - public P(final int offset, final QueryType queryType) { - this.offset = offset; - this.queryType = queryType; - } - - /** Sort into ascending offset. */ - public int compareTo(final QueryType.P o) { - - return offset - o.offset; - - } - - public int hashCode() { - - return offset; - - } - - public boolean equals(final Object o) { - - if (this == o) - return true; - - if (o instanceof P) { - - final P t = (P) o; - - return this.offset == t.offset && this.queryType == t.queryType; - - } - - return false; - - } - - public String toString() { - - return "{offset=" + offset + ",type=" + queryType + "}"; - - } - - } - - /** * Hack returns the query type based on the first occurrence of the * keyword for any known query type in the query. * @@ -94,44 +31,22 @@ * @return The query type. */ static public QueryType fromQuery(final String queryStr) { - - // force all to lower case. - final String s = queryStr.toUpperCase(); - - final int ntypes = QueryType.values().length; - - final QueryType.P[] p = new QueryType.P[ntypes]; - - int nmatch = 0; - for (int i = 0; i < ntypes; i++) { - - final QueryType queryType = getQueryType(i); - - final int offset = s.indexOf(queryType.toString()); - - if (offset == -1) - continue; - - p[nmatch++] = new P(offset, queryType); - + + try { + final ASTQueryContainer queryContainer = SyntaxTreeBuilder + .parseQuery(queryStr); + final ASTQuery query = queryContainer.getQuery(); + if(query instanceof ASTSelectQuery) return QueryType.SELECT; + if(query instanceof ASTDescribeQuery) return QueryType.DESCRIBE; + if(query instanceof ASTConstructQuery) return QueryType.CONSTRUCT; + if(query instanceof ASTAskQuery) return QueryType.ASK; + throw new RuntimeException(queryContainer.toString()); + } catch (TokenMgrError ex) { + throw new RuntimeException(ex); + } catch (ParseException ex) { + throw new RuntimeException(ex); } - if (nmatch == 0) { - - throw new RuntimeException( - "Could not determine the query type: " + queryStr); - - } - - Arrays.sort(p, 0/* fromIndex */, nmatch/* toIndex */); - - final QueryType.P tmp = p[0]; - - // System.out.println("QueryType: offset=" + tmp.offset + ", type=" - // + tmp.queryType); - - return tmp.queryType; - } - -} \ No newline at end of file + +} Modified: branches/QUADS_QUERY_BRANCH/bigdata-sails/src/test/com/bigdata/rdf/sail/TestQueryType.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-sails/src/test/com/bigdata/rdf/sail/TestQueryType.java 2011-06-20 14:14:54 UTC (rev 4741) +++ branches/QUADS_QUERY_BRANCH/bigdata-sails/src/test/com/bigdata/rdf/sail/TestQueryType.java 2011-06-20 14:41:55 UTC (rev 4742) @@ -71,6 +71,17 @@ } + public void test_select_with_ask_in_PREFIX() { + + final String s = + "prefix bd: <"+BD.NAMESPACE+"> " + + "prefix foo: <http://www.bigdata.com/test/ask/ns> " + + "select ?p ?o where {<http://blablabla.com/ask_something> ?p ?o}"; + + assertEquals(QueryType.SELECT, QueryType.fromQuery(s)); + + } + public void test_describe() { final String s = This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |