Search in sources :

Example 1 with LookupRequest

use of com.google.datastore.v1.LookupRequest 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 LookupRequest

use of com.google.datastore.v1.LookupRequest 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)

Example 3 with LookupRequest

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

the class DatastoreTest method testRuntimeException.

@Test
public void testRuntimeException() throws Exception {
    LookupRequest requestPb = LookupRequest.newBuilder().addKeys(KEY1.toPb()).build();
    String exceptionMessage = "Artificial runtime exception";
    EasyMock.expect(rpcMock.lookup(requestPb)).andThrow(new RuntimeException(exceptionMessage));
    EasyMock.replay(rpcFactoryMock, rpcMock);
    Datastore datastore = rpcMockOptions.getService();
    thrown.expect(DatastoreException.class);
    thrown.expectMessage(exceptionMessage);
    datastore.get(KEY1);
    EasyMock.verify(rpcFactoryMock, rpcMock);
}
Also used : ByteString(com.google.protobuf.ByteString) LookupRequest(com.google.datastore.v1.LookupRequest) Test(org.junit.Test)

Example 4 with LookupRequest

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

the class DatastoreTest method testNonRetryableException.

@Test
public void testNonRetryableException() throws Exception {
    LookupRequest requestPb = LookupRequest.newBuilder().addKeys(KEY1.toPb()).build();
    EasyMock.expect(rpcMock.lookup(requestPb)).andThrow(new DatastoreException(DatastoreException.UNKNOWN_CODE, "denied", "PERMISSION_DENIED")).times(1);
    EasyMock.replay(rpcFactoryMock, rpcMock);
    Datastore datastore = rpcMockOptions.getService();
    thrown.expect(DatastoreException.class);
    thrown.expectMessage("denied");
    datastore.get(KEY1);
    EasyMock.verify(rpcFactoryMock, rpcMock);
}
Also used : LookupRequest(com.google.datastore.v1.LookupRequest) Test(org.junit.Test)

Example 5 with LookupRequest

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

the class DatastoreTest method testLookupEventualConsistency.

@Test
public void testLookupEventualConsistency() {
    ReadOptions readOption = ReadOptions.newBuilder().setReadConsistencyValue(ReadConsistency.EVENTUAL_VALUE).build();
    com.google.datastore.v1.Key key = com.google.datastore.v1.Key.newBuilder().setPartitionId(PartitionId.newBuilder().setProjectId(PROJECT_ID).build()).addPath(com.google.datastore.v1.Key.PathElement.newBuilder().setKind("kind1").setName("name").build()).build();
    LookupRequest lookupRequest = LookupRequest.newBuilder().setReadOptions(readOption).addKeys(key).build();
    EasyMock.expect(rpcMock.lookup(lookupRequest)).andReturn(LookupResponse.newBuilder().build()).times(3);
    EasyMock.replay(rpcFactoryMock, rpcMock);
    Datastore datastore = rpcMockOptions.getService();
    datastore.get(KEY1, ReadOption.eventualConsistency());
    datastore.get(ImmutableList.of(KEY1), ReadOption.eventualConsistency());
    datastore.fetch(ImmutableList.of(KEY1), ReadOption.eventualConsistency());
    EasyMock.verify(rpcFactoryMock, rpcMock);
}
Also used : ReadOptions(com.google.datastore.v1.ReadOptions) LookupRequest(com.google.datastore.v1.LookupRequest) Test(org.junit.Test)

Aggregations

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