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.yaoql.client;
22  
23  import org.apache.expreval.client.NullColumnValueException;
24  import org.apache.expreval.client.ResultMissingColumnException;
25  import org.apache.expreval.expr.ExpressionTree;
26  import org.apache.hadoop.hbase.client.Result;
27  import org.apache.hadoop.hbase.hbql.client.HBqlException;
28  import org.apache.hadoop.hbase.hbql.impl.ResultSetIterator;
29  import org.apache.hadoop.hbase.hbql.util.NullIterator;
30  import org.apache.yaoql.impl.ObjectQueryImpl;
31  
32  import java.util.Collection;
33  import java.util.Iterator;
34  
35  public class ObjectResultSet<T> implements Iterable<T> {
36  
37      private final ObjectQueryImpl<T> objectQuery;
38      private final Collection<T>      objects;
39      private final ExpressionTree     expressionTree;
40  
41      private Iterator<T> objectIter = null;
42  
43      public ObjectResultSet(final ObjectQueryImpl<T> objectQuery, final Collection<T> objects) throws HBqlException {
44          this.objectQuery = objectQuery;
45          this.objects = objects;
46  
47          // In theory, this should be done only once and in ObjectQuery, but
48          // since it requires the objects to get the mapping, I do it here
49          this.expressionTree = getObjectQuery().getExpressionTree(this.getObjects());
50      }
51  
52      private ObjectQueryImpl<T> getObjectQuery() {
53          return this.objectQuery;
54      }
55  
56      private Collection<T> getObjects() {
57          return this.objects;
58      }
59  
60      private Iterator<T> getObjectIter() {
61          return this.objectIter;
62      }
63  
64      private void setObjectIter(final Iterator<T> objectIter) {
65          this.objectIter = objectIter;
66      }
67  
68      private ExpressionTree getExpressionTree() {
69          return this.expressionTree;
70      }
71  
72      public Iterator<T> iterator() {
73  
74          try {
75              return new ResultSetIterator<T, Object>(null) {
76  
77                  protected T fetchNextObject() throws HBqlException {
78  
79                      if (getObjectIter() == null)
80                          setObjectIter(getObjects().iterator());
81  
82                      while (getObjectIter().hasNext()) {
83                          final T val = getObjectIter().next();
84                          try {
85                              if (getExpressionTree() == null || getExpressionTree().evaluate(null, val))
86                                  return getObjectQuery().callOnEachObject(val);
87                          }
88                          catch (ResultMissingColumnException e) {
89                              // Just skip and do nothing
90                          }
91                          catch (NullColumnValueException e) {
92                              // Just skip and do nothing
93                          }
94                      }
95  
96                      this.markIteratorComplete();
97                      return null;
98                  }
99  
100                 protected void setNextObject(final T nextObject, final boolean fromExceptionCatch) {
101                     this.setNextObject(nextObject);
102                 }
103 
104                 protected boolean moreResultsPending() {
105                     return false;
106                 }
107 
108                 protected Iterator<Result> getNextResultIterator() throws HBqlException {
109                     return null;
110                 }
111 
112                 protected void iteratorCompleteAction() {
113                     getObjectQuery().callOnQueryComplete();
114                 }
115             };
116         }
117         catch (HBqlException e) {
118             e.printStackTrace();
119             return new NullIterator<T>();
120         }
121     }
122 }