View Javadoc

1   /*
2    * Copyright (c) 2010.  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.client.Result;
24  import org.apache.hadoop.hbase.client.ResultScanner;
25  import org.apache.hadoop.hbase.hbql.client.HBqlException;
26  import org.apache.hadoop.hbase.hbql.statement.select.RowRequest;
27  import org.apache.hadoop.hbase.hbql.util.CompletionQueue;
28  import org.apache.hadoop.hbase.hbql.util.NullIterator;
29  
30  import java.util.Iterator;
31  import java.util.List;
32  
33  
34  public class ResultScannerExecutorResultSet<T> extends HResultSetImpl<T, ResultScanner> {
35  
36      ResultScannerExecutorResultSet(final Query<T> query, final ResultScannerExecutor executor) throws HBqlException {
37          super(query, executor);
38      }
39  
40      protected void submitWork(final List<RowRequest> rowRequestList) {
41          for (final RowRequest rowRequest : rowRequestList) {
42              final Runnable job = new Runnable() {
43                  public void run() {
44                      try {
45                          final ResultScanner resultScanner = rowRequest.getResultScanner(getMappingContext().getMapping(),
46                                                                                          getWithArgs(),
47                                                                                          getTableWrapper().getHTable());
48                          getCompletionQueueExecutor().putElement(resultScanner);
49                      }
50                      catch (HBqlException e) {
51                          e.printStackTrace();
52                      }
53                      finally {
54                          getCompletionQueueExecutor().putCompletion();
55                      }
56                  }
57              };
58              this.getCompletionQueueExecutor().submitWorkToThreadPool(job);
59          }
60      }
61  
62  
63      public Iterator<T> iterator() {
64  
65          try {
66              return new ResultSetIterator<T, ResultScanner>(this) {
67  
68                  protected boolean moreResultsPending() {
69                      return getCompletionQueueExecutor().moreResultsPending();
70                  }
71  
72                  protected Iterator<Result> getNextResultIterator() throws HBqlException {
73                      final ResultScanner resultScanner;
74                      while (true) {
75                          final CompletionQueue.Element<ResultScanner> element = getCompletionQueueExecutor().takeElement();
76                          if (element.isCompletionToken()) {
77                              if (!moreResultsPending()) {
78                                  resultScanner = null;
79                                  break;
80                              }
81                          }
82                          else {
83                              resultScanner = element.getValue();
84                              break;
85                          }
86                      }
87  
88                      setCurrentResultScanner(resultScanner);
89                      return (getCurrentResultScanner() == null) ? null : getCurrentResultScanner().iterator();
90                  }
91              };
92          }
93          catch (HBqlException e) {
94              e.printStackTrace();
95              getQuery().callOnException(e);
96              return new NullIterator<T>();
97          }
98      }
99  }