001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver;
019
020import com.google.protobuf.Descriptors.MethodDescriptor;
021import com.google.protobuf.Message;
022import com.google.protobuf.Service;
023import com.google.protobuf.ServiceException;
024import java.io.IOException;
025import java.util.ArrayList;
026import java.util.Iterator;
027import java.util.List;
028import java.util.Map;
029import java.util.concurrent.TimeUnit;
030import org.apache.hadoop.conf.Configuration;
031import org.apache.hadoop.hbase.Cell;
032import org.apache.hadoop.hbase.CompareOperator;
033import org.apache.hadoop.hbase.HTableDescriptor;
034import org.apache.hadoop.hbase.TableName;
035import org.apache.hadoop.hbase.client.Append;
036import org.apache.hadoop.hbase.client.Delete;
037import org.apache.hadoop.hbase.client.Durability;
038import org.apache.hadoop.hbase.client.Get;
039import org.apache.hadoop.hbase.client.Increment;
040import org.apache.hadoop.hbase.client.Put;
041import org.apache.hadoop.hbase.client.RegionLocator;
042import org.apache.hadoop.hbase.client.Result;
043import org.apache.hadoop.hbase.client.ResultScanner;
044import org.apache.hadoop.hbase.client.Row;
045import org.apache.hadoop.hbase.client.RowMutations;
046import org.apache.hadoop.hbase.client.Scan;
047import org.apache.hadoop.hbase.client.Table;
048import org.apache.hadoop.hbase.client.TableDescriptor;
049import org.apache.hadoop.hbase.client.coprocessor.Batch.Call;
050import org.apache.hadoop.hbase.client.coprocessor.Batch.Callback;
051import org.apache.hadoop.hbase.client.metrics.ScanMetrics;
052import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
053import org.apache.hadoop.hbase.filter.Filter;
054import org.apache.hadoop.hbase.ipc.CoprocessorRpcChannel;
055
056/**
057 * An implementation of {@link Table} that sits directly on a Region; it decorates the passed in
058 * Region instance with the Table API. Some API is not implemented yet (throws
059 * {@link UnsupportedOperationException}) mostly because no need as yet or it necessitates copying a
060 * load of code local from RegionServer.
061 * <p>
062 * Use as an instance of a {@link Table} in-the-small -- no networking or servers necessary -- or to
063 * write a test that can run directly against the datastore and then over the network.
064 */
065public class RegionAsTable implements Table {
066  private final Region region;
067
068  /**
069   * @param region Region to decorate with Table API.
070   */
071  public RegionAsTable(final Region region) {
072    this.region = region;
073  }
074
075  @Override
076  public TableName getName() {
077    return this.region.getTableDescriptor().getTableName();
078  }
079
080  @Override
081  public Configuration getConfiguration() {
082    throw new UnsupportedOperationException();
083  }
084
085  @Override
086  @Deprecated
087  public HTableDescriptor getTableDescriptor() throws IOException {
088    return new HTableDescriptor(this.region.getTableDescriptor());
089  }
090
091  @Override
092  public TableDescriptor getDescriptor() throws IOException {
093    return this.region.getTableDescriptor();
094  }
095
096  @Override
097  public boolean exists(Get get) throws IOException {
098    if (!get.isCheckExistenceOnly()) throw new IllegalArgumentException();
099    return get(get) != null;
100  }
101
102  @Override
103  public boolean[] exists(List<Get> gets) throws IOException {
104    boolean[] results = new boolean[gets.size()];
105    int index = 0;
106    for (Get get : gets) {
107      results[index++] = exists(get);
108    }
109    return results;
110  }
111
112  @Override
113  public void batch(List<? extends Row> actions, Object[] results)
114    throws IOException, InterruptedException {
115    throw new UnsupportedOperationException();
116  }
117
118  @Override
119  public <R> void batchCallback(List<? extends Row> actions, Object[] results, Callback<R> callback)
120    throws IOException, InterruptedException {
121    throw new UnsupportedOperationException();
122  }
123
124  @Override
125  public Result get(Get get) throws IOException {
126    return this.region.get(get);
127  }
128
129  @Override
130  public Result[] get(List<Get> gets) throws IOException {
131    Result[] results = new Result[gets.size()];
132    int index = 0;
133    for (Get get : gets) {
134      results[index++] = get(get);
135    }
136    return results;
137  }
138
139  static class RegionScannerToResultScannerAdaptor implements ResultScanner {
140    private static final Result[] EMPTY_RESULT_ARRAY = new Result[0];
141    private final RegionScanner regionScanner;
142
143    RegionScannerToResultScannerAdaptor(final RegionScanner regionScanner) {
144      this.regionScanner = regionScanner;
145    }
146
147    @Override
148    public Iterator<Result> iterator() {
149      throw new UnsupportedOperationException();
150    }
151
152    @Override
153    public Result next() throws IOException {
154      List<Cell> cells = new ArrayList<>();
155      return regionScanner.next(cells) ? Result.create(cells) : null;
156    }
157
158    @Override
159    public Result[] next(int nbRows) throws IOException {
160      List<Result> results = new ArrayList<>(nbRows);
161      for (int i = 0; i < nbRows; i++) {
162        Result result = next();
163        if (result == null) break;
164        results.add(result);
165      }
166      return results.toArray(EMPTY_RESULT_ARRAY);
167    }
168
169    @Override
170    public void close() {
171      try {
172        regionScanner.close();
173      } catch (IOException e) {
174        throw new RuntimeException(e);
175      }
176    }
177
178    @Override
179    public boolean renewLease() {
180      throw new UnsupportedOperationException();
181    }
182
183    @Override
184    public ScanMetrics getScanMetrics() {
185      throw new UnsupportedOperationException();
186    }
187  };
188
189  @Override
190  public ResultScanner getScanner(Scan scan) throws IOException {
191    return new RegionScannerToResultScannerAdaptor(this.region.getScanner(scan));
192  }
193
194  @Override
195  public ResultScanner getScanner(byte[] family) throws IOException {
196    return getScanner(new Scan().addFamily(family));
197  }
198
199  @Override
200  public ResultScanner getScanner(byte[] family, byte[] qualifier) throws IOException {
201    return getScanner(new Scan().addColumn(family, qualifier));
202  }
203
204  @Override
205  public void put(Put put) throws IOException {
206    this.region.put(put);
207  }
208
209  @Override
210  public void put(List<Put> puts) throws IOException {
211    for (Put put : puts)
212      put(put);
213  }
214
215  @Override
216  @Deprecated
217  public boolean checkAndPut(byte[] row, byte[] family, byte[] qualifier, byte[] value, Put put)
218    throws IOException {
219    throw new UnsupportedOperationException();
220  }
221
222  @Override
223  @Deprecated
224  public boolean checkAndPut(byte[] row, byte[] family, byte[] qualifier, CompareOp compareOp,
225    byte[] value, Put put) throws IOException {
226    throw new UnsupportedOperationException();
227  }
228
229  @Override
230  @Deprecated
231  public boolean checkAndPut(byte[] row, byte[] family, byte[] qualifier, CompareOperator compareOp,
232    byte[] value, Put put) throws IOException {
233    throw new UnsupportedOperationException();
234  }
235
236  @Override
237  public void delete(Delete delete) throws IOException {
238    this.region.delete(delete);
239  }
240
241  @Override
242  public void delete(List<Delete> deletes) throws IOException {
243    for (Delete delete : deletes)
244      delete(delete);
245  }
246
247  @Override
248  public boolean checkAndDelete(byte[] row, byte[] family, byte[] qualifier, byte[] value,
249    Delete delete) throws IOException {
250    throw new UnsupportedOperationException();
251  }
252
253  @Override
254  @Deprecated
255  public boolean checkAndDelete(byte[] row, byte[] family, byte[] qualifier, CompareOp compareOp,
256    byte[] value, Delete delete) throws IOException {
257    throw new UnsupportedOperationException();
258  }
259
260  @Override
261  @Deprecated
262  public boolean checkAndDelete(byte[] row, byte[] family, byte[] qualifier,
263    CompareOperator compareOp, byte[] value, Delete delete) throws IOException {
264    throw new UnsupportedOperationException();
265  }
266
267  @Override
268  public CheckAndMutateBuilder checkAndMutate(byte[] row, byte[] family) {
269    throw new UnsupportedOperationException();
270  }
271
272  @Override
273  public CheckAndMutateWithFilterBuilder checkAndMutate(byte[] row, Filter filter) {
274    throw new UnsupportedOperationException();
275  }
276
277  @Override
278  public Result mutateRow(RowMutations rm) throws IOException {
279    throw new UnsupportedOperationException();
280  }
281
282  @Override
283  public Result append(Append append) throws IOException {
284    return this.region.append(append);
285  }
286
287  @Override
288  public Result increment(Increment increment) throws IOException {
289    return this.region.increment(increment);
290  }
291
292  @Override
293  public long incrementColumnValue(byte[] row, byte[] family, byte[] qualifier, long amount)
294    throws IOException {
295    throw new UnsupportedOperationException();
296  }
297
298  @Override
299  public long incrementColumnValue(byte[] row, byte[] family, byte[] qualifier, long amount,
300    Durability durability) throws IOException {
301    throw new UnsupportedOperationException();
302  }
303
304  /**
305   * This call will NOT close the underlying region.
306   */
307  @Override
308  public void close() throws IOException {
309  }
310
311  @Override
312  public CoprocessorRpcChannel coprocessorService(byte[] row) {
313    throw new UnsupportedOperationException();
314  }
315
316  @Override
317  public <T extends Service, R> Map<byte[], R> coprocessorService(Class<T> service, byte[] startKey,
318    byte[] endKey, Call<T, R> callable) throws ServiceException, Throwable {
319    throw new UnsupportedOperationException();
320  }
321
322  @Override
323  public <T extends Service, R> void coprocessorService(Class<T> service, byte[] startKey,
324    byte[] endKey, Call<T, R> callable, Callback<R> callback) throws ServiceException, Throwable {
325    throw new UnsupportedOperationException();
326  }
327
328  @Override
329  public <R extends Message> Map<byte[], R> batchCoprocessorService(
330    MethodDescriptor methodDescriptor, Message request, byte[] startKey, byte[] endKey,
331    R responsePrototype) throws ServiceException, Throwable {
332    throw new UnsupportedOperationException();
333  }
334
335  @Override
336  public <R extends Message> void batchCoprocessorService(MethodDescriptor methodDescriptor,
337    Message request, byte[] startKey, byte[] endKey, R responsePrototype, Callback<R> callback)
338    throws ServiceException, Throwable {
339    throw new UnsupportedOperationException();
340  }
341
342  @Override
343  @Deprecated
344  public boolean checkAndMutate(byte[] row, byte[] family, byte[] qualifier, CompareOp compareOp,
345    byte[] value, RowMutations mutation) throws IOException {
346    throw new UnsupportedOperationException();
347  }
348
349  @Override
350  @Deprecated
351  public boolean checkAndMutate(byte[] row, byte[] family, byte[] qualifier,
352    CompareOperator compareOp, byte[] value, RowMutations mutation) throws IOException {
353    throw new UnsupportedOperationException();
354  }
355
356  @Override
357  @Deprecated
358  public void setOperationTimeout(int operationTimeout) {
359    throw new UnsupportedOperationException();
360  }
361
362  @Override
363  @Deprecated
364  public int getOperationTimeout() {
365    throw new UnsupportedOperationException();
366  }
367
368  @Override
369  @Deprecated
370  public void setRpcTimeout(int rpcTimeout) {
371    throw new UnsupportedOperationException();
372  }
373
374  @Override
375  public long getReadRpcTimeout(TimeUnit unit) {
376    throw new UnsupportedOperationException();
377  }
378
379  @Override
380  @Deprecated
381  public void setWriteRpcTimeout(int writeRpcTimeout) {
382    throw new UnsupportedOperationException();
383  }
384
385  @Override
386  public long getOperationTimeout(TimeUnit unit) {
387    throw new UnsupportedOperationException();
388  }
389
390  @Override
391  @Deprecated
392  public void setReadRpcTimeout(int readRpcTimeout) {
393    throw new UnsupportedOperationException();
394  }
395
396  @Override
397  public long getWriteRpcTimeout(TimeUnit unit) {
398    throw new UnsupportedOperationException();
399  }
400
401  @Override
402  @Deprecated
403  public int getRpcTimeout() {
404    throw new UnsupportedOperationException();
405  }
406
407  @Override
408  public long getRpcTimeout(TimeUnit unit) {
409    throw new UnsupportedOperationException();
410  }
411
412  @Override
413  @Deprecated
414  public int getWriteRpcTimeout() {
415    throw new UnsupportedOperationException();
416  }
417
418  @Override
419  @Deprecated
420  public int getReadRpcTimeout() {
421    throw new UnsupportedOperationException();
422  }
423
424  @Override
425  public RegionLocator getRegionLocator() throws IOException {
426    throw new UnsupportedOperationException();
427  }
428}