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; 026 027import org.apache.hadoop.conf.Configuration; 028import org.apache.hadoop.hbase.Cell; 029import org.apache.hadoop.hbase.HTableDescriptor; 030import org.apache.hadoop.hbase.TableName; 031import org.apache.hadoop.hbase.CompareOperator; 032import org.apache.hadoop.hbase.client.Append; 033import org.apache.hadoop.hbase.client.Delete; 034import org.apache.hadoop.hbase.client.Durability; 035import org.apache.hadoop.hbase.client.Get; 036import org.apache.hadoop.hbase.client.Increment; 037import org.apache.hadoop.hbase.client.Put; 038import org.apache.hadoop.hbase.client.Result; 039import org.apache.hadoop.hbase.client.ResultScanner; 040import org.apache.hadoop.hbase.client.Row; 041import org.apache.hadoop.hbase.client.RowMutations; 042import org.apache.hadoop.hbase.client.Scan; 043import org.apache.hadoop.hbase.client.Table; 044import org.apache.hadoop.hbase.client.TableDescriptor; 045import org.apache.hadoop.hbase.client.coprocessor.Batch.Call; 046import org.apache.hadoop.hbase.client.coprocessor.Batch.Callback; 047import org.apache.hadoop.hbase.client.metrics.ScanMetrics; 048import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; 049import org.apache.hadoop.hbase.ipc.CoprocessorRpcChannel; 050 051import com.google.protobuf.Descriptors.MethodDescriptor; 052import com.google.protobuf.Message; 053import com.google.protobuf.Service; 054import com.google.protobuf.ServiceException; 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 void 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) 301 throws IOException { 302 throw new UnsupportedOperationException(); 303 } 304 305 /** 306 * This call will NOT close the underlying region. 307 */ 308 @Override 309 public void close() throws IOException { 310 } 311 312 @Override 313 public CoprocessorRpcChannel coprocessorService(byte[] row) { 314 throw new UnsupportedOperationException(); 315 } 316 317 @Override 318 public <T extends Service, R> Map<byte[], R> coprocessorService(Class<T> service, byte[] startKey, 319 byte[] endKey, Call<T, R> callable) 320 throws ServiceException, Throwable { 321 throw new UnsupportedOperationException(); 322 } 323 324 @Override 325 public <T extends Service, R> void coprocessorService(Class<T> service, byte[] startKey, 326 byte[] endKey, Call<T, R> callable, Callback<R> callback) 327 throws ServiceException, Throwable { 328 throw new UnsupportedOperationException(); 329 } 330 331 @Override 332 public <R extends Message> Map<byte[], R> batchCoprocessorService(MethodDescriptor 333 methodDescriptor, Message request, 334 byte[] startKey, byte[] endKey, R responsePrototype) 335 throws ServiceException, Throwable { 336 throw new UnsupportedOperationException(); 337 } 338 339 @Override 340 public <R extends Message> void batchCoprocessorService(MethodDescriptor methodDescriptor, 341 Message request, byte[] startKey, byte[] endKey, R responsePrototype, Callback<R> callback) 342 throws ServiceException, Throwable { 343 throw new UnsupportedOperationException(); 344 } 345 346 @Override 347 @Deprecated 348 public boolean checkAndMutate(byte[] row, byte[] family, byte[] qualifier, CompareOp compareOp, 349 byte[] value, RowMutations mutation) 350 throws IOException { 351 throw new UnsupportedOperationException(); 352 } 353 354 @Override 355 @Deprecated 356 public boolean checkAndMutate(byte[] row, byte[] family, byte[] qualifier, 357 CompareOperator compareOp, byte[] value, RowMutations mutation) throws IOException { 358 throw new UnsupportedOperationException(); 359 } 360 361 @Override 362 @Deprecated 363 public void setOperationTimeout(int operationTimeout) { 364 throw new UnsupportedOperationException(); 365 } 366 367 @Override 368 @Deprecated 369 public int getOperationTimeout() { 370 throw new UnsupportedOperationException(); 371 } 372 373 @Override 374 @Deprecated 375 public void setRpcTimeout(int rpcTimeout) { 376 throw new UnsupportedOperationException(); 377 } 378 379 @Override 380 public long getReadRpcTimeout(TimeUnit unit) { 381 throw new UnsupportedOperationException(); 382 } 383 384 @Override 385 @Deprecated 386 public void setWriteRpcTimeout(int writeRpcTimeout) {throw new UnsupportedOperationException(); } 387 388 @Override 389 public long getOperationTimeout(TimeUnit unit) { 390 throw new UnsupportedOperationException(); 391 } 392 393 @Override 394 @Deprecated 395 public void setReadRpcTimeout(int readRpcTimeout) {throw new UnsupportedOperationException(); } 396 397 @Override 398 public long getWriteRpcTimeout(TimeUnit unit) { 399 throw new UnsupportedOperationException(); 400 } 401 402 @Override 403 @Deprecated 404 public int getRpcTimeout() { 405 throw new UnsupportedOperationException(); 406 } 407 408 @Override 409 public long getRpcTimeout(TimeUnit unit) { 410 throw new UnsupportedOperationException(); 411 } 412 413 @Override 414 @Deprecated 415 public int getWriteRpcTimeout() { throw new UnsupportedOperationException(); } 416 417 @Override 418 @Deprecated 419 public int getReadRpcTimeout() { throw new UnsupportedOperationException(); } 420}