use of com.google.datastore.v1.Mutation in project YCSB by brianfrankcooper.
the class GoogleDatastoreClient method doSingleItemMutation.
private Status doSingleItemMutation(String table, String key, @Nullable HashMap<String, ByteIterator> values, MutationType mutationType) {
// First build the key.
Key.Builder datastoreKey = buildPrimaryKey(table, key);
// Build a commit request in non-transactional mode.
// Single item mutation to google datastore
// is always atomic and strongly consistent. Transaction is only necessary
// for multi-item mutation, or Read-modify-write operation.
CommitRequest.Builder commitRequest = CommitRequest.newBuilder();
commitRequest.setMode(Mode.NON_TRANSACTIONAL);
if (mutationType == MutationType.DELETE) {
commitRequest.addMutationsBuilder().setDelete(datastoreKey);
} else {
// If this is not for delete, build the entity.
Entity.Builder entityBuilder = Entity.newBuilder();
entityBuilder.setKey(datastoreKey);
for (Entry<String, ByteIterator> val : values.entrySet()) {
entityBuilder.getMutableProperties().put(val.getKey(), Value.newBuilder().setStringValue(val.getValue().toString()).build());
}
Entity entity = entityBuilder.build();
logger.debug("entity built as: " + entity.toString());
if (mutationType == MutationType.UPSERT) {
commitRequest.addMutationsBuilder().setUpsert(entity);
} else if (mutationType == MutationType.UPDATE) {
commitRequest.addMutationsBuilder().setUpdate(entity);
} else {
throw new RuntimeException("Impossible MutationType, code bug.");
}
}
try {
datastore.commit(commitRequest.build());
logger.debug("successfully committed.");
} catch (DatastoreException exception) {
// Catch all Datastore rpc errors.
// Log the exception, the name of the method called and the error code.
logger.error(String.format("Datastore Exception when committing (%s): %s %s", exception.getMessage(), exception.getMethodName(), exception.getCode()));
// will bubble up to the user as part of the YCSB Status "name".
return new Status("ERROR-" + exception.getCode(), exception.getMessage());
}
return Status.OK;
}
use of com.google.datastore.v1.Mutation in project simple-bigtable by spotify.
the class BigtableMutationImplTest method testWriteColumnValue.
@Test
public void testWriteColumnValue() throws Exception {
final BigtableMutationImpl bigtableMutationImpl = (BigtableMutationImpl) this.bigtableMutation.write("family:qualifier", ByteString.copyFromUtf8("value")).write("family", "qualifier", ByteString.copyFromUtf8("value"));
verifyGetMutateRowRequest();
bigtableMutationImpl.execute();
bigtableMutationImpl.executeAsync();
assertEquals(2, bigtableMutationImpl.getMutateRowRequest().getMutationsCount());
for (Mutation mutation : bigtableMutationImpl.getMutateRowRequest().getMutationsList()) {
assertEquals(Mutation.MutationCase.SET_CELL, mutation.getMutationCase());
assertEquals("family", mutation.getSetCell().getFamilyName());
assertEquals("qualifier", mutation.getSetCell().getColumnQualifier().toStringUtf8());
assertEquals("value", mutation.getSetCell().getValue().toStringUtf8());
assertEquals(Mutation.DeleteFromRow.getDefaultInstance(), mutation.getDeleteFromRow());
assertEquals(Mutation.DeleteFromFamily.getDefaultInstance(), mutation.getDeleteFromFamily());
assertEquals(Mutation.DeleteFromColumn.getDefaultInstance(), mutation.getDeleteFromColumn());
// Check timestamp in last 1 second
final long tsMillis = TimeUnit.MICROSECONDS.toMillis(mutation.getSetCell().getTimestampMicros());
final long nowMillis = System.currentTimeMillis();
assertTrue(tsMillis <= nowMillis && tsMillis > nowMillis - TimeUnit.SECONDS.toMillis(1));
}
verify(bigtableMock.getMockedDataClient()).mutateRow(bigtableMutation.getMutateRowRequest().build());
verify(bigtableMock.getMockedDataClient()).mutateRowAsync(bigtableMutation.getMutateRowRequest().build());
verifyNoMoreInteractions(bigtableMock.getMockedDataClient());
}
use of com.google.datastore.v1.Mutation in project simple-bigtable by spotify.
the class BigtableMutationImplTest method testDeleteRow.
@Test
public void testDeleteRow() throws Exception {
final BigtableMutationImpl bigtableMutationImpl = (BigtableMutationImpl) this.bigtableMutation.deleteRow();
verifyGetMutateRowRequest();
bigtableMutationImpl.execute();
bigtableMutationImpl.executeAsync();
assertEquals(1, bigtableMutationImpl.getMutateRowRequest().getMutationsCount());
final Mutation mutation = bigtableMutationImpl.getMutateRowRequest().getMutations(0);
assertEquals(Mutation.MutationCase.DELETE_FROM_ROW, mutation.getMutationCase());
assertEquals(Mutation.DeleteFromRow.getDefaultInstance(), mutation.getDeleteFromRow());
assertEquals(Mutation.DeleteFromFamily.getDefaultInstance(), mutation.getDeleteFromFamily());
assertEquals(Mutation.DeleteFromColumn.getDefaultInstance(), mutation.getDeleteFromColumn());
assertEquals(Mutation.SetCell.getDefaultInstance(), mutation.getSetCell());
verify(bigtableMock.getMockedDataClient()).mutateRow(bigtableMutation.getMutateRowRequest().build());
verify(bigtableMock.getMockedDataClient()).mutateRowAsync(bigtableMutation.getMutateRowRequest().build());
verifyNoMoreInteractions(bigtableMock.getMockedDataClient());
}
use of com.google.datastore.v1.Mutation in project simple-bigtable by spotify.
the class BigtableMutationImplTest method testDeleteColumn.
@Test
public void testDeleteColumn() throws Exception {
final BigtableMutationImpl bigtableMutationImpl = (BigtableMutationImpl) this.bigtableMutation.deleteColumn("family:qualifier");
verifyGetMutateRowRequest();
bigtableMutationImpl.execute();
bigtableMutationImpl.executeAsync();
assertEquals(1, bigtableMutationImpl.getMutateRowRequest().getMutationsCount());
final Mutation mutation = bigtableMutationImpl.getMutateRowRequest().getMutations(0);
assertEquals(Mutation.MutationCase.DELETE_FROM_COLUMN, mutation.getMutationCase());
assertEquals("family", mutation.getDeleteFromColumn().getFamilyName());
assertEquals("qualifier", mutation.getDeleteFromColumn().getColumnQualifier().toStringUtf8());
assertEquals(Mutation.DeleteFromRow.getDefaultInstance(), mutation.getDeleteFromRow());
assertEquals(Mutation.DeleteFromFamily.getDefaultInstance(), mutation.getDeleteFromFamily());
assertEquals(Mutation.SetCell.getDefaultInstance(), mutation.getSetCell());
verify(bigtableMock.getMockedDataClient()).mutateRow(bigtableMutation.getMutateRowRequest().build());
verify(bigtableMock.getMockedDataClient()).mutateRowAsync(bigtableMutation.getMutateRowRequest().build());
verifyNoMoreInteractions(bigtableMock.getMockedDataClient());
}
use of com.google.datastore.v1.Mutation in project SpinalTap by airbnb.
the class ColumnSerializationUtilTest method testDeserializeColumn.
@Test
public void testDeserializeColumn() throws Exception {
Mutation mutation = new Mutation(MutationType.DELETE, TIMESTAMP, SOURCE_ID, DATA_SOURCE, BINLOG_HEADER, TABLE, getEntity());
TSerializer serializer = new TSerializer(new TBinaryProtocol.Factory());
TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory());
byte[] serialized = serializer.serialize(mutation);
Mutation deserialized = new Mutation();
deserializer.deserialize(deserialized, serialized);
assertEquals(mutation, deserialized);
}
Aggregations