PutAdapter extends MutationAdapter<Put> { @Override public void adapt( Put operation, com.google.cloud.bigtable.data.v2.models.MutationApi<?> mutation) { if (operation.isEmpty()) { throw new IllegalArgumentException("No columns to insert"); } long currentTimestampMicros = setClientTimestamp ? clock.currentTimeMillis() * 1000 : -1; final int rowLength = operation.getRow().length; for (Entry<byte[], List<Cell>> entry : operation.getFamilyCellMap().entrySet()) { ByteString familyString = ByteString.copyFrom(entry.getKey()); int familySize = familyString.size(); for (Cell cell : entry.getValue()) { int qualifierLength = cell.getQualifierLength(); int valueLength = cell.getValueLength(); int keyValueSize = (20 + rowLength + familySize + qualifierLength + valueLength); if (maxKeyValueSize > 0 && keyValueSize > maxKeyValueSize) { throw new IllegalArgumentException("KeyValue size too large"); } ByteString cellQualifierByteString = ByteString.copyFrom( cell.getQualifierArray(), cell.getQualifierOffset(), qualifierLength); ByteString value = ByteString.copyFrom(cell.getValueArray(), cell.getValueOffset(), valueLength); long timestampMicros = currentTimestampMicros; if (cell.getTimestamp() != HConstants.LATEST_TIMESTAMP) { timestampMicros = TimestampConverter.hbase2bigtable(cell.getTimestamp()); } mutation.setCell( familyString.toStringUtf8(), cellQualifierByteString, timestampMicros, value); } } } PutAdapter(int maxKeyValueSize);  PutAdapter(int maxKeyValueSize, boolean setClientTimestamp); @Override void adapt(
      Put operation, com.google.cloud.bigtable.data.v2.models.MutationApi<?> mutation);  }
