DeleteAdapter extends MutationAdapter<Delete> { @Override public void adapt( Delete operation, com.google.cloud.bigtable.data.v2.models.MutationApi<?> mutation) { if (operation.getFamilyCellMap().isEmpty()) { throwIfUnsupportedDeleteRow(operation); mutation.deleteRow(); } else { for (Map.Entry<byte[], List<Cell>> entry : operation.getFamilyCellMap().entrySet()) { ByteString familyByteString = ByteString.copyFrom(entry.getKey()); for (Cell cell : entry.getValue()) { if (isColumnDelete(cell) || isPointDelete(cell)) { if (isPointDelete(cell)) { throwIfUnsupportedPointDelete(cell); } addDeleteFromColumnMods(familyByteString, cell, mutation); } else if (isFamilyDelete(cell)) { throwIfUnsupportedDeleteFamily(cell); mutation.deleteFamily(familyByteString.toStringUtf8()); } else if (isFamilyVersionDelete(cell)) { throwOnUnsupportedDeleteFamilyVersion(cell); } else { throwOnUnsupportedCellType(cell); } } } } }  @Override void adapt(
      Delete operation, com.google.cloud.bigtable.data.v2.models.MutationApi<?> mutation);  }
@Test public void testFullRowDelete() { byte[] rowKey = randomHelper.randomData("rk1-"); Delete delete = new Delete(rowKey); MutateRowRequest request = adapt(delete); Assert.assertArrayEquals(rowKey, request.getRowKey().toByteArray()); Assert.assertEquals(1, request.getMutationsCount()); Mutation.MutationCase mutationCase = request.getMutations(0).getMutationCase(); Assert.assertEquals(MutationCase.DELETE_FROM_ROW, mutationCase); }
@Test public void testDeleteRowAtTimestampIsUnsupported() { byte[] rowKey = randomHelper.randomData("rk1-"); Delete delete = new Delete(rowKey, 1000L); expectedException.expect(UnsupportedOperationException.class); expectedException.expectMessage("Cannot perform row deletion at timestamp"); deleteAdapter.adapt(delete, com.google.cloud.bigtable.data.v2.models.Mutation.create()); }
@Test public void testColumnFamilyDelete() { byte[] rowKey = randomHelper.randomData("rk1-"); byte[] family = randomHelper.randomData("family1-"); Delete delete = new Delete(rowKey); delete.addFamily(family); MutateRowRequest request = adapt(delete); Assert.assertArrayEquals(rowKey, request.getRowKey().toByteArray()); Assert.assertEquals(1, request.getMutationsCount()); MutationCase mutationCase = request.getMutations(0).getMutationCase(); Assert.assertEquals(MutationCase.DELETE_FROM_FAMILY, mutationCase); Mutation.DeleteFromFamily deleteFromFamily = request.getMutations(0).getDeleteFromFamily(); Assert.assertArrayEquals(family, deleteFromFamily.getFamilyNameBytes().toByteArray()); }
@Test public void testColumnFamilyDeleteAtTimestampFails() { byte[] rowKey = randomHelper.randomData("rk1-"); Delete delete = new Delete(rowKey); delete.addFamily(Bytes.toBytes("family1"), 10000L); expectedException.expect(UnsupportedOperationException.class); expectedException.expectMessage("Cannot perform column family deletion before timestamp"); deleteAdapter.adapt(delete, com.google.cloud.bigtable.data.v2.models.Mutation.create()); }
@Test public void testDeleteColumnAtTimestamp() { byte[] rowKey = randomHelper.randomData("rk1-"); byte[] family = randomHelper.randomData("family1-"); byte[] qualifier = randomHelper.randomData("qualifier"); long hbaseTimestamp = 1000L; long bigtableStartTimestamp = TimeUnit.MILLISECONDS.toMicros(hbaseTimestamp); long bigtableEndTimestamp = TimeUnit.MILLISECONDS.toMicros(hbaseTimestamp + 1); Delete delete = new Delete(rowKey); delete.addColumn(family, qualifier, hbaseTimestamp); MutateRowRequest request = adapt(delete); Assert.assertArrayEquals(rowKey, request.getRowKey().toByteArray()); Assert.assertEquals(1, request.getMutationsCount()); MutationCase mutationCase = request.getMutations(0).getMutationCase(); Assert.assertEquals(MutationCase.DELETE_FROM_COLUMN, mutationCase); Mutation.DeleteFromColumn deleteFromColumn = request.getMutations(0).getDeleteFromColumn(); Assert.assertArrayEquals(family, deleteFromColumn.getFamilyNameBytes().toByteArray()); Assert.assertArrayEquals(qualifier, deleteFromColumn.getColumnQualifier().toByteArray()); Assert.assertTrue(request.getMutations(0).getDeleteFromColumn().hasTimeRange()); TimestampRange timeStampRange = deleteFromColumn.getTimeRange(); Assert.assertEquals(bigtableStartTimestamp, timeStampRange.getStartTimestampMicros()); Assert.assertEquals(bigtableEndTimestamp, timeStampRange.getEndTimestampMicros()); }
@Test public void testDeleteLatestColumnThrows() { byte[] rowKey = randomHelper.randomData("rk1-"); byte[] family = randomHelper.randomData("family1-"); byte[] qualifier = randomHelper.randomData("qualifier"); Delete delete = new Delete(rowKey); delete.addColumn(family, qualifier); expectedException.expect(UnsupportedOperationException.class); expectedException.expectMessage("Cannot delete single latest cell"); deleteAdapter.adapt(delete, com.google.cloud.bigtable.data.v2.models.Mutation.create()); }
@Test public void testDeleteColumnBeforeTimestamp() { byte[] rowKey = randomHelper.randomData("rk1-"); byte[] family = randomHelper.randomData("family1-"); byte[] qualifier = randomHelper.randomData("qualifier"); long hbaseTimestamp = 1000L; long bigtableTimestamp = TimeUnit.MILLISECONDS.toMicros(hbaseTimestamp + 1); Delete delete = new Delete(rowKey); delete.addColumns(family, qualifier, hbaseTimestamp); MutateRowRequest request = adapt(delete); Assert.assertArrayEquals(rowKey, request.getRowKey().toByteArray()); Assert.assertEquals(1, request.getMutationsCount()); Assert.assertEquals(MutationCase.DELETE_FROM_COLUMN, request.getMutations(0).getMutationCase()); Mutation.DeleteFromColumn deleteFromColumn = request.getMutations(0).getDeleteFromColumn(); Assert.assertArrayEquals(qualifier, deleteFromColumn.getColumnQualifier().toByteArray()); Assert.assertTrue(request.getMutations(0).getDeleteFromColumn().hasTimeRange()); TimestampRange timeRange = deleteFromColumn.getTimeRange(); Assert.assertEquals(0L, timeRange.getStartTimestampMicros()); Assert.assertEquals(bigtableTimestamp, timeRange.getEndTimestampMicros()); }
@Test public void testDeleteFamilyVersionIsUnsupported() { byte[] rowKey = randomHelper.randomData("rk1-"); byte[] family = randomHelper.randomData("family1-"); long hbaseTimestamp = 1000L; Delete delete = new Delete(rowKey); delete.addFamilyVersion(family, hbaseTimestamp); expectedException.expect(UnsupportedOperationException.class); expectedException.expectMessage("Cannot perform column family deletion at timestamp"); deleteAdapter.adapt(delete, com.google.cloud.bigtable.data.v2.models.Mutation.create()); }