You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@accumulo.apache.org by bi...@apache.org on 2011/12/05 21:05:51 UTC
svn commit: r1210600 [15/16] - in
/incubator/accumulo/trunk/contrib/accumulo_sample: ./ ingest/
ingest/src/main/java/aggregator/ ingest/src/main/java/ingest/
ingest/src/main/java/iterator/ ingest/src/main/java/normalizer/
ingest/src/main/java/protobuf/...
Modified: incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/parser/TreeBuilder.java
URL: http://svn.apache.org/viewvc/incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/parser/TreeBuilder.java?rev=1210600&r1=1210599&r2=1210600&view=diff
==============================================================================
--- incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/parser/TreeBuilder.java (original)
+++ incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/parser/TreeBuilder.java Mon Dec 5 20:05:49 2011
@@ -1,19 +1,19 @@
/*
-* Licensed to the Apache Software Foundation (ASF) under one or more
-* contributor license agreements. See the NOTICE file distributed with
-* this work for additional information regarding copyright ownership.
-* The ASF licenses this file to You 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
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* 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.
-*/
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 parser;
import java.io.StringReader;
@@ -81,597 +81,595 @@ import parser.QueryParser.TermResult;
import com.google.common.collect.Multimap;
/**
- * Class that parses the query and returns a tree of TreeNode's. This class
- * rolls up clauses that are below like conjunctions (AND, OR) for the purposes
- * of creating intersecting iterators.
+ * Class that parses the query and returns a tree of TreeNode's. This class rolls up clauses that are below like conjunctions (AND, OR) for the purposes of
+ * creating intersecting iterators.
*
*/
public class TreeBuilder implements ParserVisitor {
-
- class RootNode extends JexlNode {
-
- public RootNode(int id) {
- super(id);
- }
-
- public RootNode(Parser p, int id) {
- super(p, id);
- }
-
- }
-
- private TreeNode rootNode = null;
- private TreeNode currentNode = null;
- private boolean currentlyInCheckChildren = false;
-
- public TreeBuilder(String query) throws ParseException {
- Parser p = new Parser(new StringReader(";"));
- ASTJexlScript script = p.parse(new StringReader(query), null);
- //Check to see if the child node is an AND or OR. If not, then
- //there must be just a single value in the query expression
- rootNode = new TreeNode();
- rootNode.setType(RootNode.class);
- currentNode = rootNode;
- EvaluationContext ctx = new EvaluationContext();
- script.childrenAccept(this, ctx);
- }
-
- public TreeBuilder(ASTJexlScript script) {
- //Check to see if the child node is an AND or OR. If not, then
- //there must be just a single value in the query expression
- rootNode = new TreeNode();
- rootNode.setType(RootNode.class);
- currentNode = rootNode;
- EvaluationContext ctx = new EvaluationContext();
- script.childrenAccept(this, ctx);
- }
-
- public TreeNode getRootNode() {
- return this.rootNode;
- }
-
- public Object visit(SimpleNode node, Object data) {
- return null;
- }
-
- public Object visit(ASTJexlScript node, Object data) {
- return null;
- }
-
- public Object visit(ASTBlock node, Object data) {
- return null;
- }
-
- public Object visit(ASTAmbiguous node, Object data) {
- return null;
- }
-
- public Object visit(ASTIfStatement node, Object data) {
- return null;
- }
-
- public Object visit(ASTWhileStatement node, Object data) {
- return null;
- }
-
- public Object visit(ASTForeachStatement node, Object data) {
- return null;
- }
-
- public Object visit(ASTAssignment node, Object data) {
- return null;
- }
-
- public Object visit(ASTTernaryNode node, Object data) {
- return null;
- }
-
- /**
- * @param node
- * @param failClass
- * @return false if any of the nodes equals the fail class or contain a NOT in the subtree
- */
- private boolean nodeCheck(JexlNode node, Class<?> failClass) {
- if (node.getClass().equals(failClass) || node.getClass().equals(ASTNotNode.class))
- return false;
- else {
- for (int i = 0; i < node.jjtGetNumChildren(); i++) {
- if (!nodeCheck(node.jjtGetChild(i), failClass))
- return false;
- }
- }
- return true;
- }
-
- /**
- * Checks to see if all of the child nodes are of the same type (AND/OR)
- * and if so then aggregates all of the child terms. If not returns null.
- *
- * @param parent
- * @param parentNode
- * @return Map of field names to query terms or null
- */
- private Multimap<String,QueryTerm> checkChildren(JexlNode parent, EvaluationContext ctx) {
- //If the current node is an AND, then make sure that there is no
- //OR descendant node, and vice versa. If this is true, then we call
- //roll up all of the descendent values.
- this.currentlyInCheckChildren = true;
- Multimap<String,QueryTerm> rolledUpTerms = null;
- boolean result = false;
- if (parent.getClass().equals(ASTOrNode.class)) {
- for (int i = 0; i < parent.jjtGetNumChildren(); i++) {
- result = nodeCheck(parent.jjtGetChild(i), ASTAndNode.class);
- if (!result)
- break;
- }
- } else {
- for (int i = 0; i < parent.jjtGetNumChildren(); i++) {
- result = nodeCheck(parent.jjtGetChild(i), ASTOrNode.class);
- if (!result)
- break;
- }
- }
- if (result) {
- //Set current node to a fake node and
- //roll up the children from this node using the visitor pattern.
- TreeNode rollupFakeNode = new TreeNode();
- TreeNode previous = this.currentNode;
- this.currentNode = rollupFakeNode;
- //Run the visitor with the fake node.
- parent.childrenAccept(this, ctx);
- //Get the terms from the fake node
- rolledUpTerms = this.currentNode.getTerms();
- //Reset the current node pointer
- this.currentNode = previous;
- }
- this.currentlyInCheckChildren = false;
- return rolledUpTerms;
- }
-
- public Object visit(ASTOrNode node, Object data) {
- boolean previouslyInOrContext = false;
- EvaluationContext ctx = null;
- if (null != data && data instanceof EvaluationContext) {
- ctx = (EvaluationContext) data;
- previouslyInOrContext = ctx.inOrContext;
- } else {
- ctx = new EvaluationContext();
- }
- ctx.inOrContext = true;
- //Are we being called from the checkChildren method? If so, then we
- //are rolling up terms. If not, then we need to call check children.
- if (currentlyInCheckChildren ) {
- //Process both sides of this node.
- node.jjtGetChild(0).jjtAccept(this, data);
- node.jjtGetChild(1).jjtAccept(this, data);
- } else {
- //Create a new OR node under the current node.
- TreeNode orNode = new TreeNode();
- orNode.setType(ASTOrNode.class);
- orNode.setParent(this.currentNode);
- this.currentNode.getChildren().add(orNode);
- Multimap<String,QueryTerm> terms = checkChildren(node, ctx);
- if (terms == null) {
- //Then there was no rollup, set the current node to the orNode
- //and process the children. Be sure to set the current Node to
- //the or node in between calls because we could be processing
- //an AND node below and the current node will have been switched.
- //Process both sides of this node.
- currentNode = orNode;
- node.jjtGetChild(0).jjtAccept(this, data);
- currentNode = orNode;
- node.jjtGetChild(1).jjtAccept(this, data);
- } else {
- //There was a rollup, don't process the children and set the terms
- //on the or node.
- orNode.setTerms(terms);
- }
- }
- //reset the state
- if (null != data && !previouslyInOrContext)
- ctx.inOrContext = false;
- return null;
- }
-
- public Object visit(ASTAndNode node, Object data) {
- boolean previouslyInAndContext = false;
- EvaluationContext ctx = null;
- if (null != data && data instanceof EvaluationContext) {
- ctx = (EvaluationContext) data;
- previouslyInAndContext = ctx.inAndContext;
- } else {
- ctx = new EvaluationContext();
- }
- ctx.inAndContext = true;
- //Are we being called from the checkChildren method? If so, then we
- //are rolling up terms. If not, then we need to call check children.
- if (currentlyInCheckChildren ) {
- //Process both sides of this node.
- node.jjtGetChild(0).jjtAccept(this, data);
- node.jjtGetChild(1).jjtAccept(this, data);
- } else {
- //Create a new And node under the current node.
- TreeNode andNode = new TreeNode();
- andNode.setType(ASTAndNode.class);
- andNode.setParent(this.currentNode);
- this.currentNode.getChildren().add(andNode);
- Multimap<String,QueryTerm> terms = checkChildren(node, ctx);
- if (terms == null) {
- //Then there was no rollup, set the current node to the orNode
- //and process the children. Be sure to set the current Node to
- //the and node in between calls because we could be processing
- //an OR node below and the current node will have been switched.
- //Process both sides of this node.
- currentNode = andNode;
- node.jjtGetChild(0).jjtAccept(this, data);
- currentNode = andNode;
- node.jjtGetChild(1).jjtAccept(this, data);
- } else {
- //There was a rollup, don't process the children and set the terms
- //on the or node.
- andNode.setTerms(terms);
- }
- }
- if (null != data && !previouslyInAndContext)
- ctx.inAndContext = false;
- return null;
- }
-
- public Object visit(ASTBitwiseOrNode node, Object data) {
- return null;
- }
-
- public Object visit(ASTBitwiseXorNode node, Object data) {
- return null;
- }
-
- public Object visit(ASTBitwiseAndNode node, Object data) {
- return null;
- }
-
- public Object visit(ASTEQNode node, Object data) {
- StringBuilder fieldName = new StringBuilder();
- ObjectHolder value = new ObjectHolder();
- //Process both sides of this node.
- Object left = node.jjtGetChild(0).jjtAccept(this, data);
- Object right = node.jjtGetChild(1).jjtAccept(this, data);
- //Ignore functions in the query
- if (left instanceof FunctionResult || right instanceof FunctionResult)
- return null;
- decodeResults(left, right, fieldName, value);
- //We need to check to see if we are in a NOT context. If so,
- //then we need to reverse the negation.
- boolean negated = false;
- if (null != data && data instanceof EvaluationContext) {
- EvaluationContext ctx = (EvaluationContext) data;
- if (ctx.inNotContext)
- negated = !negated;
- }
- QueryTerm term = new QueryTerm(negated, JexlOperatorConstants.getOperator(node.getClass()), value.getObject());
- this.currentNode.getTerms().put(fieldName.toString(), term);
- return null;
- }
-
- public Object visit(ASTNENode node, Object data) {
- StringBuilder fieldName = new StringBuilder();
- ObjectHolder value = new ObjectHolder();
- //Process both sides of this node.
- Object left = node.jjtGetChild(0).jjtAccept(this, data);
- Object right = node.jjtGetChild(1).jjtAccept(this, data);
- //Ignore functions in the query
- if (left instanceof FunctionResult || right instanceof FunctionResult)
- return null;
- decodeResults(left, right, fieldName, value);
- //We need to check to see if we are in a NOT context. If so,
- //then we need to reverse the negation.
- boolean negated = true;
- if (null != data && data instanceof EvaluationContext) {
- EvaluationContext ctx = (EvaluationContext) data;
- if (ctx.inNotContext)
- negated = !negated;
- }
- QueryTerm term = new QueryTerm(negated, JexlOperatorConstants.getOperator(node.getClass()), value.getObject());
- this.currentNode.getTerms().put(fieldName.toString(), term);
- return null;
- }
-
- public Object visit(ASTLTNode node, Object data) {
- StringBuilder fieldName = new StringBuilder();
- ObjectHolder value = new ObjectHolder();
- //Process both sides of this node.
- Object left = node.jjtGetChild(0).jjtAccept(this, data);
- Object right = node.jjtGetChild(1).jjtAccept(this, data);
- //Ignore functions in the query
- if (left instanceof FunctionResult || right instanceof FunctionResult)
- return null;
- decodeResults(left, right, fieldName, value);
- //We need to check to see if we are in a NOT context. If so,
- //then we need to reverse the negation.
- boolean negated = false;
- if (null != data && data instanceof EvaluationContext) {
- EvaluationContext ctx = (EvaluationContext) data;
- if (ctx.inNotContext)
- negated = !negated;
- }
- QueryTerm term = new QueryTerm(negated, JexlOperatorConstants.getOperator(node.getClass()), value.getObject());
- this.currentNode.getTerms().put(fieldName.toString(), term);
- return null;
- }
-
- public Object visit(ASTGTNode node, Object data) {
- StringBuilder fieldName = new StringBuilder();
- ObjectHolder value = new ObjectHolder();
- //Process both sides of this node.
- Object left = node.jjtGetChild(0).jjtAccept(this, data);
- Object right = node.jjtGetChild(1).jjtAccept(this, data);
- //Ignore functions in the query
- if (left instanceof FunctionResult || right instanceof FunctionResult)
- return null;
- decodeResults(left, right, fieldName, value);
- //We need to check to see if we are in a NOT context. If so,
- //then we need to reverse the negation.
- boolean negated = false;
- if (null != data && data instanceof EvaluationContext) {
- EvaluationContext ctx = (EvaluationContext) data;
- if (ctx.inNotContext)
- negated = !negated;
- }
- QueryTerm term = new QueryTerm(negated, JexlOperatorConstants.getOperator(node.getClass()), value.getObject());
- this.currentNode.getTerms().put(fieldName.toString(), term);
- return null;
- }
-
- public Object visit(ASTLENode node, Object data) {
- StringBuilder fieldName = new StringBuilder();
- ObjectHolder value = new ObjectHolder();
- //Process both sides of this node.
- Object left = node.jjtGetChild(0).jjtAccept(this, data);
- Object right = node.jjtGetChild(1).jjtAccept(this, data);
- //Ignore functions in the query
- if (left instanceof FunctionResult || right instanceof FunctionResult)
- return null;
- decodeResults(left, right, fieldName, value);
- //We need to check to see if we are in a NOT context. If so,
- //then we need to reverse the negation.
- boolean negated = false;
- if (null != data && data instanceof EvaluationContext) {
- EvaluationContext ctx = (EvaluationContext) data;
- if (ctx.inNotContext)
- negated = !negated;
- }
- QueryTerm term = new QueryTerm(negated, JexlOperatorConstants.getOperator(node.getClass()), value.getObject());
- this.currentNode.getTerms().put(fieldName.toString(), term);
- return null;
- }
-
- public Object visit(ASTGENode node, Object data) {
- StringBuilder fieldName = new StringBuilder();
- ObjectHolder value = new ObjectHolder();
- //Process both sides of this node.
- Object left = node.jjtGetChild(0).jjtAccept(this, data);
- Object right = node.jjtGetChild(1).jjtAccept(this, data);
- //Ignore functions in the query
- if (left instanceof FunctionResult || right instanceof FunctionResult)
- return null;
- decodeResults(left, right, fieldName, value);
- //We need to check to see if we are in a NOT context. If so,
- //then we need to reverse the negation.
- boolean negated = false;
- if (null != data && data instanceof EvaluationContext) {
- EvaluationContext ctx = (EvaluationContext) data;
- if (ctx.inNotContext)
- negated = !negated;
- }
- QueryTerm term = new QueryTerm(negated, JexlOperatorConstants.getOperator(node.getClass()), value.getObject());
- this.currentNode.getTerms().put(fieldName.toString(), term);
- return null;
- }
-
- public Object visit(ASTERNode node, Object data) {
- StringBuilder fieldName = new StringBuilder();
- ObjectHolder value = new ObjectHolder();
- //Process both sides of this node.
- Object left = node.jjtGetChild(0).jjtAccept(this, data);
- Object right = node.jjtGetChild(1).jjtAccept(this, data);
- //Ignore functions in the query
- if (left instanceof FunctionResult || right instanceof FunctionResult)
- return null;
- decodeResults(left, right, fieldName, value);
- //We need to check to see if we are in a NOT context. If so,
- //then we need to reverse the negation.
- boolean negated = false;
- if (null != data && data instanceof EvaluationContext) {
- EvaluationContext ctx = (EvaluationContext) data;
- if (ctx.inNotContext)
- negated = !negated;
- }
- QueryTerm term = new QueryTerm(negated, JexlOperatorConstants.getOperator(node.getClass()), value.getObject());
- this.currentNode.getTerms().put(fieldName.toString(), term);
- return null;
- }
-
- public Object visit(ASTNRNode node, Object data) {
- StringBuilder fieldName = new StringBuilder();
- ObjectHolder value = new ObjectHolder();
- //Process both sides of this node.
- Object left = node.jjtGetChild(0).jjtAccept(this, data);
- Object right = node.jjtGetChild(1).jjtAccept(this, data);
- //Ignore functions in the query
- if (left instanceof FunctionResult || right instanceof FunctionResult)
- return null;
- decodeResults(left, right, fieldName, value);
- //We need to check to see if we are in a NOT context. If so,
- //then we need to reverse the negation.
- boolean negated = true;
- if (null != data && data instanceof EvaluationContext) {
- EvaluationContext ctx = (EvaluationContext) data;
- if (ctx.inNotContext)
- negated = !negated;
- }
- QueryTerm term = new QueryTerm(negated, "!~", value.getObject());
- this.currentNode.getTerms().put(fieldName.toString(), term);
- return null;
- }
-
- public Object visit(ASTAdditiveNode node, Object data) {
- return null;
- }
-
- public Object visit(ASTAdditiveOperator node, Object data) {
- return null;
- }
-
- public Object visit(ASTMulNode node, Object data) {
- return null;
- }
-
- public Object visit(ASTDivNode node, Object data) {
- return null;
- }
-
- public Object visit(ASTModNode node, Object data) {
- return null;
- }
-
- public Object visit(ASTUnaryMinusNode node, Object data) {
- return null;
- }
-
- public Object visit(ASTBitwiseComplNode node, Object data) {
- return null;
- }
-
- public Object visit(ASTNotNode node, Object data) {
- boolean previouslyInNotContext = false;
- EvaluationContext ctx = null;
- if (null != data && data instanceof EvaluationContext) {
- ctx = (EvaluationContext) data;
- previouslyInNotContext = ctx.inNotContext;
- } else {
- ctx = new EvaluationContext();
- }
- ctx.inNotContext = true;
- //Create a new node in the tree to represent the NOT
- //Create a new And node under the current node.
- TreeNode notNode = new TreeNode();
- notNode.setType(ASTNotNode.class);
- notNode.setParent(this.currentNode);
- this.currentNode.getChildren().add(notNode);
- this.currentNode = notNode;
- //Process both sides of this node.
- node.jjtGetChild(0).jjtAccept(this, ctx);
- //reset the state
- if (null != data && !previouslyInNotContext)
- ctx.inNotContext = false;
- return null;
- }
-
- public Object visit(ASTIdentifier node, Object data) {
- return new TermResult(node.image);
- }
-
- public Object visit(ASTNullLiteral node, Object data) {
- return new LiteralResult(node.image);
- }
-
- public Object visit(ASTTrueNode node, Object data) {
- return new LiteralResult(node.image);
- }
-
- public Object visit(ASTFalseNode node, Object data) {
- return new LiteralResult(node.image);
- }
-
- public Object visit(ASTIntegerLiteral node, Object data) {
- return new LiteralResult(node.image);
- }
-
- public Object visit(ASTFloatLiteral node, Object data) {
- return new LiteralResult(node.image);
- }
-
- public Object visit(ASTStringLiteral node, Object data) {
- return new LiteralResult("'"+node.image+"'");
- }
-
- public Object visit(ASTArrayLiteral node, Object data) {
- return null;
- }
-
- public Object visit(ASTMapLiteral node, Object data) {
- return null;
- }
-
- public Object visit(ASTMapEntry node, Object data) {
- return null;
- }
-
- public Object visit(ASTEmptyFunction node, Object data) {
- return null;
- }
-
- public Object visit(ASTSizeFunction node, Object data) {
- return null;
- }
-
- public Object visit(ASTFunctionNode node, Object data) {
- // objectNode 0 is the prefix
- // objectNode 1 is the identifier , the others are parameters.
- // process the remaining arguments
- FunctionResult fr = new FunctionResult();
- int argc = node.jjtGetNumChildren() - 2;
- for (int i = 0; i < argc; i++) {
- //Process both sides of this node.
- Object result = node.jjtGetChild(i + 2).jjtAccept(this, data);
- if (result instanceof TermResult) {
- TermResult tr = (TermResult) result;
- fr.getTerms().add(tr);
- }
- }
- return fr;
- }
-
- public Object visit(ASTMethodNode node, Object data) {
- return null;
- }
-
- public Object visit(ASTSizeMethod node, Object data) {
- return null;
- }
-
- public Object visit(ASTConstructorNode node, Object data) {
- return null;
- }
-
- public Object visit(ASTArrayAccess node, Object data) {
- return null;
- }
-
- public Object visit(ASTReference node, Object data) {
- return node.jjtGetChild(0).jjtAccept(this, data);
- }
-
- private void decodeResults(Object left, Object right, StringBuilder fieldName, ObjectHolder holder) {
- if (left instanceof TermResult) {
- TermResult tr = (TermResult) left;
- fieldName.append((String) tr.value);
- //Then the right has to be the value
- if (right instanceof LiteralResult) {
- holder.setObject(((LiteralResult) right).value);
- } else {
- throw new IllegalArgumentException("Object mismatch");
- }
- } else if (right instanceof TermResult) {
- TermResult tr = (TermResult) right;
- fieldName.append((String) tr.value);
- if (left instanceof LiteralResult) {
- holder.setObject(((LiteralResult) left).value);
- } else {
- throw new IllegalArgumentException("Object mismatch");
- }
-
- } else {
- throw new IllegalArgumentException("No Term specified in query");
- }
- }
+
+ class RootNode extends JexlNode {
+
+ public RootNode(int id) {
+ super(id);
+ }
+
+ public RootNode(Parser p, int id) {
+ super(p, id);
+ }
+
+ }
+
+ private TreeNode rootNode = null;
+ private TreeNode currentNode = null;
+ private boolean currentlyInCheckChildren = false;
+
+ public TreeBuilder(String query) throws ParseException {
+ Parser p = new Parser(new StringReader(";"));
+ ASTJexlScript script = p.parse(new StringReader(query), null);
+ // Check to see if the child node is an AND or OR. If not, then
+ // there must be just a single value in the query expression
+ rootNode = new TreeNode();
+ rootNode.setType(RootNode.class);
+ currentNode = rootNode;
+ EvaluationContext ctx = new EvaluationContext();
+ script.childrenAccept(this, ctx);
+ }
+
+ public TreeBuilder(ASTJexlScript script) {
+ // Check to see if the child node is an AND or OR. If not, then
+ // there must be just a single value in the query expression
+ rootNode = new TreeNode();
+ rootNode.setType(RootNode.class);
+ currentNode = rootNode;
+ EvaluationContext ctx = new EvaluationContext();
+ script.childrenAccept(this, ctx);
+ }
+
+ public TreeNode getRootNode() {
+ return this.rootNode;
+ }
+
+ public Object visit(SimpleNode node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTJexlScript node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTBlock node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTAmbiguous node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTIfStatement node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTWhileStatement node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTForeachStatement node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTAssignment node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTTernaryNode node, Object data) {
+ return null;
+ }
+
+ /**
+ * @param node
+ * @param failClass
+ * @return false if any of the nodes equals the fail class or contain a NOT in the subtree
+ */
+ private boolean nodeCheck(JexlNode node, Class<?> failClass) {
+ if (node.getClass().equals(failClass) || node.getClass().equals(ASTNotNode.class))
+ return false;
+ else {
+ for (int i = 0; i < node.jjtGetNumChildren(); i++) {
+ if (!nodeCheck(node.jjtGetChild(i), failClass))
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Checks to see if all of the child nodes are of the same type (AND/OR) and if so then aggregates all of the child terms. If not returns null.
+ *
+ * @param parent
+ * @param parentNode
+ * @return Map of field names to query terms or null
+ */
+ private Multimap<String,QueryTerm> checkChildren(JexlNode parent, EvaluationContext ctx) {
+ // If the current node is an AND, then make sure that there is no
+ // OR descendant node, and vice versa. If this is true, then we call
+ // roll up all of the descendent values.
+ this.currentlyInCheckChildren = true;
+ Multimap<String,QueryTerm> rolledUpTerms = null;
+ boolean result = false;
+ if (parent.getClass().equals(ASTOrNode.class)) {
+ for (int i = 0; i < parent.jjtGetNumChildren(); i++) {
+ result = nodeCheck(parent.jjtGetChild(i), ASTAndNode.class);
+ if (!result)
+ break;
+ }
+ } else {
+ for (int i = 0; i < parent.jjtGetNumChildren(); i++) {
+ result = nodeCheck(parent.jjtGetChild(i), ASTOrNode.class);
+ if (!result)
+ break;
+ }
+ }
+ if (result) {
+ // Set current node to a fake node and
+ // roll up the children from this node using the visitor pattern.
+ TreeNode rollupFakeNode = new TreeNode();
+ TreeNode previous = this.currentNode;
+ this.currentNode = rollupFakeNode;
+ // Run the visitor with the fake node.
+ parent.childrenAccept(this, ctx);
+ // Get the terms from the fake node
+ rolledUpTerms = this.currentNode.getTerms();
+ // Reset the current node pointer
+ this.currentNode = previous;
+ }
+ this.currentlyInCheckChildren = false;
+ return rolledUpTerms;
+ }
+
+ public Object visit(ASTOrNode node, Object data) {
+ boolean previouslyInOrContext = false;
+ EvaluationContext ctx = null;
+ if (null != data && data instanceof EvaluationContext) {
+ ctx = (EvaluationContext) data;
+ previouslyInOrContext = ctx.inOrContext;
+ } else {
+ ctx = new EvaluationContext();
+ }
+ ctx.inOrContext = true;
+ // Are we being called from the checkChildren method? If so, then we
+ // are rolling up terms. If not, then we need to call check children.
+ if (currentlyInCheckChildren) {
+ // Process both sides of this node.
+ node.jjtGetChild(0).jjtAccept(this, data);
+ node.jjtGetChild(1).jjtAccept(this, data);
+ } else {
+ // Create a new OR node under the current node.
+ TreeNode orNode = new TreeNode();
+ orNode.setType(ASTOrNode.class);
+ orNode.setParent(this.currentNode);
+ this.currentNode.getChildren().add(orNode);
+ Multimap<String,QueryTerm> terms = checkChildren(node, ctx);
+ if (terms == null) {
+ // Then there was no rollup, set the current node to the orNode
+ // and process the children. Be sure to set the current Node to
+ // the or node in between calls because we could be processing
+ // an AND node below and the current node will have been switched.
+ // Process both sides of this node.
+ currentNode = orNode;
+ node.jjtGetChild(0).jjtAccept(this, data);
+ currentNode = orNode;
+ node.jjtGetChild(1).jjtAccept(this, data);
+ } else {
+ // There was a rollup, don't process the children and set the terms
+ // on the or node.
+ orNode.setTerms(terms);
+ }
+ }
+ // reset the state
+ if (null != data && !previouslyInOrContext)
+ ctx.inOrContext = false;
+ return null;
+ }
+
+ public Object visit(ASTAndNode node, Object data) {
+ boolean previouslyInAndContext = false;
+ EvaluationContext ctx = null;
+ if (null != data && data instanceof EvaluationContext) {
+ ctx = (EvaluationContext) data;
+ previouslyInAndContext = ctx.inAndContext;
+ } else {
+ ctx = new EvaluationContext();
+ }
+ ctx.inAndContext = true;
+ // Are we being called from the checkChildren method? If so, then we
+ // are rolling up terms. If not, then we need to call check children.
+ if (currentlyInCheckChildren) {
+ // Process both sides of this node.
+ node.jjtGetChild(0).jjtAccept(this, data);
+ node.jjtGetChild(1).jjtAccept(this, data);
+ } else {
+ // Create a new And node under the current node.
+ TreeNode andNode = new TreeNode();
+ andNode.setType(ASTAndNode.class);
+ andNode.setParent(this.currentNode);
+ this.currentNode.getChildren().add(andNode);
+ Multimap<String,QueryTerm> terms = checkChildren(node, ctx);
+ if (terms == null) {
+ // Then there was no rollup, set the current node to the orNode
+ // and process the children. Be sure to set the current Node to
+ // the and node in between calls because we could be processing
+ // an OR node below and the current node will have been switched.
+ // Process both sides of this node.
+ currentNode = andNode;
+ node.jjtGetChild(0).jjtAccept(this, data);
+ currentNode = andNode;
+ node.jjtGetChild(1).jjtAccept(this, data);
+ } else {
+ // There was a rollup, don't process the children and set the terms
+ // on the or node.
+ andNode.setTerms(terms);
+ }
+ }
+ if (null != data && !previouslyInAndContext)
+ ctx.inAndContext = false;
+ return null;
+ }
+
+ public Object visit(ASTBitwiseOrNode node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTBitwiseXorNode node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTBitwiseAndNode node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTEQNode node, Object data) {
+ StringBuilder fieldName = new StringBuilder();
+ ObjectHolder value = new ObjectHolder();
+ // Process both sides of this node.
+ Object left = node.jjtGetChild(0).jjtAccept(this, data);
+ Object right = node.jjtGetChild(1).jjtAccept(this, data);
+ // Ignore functions in the query
+ if (left instanceof FunctionResult || right instanceof FunctionResult)
+ return null;
+ decodeResults(left, right, fieldName, value);
+ // We need to check to see if we are in a NOT context. If so,
+ // then we need to reverse the negation.
+ boolean negated = false;
+ if (null != data && data instanceof EvaluationContext) {
+ EvaluationContext ctx = (EvaluationContext) data;
+ if (ctx.inNotContext)
+ negated = !negated;
+ }
+ QueryTerm term = new QueryTerm(negated, JexlOperatorConstants.getOperator(node.getClass()), value.getObject());
+ this.currentNode.getTerms().put(fieldName.toString(), term);
+ return null;
+ }
+
+ public Object visit(ASTNENode node, Object data) {
+ StringBuilder fieldName = new StringBuilder();
+ ObjectHolder value = new ObjectHolder();
+ // Process both sides of this node.
+ Object left = node.jjtGetChild(0).jjtAccept(this, data);
+ Object right = node.jjtGetChild(1).jjtAccept(this, data);
+ // Ignore functions in the query
+ if (left instanceof FunctionResult || right instanceof FunctionResult)
+ return null;
+ decodeResults(left, right, fieldName, value);
+ // We need to check to see if we are in a NOT context. If so,
+ // then we need to reverse the negation.
+ boolean negated = true;
+ if (null != data && data instanceof EvaluationContext) {
+ EvaluationContext ctx = (EvaluationContext) data;
+ if (ctx.inNotContext)
+ negated = !negated;
+ }
+ QueryTerm term = new QueryTerm(negated, JexlOperatorConstants.getOperator(node.getClass()), value.getObject());
+ this.currentNode.getTerms().put(fieldName.toString(), term);
+ return null;
+ }
+
+ public Object visit(ASTLTNode node, Object data) {
+ StringBuilder fieldName = new StringBuilder();
+ ObjectHolder value = new ObjectHolder();
+ // Process both sides of this node.
+ Object left = node.jjtGetChild(0).jjtAccept(this, data);
+ Object right = node.jjtGetChild(1).jjtAccept(this, data);
+ // Ignore functions in the query
+ if (left instanceof FunctionResult || right instanceof FunctionResult)
+ return null;
+ decodeResults(left, right, fieldName, value);
+ // We need to check to see if we are in a NOT context. If so,
+ // then we need to reverse the negation.
+ boolean negated = false;
+ if (null != data && data instanceof EvaluationContext) {
+ EvaluationContext ctx = (EvaluationContext) data;
+ if (ctx.inNotContext)
+ negated = !negated;
+ }
+ QueryTerm term = new QueryTerm(negated, JexlOperatorConstants.getOperator(node.getClass()), value.getObject());
+ this.currentNode.getTerms().put(fieldName.toString(), term);
+ return null;
+ }
+
+ public Object visit(ASTGTNode node, Object data) {
+ StringBuilder fieldName = new StringBuilder();
+ ObjectHolder value = new ObjectHolder();
+ // Process both sides of this node.
+ Object left = node.jjtGetChild(0).jjtAccept(this, data);
+ Object right = node.jjtGetChild(1).jjtAccept(this, data);
+ // Ignore functions in the query
+ if (left instanceof FunctionResult || right instanceof FunctionResult)
+ return null;
+ decodeResults(left, right, fieldName, value);
+ // We need to check to see if we are in a NOT context. If so,
+ // then we need to reverse the negation.
+ boolean negated = false;
+ if (null != data && data instanceof EvaluationContext) {
+ EvaluationContext ctx = (EvaluationContext) data;
+ if (ctx.inNotContext)
+ negated = !negated;
+ }
+ QueryTerm term = new QueryTerm(negated, JexlOperatorConstants.getOperator(node.getClass()), value.getObject());
+ this.currentNode.getTerms().put(fieldName.toString(), term);
+ return null;
+ }
+
+ public Object visit(ASTLENode node, Object data) {
+ StringBuilder fieldName = new StringBuilder();
+ ObjectHolder value = new ObjectHolder();
+ // Process both sides of this node.
+ Object left = node.jjtGetChild(0).jjtAccept(this, data);
+ Object right = node.jjtGetChild(1).jjtAccept(this, data);
+ // Ignore functions in the query
+ if (left instanceof FunctionResult || right instanceof FunctionResult)
+ return null;
+ decodeResults(left, right, fieldName, value);
+ // We need to check to see if we are in a NOT context. If so,
+ // then we need to reverse the negation.
+ boolean negated = false;
+ if (null != data && data instanceof EvaluationContext) {
+ EvaluationContext ctx = (EvaluationContext) data;
+ if (ctx.inNotContext)
+ negated = !negated;
+ }
+ QueryTerm term = new QueryTerm(negated, JexlOperatorConstants.getOperator(node.getClass()), value.getObject());
+ this.currentNode.getTerms().put(fieldName.toString(), term);
+ return null;
+ }
+
+ public Object visit(ASTGENode node, Object data) {
+ StringBuilder fieldName = new StringBuilder();
+ ObjectHolder value = new ObjectHolder();
+ // Process both sides of this node.
+ Object left = node.jjtGetChild(0).jjtAccept(this, data);
+ Object right = node.jjtGetChild(1).jjtAccept(this, data);
+ // Ignore functions in the query
+ if (left instanceof FunctionResult || right instanceof FunctionResult)
+ return null;
+ decodeResults(left, right, fieldName, value);
+ // We need to check to see if we are in a NOT context. If so,
+ // then we need to reverse the negation.
+ boolean negated = false;
+ if (null != data && data instanceof EvaluationContext) {
+ EvaluationContext ctx = (EvaluationContext) data;
+ if (ctx.inNotContext)
+ negated = !negated;
+ }
+ QueryTerm term = new QueryTerm(negated, JexlOperatorConstants.getOperator(node.getClass()), value.getObject());
+ this.currentNode.getTerms().put(fieldName.toString(), term);
+ return null;
+ }
+
+ public Object visit(ASTERNode node, Object data) {
+ StringBuilder fieldName = new StringBuilder();
+ ObjectHolder value = new ObjectHolder();
+ // Process both sides of this node.
+ Object left = node.jjtGetChild(0).jjtAccept(this, data);
+ Object right = node.jjtGetChild(1).jjtAccept(this, data);
+ // Ignore functions in the query
+ if (left instanceof FunctionResult || right instanceof FunctionResult)
+ return null;
+ decodeResults(left, right, fieldName, value);
+ // We need to check to see if we are in a NOT context. If so,
+ // then we need to reverse the negation.
+ boolean negated = false;
+ if (null != data && data instanceof EvaluationContext) {
+ EvaluationContext ctx = (EvaluationContext) data;
+ if (ctx.inNotContext)
+ negated = !negated;
+ }
+ QueryTerm term = new QueryTerm(negated, JexlOperatorConstants.getOperator(node.getClass()), value.getObject());
+ this.currentNode.getTerms().put(fieldName.toString(), term);
+ return null;
+ }
+
+ public Object visit(ASTNRNode node, Object data) {
+ StringBuilder fieldName = new StringBuilder();
+ ObjectHolder value = new ObjectHolder();
+ // Process both sides of this node.
+ Object left = node.jjtGetChild(0).jjtAccept(this, data);
+ Object right = node.jjtGetChild(1).jjtAccept(this, data);
+ // Ignore functions in the query
+ if (left instanceof FunctionResult || right instanceof FunctionResult)
+ return null;
+ decodeResults(left, right, fieldName, value);
+ // We need to check to see if we are in a NOT context. If so,
+ // then we need to reverse the negation.
+ boolean negated = true;
+ if (null != data && data instanceof EvaluationContext) {
+ EvaluationContext ctx = (EvaluationContext) data;
+ if (ctx.inNotContext)
+ negated = !negated;
+ }
+ QueryTerm term = new QueryTerm(negated, "!~", value.getObject());
+ this.currentNode.getTerms().put(fieldName.toString(), term);
+ return null;
+ }
+
+ public Object visit(ASTAdditiveNode node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTAdditiveOperator node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTMulNode node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTDivNode node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTModNode node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTUnaryMinusNode node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTBitwiseComplNode node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTNotNode node, Object data) {
+ boolean previouslyInNotContext = false;
+ EvaluationContext ctx = null;
+ if (null != data && data instanceof EvaluationContext) {
+ ctx = (EvaluationContext) data;
+ previouslyInNotContext = ctx.inNotContext;
+ } else {
+ ctx = new EvaluationContext();
+ }
+ ctx.inNotContext = true;
+ // Create a new node in the tree to represent the NOT
+ // Create a new And node under the current node.
+ TreeNode notNode = new TreeNode();
+ notNode.setType(ASTNotNode.class);
+ notNode.setParent(this.currentNode);
+ this.currentNode.getChildren().add(notNode);
+ this.currentNode = notNode;
+ // Process both sides of this node.
+ node.jjtGetChild(0).jjtAccept(this, ctx);
+ // reset the state
+ if (null != data && !previouslyInNotContext)
+ ctx.inNotContext = false;
+ return null;
+ }
+
+ public Object visit(ASTIdentifier node, Object data) {
+ return new TermResult(node.image);
+ }
+
+ public Object visit(ASTNullLiteral node, Object data) {
+ return new LiteralResult(node.image);
+ }
+
+ public Object visit(ASTTrueNode node, Object data) {
+ return new LiteralResult(node.image);
+ }
+
+ public Object visit(ASTFalseNode node, Object data) {
+ return new LiteralResult(node.image);
+ }
+
+ public Object visit(ASTIntegerLiteral node, Object data) {
+ return new LiteralResult(node.image);
+ }
+
+ public Object visit(ASTFloatLiteral node, Object data) {
+ return new LiteralResult(node.image);
+ }
+
+ public Object visit(ASTStringLiteral node, Object data) {
+ return new LiteralResult("'" + node.image + "'");
+ }
+
+ public Object visit(ASTArrayLiteral node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTMapLiteral node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTMapEntry node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTEmptyFunction node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTSizeFunction node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTFunctionNode node, Object data) {
+ // objectNode 0 is the prefix
+ // objectNode 1 is the identifier , the others are parameters.
+ // process the remaining arguments
+ FunctionResult fr = new FunctionResult();
+ int argc = node.jjtGetNumChildren() - 2;
+ for (int i = 0; i < argc; i++) {
+ // Process both sides of this node.
+ Object result = node.jjtGetChild(i + 2).jjtAccept(this, data);
+ if (result instanceof TermResult) {
+ TermResult tr = (TermResult) result;
+ fr.getTerms().add(tr);
+ }
+ }
+ return fr;
+ }
+
+ public Object visit(ASTMethodNode node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTSizeMethod node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTConstructorNode node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTArrayAccess node, Object data) {
+ return null;
+ }
+
+ public Object visit(ASTReference node, Object data) {
+ return node.jjtGetChild(0).jjtAccept(this, data);
+ }
+
+ private void decodeResults(Object left, Object right, StringBuilder fieldName, ObjectHolder holder) {
+ if (left instanceof TermResult) {
+ TermResult tr = (TermResult) left;
+ fieldName.append((String) tr.value);
+ // Then the right has to be the value
+ if (right instanceof LiteralResult) {
+ holder.setObject(((LiteralResult) right).value);
+ } else {
+ throw new IllegalArgumentException("Object mismatch");
+ }
+ } else if (right instanceof TermResult) {
+ TermResult tr = (TermResult) right;
+ fieldName.append((String) tr.value);
+ if (left instanceof LiteralResult) {
+ holder.setObject(((LiteralResult) left).value);
+ } else {
+ throw new IllegalArgumentException("Object mismatch");
+ }
+
+ } else {
+ throw new IllegalArgumentException("No Term specified in query");
+ }
+ }
}
Modified: incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/parser/TreeNode.java
URL: http://svn.apache.org/viewvc/incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/parser/TreeNode.java?rev=1210600&r1=1210599&r2=1210600&view=diff
==============================================================================
--- incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/parser/TreeNode.java (original)
+++ incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/parser/TreeNode.java Mon Dec 5 20:05:49 2011
@@ -1,19 +1,19 @@
/*
-* Licensed to the Apache Software Foundation (ASF) under one or more
-* contributor license agreements. See the NOTICE file distributed with
-* this work for additional information regarding copyright ownership.
-* The ASF licenses this file to You 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
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* 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.
-*/
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 parser;
import java.util.ArrayList;
@@ -31,206 +31,205 @@ import com.google.common.collect.HashMul
import com.google.common.collect.Multimap;
public class TreeNode {
-
- private Class<? extends JexlNode> type = null;
- /* navigation elements */
- private TreeNode parent = null;
- private List<TreeNode> children = new ArrayList<TreeNode>();
- private Multimap<String, QueryTerm> terms = HashMultimap.create();
-
- public TreeNode() {
- super();
- }
-
- public Class<? extends JexlNode> getType() {
- return type;
- }
-
- public TreeNode getParent() {
- return parent;
- }
-
- public List<TreeNode> getChildren() {
- return children;
- }
-
- public Enumeration<TreeNode> getChildrenAsEnumeration() {
- return Collections.enumeration(children);
- }
-
- public Multimap<String, QueryTerm> getTerms() {
- return terms;
- }
-
- public void setType(Class<? extends JexlNode> type) {
- this.type = type;
- }
-
- public void setParent(TreeNode parent) {
- this.parent = parent;
- }
-
- public void setChildren(List<TreeNode> children) {
- this.children = children;
- }
-
- public void setTerms(Multimap<String, QueryTerm> terms) {
- this.terms = terms;
- }
-
- public boolean isLeaf(){
- return children.isEmpty();
- }
-
- @Override
- public String toString() {
- StringBuilder buf = new StringBuilder();
- buf.append("Type: ").append(type.getSimpleName());
- buf.append(" Terms: ");
- if (null == terms) {
- buf.append("null");
+
+ private Class<? extends JexlNode> type = null;
+ /* navigation elements */
+ private TreeNode parent = null;
+ private List<TreeNode> children = new ArrayList<TreeNode>();
+ private Multimap<String,QueryTerm> terms = HashMultimap.create();
+
+ public TreeNode() {
+ super();
+ }
+
+ public Class<? extends JexlNode> getType() {
+ return type;
+ }
+
+ public TreeNode getParent() {
+ return parent;
+ }
+
+ public List<TreeNode> getChildren() {
+ return children;
+ }
+
+ public Enumeration<TreeNode> getChildrenAsEnumeration() {
+ return Collections.enumeration(children);
+ }
+
+ public Multimap<String,QueryTerm> getTerms() {
+ return terms;
+ }
+
+ public void setType(Class<? extends JexlNode> type) {
+ this.type = type;
+ }
+
+ public void setParent(TreeNode parent) {
+ this.parent = parent;
+ }
+
+ public void setChildren(List<TreeNode> children) {
+ this.children = children;
+ }
+
+ public void setTerms(Multimap<String,QueryTerm> terms) {
+ this.terms = terms;
+ }
+
+ public boolean isLeaf() {
+ return children.isEmpty();
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder buf = new StringBuilder();
+ buf.append("Type: ").append(type.getSimpleName());
+ buf.append(" Terms: ");
+ if (null == terms) {
+ buf.append("null");
+ } else {
+ buf.append(terms.toString());
+ }
+ return buf.toString();
+ }
+
+ public final Enumeration<?> depthFirstEnumeration() {
+ return new PostorderEnumeration(this);
+ }
+
+ public Enumeration<?> breadthFirstEnumeration() {
+ return new BreadthFirstEnumeration(this);
+ }
+
+ public final class PostorderEnumeration implements Enumeration<TreeNode> {
+
+ protected TreeNode root;
+ protected Enumeration<TreeNode> children;
+ protected Enumeration<TreeNode> subtree;
+
+ public PostorderEnumeration(TreeNode rootNode) {
+ super();
+ root = rootNode;
+ children = root.getChildrenAsEnumeration();
+ subtree = EMPTY_ENUMERATION;
+ }
+
+ public boolean hasMoreElements() {
+ return root != null;
+ }
+
+ public TreeNode nextElement() {
+ TreeNode retval;
+
+ if (subtree.hasMoreElements()) {
+ retval = subtree.nextElement();
+ } else if (children.hasMoreElements()) {
+ subtree = new PostorderEnumeration((TreeNode) children.nextElement());
+ retval = subtree.nextElement();
+ } else {
+ retval = root;
+ root = null;
+ }
+
+ return retval;
+ }
+ } // End of class PostorderEnumeration
+
+ static public final Enumeration<TreeNode> EMPTY_ENUMERATION = new Enumeration<TreeNode>() {
+
+ public boolean hasMoreElements() {
+ return false;
+ }
+
+ public TreeNode nextElement() {
+ throw new NoSuchElementException("No more elements");
+ }
+ };
+
+ final class BreadthFirstEnumeration implements Enumeration<TreeNode> {
+ protected Queue queue;
+
+ public BreadthFirstEnumeration(TreeNode rootNode) {
+ super();
+ Vector<TreeNode> v = new Vector<TreeNode>(1);
+ v.addElement(rootNode); // PENDING: don't really need a vector
+ queue = new Queue();
+ queue.enqueue(v.elements());
+ }
+
+ public boolean hasMoreElements() {
+ return (!queue.isEmpty() && ((Enumeration<?>) queue.firstObject()).hasMoreElements());
+ }
+
+ public TreeNode nextElement() {
+ Enumeration<?> enumer = (Enumeration<?>) queue.firstObject();
+ TreeNode node = (TreeNode) enumer.nextElement();
+ Enumeration<?> children = node.getChildrenAsEnumeration();
+
+ if (!enumer.hasMoreElements()) {
+ queue.dequeue();
+ }
+ if (children.hasMoreElements()) {
+ queue.enqueue(children);
+ }
+ return node;
+ }
+
+ // A simple queue with a linked list data structure.
+ final class Queue {
+ QNode head; // null if empty
+ QNode tail;
+
+ final class QNode {
+ public Object object;
+ public QNode next; // null if end
+
+ public QNode(Object object, QNode next) {
+ this.object = object;
+ this.next = next;
+ }
+ }
+
+ public void enqueue(Object anObject) {
+ if (head == null) {
+ head = tail = new QNode(anObject, null);
} else {
- buf.append(terms.toString());
+ tail.next = new QNode(anObject, null);
+ tail = tail.next;
}
- return buf.toString();
- }
-
- public final Enumeration<?> depthFirstEnumeration() {
- return new PostorderEnumeration(this);
- }
-
- public Enumeration<?> breadthFirstEnumeration() {
- return new BreadthFirstEnumeration(this);
- }
-
- public final class PostorderEnumeration implements Enumeration<TreeNode> {
-
- protected TreeNode root;
- protected Enumeration<TreeNode> children;
- protected Enumeration<TreeNode> subtree;
-
- public PostorderEnumeration(TreeNode rootNode) {
- super();
- root = rootNode;
- children = root.getChildrenAsEnumeration();
- subtree = EMPTY_ENUMERATION;
- }
-
- public boolean hasMoreElements() {
- return root != null;
- }
-
- public TreeNode nextElement() {
- TreeNode retval;
-
- if (subtree.hasMoreElements()) {
- retval = subtree.nextElement();
- } else if (children.hasMoreElements()) {
- subtree = new PostorderEnumeration(
- (TreeNode) children.nextElement());
- retval = subtree.nextElement();
- } else {
- retval = root;
- root = null;
- }
-
- return retval;
- }
- } // End of class PostorderEnumeration
- static public final Enumeration<TreeNode> EMPTY_ENUMERATION = new Enumeration<TreeNode>() {
-
- public boolean hasMoreElements() {
- return false;
- }
-
- public TreeNode nextElement() {
- throw new NoSuchElementException("No more elements");
+ }
+
+ public Object dequeue() {
+ if (head == null) {
+ throw new NoSuchElementException("No more elements");
+ }
+
+ Object retval = head.object;
+ QNode oldHead = head;
+ head = head.next;
+ if (head == null) {
+ tail = null;
+ } else {
+ oldHead.next = null;
}
- };
-
- final class BreadthFirstEnumeration implements Enumeration<TreeNode> {
- protected Queue queue;
-
- public BreadthFirstEnumeration(TreeNode rootNode) {
- super();
- Vector<TreeNode> v = new Vector<TreeNode>(1);
- v.addElement(rootNode); // PENDING: don't really need a vector
- queue = new Queue();
- queue.enqueue(v.elements());
- }
-
- public boolean hasMoreElements() {
- return (!queue.isEmpty() &&
- ((Enumeration<?>)queue.firstObject()).hasMoreElements());
- }
-
- public TreeNode nextElement() {
- Enumeration<?> enumer = (Enumeration<?>)queue.firstObject();
- TreeNode node = (TreeNode)enumer.nextElement();
- Enumeration<?> children = node.getChildrenAsEnumeration();
-
- if (!enumer.hasMoreElements()) {
- queue.dequeue();
- }
- if (children.hasMoreElements()) {
- queue.enqueue(children);
- }
- return node;
- }
-
-
- // A simple queue with a linked list data structure.
- final class Queue {
- QNode head; // null if empty
- QNode tail;
-
- final class QNode {
- public Object object;
- public QNode next; // null if end
- public QNode(Object object, QNode next) {
- this.object = object;
- this.next = next;
- }
- }
-
- public void enqueue(Object anObject) {
- if (head == null) {
- head = tail = new QNode(anObject, null);
- } else {
- tail.next = new QNode(anObject, null);
- tail = tail.next;
- }
- }
-
- public Object dequeue() {
- if (head == null) {
- throw new NoSuchElementException("No more elements");
- }
-
- Object retval = head.object;
- QNode oldHead = head;
- head = head.next;
- if (head == null) {
- tail = null;
- } else {
- oldHead.next = null;
- }
- return retval;
- }
-
- public Object firstObject() {
- if (head == null) {
- throw new NoSuchElementException("No more elements");
- }
-
- return head.object;
- }
-
- public boolean isEmpty() {
- return head == null;
- }
-
- } // End of class Queue
-
- } // End of class BreadthFirstEnumeration
+ return retval;
+ }
+
+ public Object firstObject() {
+ if (head == null) {
+ throw new NoSuchElementException("No more elements");
+ }
+
+ return head.object;
+ }
+
+ public boolean isEmpty() {
+ return head == null;
+ }
+
+ } // End of class Queue
+
+ } // End of class BreadthFirstEnumeration
}
Modified: incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/sample/Document.java
URL: http://svn.apache.org/viewvc/incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/sample/Document.java?rev=1210600&r1=1210599&r2=1210600&view=diff
==============================================================================
--- incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/sample/Document.java (original)
+++ incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/sample/Document.java Mon Dec 5 20:05:49 2011
@@ -1,19 +1,19 @@
/*
-* Licensed to the Apache Software Foundation (ASF) under one or more
-* contributor license agreements. See the NOTICE file distributed with
-* this work for additional information regarding copyright ownership.
-* The ASF licenses this file to You 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
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* 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.
-*/
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 sample;
import java.util.ArrayList;
@@ -25,37 +25,37 @@ import javax.xml.bind.annotation.XmlElem
@XmlAccessorType(XmlAccessType.FIELD)
public class Document {
-
- @XmlElement
- private String id = null;
-
- @XmlElement
- private List<Field> field = new ArrayList<Field>();
-
- public Document() {
- super();
- }
-
- public Document(String id, List<Field> fields) {
- super();
- this.id = id;
- this.field = fields;
- }
-
- public String getId() {
- return id;
- }
-
- public List<Field> getFields() {
- return field;
- }
-
- public void setId(String id) {
- this.id = id;
- }
-
- public void setFields(List<Field> fields) {
- this.field = fields;
- }
-
+
+ @XmlElement
+ private String id = null;
+
+ @XmlElement
+ private List<Field> field = new ArrayList<Field>();
+
+ public Document() {
+ super();
+ }
+
+ public Document(String id, List<Field> fields) {
+ super();
+ this.id = id;
+ this.field = fields;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public List<Field> getFields() {
+ return field;
+ }
+
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ public void setFields(List<Field> fields) {
+ this.field = fields;
+ }
+
}
Modified: incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/sample/Field.java
URL: http://svn.apache.org/viewvc/incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/sample/Field.java?rev=1210600&r1=1210599&r2=1210600&view=diff
==============================================================================
--- incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/sample/Field.java (original)
+++ incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/sample/Field.java Mon Dec 5 20:05:49 2011
@@ -1,19 +1,19 @@
/*
-* Licensed to the Apache Software Foundation (ASF) under one or more
-* contributor license agreements. See the NOTICE file distributed with
-* this work for additional information regarding copyright ownership.
-* The ASF licenses this file to You 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
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* 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.
-*/
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 sample;
import javax.xml.bind.annotation.XmlAccessType;
@@ -23,36 +23,36 @@ import javax.xml.bind.annotation.XmlValu
@XmlAccessorType(XmlAccessType.FIELD)
public class Field {
-
- @XmlAttribute
- private String name = null;
- @XmlValue
- private String value = null;
-
- public Field() {
- super();
- }
-
- public Field(String fieldName, String fieldValue) {
- super();
- this.name = fieldName;
- this.value = fieldValue;
- }
-
- public String getFieldName() {
- return name;
- }
-
- public String getFieldValue() {
- return value;
- }
-
- public void setFieldName(String fieldName) {
- this.name = fieldName;
- }
-
- public void setFieldValue(String fieldValue) {
- this.value = fieldValue;
- }
-
+
+ @XmlAttribute
+ private String name = null;
+ @XmlValue
+ private String value = null;
+
+ public Field() {
+ super();
+ }
+
+ public Field(String fieldName, String fieldValue) {
+ super();
+ this.name = fieldName;
+ this.value = fieldValue;
+ }
+
+ public String getFieldName() {
+ return name;
+ }
+
+ public String getFieldValue() {
+ return value;
+ }
+
+ public void setFieldName(String fieldName) {
+ this.name = fieldName;
+ }
+
+ public void setFieldValue(String fieldValue) {
+ this.value = fieldValue;
+ }
+
}
Modified: incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/sample/Results.java
URL: http://svn.apache.org/viewvc/incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/sample/Results.java?rev=1210600&r1=1210599&r2=1210600&view=diff
==============================================================================
--- incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/sample/Results.java (original)
+++ incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/sample/Results.java Mon Dec 5 20:05:49 2011
@@ -1,19 +1,19 @@
/*
-* Licensed to the Apache Software Foundation (ASF) under one or more
-* contributor license agreements. See the NOTICE file distributed with
-* this work for additional information regarding copyright ownership.
-* The ASF licenses this file to You 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
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* 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.
-*/
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 sample;
import java.util.ArrayList;
@@ -27,28 +27,27 @@ import javax.xml.bind.annotation.XmlRoot
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Results {
-
- @XmlElement
- private List<Document> document = new ArrayList<Document>();
-
- public Results() {
- super();
- }
-
- public List<Document> getResults() {
- return document;
- }
-
- public void setResults(List<Document> results) {
- this.document = results;
- }
-
- public int size() {
- if (null == document)
- return 0;
- else
- return document.size();
- }
-
-
+
+ @XmlElement
+ private List<Document> document = new ArrayList<Document>();
+
+ public Results() {
+ super();
+ }
+
+ public List<Document> getResults() {
+ return document;
+ }
+
+ public void setResults(List<Document> results) {
+ this.document = results;
+ }
+
+ public int size() {
+ if (null == document)
+ return 0;
+ else
+ return document.size();
+ }
+
}
Modified: incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/sample/query/IQuery.java
URL: http://svn.apache.org/viewvc/incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/sample/query/IQuery.java?rev=1210600&r1=1210599&r2=1210600&view=diff
==============================================================================
--- incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/sample/query/IQuery.java (original)
+++ incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/sample/query/IQuery.java Mon Dec 5 20:05:49 2011
@@ -1,19 +1,19 @@
/*
-* Licensed to the Apache Software Foundation (ASF) under one or more
-* contributor license agreements. See the NOTICE file distributed with
-* this work for additional information regarding copyright ownership.
-* The ASF licenses this file to You 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
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* 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.
-*/
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 sample.query;
import javax.ws.rs.Consumes;
@@ -29,44 +29,44 @@ import sample.Results;
@Path("/Query")
public interface IQuery {
-
- @GET
- @POST
- @Path("/html")
- @Consumes("*/*")
- @GZIP
- public String html(@QueryParam("query") String query, @QueryParam("auths") String auths);
-
- @GET
- @POST
- @Path("/xml")
- @Consumes("*/*")
- @Produces("application/xml")
- @GZIP
- public Results xml(@QueryParam("query") String query, @QueryParam("auths") String auths);
-
- @GET
- @POST
- @Path("/json")
- @Consumes("*/*")
- @Produces("application/json")
- @GZIP
- public Results json(@QueryParam("query") String query, @QueryParam("auths") String auths);
-
- @GET
- @POST
- @Path("/yaml")
- @Consumes("*/*")
- @Produces("text/x-yaml")
- @GZIP
- public Results yaml(@QueryParam("query") String query, @QueryParam("auths") String auths);
-
- @GET
- @POST
- @Path("/content")
- @Consumes("*/*")
- @Produces("application/xml")
- @GZIP
- public Results content(@QueryParam("query") String query, @QueryParam("auths") String auths);
-
+
+ @GET
+ @POST
+ @Path("/html")
+ @Consumes("*/*")
+ @GZIP
+ public String html(@QueryParam("query") String query, @QueryParam("auths") String auths);
+
+ @GET
+ @POST
+ @Path("/xml")
+ @Consumes("*/*")
+ @Produces("application/xml")
+ @GZIP
+ public Results xml(@QueryParam("query") String query, @QueryParam("auths") String auths);
+
+ @GET
+ @POST
+ @Path("/json")
+ @Consumes("*/*")
+ @Produces("application/json")
+ @GZIP
+ public Results json(@QueryParam("query") String query, @QueryParam("auths") String auths);
+
+ @GET
+ @POST
+ @Path("/yaml")
+ @Consumes("*/*")
+ @Produces("text/x-yaml")
+ @GZIP
+ public Results yaml(@QueryParam("query") String query, @QueryParam("auths") String auths);
+
+ @GET
+ @POST
+ @Path("/content")
+ @Consumes("*/*")
+ @Produces("application/xml")
+ @GZIP
+ public Results content(@QueryParam("query") String query, @QueryParam("auths") String auths);
+
}
Modified: incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/sample/query/Query.java
URL: http://svn.apache.org/viewvc/incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/sample/query/Query.java?rev=1210600&r1=1210599&r2=1210600&view=diff
==============================================================================
--- incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/sample/query/Query.java (original)
+++ incubator/accumulo/trunk/contrib/accumulo_sample/query/src/main/java/sample/query/Query.java Mon Dec 5 20:05:49 2011
@@ -1,19 +1,19 @@
/*
-* Licensed to the Apache Software Foundation (ASF) under one or more
-* contributor license agreements. See the NOTICE file distributed with
-* this work for additional information regarding copyright ownership.
-* The ASF licenses this file to You 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
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* 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.
-*/
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 sample.query;
import java.io.IOException;
@@ -53,192 +53,192 @@ import org.apache.accumulo.core.client.Z
@Stateless
@Local(IQuery.class)
public class Query implements IQuery {
-
- private static final Logger log = Logger.getLogger(Query.class);
-
- //Inject values from XML configuration file
- @Resource(name="instanceName")
- private String instanceName;
-
- @Resource(name="zooKeepers")
- private String zooKeepers;
-
- @Resource(name="username")
- private String username;
-
- @Resource(name="password")
- private String password;
-
- @Resource(name="tableName")
- private String tableName;
-
- @Resource(name="partitions")
- private int partitions;
-
- @Resource(name="threads")
- private int threads;
-
- private static final String XSL = "http://localhost:8080/accumulo-sample/style.xsl";
-
- @PostConstruct
- public void init() {
- log.info("Post Construct");
- }
-
- @PreDestroy
- public void close() {
- log.info("Close called.");
- }
-
- /*
- * (non-Javadoc)
- * @see sample.query.IQuery#html(java.lang.String, java.lang.String)
- */
- public String html(String query, String auths) {
- log.info("HTML query: " + query);
- URL u;
- try {
- u = new URL(XSL);
- } catch (MalformedURLException e1) {
- throw new EJBException("Unable to load XSL stylesheet", e1);
- }
- InputStream xslContent;
- try {
- xslContent = u.openStream();
- } catch (IOException e1) {
- throw new EJBException("Unable to get xsl content", e1);
- }
-
- StringWriter xml = new StringWriter();
- StringWriter html = new StringWriter();
-
- Results results = query(query, auths);
- try {
- //Marshall the query results object
- JAXBContext ctx = JAXBContext.newInstance(Results.class);
- Marshaller m = ctx.createMarshaller();
- m.marshal(results, xml);
-
- //Perform XSL transform on the xml.
- StringReader reader = new StringReader(xml.toString());
- TransformerFactory tf = TransformerFactory.newInstance();
- //Create the transformer from the xsl
- Templates xsl = tf.newTemplates(new StreamSource(xslContent));
- Transformer t = xsl.newTransformer();
- t.transform(new StreamSource(reader), new StreamResult(html));
-
- } catch (Exception e) {
- throw new EJBException("Error processing query results", e);
- } finally {
- try {
- xslContent.close();
- } catch (IOException e) {
- throw new EJBException("Unable to close input stream", e);
- }
- }
- return html.toString();
- }
-
- /*
- * (non-Javadoc)
- * @see sample.query.IQuery#xml(java.lang.String, java.lang.String)
- */
- public Results xml(String query, String auths) {
- log.info("XML query: " + query);
- return query(query, auths);
- }
-
- /*
- * (non-Javadoc)
- * @see sample.query.IQuery#json(java.lang.String, java.lang.String)
- */
- public Results json(String query, String auths) {
- log.info("JSON query: " + query);
- return query(query, auths);
- }
-
- /*
- * (non-Javadoc)
- * @see sample.query.IQuery#yaml(java.lang.String, java.lang.String)
- */
- public Results yaml(String query, String auths) {
- log.info("YAML query: " + query);
- return query(query, auths);
- }
-
- /*
- * (non-Javadoc)
- * @see sample.query.IQuery#content(java.lang.String, java.lang.String)
- */
- public Results content(String query, String auths) {
- log.info("Content query: " + query);
- Connector connector = null;
- if (null == instanceName || null == zooKeepers || null == username || null == password)
- throw new EJBException("Required parameters not set. [instanceName = " + this.instanceName +
- ", zookeepers = " + this.zooKeepers + ", username = " + this.username +
- ", password = " + this.password +"]. Check values in ejb-jar.xml");
- Instance instance = new ZooKeeperInstance(this.instanceName, this.zooKeepers);
- try {
- log.info("Connecting to [instanceName = " + this.instanceName +
- ", zookeepers = " + this.zooKeepers + ", username = " + this.username +
- ", password = " + this.password +"].");
- connector = instance.getConnector(this.username, this.password.getBytes());
- } catch (Exception e) {
- throw new EJBException("Error getting connector from instance", e);
- }
-
- //Create list of auths
- List<String> authorizations = new ArrayList<String>();
- for (String a : auths.split(","))
- authorizations.add(a);
-
- ContentLogic table = new ContentLogic();
- table.setTableName(tableName);
- return table.runQuery(connector, query, authorizations);
-
- }
-
- /**
- * calls the query logic with the parameters, returns results
- *
- * @param query
- * @param auths
- * @return
- * @throws ParseException
- */
- public Results query(String query, String auths) {
-
- Connector connector = null;
- if (null == instanceName || null == zooKeepers || null == username || null == password)
- throw new EJBException("Required parameters not set. [instanceName = " + this.instanceName +
- ", zookeepers = " + this.zooKeepers + ", username = " + this.username +
- ", password = " + this.password +"]. Check values in ejb-jar.xml");
- Instance instance = new ZooKeeperInstance(this.instanceName, this.zooKeepers);
- try {
- log.info("Connecting to [instanceName = " + this.instanceName +
- ", zookeepers = " + this.zooKeepers + ", username = " + this.username +
- ", password = " + this.password +"].");
- connector = instance.getConnector(this.username, this.password.getBytes());
- } catch (Exception e) {
- throw new EJBException("Error getting connector from instance", e);
- }
-
-
- //Create list of auths
- List<String> authorizations = new ArrayList<String>();
- for (String a : auths.split(","))
- authorizations.add(a);
-
- QueryLogic table = new QueryLogic();
- table.setTableName(tableName);
- table.setMetadataTableName(tableName+"Metadata");
- table.setIndexTableName(tableName+"Index");
- table.setReverseIndexTableName(tableName+"ReverseIndex");
- table.setQueryThreads(threads);
- table.setUnevaluatedFields("TEXT");
- table.setNumPartitions(partitions);
- table.setUseReadAheadIterator(false);
- return table.runQuery(connector, authorizations, query, null, null, null);
- }
-
+
+ private static final Logger log = Logger.getLogger(Query.class);
+
+ // Inject values from XML configuration file
+ @Resource(name = "instanceName")
+ private String instanceName;
+
+ @Resource(name = "zooKeepers")
+ private String zooKeepers;
+
+ @Resource(name = "username")
+ private String username;
+
+ @Resource(name = "password")
+ private String password;
+
+ @Resource(name = "tableName")
+ private String tableName;
+
+ @Resource(name = "partitions")
+ private int partitions;
+
+ @Resource(name = "threads")
+ private int threads;
+
+ private static final String XSL = "http://localhost:8080/accumulo-sample/style.xsl";
+
+ @PostConstruct
+ public void init() {
+ log.info("Post Construct");
+ }
+
+ @PreDestroy
+ public void close() {
+ log.info("Close called.");
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see sample.query.IQuery#html(java.lang.String, java.lang.String)
+ */
+ public String html(String query, String auths) {
+ log.info("HTML query: " + query);
+ URL u;
+ try {
+ u = new URL(XSL);
+ } catch (MalformedURLException e1) {
+ throw new EJBException("Unable to load XSL stylesheet", e1);
+ }
+ InputStream xslContent;
+ try {
+ xslContent = u.openStream();
+ } catch (IOException e1) {
+ throw new EJBException("Unable to get xsl content", e1);
+ }
+
+ StringWriter xml = new StringWriter();
+ StringWriter html = new StringWriter();
+
+ Results results = query(query, auths);
+ try {
+ // Marshall the query results object
+ JAXBContext ctx = JAXBContext.newInstance(Results.class);
+ Marshaller m = ctx.createMarshaller();
+ m.marshal(results, xml);
+
+ // Perform XSL transform on the xml.
+ StringReader reader = new StringReader(xml.toString());
+ TransformerFactory tf = TransformerFactory.newInstance();
+ // Create the transformer from the xsl
+ Templates xsl = tf.newTemplates(new StreamSource(xslContent));
+ Transformer t = xsl.newTransformer();
+ t.transform(new StreamSource(reader), new StreamResult(html));
+
+ } catch (Exception e) {
+ throw new EJBException("Error processing query results", e);
+ } finally {
+ try {
+ xslContent.close();
+ } catch (IOException e) {
+ throw new EJBException("Unable to close input stream", e);
+ }
+ }
+ return html.toString();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see sample.query.IQuery#xml(java.lang.String, java.lang.String)
+ */
+ public Results xml(String query, String auths) {
+ log.info("XML query: " + query);
+ return query(query, auths);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see sample.query.IQuery#json(java.lang.String, java.lang.String)
+ */
+ public Results json(String query, String auths) {
+ log.info("JSON query: " + query);
+ return query(query, auths);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see sample.query.IQuery#yaml(java.lang.String, java.lang.String)
+ */
+ public Results yaml(String query, String auths) {
+ log.info("YAML query: " + query);
+ return query(query, auths);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see sample.query.IQuery#content(java.lang.String, java.lang.String)
+ */
+ public Results content(String query, String auths) {
+ log.info("Content query: " + query);
+ Connector connector = null;
+ if (null == instanceName || null == zooKeepers || null == username || null == password)
+ throw new EJBException("Required parameters not set. [instanceName = " + this.instanceName + ", zookeepers = " + this.zooKeepers + ", username = "
+ + this.username + ", password = " + this.password + "]. Check values in ejb-jar.xml");
+ Instance instance = new ZooKeeperInstance(this.instanceName, this.zooKeepers);
+ try {
+ log.info("Connecting to [instanceName = " + this.instanceName + ", zookeepers = " + this.zooKeepers + ", username = " + this.username + ", password = "
+ + this.password + "].");
+ connector = instance.getConnector(this.username, this.password.getBytes());
+ } catch (Exception e) {
+ throw new EJBException("Error getting connector from instance", e);
+ }
+
+ // Create list of auths
+ List<String> authorizations = new ArrayList<String>();
+ for (String a : auths.split(","))
+ authorizations.add(a);
+
+ ContentLogic table = new ContentLogic();
+ table.setTableName(tableName);
+ return table.runQuery(connector, query, authorizations);
+
+ }
+
+ /**
+ * calls the query logic with the parameters, returns results
+ *
+ * @param query
+ * @param auths
+ * @return
+ * @throws ParseException
+ */
+ public Results query(String query, String auths) {
+
+ Connector connector = null;
+ if (null == instanceName || null == zooKeepers || null == username || null == password)
+ throw new EJBException("Required parameters not set. [instanceName = " + this.instanceName + ", zookeepers = " + this.zooKeepers + ", username = "
+ + this.username + ", password = " + this.password + "]. Check values in ejb-jar.xml");
+ Instance instance = new ZooKeeperInstance(this.instanceName, this.zooKeepers);
+ try {
+ log.info("Connecting to [instanceName = " + this.instanceName + ", zookeepers = " + this.zooKeepers + ", username = " + this.username + ", password = "
+ + this.password + "].");
+ connector = instance.getConnector(this.username, this.password.getBytes());
+ } catch (Exception e) {
+ throw new EJBException("Error getting connector from instance", e);
+ }
+
+ // Create list of auths
+ List<String> authorizations = new ArrayList<String>();
+ for (String a : auths.split(","))
+ authorizations.add(a);
+
+ QueryLogic table = new QueryLogic();
+ table.setTableName(tableName);
+ table.setMetadataTableName(tableName + "Metadata");
+ table.setIndexTableName(tableName + "Index");
+ table.setReverseIndexTableName(tableName + "ReverseIndex");
+ table.setQueryThreads(threads);
+ table.setUnevaluatedFields("TEXT");
+ table.setNumPartitions(partitions);
+ table.setUseReadAheadIterator(false);
+ return table.runQuery(connector, authorizations, query, null, null, null);
+ }
+
}