Search in sources :

Example 56 with ResultSet

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;
    }
}
Also used : Joiner(com.google.common.base.Joiner) Statement(com.google.cloud.spanner.Statement) ResultSet(com.google.cloud.spanner.ResultSet) DBException(site.ycsb.DBException)

Example 57 with ResultSet

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;
    }
}
Also used : KeySet(com.google.cloud.spanner.KeySet) ByteIterator(site.ycsb.ByteIterator) StringByteIterator(site.ycsb.StringByteIterator) HashMap(java.util.HashMap) ResultSet(com.google.cloud.spanner.ResultSet) DBException(site.ycsb.DBException)

Example 58 with ResultSet

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;
    }
}
Also used : Joiner(com.google.common.base.Joiner) ByteIterator(site.ycsb.ByteIterator) StringByteIterator(site.ycsb.StringByteIterator) HashMap(java.util.HashMap) Statement(com.google.cloud.spanner.Statement) ResultSet(com.google.cloud.spanner.ResultSet) DBException(site.ycsb.DBException)

Example 59 with ResultSet

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());
        }
    }
}
Also used : Array(java.sql.Array) ByteArray(com.google.cloud.ByteArray) CloudSpannerStatement(nl.topicus.jdbc.statement.CloudSpannerStatement) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet) ResultSet(com.google.cloud.spanner.ResultSet) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet) Test(org.junit.Test) UnitTest(nl.topicus.jdbc.test.category.UnitTest)

Example 60 with ResultSet

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);
}
Also used : ResultSet(com.google.cloud.spanner.ResultSet) Mutation(com.google.cloud.spanner.Mutation)

Aggregations

ResultSet (com.google.cloud.spanner.ResultSet)71 Test (org.junit.Test)25 Statement (com.google.cloud.spanner.Statement)15 Struct (com.google.cloud.spanner.Struct)12 ArrayList (java.util.ArrayList)12 KeySet (com.google.cloud.spanner.KeySet)11 IntegrationTest (com.google.cloud.spanner.IntegrationTest)10 ReadContext (com.google.cloud.spanner.ReadContext)10 DatabaseClient (com.google.cloud.spanner.DatabaseClient)8 BatchReadOnlyTransaction (com.google.cloud.spanner.BatchReadOnlyTransaction)5 Mutation (com.google.cloud.spanner.Mutation)5 Partition (com.google.cloud.spanner.Partition)5 ReadOnlyTransaction (com.google.cloud.spanner.ReadOnlyTransaction)5 HashMap (java.util.HashMap)5 Key (com.google.cloud.spanner.Key)4 ReadOption (com.google.cloud.spanner.Options.ReadOption)4 Spanner (com.google.cloud.spanner.Spanner)4 SpannerOptions (com.google.cloud.spanner.SpannerOptions)4 TransactionContext (com.google.cloud.spanner.TransactionContext)4 Joiner (com.google.common.base.Joiner)4