Search in sources :

Example 1 with Entity

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

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

the class GoogleDatastoreClient method doSingleItemMutation.

private Status doSingleItemMutation(String table, String key, @Nullable HashMap<String, ByteIterator> values, MutationType mutationType) {
    // First build the key.
    Key.Builder datastoreKey = buildPrimaryKey(table, key);
    // Build a commit request in non-transactional mode.
    // Single item mutation to google datastore
    // is always atomic and strongly consistent. Transaction is only necessary
    // for multi-item mutation, or Read-modify-write operation.
    CommitRequest.Builder commitRequest = CommitRequest.newBuilder();
    commitRequest.setMode(Mode.NON_TRANSACTIONAL);
    if (mutationType == MutationType.DELETE) {
        commitRequest.addMutationsBuilder().setDelete(datastoreKey);
    } else {
        // If this is not for delete, build the entity.
        Entity.Builder entityBuilder = Entity.newBuilder();
        entityBuilder.setKey(datastoreKey);
        for (Entry<String, ByteIterator> val : values.entrySet()) {
            entityBuilder.getMutableProperties().put(val.getKey(), Value.newBuilder().setStringValue(val.getValue().toString()).build());
        }
        Entity entity = entityBuilder.build();
        logger.debug("entity built as: " + entity.toString());
        if (mutationType == MutationType.UPSERT) {
            commitRequest.addMutationsBuilder().setUpsert(entity);
        } else if (mutationType == MutationType.UPDATE) {
            commitRequest.addMutationsBuilder().setUpdate(entity);
        } else {
            throw new RuntimeException("Impossible MutationType, code bug.");
        }
    }
    try {
        datastore.commit(commitRequest.build());
        logger.debug("successfully committed.");
    } catch (DatastoreException exception) {
        // Catch all Datastore rpc errors.
        // Log the exception, the name of the method called and the error code.
        logger.error(String.format("Datastore Exception when committing (%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());
    }
    return Status.OK;
}
Also used : Status(com.yahoo.ycsb.Status) StringByteIterator(com.yahoo.ycsb.StringByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator) DatastoreException(com.google.datastore.v1.client.DatastoreException)

Example 3 with Entity

use of com.google.datastore.v1.Entity in project Denizen-For-Bukkit by DenizenScript.

the class WorldHelper_v1_8_R3 method setWorldAccess.

@Override
public void setWorldAccess(World world, final WorldAccess worldAccess) {
    if (worldAccessMap.containsKey(world)) {
        removeWorldAccess(world);
    }
    IWorldAccess nmsWorldAccess = new IWorldAccess() {

        @Override
        public void a(BlockPosition blockPosition) {
        }

        @Override
        public void b(BlockPosition blockPosition) {
        }

        @Override
        public void a(int i, int i1, int i2, int i3, int i4, int i5) {
        }

        @Override
        public void a(String s, double v, double v1, double v2, float v3, float v4) {
        }

        @Override
        public void a(EntityHuman entityHuman, String s, double v, double v1, double v2, float v3, float v4) {
        }

        @Override
        public void a(int i, boolean b, double v, double v1, double v2, double v3, double v4, double v5, int... ints) {
        }

        @Override
        public void a(Entity entity) {
        }

        @Override
        public void b(Entity entity) {
            worldAccess.despawn(entity.getBukkitEntity());
        }

        @Override
        public void a(String s, BlockPosition blockPosition) {
        }

        @Override
        public void a(int i, BlockPosition blockPosition, int i1) {
        }

        @Override
        public void a(EntityHuman entityHuman, int i, BlockPosition blockPosition, int i1) {
        }

        @Override
        public void b(int i, BlockPosition blockPosition, int i1) {
        }
    };
    worldAccessMap.put(world, nmsWorldAccess);
    ((CraftWorld) world).getHandle().addIWorldAccess(nmsWorldAccess);
}
Also used : IWorldAccess(net.minecraft.server.v1_8_R3.IWorldAccess) EntityHuman(net.minecraft.server.v1_8_R3.EntityHuman) Entity(net.minecraft.server.v1_8_R3.Entity) BlockPosition(net.minecraft.server.v1_8_R3.BlockPosition)

Example 4 with Entity

use of com.google.datastore.v1.Entity in project beam by apache.

the class DatastoreV1Test method testAddEntities.

@Test
public /**
   * Test that entities with valid keys are transformed to upsert mutations.
   */
void testAddEntities() throws Exception {
    Key key = makeKey("bird", "finch").build();
    Entity entity = Entity.newBuilder().setKey(key).build();
    UpsertFn upsertFn = new UpsertFn();
    Mutation exceptedMutation = makeUpsert(entity).build();
    assertEquals(upsertFn.apply(entity), exceptedMutation);
}
Also used : Entity(com.google.datastore.v1.Entity) DeleteEntity(org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.DeleteEntity) UpsertFn(org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.UpsertFn) Mutation(com.google.datastore.v1.Mutation) DeleteKey(org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.DeleteKey) Key(com.google.datastore.v1.Key) DatastoreHelper.makeKey(com.google.datastore.v1.client.DatastoreHelper.makeKey) DatastoreV1.isValidKey(org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.isValidKey) Test(org.junit.Test)

Example 5 with Entity

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

Entity (com.google.datastore.v1.Entity)9 Test (org.junit.Test)9 DeleteEntity (org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.DeleteEntity)7 Key (com.google.datastore.v1.Key)5 RunQueryResponse (com.google.datastore.v1.RunQueryResponse)5 QueryResultBatch (com.google.datastore.v1.QueryResultBatch)4 RunQueryRequest (com.google.datastore.v1.RunQueryRequest)4 DatastoreHelper.makeKey (com.google.datastore.v1.client.DatastoreHelper.makeKey)4 DeleteKey (org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.DeleteKey)4 DatastoreV1.isValidKey (org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.isValidKey)4 Mutation (com.google.datastore.v1.Mutation)3 Query (com.google.datastore.v1.Query)3 ArrayList (java.util.ArrayList)3 Function (com.google.common.base.Function)2 Optional (com.google.common.base.Optional)2 Datastore (com.google.datastore.v1.client.Datastore)2 DatastoreException (com.google.datastore.v1.client.DatastoreException)2 Status (com.yahoo.ycsb.Status)2 StringByteIterator (com.yahoo.ycsb.StringByteIterator)2 Date (java.util.Date)2