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, Map<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 scan.
@Override
public Status scan(String table, String startKey, int recordCount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
if (queriesForReads) {
return scanUsingQuery(table, startKey, recordCount, fields, result);
}
Iterable<String> columns = fields == null ? STANDARD_FIELDS : fields;
KeySet keySet = KeySet.newBuilder().addRange(KeyRange.closedClosed(Key.of(startKey), Key.of())).build();
try (ResultSet resultSet = dbClient.singleUse(timestampBound).read(table, keySet, columns, Options.limit(recordCount))) {
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, "scan()", 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;
}
}
use of com.google.cloud.spanner.ResultSet in project spanner-jdbc by olavloite.
the class CloudSpannerDataTypeTest method testGetArrayElements.
@Test
public void testGetArrayElements() throws SQLException {
ResultSet googleResultSet = Mockito.mock(ResultSet.class);
when(googleResultSet.getBooleanList(0)).thenReturn(Arrays.asList(true, true, false));
when(googleResultSet.getBytesList(0)).thenReturn(Arrays.asList(ByteArray.copyFrom("foo"), ByteArray.copyFrom("bar")));
when(googleResultSet.getDateList(0)).thenReturn(Arrays.asList(com.google.cloud.Date.fromYearMonthDay(2017, 10, 1), com.google.cloud.Date.fromYearMonthDay(2017, 9, 1)));
when(googleResultSet.getDoubleList(0)).thenReturn(Arrays.asList(1d, 2d, 3d));
when(googleResultSet.getLongList(0)).thenReturn(Arrays.asList(1l, 2l, 3l));
when(googleResultSet.getStringList(0)).thenReturn(Arrays.asList("foo", "bar"));
when(googleResultSet.getTimestampList(0)).thenReturn(Arrays.asList(com.google.cloud.Timestamp.now(), com.google.cloud.Timestamp.now()));
when(googleResultSet.next()).thenReturn(true);
try (CloudSpannerResultSet rs = new CloudSpannerResultSet(Mockito.mock(CloudSpannerStatement.class), googleResultSet, "SELECT * FROM FOO")) {
rs.next();
for (CloudSpannerDataType type : CloudSpannerDataType.values()) {
when(googleResultSet.getColumnType(0)).thenReturn(Type.array(type.getGoogleType()));
Array array = rs.getArray(1);
assertTrue(array.getArray().getClass().isArray());
assertArrayEquals((Object[]) array.getArray(), (Object[]) ((Array) rs.getObject(1)).getArray());
}
}
}
use of com.google.cloud.spanner.ResultSet in project spanner-jdbc by olavloite.
the class XATransaction method commitPrepared.
static void commitPrepared(TransactionContext transaction, String xid) throws SQLException {
try (ResultSet rs = transaction.executeQuery(getPreparedMutationsStatement(xid))) {
while (rs.next()) {
String serialized = rs.getString(1);
Mutation mutation = deserializeMutation(serialized);
transaction.buffer(mutation);
}
}
cleanupPrepared(transaction, xid);
}
Aggregations