Search in sources :

Example 1 with LookupResponse

use of com.google.datastore.v1.LookupResponse in project YCSB by brianfrankcooper.

the class GoogleDatastoreClient method read.

@Override
public Status read(String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) {
    LookupRequest.Builder lookupRequest = LookupRequest.newBuilder();
    lookupRequest.addKeys(buildPrimaryKey(table, key));
    lookupRequest.getReadOptionsBuilder().setReadConsistency(this.readConsistency);
    // Note above, datastore lookupRequest always reads the entire entity, it
    // does not support reading a subset of "fields" (properties) of an entity.
    logger.debug("Built lookup request as: " + lookupRequest.toString());
    LookupResponse response = null;
    try {
        response = datastore.lookup(lookupRequest.build());
    } catch (DatastoreException exception) {
        logger.error(String.format("Datastore Exception when reading (%s): %s %s", exception.getMessage(), exception.getMethodName(), exception.getCode()));
        // will bubble up to the user as part of the YCSB Status "name".
        return new Status("ERROR-" + exception.getCode(), exception.getMessage());
    }
    if (response.getFoundCount() == 0) {
        return new Status("ERROR-404", "Not Found, key is: " + key);
    } else if (response.getFoundCount() > 1) {
        // entity back. Unexpected State.
        return Status.UNEXPECTED_STATE;
    }
    Entity entity = response.getFound(0).getEntity();
    logger.debug("Read entity: " + entity.toString());
    Map<String, Value> properties = entity.getProperties();
    Set<String> propertiesToReturn = (fields == null ? properties.keySet() : fields);
    for (String name : propertiesToReturn) {
        if (properties.containsKey(name)) {
            result.put(name, new StringByteIterator(properties.get(name).getStringValue()));
        }
    }
    return Status.OK;
}
Also used : Status(com.yahoo.ycsb.Status) StringByteIterator(com.yahoo.ycsb.StringByteIterator) DatastoreException(com.google.datastore.v1.client.DatastoreException)

Example 2 with LookupResponse

use of com.google.datastore.v1.LookupResponse in project google-cloud-java by GoogleCloudPlatform.

the class DatastoreTest method testRetryableException.

@Test
public void testRetryableException() throws Exception {
    LookupRequest requestPb = LookupRequest.newBuilder().addKeys(KEY1.toPb()).build();
    LookupResponse responsePb = LookupResponse.newBuilder().addFound(EntityResult.newBuilder().setEntity(ENTITY1.toPb())).build();
    EasyMock.expect(rpcMock.lookup(requestPb)).andThrow(new DatastoreException(14, "UNAVAILABLE", "UNAVAILABLE", null)).andReturn(responsePb);
    EasyMock.replay(rpcFactoryMock, rpcMock);
    Datastore datastore = rpcMockOptions.getService();
    Entity entity = datastore.get(KEY1);
    assertEquals(ENTITY1, entity);
    EasyMock.verify(rpcFactoryMock, rpcMock);
}
Also used : LookupResponse(com.google.datastore.v1.LookupResponse) LookupRequest(com.google.datastore.v1.LookupRequest) Test(org.junit.Test)

Aggregations

LookupRequest (com.google.datastore.v1.LookupRequest)1 LookupResponse (com.google.datastore.v1.LookupResponse)1 DatastoreException (com.google.datastore.v1.client.DatastoreException)1 Status (com.yahoo.ycsb.Status)1 StringByteIterator (com.yahoo.ycsb.StringByteIterator)1 Test (org.junit.Test)1