use of com.google.datastore.v1.BeginTransactionRequest in project grpc-gcp-java by GoogleCloudPlatform.
the class SpannerClientV1TestCases method prepareTestData.
void prepareTestData() throws InterruptedException {
int columnBytes = Integer.min(payload, MAX_SIZE_PER_COLUMN);
int rows = (payload - 1) / columnBytes + 1;
char[] charArray = new char[columnBytes];
Arrays.fill(charArray, 'z');
String colContent = new String(charArray);
SpannerClient client = getClient();
Session session = client.createSession(CreateSessionRequest.newBuilder().setDatabase(database).build());
long start = System.currentTimeMillis();
// Clean the existing data.
BeginTransactionRequest request = BeginTransactionRequest.newBuilder().setSession(session.getName()).setOptions(TransactionOptions.newBuilder().setReadWrite(TransactionOptions.ReadWrite.getDefaultInstance()).build()).build();
Transaction txn = client.beginTransaction(request);
client.commit(CommitRequest.newBuilder().addMutations(Mutation.newBuilder().setDelete(Mutation.Delete.newBuilder().setTable(LARGE_TABLE).setKeySet(KeySet.newBuilder().setAll(true).build()).build()).build()).setSession(session.getName()).setTransactionId(txn.getId()).build());
System.out.println(String.format("\nDeleted the previous large_table in %d ms.", System.currentTimeMillis() - start));
// Add the payload data.
start = System.currentTimeMillis();
for (int i = 0; i < rows; i++) {
txn = client.beginTransaction(request);
client.commit(CommitRequest.newBuilder().addMutations(Mutation.newBuilder().setInsertOrUpdate(Mutation.Write.newBuilder().addColumns("id").addColumns("data").addValues(ListValue.newBuilder().addValues(Value.newBuilder().setStringValue("payload" + i)).addValues(Value.newBuilder().setStringValue(colContent)).build()).setTable(LARGE_TABLE).build()).build()).setSession(session.getName()).setTransactionId(txn.getId()).build());
}
System.out.println(String.format("Successfully added ColumnBytes: %d, Rows: %d to large_table in %d ms.", columnBytes, rows, System.currentTimeMillis() - start));
cleanUpClient(client, session.getName());
}
use of com.google.datastore.v1.BeginTransactionRequest in project grpc-gcp-java by GoogleCloudPlatform.
the class SpannerTestCases method prepareTestData.
void prepareTestData() throws InterruptedException {
ManagedChannel channel = getChannel();
SpannerBlockingStub stub = getBlockingStub(channel);
// Because of max data size, we need to separate into different rows.
int columnBytes = Integer.min(payload, MAX_SIZE_PER_COLUMN);
int rows = (payload - 1) / columnBytes + 1;
char[] charArray = new char[columnBytes];
Arrays.fill(charArray, 'z');
String colContent = new String(charArray);
Session session = stub.createSession(CreateSessionRequest.newBuilder().setDatabase(database).build());
long start = System.currentTimeMillis();
// Clean the data in the table.
BeginTransactionRequest request = BeginTransactionRequest.newBuilder().setSession(session.getName()).setOptions(TransactionOptions.newBuilder().setReadWrite(TransactionOptions.ReadWrite.getDefaultInstance()).build()).build();
Transaction txn = stub.beginTransaction(request);
stub.commit(CommitRequest.newBuilder().addMutations(Mutation.newBuilder().setDelete(Mutation.Delete.newBuilder().setTable(LARGE_TABLE).setKeySet(KeySet.newBuilder().setAll(true).build()).build()).build()).setSession(session.getName()).setTransactionId(txn.getId()).build());
System.out.println(String.format("\nDeleted the previous large_table in %d ms.", System.currentTimeMillis() - start));
// Add the payload data.
start = System.currentTimeMillis();
for (int i = 0; i < rows; i++) {
txn = stub.beginTransaction(request);
stub.commit(CommitRequest.newBuilder().addMutations(Mutation.newBuilder().setInsertOrUpdate(Mutation.Write.newBuilder().addColumns("id").addColumns("data").addValues(ListValue.newBuilder().addValues(Value.newBuilder().setStringValue("payload" + i)).addValues(Value.newBuilder().setStringValue(colContent)).build()).setTable(LARGE_TABLE).build()).build()).setSession(session.getName()).setTransactionId(txn.getId()).build());
}
System.out.println(String.format("Successfully added ColumnBytes: %d, Rows: %d to large_table in %d ms.", columnBytes, rows, System.currentTimeMillis() - start));
stub.deleteSession(DeleteSessionRequest.newBuilder().setName(session.getName()).build());
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
use of com.google.datastore.v1.BeginTransactionRequest in project appengine-java-standard by GoogleCloudPlatform.
the class CloudDatastoreV1ClientImplTest method testRetries_MaxRetriesExceeded.
@Test
public void testRetries_MaxRetriesExceeded() throws Exception {
BeginTransactionRequest req = BeginTransactionRequest.getDefaultInstance();
CloudDatastoreV1ClientImpl client = new CloudDatastoreV1ClientImpl(datastore, 2);
DatastoreException lastException = retryableException();
when(datastore.beginTransaction(req)).thenThrow(retryableException()).thenThrow(retryableException()).thenThrow(lastException);
try {
client.beginTransaction(req).get();
fail("expected exception");
} catch (ExecutionException e) {
assertThat(e).hasCauseThat().hasCauseThat().isEqualTo(lastException);
}
}
use of com.google.datastore.v1.BeginTransactionRequest in project appengine-java-standard by GoogleCloudPlatform.
the class CloudDatastoreV1ClientImplTest method testStackTrace.
@Test
public void testStackTrace() throws Throwable {
BeginTransactionRequest req = BeginTransactionRequest.getDefaultInstance();
when(datastore.beginTransaction(req)).thenThrow(new DatastoreException("beginTransaction", Code.INVALID_ARGUMENT, "message", null));
CloudDatastoreV1ClientImpl client = newClient(datastore);
Future<BeginTransactionResponse> future = makeBeginTransactionCallForTest(client, req);
try {
future.get();
fail("expected exception");
} catch (ExecutionException e) {
assertThat(e).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
// The test helper method used to call beginTransaction() appears in the error message
// (even though it is no longer on the stack).
assertThat(e).hasMessageThat().contains("makeBeginTransactionCallForTest");
// The underlying cause is also present.
assertThat(e).hasCauseThat().hasCauseThat().isInstanceOf(DatastoreException.class);
}
}
use of com.google.datastore.v1.BeginTransactionRequest in project appengine-java-standard by GoogleCloudPlatform.
the class CloudDatastoreV1ClientImplTest method testRetries.
@Test
public void testRetries() throws Exception {
BeginTransactionRequest req = BeginTransactionRequest.getDefaultInstance();
CloudDatastoreV1ClientImpl client = new CloudDatastoreV1ClientImpl(datastore, 2);
when(datastore.beginTransaction(req)).thenThrow(retryableException()).thenThrow(retryableException()).thenReturn(BeginTransactionResponse.getDefaultInstance());
client.beginTransaction(req).get();
}
Aggregations