Search in sources :

Example 6 with ResultSet

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

Example 7 with ResultSet

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

Example 8 with ResultSet

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;
}
Also used : TransactionContext(com.google.cloud.spanner.TransactionContext) ArgumentMatchers(org.mockito.ArgumentMatchers) Mockito.when(org.mockito.Mockito.when) Mutation(com.google.cloud.spanner.Mutation) ArrayList(java.util.ArrayList) Answer(org.mockito.stubbing.Answer) ResultSet(com.google.cloud.spanner.ResultSet) SQLException(java.sql.SQLException) List(java.util.List) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Mockito.doAnswer(org.mockito.Mockito.doAnswer) RecoveredXid(nl.topicus.jdbc.xa.RecoveredXid) Xid(javax.transaction.xa.Xid) Mockito.mock(org.mockito.Mockito.mock) TransactionContext(com.google.cloud.spanner.TransactionContext) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResultSet(com.google.cloud.spanner.ResultSet) Mutation(com.google.cloud.spanner.Mutation)

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

Example 10 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) StringByteIterator(com.yahoo.ycsb.StringByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator) HashMap(java.util.HashMap) Statement(com.google.cloud.spanner.Statement) ResultSet(com.google.cloud.spanner.ResultSet) DBException(com.yahoo.ycsb.DBException)

Aggregations

ResultSet (com.google.cloud.spanner.ResultSet)24 Test (org.junit.Test)12 IntegrationTest (com.google.cloud.spanner.IntegrationTest)10 Statement (com.google.cloud.spanner.Statement)7 KeySet (com.google.cloud.spanner.KeySet)3 Mutation (com.google.cloud.spanner.Mutation)3 DBException (com.yahoo.ycsb.DBException)3 HashMap (java.util.HashMap)3 ByteArray (com.google.cloud.ByteArray)2 DatabaseClient (com.google.cloud.spanner.DatabaseClient)2 Struct (com.google.cloud.spanner.Struct)2 TransactionContext (com.google.cloud.spanner.TransactionContext)2 Joiner (com.google.common.base.Joiner)2 ResultSetStats (com.google.spanner.v1.ResultSetStats)2 ByteIterator (com.yahoo.ycsb.ByteIterator)2 StringByteIterator (com.yahoo.ycsb.StringByteIterator)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 Database (com.google.cloud.spanner.Database)1 SpannerException (com.google.cloud.spanner.SpannerException)1