BatchExecutor { public void batch(List<? extends Row> actions, @Nullable Object[] results) throws IOException, InterruptedException { batchCallback(actions, results, null); } BatchExecutor(
      BigtableApi bigtableApi, BigtableHBaseSettings settings, HBaseRequestAdapter adapter); void batch(List<? extends Row> actions, @Nullable Object[] results); List<ApiFuture<?>> issueAsyncRowRequests(
      List<? extends Row> actions, Object[] results, Batch.Callback<R> callback); Result[] batch(List<? extends Row> actions); void batchCallback(
      List<? extends Row> actions, Object[] results, Batch.Callback<R> callback); boolean[] exists(List<Get> gets); static final byte[] NO_REGION; }
@Test public void testGet() throws Exception { when(mockBulkRead.add(any(ByteString.class), any(Filters.Filter.class))).thenReturn(mockFuture); final byte[] key = randomBytes(8); Result response = Result.create( ImmutableList.<Cell>of( new RowCell( key, Bytes.toBytes("family"), Bytes.toBytes(""), 1000L, Bytes.toBytes("value")))); setFuture(response); Result[] results = batch(ImmutableList.<Row>of(new Get(key))); assertTrue(matchesRow(response).matches(results[0])); }
@Test public void testShutdownService() throws Exception { when(mockFuture.get()).thenThrow(new IllegalStateException("closed")); try { batch(Arrays.asList(randomPut())); } catch (RetriesExhaustedWithDetailsException e) { Assert.assertEquals(1, e.getCauses().size()); Assert.assertEquals(IllegalStateException.class, e.getCause(0).getClass()); } }
@Test public void testAsyncException() throws Exception { String message = "Something bad happened"; when(mockFuture.get()).thenThrow(new RuntimeException(message)); try { batch(Arrays.asList(randomPut())); } catch (RetriesExhaustedWithDetailsException e) { Assert.assertEquals(1, e.getCauses().size()); Assert.assertEquals(RuntimeException.class, e.getCause(0).getClass()); Assert.assertEquals(message, e.getCause(0).getMessage()); } }
@Test public void testPartialResults() throws Exception { when(mockBigtableApi.getDataClient()).thenReturn(mockDataClientWrapper); when(mockDataClientWrapper.createBulkRead(isA(String.class))).thenReturn(mockBulkRead); byte[] key1 = randomBytes(8); byte[] key2 = randomBytes(8); Result expected = Result.create( ImmutableList.<org.apache.hadoop.hbase.Cell>of( new RowCell( key1, Bytes.toBytes("cf"), Bytes.toBytes(""), 10, Bytes.toBytes("hi!"), ImmutableList.<String>of()))); RuntimeException exception = new RuntimeException("Something bad happened"); when(mockBulkRead.add(any(ByteString.class), any(Filters.Filter.class))) .thenReturn(ApiFutures.immediateFuture(expected)) .thenReturn(ApiFutures.<Result>immediateFailedFuture(exception)); List<Get> gets = Arrays.asList(new Get(key1), new Get(key2)); Object[] results = new Object[2]; try { createExecutor().batch(gets, results); } catch (RetriesExhaustedWithDetailsException ignored) { } assertTrue("first result is a result", results[0] instanceof Result); assertTrue(matchesRow(expected).matches(results[0])); Assert.assertEquals(exception, results[1]); }
@Test public void testBatchBulkGets() throws Exception { final List<Get> gets = new ArrayList<>(10); final List<ApiFuture<Result>> expected = new ArrayList<>(10); gets.add(new Get(Bytes.toBytes("key0"))); expected.add(ApiFutures.<Result>immediateFuture(null)); for (int i = 1; i < 10; i++) { byte[] row_key = randomBytes(8); gets.add(new Get(row_key)); ByteString key = ByteStringer.wrap(row_key); ByteString cellValue = ByteString.copyFrom(randomBytes(8)); expected.add( ApiFutures.immediateFuture( Result.create( ImmutableList.<Cell>of( new RowCell( key.toByteArray(), Bytes.toBytes("family"), Bytes.toBytes(""), System.nanoTime() / 1000, cellValue.toByteArray()))))); } when(mockBulkRead.add(any(ByteString.class), any(Filters.Filter.class))) .then( new Answer<ApiFuture<Result>>() { final AtomicInteger counter = new AtomicInteger(); @Override public ApiFuture<Result> answer(InvocationOnMock invocation) throws Throwable { return expected.get(counter.getAndIncrement()); } }); ByteString key = ByteStringer.wrap(randomBytes(8)); ByteString cellValue = ByteString.copyFrom(randomBytes(8)); Result row = Result.create( ImmutableList.<Cell>of( new RowCell( key.toByteArray(), Bytes.toBytes("family"), Bytes.toBytes(""), 1000L, cellValue.toByteArray()))); when(mockFuture.get()).thenReturn(row); Result[] results = createExecutor().batch(gets); verify(mockBulkRead, times(10)).add(any(ByteString.class), any(Filters.Filter.class)); verify(mockBulkRead, times(1)).sendOutstanding(); assertTrue(matchesRow(Result.EMPTY_RESULT).matches(results[0])); for (int i = 1; i < results.length; i++) { assertTrue( "Expected " + Bytes.toString(gets.get(i).getRow()) + " but was " + Bytes.toString(results[i].getRow()), Bytes.equals(results[i].getRow(), gets.get(i).getRow())); } }