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 060 * a load of code local from RegionServer. 061 * 062 * <p>Use as an instance of a {@link Table} in-the-small -- no networking or servers 063 * necessary -- or to write a test that can run directly against the datastore and then 064 * over the network. 065 */ 066public class RegionAsTable implements Table { 067 private final Region region; 068 069 /** 070 * @param region Region to decorate with Table API. 071 */ 072 public RegionAsTable(final Region region) { 073 this.region = region; 074 } 075 076 @Override 077 public TableName getName() { 078 return this.region.getTableDescriptor().getTableName(); 079 } 080 081 @Override 082 public Configuration getConfiguration() { 083 throw new UnsupportedOperationException(); 084 } 085 086 @Override 087 @Deprecated 088 public HTableDescriptor getTableDescriptor() throws IOException { 089 return new HTableDescriptor(this.region.getTableDescriptor()); 090 } 091 092 @Override 093 public TableDescriptor getDescriptor() throws IOException { 094 return this.region.getTableDescriptor(); 095 } 096 097 @Override 098 public boolean exists(Get get) throws IOException { 099 if (!get.isCheckExistenceOnly()) throw new IllegalArgumentException(); 100 return get(get) != null; 101 } 102 103 @Override 104 public boolean[] exists(List<Get> gets) throws IOException { 105 boolean [] results = new boolean[gets.size()]; 106 int index = 0; 107 for (Get get: gets) { 108 results[index++] = exists(get); 109 } 110 return results; 111 } 112 113 @Override 114 public void batch(List<? extends Row> actions, Object[] results) 115 throws IOException, InterruptedException { 116 throw new UnsupportedOperationException(); 117 } 118 119 @Override 120 public <R> void batchCallback(List<? extends Row> actions, Object[] results, 121 Callback<R> callback) 122 throws IOException, InterruptedException { 123 throw new UnsupportedOperationException(); 124 } 125 126 @Override 127 public Result get(Get get) throws IOException { 128 return this.region.get(get); 129 } 130 131 @Override 132 public Result[] get(List<Get> gets) throws IOException { 133 Result [] results = new Result[gets.size()]; 134 int index = 0; 135 for (Get get: gets) { 136 results[index++] = get(get); 137 } 138 return results; 139 } 140 141 static class RegionScannerToResultScannerAdaptor implements ResultScanner { 142 private static final Result [] EMPTY_RESULT_ARRAY = new Result[0]; 143 private final RegionScanner regionScanner; 144 145 RegionScannerToResultScannerAdaptor(final RegionScanner regionScanner) { 146 this.regionScanner = regionScanner; 147 } 148 149 @Override 150 public Iterator<Result> iterator() { 151 throw new UnsupportedOperationException(); 152 } 153 154 @Override 155 public Result next() throws IOException { 156 List<Cell> cells = new ArrayList<>(); 157 return regionScanner.next(cells)? Result.create(cells): null; 158 } 159 160 @Override 161 public Result[] next(int nbRows) throws IOException { 162 List<Result> results = new ArrayList<>(nbRows); 163 for (int i = 0; i < nbRows; i++) { 164 Result result = next(); 165 if (result == null) break; 166 results.add(result); 167 } 168 return results.toArray(EMPTY_RESULT_ARRAY); 169 } 170 171 @Override 172 public void close() { 173 try { 174 regionScanner.close(); 175 } catch (IOException e) { 176 throw new RuntimeException(e); 177 } 178 } 179 180 @Override 181 public boolean renewLease() { 182 throw new UnsupportedOperationException(); 183 } 184 185 @Override 186 public ScanMetrics getScanMetrics() { 187 throw new UnsupportedOperationException(); 188 } 189 }; 190 191 @Override 192 public ResultScanner getScanner(Scan scan) throws IOException { 193 return new RegionScannerToResultScannerAdaptor(this.region.getScanner(scan)); 194 } 195 196 @Override 197 public ResultScanner getScanner(byte[] family) throws IOException { 198 return getScanner(new Scan().addFamily(family)); 199 } 200 201 @Override 202 public ResultScanner getScanner(byte[] family, byte[] qualifier) throws IOException { 203 return getScanner(new Scan().addColumn(family, qualifier)); 204 } 205 206 @Override 207 public void put(Put put) throws IOException { 208 this.region.put(put); 209 } 210 211 @Override 212 public void put(List<Put> puts) throws IOException { 213 for (Put put: puts) put(put); 214 } 215 216 @Override 217 @Deprecated 218 public boolean checkAndPut(byte[] row, byte[] family, byte[] qualifier, byte[] value, Put put) 219 throws IOException { 220 throw new UnsupportedOperationException(); 221 } 222 223 @Override 224 @Deprecated 225 public boolean checkAndPut(byte[] row, byte[] family, byte[] qualifier, CompareOp compareOp, 226 byte[] value, Put put) 227 throws IOException { 228 throw new UnsupportedOperationException(); 229 } 230 231 @Override 232 @Deprecated 233 public boolean checkAndPut(byte[] row, byte[] family, byte[] qualifier, 234 CompareOperator compareOp, byte[] value, Put put) 235 throws IOException { 236 throw new UnsupportedOperationException(); 237 } 238 239 @Override 240 public void delete(Delete delete) throws IOException { 241 this.region.delete(delete); 242 } 243 244 @Override 245 public void delete(List<Delete> deletes) throws IOException { 246 for(Delete delete: deletes) delete(delete); 247 } 248 249 @Override 250 public boolean checkAndDelete(byte[] row, byte[] family, byte[] qualifier, byte[] value, 251 Delete delete) 252 throws IOException { 253 throw new UnsupportedOperationException(); 254 } 255 256 @Override 257 @Deprecated 258 public boolean checkAndDelete(byte[] row, byte[] family, byte[] qualifier, 259 CompareOp compareOp, byte[] value, Delete delete) 260 throws IOException { 261 throw new UnsupportedOperationException(); 262 } 263 264 @Override 265 @Deprecated 266 public boolean checkAndDelete(byte[] row, byte[] family, byte[] qualifier, 267 CompareOperator compareOp, byte[] value, Delete delete) 268 throws IOException { 269 throw new UnsupportedOperationException(); 270 } 271 272 @Override 273 public CheckAndMutateBuilder checkAndMutate(byte[] row, byte[] family) { 274 throw new UnsupportedOperationException(); 275 } 276 277 @Override 278 public CheckAndMutateWithFilterBuilder checkAndMutate(byte[] row, Filter filter) { 279 throw new UnsupportedOperationException(); 280 } 281 282 @Override 283 public Result mutateRow(RowMutations rm) throws IOException { 284 throw new UnsupportedOperationException(); 285 } 286 287 @Override 288 public Result append(Append append) throws IOException { 289 return this.region.append(append); 290 } 291 292 @Override 293 public Result increment(Increment increment) throws IOException { 294 return this.region.increment(increment); 295 } 296 297 @Override 298 public long incrementColumnValue(byte[] row, byte[] family, byte[] qualifier, long amount) 299 throws IOException { 300 throw new UnsupportedOperationException(); 301 } 302 303 @Override 304 public long incrementColumnValue(byte[] row, byte[] family, byte[] qualifier, long amount, 305 Durability durability) 306 throws IOException { 307 throw new UnsupportedOperationException(); 308 } 309 310 /** 311 * This call will NOT close the underlying region. 312 */ 313 @Override 314 public void close() throws IOException { 315 } 316 317 @Override 318 public CoprocessorRpcChannel coprocessorService(byte[] row) { 319 throw new UnsupportedOperationException(); 320 } 321 322 @Override 323 public <T extends Service, R> Map<byte[], R> coprocessorService(Class<T> service, byte[] startKey, 324 byte[] endKey, Call<T, R> callable) 325 throws ServiceException, Throwable { 326 throw new UnsupportedOperationException(); 327 } 328 329 @Override 330 public <T extends Service, R> void coprocessorService(Class<T> service, byte[] startKey, 331 byte[] endKey, Call<T, R> callable, Callback<R> callback) 332 throws ServiceException, Throwable { 333 throw new UnsupportedOperationException(); 334 } 335 336 @Override 337 public <R extends Message> Map<byte[], R> batchCoprocessorService(MethodDescriptor 338 methodDescriptor, Message request, 339 byte[] startKey, byte[] endKey, R responsePrototype) 340 throws ServiceException, Throwable { 341 throw new UnsupportedOperationException(); 342 } 343 344 @Override 345 public <R extends Message> void batchCoprocessorService(MethodDescriptor methodDescriptor, 346 Message request, byte[] startKey, byte[] endKey, R responsePrototype, Callback<R> callback) 347 throws ServiceException, Throwable { 348 throw new UnsupportedOperationException(); 349 } 350 351 @Override 352 @Deprecated 353 public boolean checkAndMutate(byte[] row, byte[] family, byte[] qualifier, CompareOp compareOp, 354 byte[] value, RowMutations mutation) 355 throws IOException { 356 throw new UnsupportedOperationException(); 357 } 358 359 @Override 360 @Deprecated 361 public boolean checkAndMutate(byte[] row, byte[] family, byte[] qualifier, 362 CompareOperator compareOp, byte[] value, RowMutations mutation) throws IOException { 363 throw new UnsupportedOperationException(); 364 } 365 366 @Override 367 @Deprecated 368 public void setOperationTimeout(int operationTimeout) { 369 throw new UnsupportedOperationException(); 370 } 371 372 @Override 373 @Deprecated 374 public int getOperationTimeout() { 375 throw new UnsupportedOperationException(); 376 } 377 378 @Override 379 @Deprecated 380 public void setRpcTimeout(int rpcTimeout) { 381 throw new UnsupportedOperationException(); 382 } 383 384 @Override 385 public long getReadRpcTimeout(TimeUnit unit) { 386 throw new UnsupportedOperationException(); 387 } 388 389 @Override 390 @Deprecated 391 public void setWriteRpcTimeout(int writeRpcTimeout) {throw new UnsupportedOperationException(); } 392 393 @Override 394 public long getOperationTimeout(TimeUnit unit) { 395 throw new UnsupportedOperationException(); 396 } 397 398 @Override 399 @Deprecated 400 public void setReadRpcTimeout(int readRpcTimeout) {throw new UnsupportedOperationException(); } 401 402 @Override 403 public long getWriteRpcTimeout(TimeUnit unit) { 404 throw new UnsupportedOperationException(); 405 } 406 407 @Override 408 @Deprecated 409 public int getRpcTimeout() { 410 throw new UnsupportedOperationException(); 411 } 412 413 @Override 414 public long getRpcTimeout(TimeUnit unit) { 415 throw new UnsupportedOperationException(); 416 } 417 418 @Override 419 @Deprecated 420 public int getWriteRpcTimeout() { 421 throw new UnsupportedOperationException(); 422 } 423 424 @Override 425 @Deprecated 426 public int getReadRpcTimeout() { 427 throw new UnsupportedOperationException(); 428 } 429 430 @Override 431 public RegionLocator getRegionLocator() throws IOException { 432 throw new UnsupportedOperationException(); 433 } 434}