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;
}
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);
}
Aggregations