use of com.google.cloud.spanner.v1.SpannerClient in project java-spanner by googleapis.
the class RetryOnInvalidatedSessionTest method startStaticServer.
@BeforeClass
public static void startStaticServer() throws IOException {
mockSpanner = new MockSpannerServiceImpl();
// We don't want any unpredictable aborted transactions.
mockSpanner.setAbortProbability(0.0D);
mockSpanner.putStatementResult(StatementResult.read("FOO", KeySet.all(), Collections.singletonList("BAR"), READ_RESULTSET));
mockSpanner.putStatementResult(StatementResult.read("FOO", KeySet.singleKey(Key.of()), Collections.singletonList("BAR"), READ_ROW_RESULTSET));
mockSpanner.putStatementResult(StatementResult.query(SELECT1AND2, SELECT1_RESULTSET));
mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT));
String uniqueName = InProcessServerBuilder.generateName();
server = InProcessServerBuilder.forName(uniqueName).directExecutor().addService(mockSpanner).build().start();
channelProvider = LocalChannelProvider.create(uniqueName);
SpannerSettings settings = SpannerSettings.newBuilder().setTransportChannelProvider(channelProvider).setCredentialsProvider(NoCredentialsProvider.create()).build();
spannerClient = SpannerClient.create(settings);
executor = Executors.newSingleThreadExecutor();
}
use of com.google.cloud.spanner.v1.SpannerClient in project java-spanner by googleapis.
the class TransactionManagerAbortedTest method startStaticServer.
@BeforeClass
public static void startStaticServer() throws IOException {
mockSpanner = new MockSpannerServiceImpl();
// We don't want any unpredictable aborted transactions.
mockSpanner.setAbortProbability(0.0D);
mockSpanner.putStatementResult(StatementResult.read("FOO", KeySet.all(), Collections.singletonList("BAR"), READ_RESULTSET));
mockSpanner.putStatementResult(StatementResult.read("FOO", KeySet.singleKey(Key.of()), Collections.singletonList("BAR"), READ_ROW_RESULTSET));
mockSpanner.putStatementResult(StatementResult.query(SELECT1AND2, SELECT1AND2_RESULTSET));
mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT));
mockSpanner.putStatementResult(StatementResult.exception(UPDATE_ABORTED_STATEMENT, mockSpanner.createAbortedException(ByteString.copyFromUtf8("test"))));
String uniqueName = InProcessServerBuilder.generateName();
server = InProcessServerBuilder.forName(uniqueName).scheduledExecutorService(new ScheduledThreadPoolExecutor(1)).addService(mockSpanner).build().start();
channelProvider = LocalChannelProvider.create(uniqueName);
SpannerSettings settings = SpannerSettings.newBuilder().setTransportChannelProvider(channelProvider).setCredentialsProvider(NoCredentialsProvider.create()).build();
spannerClient = SpannerClient.create(settings);
}
use of com.google.cloud.spanner.v1.SpannerClient 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.cloud.spanner.v1.SpannerClient in project grpc-gcp-java by GoogleCloudPlatform.
the class SpannerClientV1TestCases method testExecuteSql.
void testExecuteSql() throws InterruptedException {
System.out.println("\nTestExecuteSql");
SpannerClient client = getClient();
Session session = client.createSession(CreateSessionRequest.newBuilder().setDatabase(database).build());
ExecuteSqlRequest request = ExecuteSqlRequest.newBuilder().setSession(session.getName()).setSql("select * FROM " + TABLE).build();
RpcCall<ExecuteSqlRequest, ResultSet> rpcCall = (ExecuteSqlRequest req) -> client.executeSql(req);
doTestBlocking(request, rpcCall);
cleanUpClient(client, session.getName());
}
use of com.google.cloud.spanner.v1.SpannerClient in project grpc-gcp-java by GoogleCloudPlatform.
the class SpannerClientV1TestCases method testPartitionQuery.
void testPartitionQuery() throws InterruptedException {
System.out.println("\nTestPartitionQuery");
SpannerClient client = getClient();
Session session = client.createSession(CreateSessionRequest.newBuilder().setDatabase(database).build());
TransactionOptions options = TransactionOptions.newBuilder().setReadOnly(TransactionOptions.ReadOnly.getDefaultInstance()).build();
TransactionSelector selector = TransactionSelector.newBuilder().setBegin(options).build();
PartitionQueryRequest request = PartitionQueryRequest.newBuilder().setSession(session.getName()).setSql("select * FROM " + LARGE_TABLE).setTransaction(selector).build();
RpcCall<PartitionQueryRequest, PartitionResponse> rpcCall = (PartitionQueryRequest req) -> client.partitionQuery(req);
doTestBlocking(request, rpcCall);
cleanUpClient(client, session.getName());
}
Aggregations