1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
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 }