@Test public void testSingleCellIsConverted() { byte[] row = dataHelper.randomData("rk-"); byte[] family = dataHelper.randomData("f"); byte[] qualifier = dataHelper.randomData("qual"); byte[] value = dataHelper.randomData("v1"); long timestamp = 2L; Put hbasePut = new Put(row); hbasePut.addColumn(family, qualifier, timestamp, value); MutateRowRequest request = adapt(hbasePut); Assert.assertArrayEquals(row, request.getRowKey().toByteArray()); Assert.assertEquals(1, request.getMutationsCount()); Mutation mutation = request.getMutations(0); Assert.assertEquals(MutationCase.SET_CELL, mutation.getMutationCase()); SetCell setCell = mutation.getSetCell(); Assert.assertArrayEquals(family, setCell.getFamilyNameBytes().toByteArray()); Assert.assertArrayEquals(qualifier, setCell.getColumnQualifier().toByteArray()); Assert.assertEquals(TimeUnit.MILLISECONDS.toMicros(timestamp), setCell.getTimestampMicros()); Assert.assertArrayEquals(value, setCell.getValue().toByteArray()); }
@Test public void testMultipleCellsInOneFamilyAreConverted() { byte[] row = dataHelper.randomData("rk-"); byte[] family = dataHelper.randomData("f1"); byte[] qualifier1 = dataHelper.randomData("qual1"); byte[] qualifier2 = dataHelper.randomData("qual2"); byte[] value1 = dataHelper.randomData("v1"); byte[] value2 = dataHelper.randomData("v2"); long timestamp1 = 1L; long timestamp2 = 2L; Put hbasePut = new Put(row); hbasePut.addColumn(family, qualifier1, timestamp1, value1); hbasePut.addColumn(family, qualifier2, timestamp2, value2); MutateRowRequest request = adapt(hbasePut); Assert.assertArrayEquals(row, request.getRowKey().toByteArray()); Assert.assertEquals(2, request.getMutationsCount()); Mutation mutation = request.getMutations(0); Assert.assertEquals(MutationCase.SET_CELL, mutation.getMutationCase()); SetCell setCell = mutation.getSetCell(); Assert.assertArrayEquals(family, setCell.getFamilyNameBytes().toByteArray()); Assert.assertArrayEquals(qualifier1, setCell.getColumnQualifier().toByteArray()); Assert.assertEquals(TimeUnit.MILLISECONDS.toMicros(timestamp1), setCell.getTimestampMicros()); Assert.assertArrayEquals(value1, setCell.getValue().toByteArray()); Mutation mod2 = request.getMutations(1); SetCell setCell2 = mod2.getSetCell(); Assert.assertArrayEquals(family, setCell2.getFamilyNameBytes().toByteArray()); Assert.assertArrayEquals(qualifier2, setCell2.getColumnQualifier().toByteArray()); Assert.assertEquals(TimeUnit.MILLISECONDS.toMicros(timestamp2), setCell2.getTimestampMicros()); Assert.assertArrayEquals(value2, setCell2.getValue().toByteArray()); }
@Test public void testMultipleCellsInMultipleFamiliesAreConverted() { byte[] row = dataHelper.randomData("rk-"); byte[] family1 = dataHelper.randomData("f1"); byte[] family2 = dataHelper.randomData("f2"); byte[] qualifier1 = dataHelper.randomData("qual1"); byte[] qualifier2 = dataHelper.randomData("qual2"); byte[] value1 = dataHelper.randomData("v1"); byte[] value2 = dataHelper.randomData("v1"); long timestamp1 = 1L; long timestamp2 = 2L; Put hbasePut = new Put(row); hbasePut.addColumn(family1, qualifier1, timestamp1, value1); hbasePut.addColumn(family2, qualifier2, timestamp2, value2); MutateRowRequest request = adapt(hbasePut); Assert.assertArrayEquals(row, request.getRowKey().toByteArray()); Assert.assertEquals(2, request.getMutationsCount()); Mutation mutation1 = request.getMutations(0); Assert.assertEquals(MutationCase.SET_CELL, mutation1.getMutationCase()); SetCell setCell = mutation1.getSetCell(); Assert.assertArrayEquals(family1, setCell.getFamilyNameBytes().toByteArray()); Assert.assertArrayEquals(qualifier1, setCell.getColumnQualifier().toByteArray()); Assert.assertEquals(TimeUnit.MILLISECONDS.toMicros(timestamp1), setCell.getTimestampMicros()); Assert.assertArrayEquals(value1, setCell.getValue().toByteArray()); Mutation mutation2 = request.getMutations(1); SetCell setCell2 = mutation2.getSetCell(); Assert.assertArrayEquals(family2, setCell2.getFamilyNameBytes().toByteArray()); Assert.assertArrayEquals(qualifier2, setCell2.getColumnQualifier().toByteArray()); Assert.assertEquals(TimeUnit.MILLISECONDS.toMicros(timestamp2), setCell2.getTimestampMicros()); Assert.assertArrayEquals(value2, setCell2.getValue().toByteArray()); }
@Test public void testUnsetTimestampsArePopulated() { byte[] row = dataHelper.randomData("rk-"); byte[] family1 = dataHelper.randomData("f1"); byte[] qualifier1 = dataHelper.randomData("qual1"); byte[] value1 = dataHelper.randomData("v1"); long startTimeMillis = System.currentTimeMillis(); Put hbasePut = new Put(row).addColumn(family1, qualifier1, value1); com.google.cloud.bigtable.data.v2.models.Mutation unsafeMutation = com.google.cloud.bigtable.data.v2.models.Mutation.createUnsafe(); adapter.adapt(hbasePut, unsafeMutation); MutateRowRequest request = RowMutation.create(TABLE_ID, ByteString.copyFrom(hbasePut.getRow()), unsafeMutation) .toProto(REQUEST_CONTEXT); Assert.assertArrayEquals(row, request.getRowKey().toByteArray()); Assert.assertEquals(1, request.getMutationsCount()); Mutation mutation = request.getMutations(0); Assert.assertEquals(MutationCase.SET_CELL, mutation.getMutationCase()); SetCell setCell = mutation.getSetCell(); Assert.assertArrayEquals(family1, setCell.getFamilyNameBytes().toByteArray()); Assert.assertArrayEquals(qualifier1, setCell.getColumnQualifier().toByteArray()); Assert.assertTrue(startTimeMillis * 1000 <= setCell.getTimestampMicros()); Assert.assertTrue(setCell.getTimestampMicros() <= System.currentTimeMillis() * 1000); Assert.assertArrayEquals(value1, setCell.getValue().toByteArray()); }
@Test public void testUnsetTimestampsAreNotPopulated() { PutAdapter adapter = new PutAdapter(-1, false); byte[] row = dataHelper.randomData("rk-"); byte[] family1 = dataHelper.randomData("f1"); byte[] qualifier1 = dataHelper.randomData("qual1"); byte[] value1 = dataHelper.randomData("v1"); Put hbasePut = new Put(row).addColumn(family1, qualifier1, value1); com.google.cloud.bigtable.data.v2.models.Mutation unsafeMutation = com.google.cloud.bigtable.data.v2.models.Mutation.createUnsafe(); adapter.adapt(hbasePut, unsafeMutation); RowMutation rowMutation = RowMutation.create(TABLE_ID, ByteString.copyFrom(hbasePut.getRow()), unsafeMutation); MutateRowRequest request = rowMutation.toProto(REQUEST_CONTEXT); Assert.assertArrayEquals(row, request.getRowKey().toByteArray()); Assert.assertEquals(1, request.getMutationsCount()); Mutation mutation = request.getMutations(0); Assert.assertEquals(MutationCase.SET_CELL, mutation.getMutationCase()); SetCell setCell = mutation.getSetCell(); Assert.assertArrayEquals(family1, setCell.getFamilyNameBytes().toByteArray()); Assert.assertArrayEquals(qualifier1, setCell.getColumnQualifier().toByteArray()); Assert.assertEquals(-1, setCell.getTimestampMicros()); Assert.assertArrayEquals(value1, setCell.getValue().toByteArray()); }
@Test(expected = IllegalArgumentException.class) public void testEmptyPut() { byte[] row = dataHelper.randomData("rk-"); Put emptyPut = new Put(row); adapt(emptyPut); }
@Test public void testRetry() { byte[] row = dataHelper.randomData("rk-"); byte[] family1 = dataHelper.randomData("f1"); byte[] qualifier1 = dataHelper.randomData("qual1"); byte[] value1 = dataHelper.randomData("v1"); Put hbasePut = new Put(row, System.currentTimeMillis()); hbasePut.addColumn(family1, qualifier1, value1); MutateRowRequest request = adapt(hbasePut); Assert.assertTrue(BigtableDataGrpcClient.IS_RETRYABLE_MUTATION.apply(request)); }