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 java.io.IOException;
021import java.util.ArrayList;
022import java.util.Iterator;
023import java.util.List;
024import java.util.Map;
025import java.util.concurrent.TimeUnit;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.Cell;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.Append;
030import org.apache.hadoop.hbase.client.Delete;
031import org.apache.hadoop.hbase.client.Durability;
032import org.apache.hadoop.hbase.client.Get;
033import org.apache.hadoop.hbase.client.Increment;
034import org.apache.hadoop.hbase.client.Put;
035import org.apache.hadoop.hbase.client.RegionLocator;
036import org.apache.hadoop.hbase.client.Result;
037import org.apache.hadoop.hbase.client.ResultScanner;
038import org.apache.hadoop.hbase.client.Row;
039import org.apache.hadoop.hbase.client.RowMutations;
040import org.apache.hadoop.hbase.client.Scan;
041import org.apache.hadoop.hbase.client.Table;
042import org.apache.hadoop.hbase.client.TableDescriptor;
043import org.apache.hadoop.hbase.client.coprocessor.Batch.Call;
044import org.apache.hadoop.hbase.client.coprocessor.Batch.Callback;
045import org.apache.hadoop.hbase.client.metrics.ScanMetrics;
046import org.apache.hadoop.hbase.filter.Filter;
047import org.apache.hadoop.hbase.ipc.CoprocessorRpcChannel;
048
049import org.apache.hbase.thirdparty.com.google.protobuf.Descriptors.MethodDescriptor;
050import org.apache.hbase.thirdparty.com.google.protobuf.Message;
051import org.apache.hbase.thirdparty.com.google.protobuf.Service;
052import org.apache.hbase.thirdparty.com.google.protobuf.ServiceException;
053
054/**
055 * An implementation of {@link Table} that sits directly on a Region; it decorates the passed in
056 * Region instance with the Table API. Some API is not implemented yet (throws
057 * {@link UnsupportedOperationException}) mostly because no need as yet or it necessitates copying
058 * a load of code local from RegionServer.
059 * 
060 * <p>Use as an instance of a {@link Table} in-the-small -- no networking or servers
061 * necessary -- or to write a test that can run directly against the datastore and then
062 * over the network.
063 */
064public class RegionAsTable implements Table {
065  private final Region region;
066
067  /**
068   * @param region Region to decorate with Table API.
069   */
070  public RegionAsTable(final Region region) {
071    this.region = region;
072  }
073
074  @Override
075  public TableName getName() {
076    return this.region.getTableDescriptor().getTableName();
077  }
078
079  @Override
080  public Configuration getConfiguration() {
081    throw new UnsupportedOperationException();
082  }
083
084  @Override
085  public TableDescriptor getDescriptor() throws IOException {
086    return this.region.getTableDescriptor();
087  }
088
089  @Override
090  public boolean exists(Get get) throws IOException {
091    if (!get.isCheckExistenceOnly()) throw new IllegalArgumentException();
092    return get(get) != null;
093  }
094
095  @Override
096  public boolean[] exists(List<Get> gets) throws IOException {
097    boolean [] results = new boolean[gets.size()];
098    int index = 0;
099    for (Get get: gets) {
100      results[index++] = exists(get);
101    }
102    return results;
103  }
104
105  @Override
106  public void batch(List<? extends Row> actions, Object[] results)
107  throws IOException, InterruptedException {
108    throw new UnsupportedOperationException();
109  }
110
111  @Override
112  public <R> void batchCallback(List<? extends Row> actions, Object[] results,
113      Callback<R> callback)
114  throws IOException, InterruptedException {
115    throw new UnsupportedOperationException();
116  }
117
118  @Override
119  public Result get(Get get) throws IOException {
120    return this.region.get(get);
121  }
122
123  @Override
124  public Result[] get(List<Get> gets) throws IOException {
125    Result [] results = new Result[gets.size()];
126    int index = 0;
127    for (Get get: gets) {
128      results[index++] = get(get);
129    }
130    return results;
131  }
132
133  static class RegionScannerToResultScannerAdaptor implements ResultScanner {
134    private static final Result [] EMPTY_RESULT_ARRAY = new Result[0];
135    private final RegionScanner regionScanner;
136
137    RegionScannerToResultScannerAdaptor(final RegionScanner regionScanner) {
138      this.regionScanner = regionScanner;
139    }
140
141    @Override
142    public Iterator<Result> iterator() {
143      throw new UnsupportedOperationException();
144    }
145
146    @Override
147    public Result next() throws IOException {
148      List<Cell> cells = new ArrayList<>();
149      return regionScanner.next(cells)? Result.create(cells): null;
150    }
151
152    @Override
153    public Result[] next(int nbRows) throws IOException {
154      List<Result> results = new ArrayList<>(nbRows);
155      for (int i = 0; i < nbRows; i++) {
156        Result result = next();
157        if (result == null) break;
158        results.add(result);
159      }
160      return results.toArray(EMPTY_RESULT_ARRAY);
161    }
162
163    @Override
164    public void close() {
165      try {
166        regionScanner.close();
167      } catch (IOException e) {
168        throw new RuntimeException(e);
169      }
170    }
171
172    @Override
173    public boolean renewLease() {
174      throw new UnsupportedOperationException();
175    }
176
177    @Override
178    public ScanMetrics getScanMetrics() {
179      throw new UnsupportedOperationException();
180    }
181  }
182
183  @Override
184  public ResultScanner getScanner(Scan scan) throws IOException {
185    return new RegionScannerToResultScannerAdaptor(this.region.getScanner(scan));
186  }
187
188  @Override
189  public ResultScanner getScanner(byte[] family) throws IOException {
190    return getScanner(new Scan().addFamily(family));
191  }
192
193  @Override
194  public ResultScanner getScanner(byte[] family, byte[] qualifier) throws IOException {
195    return getScanner(new Scan().addColumn(family, qualifier));
196  }
197
198  @Override
199  public void put(Put put) throws IOException {
200    this.region.put(put);
201  }
202
203  @Override
204  public void put(List<Put> puts) throws IOException {
205    for (Put put: puts) put(put);
206  }
207
208  @Override
209  public void delete(Delete delete) throws IOException {
210    this.region.delete(delete);
211  }
212
213  @Override
214  public void delete(List<Delete> deletes) throws IOException {
215    for(Delete delete: deletes) delete(delete);
216  }
217
218  @Override
219  public CheckAndMutateBuilder checkAndMutate(byte[] row, byte[] family) {
220    throw new UnsupportedOperationException();
221  }
222
223  @Override
224  public CheckAndMutateWithFilterBuilder checkAndMutate(byte[] row, Filter filter) {
225    throw new UnsupportedOperationException();
226  }
227
228  @Override
229  public Result mutateRow(RowMutations rm) throws IOException {
230    throw new UnsupportedOperationException();
231  }
232
233  @Override
234  public Result append(Append append) throws IOException {
235    return this.region.append(append);
236  }
237
238  @Override
239  public Result increment(Increment increment) throws IOException {
240    return this.region.increment(increment);
241  }
242
243  @Override
244  public long incrementColumnValue(byte[] row, byte[] family, byte[] qualifier, long amount)
245  throws IOException {
246    throw new UnsupportedOperationException();
247  }
248
249  @Override
250  public long incrementColumnValue(byte[] row, byte[] family, byte[] qualifier, long amount,
251      Durability durability)
252  throws IOException {
253    throw new UnsupportedOperationException();
254  }
255
256  /**
257   * This call will NOT close the underlying region.
258   */
259  @Override
260  public void close() throws IOException {
261  }
262
263  @Override
264  public CoprocessorRpcChannel coprocessorService(byte[] row) {
265    throw new UnsupportedOperationException();
266  }
267
268  @Override
269  public <T extends Service, R> Map<byte[], R> coprocessorService(Class<T> service, byte[] startKey,
270      byte[] endKey, Call<T, R> callable)
271  throws ServiceException, Throwable {
272    throw new UnsupportedOperationException();
273  }
274
275  @Override
276  public <T extends Service, R> void coprocessorService(Class<T> service, byte[] startKey,
277      byte[] endKey, Call<T, R> callable, Callback<R> callback)
278  throws ServiceException, Throwable {
279    throw new UnsupportedOperationException();
280  }
281
282  @Override
283  public <R extends Message> Map<byte[], R> batchCoprocessorService(MethodDescriptor
284      methodDescriptor, Message request,
285      byte[] startKey, byte[] endKey, R responsePrototype)
286  throws ServiceException, Throwable {
287    throw new UnsupportedOperationException();
288  }
289
290  @Override
291  public <R extends Message> void batchCoprocessorService(MethodDescriptor methodDescriptor,
292      Message request, byte[] startKey, byte[] endKey, R responsePrototype, Callback<R> callback)
293  throws ServiceException, Throwable {
294    throw new UnsupportedOperationException();
295  }
296
297  @Override
298  public long getReadRpcTimeout(TimeUnit unit) {
299    throw new UnsupportedOperationException();
300  }
301
302  @Override
303  public long getOperationTimeout(TimeUnit unit) {
304    throw new UnsupportedOperationException();
305  }
306
307  @Override
308  public long getWriteRpcTimeout(TimeUnit unit) {
309    throw new UnsupportedOperationException();
310  }
311
312  @Override
313  public long getRpcTimeout(TimeUnit unit) {
314    throw new UnsupportedOperationException();
315  }
316
317  @Override
318  public RegionLocator getRegionLocator() throws IOException {
319    throw new UnsupportedOperationException();
320  }
321}