View Javadoc

1   /*
2    * Copyright (c) 2011.  The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.expreval.expr;
22  
23  import org.apache.expreval.client.NullColumnValueException;
24  import org.apache.expreval.client.ResultMissingColumnException;
25  import org.apache.expreval.expr.literal.BooleanLiteral;
26  import org.apache.expreval.expr.node.BooleanValue;
27  import org.apache.expreval.expr.node.GenericValue;
28  import org.apache.hadoop.hbase.filter.Filter;
29  import org.apache.hadoop.hbase.hbql.client.HBqlException;
30  import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
31  import org.apache.hadoop.hbase.hbql.mapping.Mapping;
32  import org.apache.hadoop.hbase.hbql.mapping.MappingContext;
33  
34  public class ExpressionTree extends MultipleExpressionContext {
35  
36      private static FunctionTypeSignature exprSig = new FunctionTypeSignature(BooleanValue.class, BooleanValue.class);
37  
38      private boolean useResultData = false;
39      private boolean allowColumns  = true;
40  
41      private Mapping embeddedMapping = null;
42  
43      public ExpressionTree() {
44      }
45  
46      private ExpressionTree(final GenericValue rootValue) {
47          super(exprSig, rootValue);
48      }
49  
50      public static ExpressionTree newExpressionTree(final Mapping mapping, final GenericValue booleanValue) {
51          final ExpressionTree tree = new ExpressionTree(booleanValue == null ? new BooleanLiteral(true) : booleanValue);
52          // This is stashed until validate() is called.  This avoids throwing HBqlException in parser
53          tree.embeddedMapping = mapping;
54          return tree;
55      }
56  
57      // This is not done in newExpressionTree() because that is called from the parser
58      public void setEmbeddedMapping() throws HBqlException {
59          if (this.embeddedMapping != null)
60              this.setMappingContext(new MappingContext(this.embeddedMapping));
61      }
62  
63      public Boolean evaluate(final HConnectionImpl conn, final Object object) throws HBqlException,
64                                                                                      ResultMissingColumnException,
65                                                                                      NullColumnValueException {
66          return (Boolean)this.evaluate(conn, 0, this.allowColumns(), false, object);
67      }
68  
69      private GenericValue getGenericValue() {
70          return this.getGenericValue(0);
71      }
72  
73      public Filter getFilter() throws HBqlException {
74          this.validateTypes(true, true);
75          this.optimize();
76          return this.getGenericValue().getFilter();
77      }
78  
79      public String asString() {
80          return this.getGenericValue().asString();
81      }
82  
83      public void setUseResultData(final boolean useResultData) {
84          this.useResultData = useResultData;
85      }
86  
87      public boolean useResultData() {
88          return this.useResultData;
89      }
90  
91      public boolean allowColumns() {
92          return this.allowColumns;
93      }
94  
95      public void setAllowColumns(final boolean allowColumns) {
96          this.allowColumns = allowColumns;
97      }
98  }