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.statement.select;
22  
23  import org.apache.hadoop.hbase.HConstants;
24  import org.apache.hadoop.hbase.client.HTableInterface;
25  import org.apache.hadoop.hbase.client.ResultScanner;
26  import org.apache.hadoop.hbase.client.tableindexed.IndexedTable;
27  import org.apache.hadoop.hbase.hbql.client.HBqlException;
28  import org.apache.hadoop.hbase.hbql.client.Util;
29  import org.apache.hadoop.hbase.hbql.mapping.ColumnAttrib;
30  import org.apache.hadoop.hbase.hbql.mapping.Mapping;
31  import org.apache.hadoop.hbase.hbql.mapping.TableMapping;
32  import org.apache.hadoop.hbase.hbql.statement.args.WithArgs;
33  import org.apache.hadoop.hbase.hbql.util.Lists;
34  import org.apache.hadoop.hbase.util.Bytes;
35  
36  import java.io.IOException;
37  import java.util.List;
38  import java.util.Set;
39  
40  public class IndexRequest implements RowRequest {
41  
42      private final byte[]            startRow;
43      private final byte[]            stopRow;
44      private final Set<ColumnAttrib> columnAttribs;
45  
46      public IndexRequest(final byte[] startRow, final byte[] stopRow, final Set<ColumnAttrib> columnAttribs) {
47          this.startRow = startRow;
48          this.stopRow = stopRow;
49          this.columnAttribs = columnAttribs;
50      }
51  
52      private Set<ColumnAttrib> getColumnAttribs() {
53          return this.columnAttribs;
54      }
55  
56      private byte[] getStartRow() {
57          return this.startRow;
58      }
59  
60      private byte[] getStopRow() {
61          return this.stopRow;
62      }
63  
64      private byte[][] getColumns() throws HBqlException {
65  
66          final byte[][] attribs;
67  
68          if (this.getColumnAttribs() == null) {
69              attribs = null;
70          }
71          else {
72              final List<String> columnList = Lists.newArrayList();
73              for (final ColumnAttrib columnAttrib : this.getColumnAttribs())
74                  columnList.add(columnAttrib.isASelectFamilyAttrib() ? columnAttrib.getFamilyName()
75                                                                      : columnAttrib.getFamilyQualifiedName());
76              attribs = Util.getStringsAsBytes(columnList);
77          }
78          return attribs;
79      }
80  
81      public ResultScanner getResultScanner(final Mapping mapping,
82                                            final WithArgs withArgs,
83                                            final HTableInterface table) throws HBqlException {
84  
85          final IndexedTable index = (IndexedTable)table;
86  
87          byte[] startKey = null;
88          byte[] stopKey = null;
89  
90          if (this.getStartRow() != HConstants.EMPTY_START_ROW) {
91              final TableMapping tableMapping = (TableMapping)mapping;
92              tableMapping.validateKeyInfo(withArgs.getIndexName());
93              final int width = tableMapping.getKeyInfo().getWidth();
94              startKey = Bytes.add(this.getStartRow(), Util.getFixedWidthString(Character.MIN_VALUE, width));
95          }
96  
97          if (this.getStopRow() != HConstants.EMPTY_END_ROW) {
98              final TableMapping tableMapping = (TableMapping)mapping;
99              tableMapping.validateKeyInfo(withArgs.getIndexName());
100             final int width = tableMapping.getKeyInfo().getWidth();
101             stopKey = Bytes.add(this.getStopRow(), Util.getFixedWidthString(Character.MAX_VALUE, width));
102         }
103 
104         try {
105             return index.getIndexedScanner(withArgs.getIndexName(),
106                                            startKey,
107                                            stopKey,
108                                            withArgs.getColumnsUsedInIndexWhereExpr(),
109                                            withArgs.getFilterForTableIndex(),
110                                            getColumns());
111         }
112         catch (IOException e) {
113             throw new HBqlException(e);
114         }
115     }
116 }