use of com.google.cloud.spanner.ResultSet in project spanner-jdbc by olavloite.
the class XATransaction method cleanupPrepared.
private static void cleanupPrepared(TransactionContext transaction, String xid) {
boolean foundRecords = false;
KeySet.Builder builder = KeySet.newBuilder();
try (ResultSet rs = transaction.executeQuery(getPreparedMutationsStatement(xid))) {
while (rs.next()) {
foundRecords = true;
long number = rs.getLong(0);
builder.addKey(Key.of(xid, number));
}
}
if (foundRecords) {
Mutation delete = Mutation.delete(CloudSpannerXAConnection.XA_PREPARED_MUTATIONS_TABLE, builder.build());
transaction.buffer(delete);
}
}
use of com.google.cloud.spanner.ResultSet in project spanner-jdbc by olavloite.
the class TransactionContextMock method mockPreparedWithoutMutationsResultSet.
private ResultSet mockPreparedWithoutMutationsResultSet() {
ResultSet rs = mock(ResultSet.class);
when(rs.next()).thenReturn(false);
return rs;
}
use of com.google.cloud.spanner.ResultSet in project spanner-jdbc by olavloite.
the class TransactionContextMock method createTransactionContextMock.
@SuppressWarnings("unchecked")
TransactionContext createTransactionContextMock() {
TransactionContext res = mock(TransactionContext.class);
when(res.executeQuery(XATransaction.getPreparedMutationsStatement(RecoveredXid.xidToString(XID_WITHOUT_MUTATIONS)))).then(new Answer<ResultSet>() {
@Override
public ResultSet answer(InvocationOnMock invocation) throws Throwable {
return mockPreparedWithoutMutationsResultSet();
}
});
when(res.executeQuery(XATransaction.getPreparedMutationsStatement(RecoveredXid.xidToString(XID_WITH_MUTATIONS)))).then(new Answer<ResultSet>() {
@Override
public ResultSet answer(InvocationOnMock invocation) throws Throwable {
return mockPreparedWithMutationsResultSet();
}
});
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
buffer.add((Mutation) invocation.getArgument(0));
return null;
}
}).when(res).buffer(ArgumentMatchers.any(Mutation.class));
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
Iterable<Mutation> it = (Iterable<Mutation>) invocation.getArgument(0);
it.forEach(e -> buffer.add(e));
return null;
}
}).when(res).buffer(ArgumentMatchers.any(Iterable.class));
return res;
}
use of com.google.cloud.spanner.ResultSet in project YCSB by brianfrankcooper.
the class CloudSpannerClient method readUsingQuery.
private Status readUsingQuery(String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) {
Statement query;
Iterable<String> columns = fields == null ? STANDARD_FIELDS : fields;
if (fields == null || fields.size() == fieldCount) {
query = Statement.newBuilder(standardQuery).bind("key").to(key).build();
} else {
Joiner joiner = Joiner.on(',');
query = Statement.newBuilder("SELECT ").append(joiner.join(fields)).append(" FROM ").append(table).append(" WHERE id=@key").bind("key").to(key).build();
}
try (ResultSet resultSet = dbClient.singleUse(timestampBound).executeQuery(query)) {
resultSet.next();
decodeStruct(columns, resultSet, result);
if (resultSet.next()) {
throw new Exception("Expected exactly one row for each read.");
}
return Status.OK;
} catch (Exception e) {
LOGGER.log(Level.INFO, "readUsingQuery()", e);
return Status.ERROR;
}
}
use of com.google.cloud.spanner.ResultSet in project YCSB by brianfrankcooper.
the class CloudSpannerClient method scanUsingQuery.
private Status scanUsingQuery(String table, String startKey, int recordCount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
Iterable<String> columns = fields == null ? STANDARD_FIELDS : fields;
Statement query;
if (fields == null || fields.size() == fieldCount) {
query = Statement.newBuilder(standardScan).bind("startKey").to(startKey).bind("count").to(recordCount).build();
} else {
Joiner joiner = Joiner.on(',');
query = Statement.newBuilder("SELECT ").append(joiner.join(fields)).append(" FROM ").append(table).append(" WHERE id>=@startKey LIMIT @count").bind("startKey").to(startKey).bind("count").to(recordCount).build();
}
try (ResultSet resultSet = dbClient.singleUse(timestampBound).executeQuery(query)) {
while (resultSet.next()) {
HashMap<String, ByteIterator> row = new HashMap<>();
decodeStruct(columns, resultSet, row);
result.add(row);
}
return Status.OK;
} catch (Exception e) {
LOGGER.log(Level.INFO, "scanUsingQuery()", e);
return Status.ERROR;
}
}
Aggregations