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.hadoop.hbase.hbql.impl;
22  
23  import org.apache.hadoop.hbase.hbql.client.HBqlException;
24  import org.apache.hadoop.hbase.hbql.client.HRecord;
25  import org.apache.hadoop.hbase.hbql.client.HResultSet;
26  import org.apache.hadoop.hbase.hbql.client.QueryListener;
27  import org.apache.hadoop.hbase.hbql.mapping.ColumnAttrib;
28  import org.apache.hadoop.hbase.hbql.mapping.HRecordResultAccessor;
29  import org.apache.hadoop.hbase.hbql.mapping.ResultAccessor;
30  import org.apache.hadoop.hbase.hbql.statement.SelectStatement;
31  import org.apache.hadoop.hbase.hbql.statement.args.WithArgs;
32  import org.apache.hadoop.hbase.hbql.statement.select.RowRequest;
33  import org.apache.hadoop.hbase.hbql.util.Lists;
34  import org.apache.hadoop.hbase.hbql.util.Sets;
35  
36  import java.util.List;
37  import java.util.Set;
38  
39  public class Query<T> {
40  
41      private final HConnectionImpl        connection;
42      private final SelectStatement        selectStatement;
43      private final List<QueryListener<T>> queryListeners;
44  
45      private Query(final HConnectionImpl conn,
46                    final SelectStatement selectStatement,
47                    final QueryListener<T>... queryListeners) throws HBqlException {
48          this.connection = conn;
49          this.selectStatement = selectStatement;
50  
51          if (queryListeners.length > 0)
52              this.queryListeners = Lists.newArrayList();
53          else
54              this.queryListeners = null;
55  
56          for (final QueryListener<T> listener : queryListeners)
57              this.getQueryListeners().add(listener);
58  
59          this.callOnQueryInit();
60  
61          this.getSelectStmt().validate(this.getHConnectionImpl());
62          this.getSelectStmt().validateTypes();
63      }
64  
65      public static <E> Query<E> newQuery(final HConnectionImpl conn,
66                                          final SelectStatement selectStatement,
67                                          final Class clazz,
68                                          final QueryListener<E>... listeners) throws HBqlException {
69          final ResultAccessor accessor;
70          if (clazz.equals(HRecord.class)) {
71              accessor = new HRecordResultAccessor(selectStatement.getMappingContext());
72          }
73          else {
74              accessor = conn.getAnnotationMapping(clazz);
75              if (accessor == null)
76                  throw new HBqlException("Unknown class " + clazz.getName());
77          }
78  
79          selectStatement.getMappingContext().setResultAccessor(accessor);
80  
81          return new Query<E>(conn, selectStatement, listeners);
82      }
83  
84      public HConnectionImpl getHConnectionImpl() {
85          return this.connection;
86      }
87  
88      public SelectStatement getSelectStmt() {
89          return this.selectStatement;
90      }
91  
92      public List<RowRequest> getRowRequestList() throws HBqlException {
93  
94          // Get list of all columns that are used in select list and expr tree
95          final Set<ColumnAttrib> allAttribs = Sets.newHashSet();
96          allAttribs.addAll(this.getSelectStmt().getSelectAttribList());
97  
98          final WithArgs withArgs = this.getSelectStmt().getWithArgs();
99          allAttribs.addAll(withArgs.getColumnsUsedInAllWhereExprs());
100 
101         return withArgs.getRowRequestList(this.getHConnectionImpl(),
102                                           this.getSelectStmt().getMappingContext().getMapping(),
103                                           allAttribs);
104     }
105 
106     public List<QueryListener<T>> getQueryListeners() {
107         return this.queryListeners;
108     }
109 
110     public HResultSet<T> newResultSet(final boolean ignoreQueryExecutor) throws HBqlException {
111         if (!this.getHConnectionImpl().usesQueryExecutor() || ignoreQueryExecutor) {
112             return new NonExecutorResultSet<T>(this);
113         }
114         else {
115             // This may block waiting for a Executor to become available from the ExecutorPool or SingletonQueue
116             final CompletionQueueExecutor executor = this.getHConnectionImpl().getQueryExecutorForConnection();
117 
118             if (executor.threadsReadResults())
119                 return new ResultExecutorResultSet<T>(this, (ResultExecutor)executor);
120             else
121                 return new ResultScannerExecutorResultSet<T>(this, (ResultScannerExecutor)executor);
122         }
123     }
124 
125     private void callOnQueryInit() {
126         if (this.getQueryListeners() != null) {
127             for (final QueryListener<T> listener : this.getQueryListeners()) {
128                 try {
129                     listener.onQueryStart();
130                 }
131                 catch (HBqlException e) {
132                     listener.onException(QueryListener.ExceptionSource.QUERYSTART, e);
133                 }
134             }
135         }
136     }
137 
138     protected T callOnEachRow(T val) {
139         if (this.getQueryListeners() != null) {
140             for (final QueryListener<T> listener : this.getQueryListeners()) {
141                 try {
142                     listener.onEachRow(val);
143                 }
144                 catch (HBqlException e) {
145                     listener.onException(QueryListener.ExceptionSource.ONEACHROW, e);
146                 }
147             }
148         }
149         return val;
150     }
151 
152     protected void callOnQueryComplete() {
153         if (this.getQueryListeners() != null) {
154             for (final QueryListener<T> listener : this.getQueryListeners()) {
155                 try {
156                     listener.onQueryComplete();
157                 }
158                 catch (HBqlException e) {
159                     listener.onException(QueryListener.ExceptionSource.QUERYCOMPLETE, e);
160                 }
161             }
162         }
163     }
164 
165     protected void callOnException(HBqlException e) {
166         if (this.getQueryListeners() != null) {
167             for (final QueryListener<T> listener : this.getQueryListeners()) {
168                 listener.onException(QueryListener.ExceptionSource.ITERATOR, e);
169             }
170         }
171     }
172 }