本文整理了Java中com.google.cloud.datastore.Entity.getKey()
方法的一些代码示例,展示了Entity.getKey()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Entity.getKey()
方法的具体详情如下:
包路径:com.google.cloud.datastore.Entity
类名称:Entity
方法名:getKey
暂无
代码示例来源:origin: googleapis/google-cloud-java
private static List<Entity> compileEntities(Key[] keys, Iterator<Entity> entities) {
Map<Key, Entity> map = Maps.newHashMapWithExpectedSize(keys.length);
while (entities.hasNext()) {
Entity entity = entities.next();
map.put(entity.getKey(), entity);
}
List<Entity> list = new ArrayList<>(keys.length);
for (Key key : keys) {
// this will include nulls for nonexistent keys
list.add(map.get(key));
}
return list;
}
}
代码示例来源:origin: googleapis/google-cloud-java
@SafeVarargs
@Override
public final void update(Entity... entities) {
validateActive();
for (Entity entity : entities) {
Key key = entity.getKey();
if (toDelete.contains(key)) {
throw newInvalidRequest(
"Entity with the key %s was already deleted in this %s", entity.getKey(), name);
}
if (toAdd.remove(key) != null || toPut.containsKey(key)) {
toPut.put(key, entity);
} else {
toUpdate.put(key, entity);
}
}
}
代码示例来源:origin: googleapis/google-cloud-java
@Override
public void update(Entity... entities) {
if (entities.length > 0) {
List<com.google.datastore.v1.Mutation> mutationsPb = new ArrayList<>();
Map<Key, Entity> dedupEntities = new LinkedHashMap<>();
for (Entity entity : entities) {
dedupEntities.put(entity.getKey(), entity);
}
for (Entity entity : dedupEntities.values()) {
mutationsPb.add(
com.google.datastore.v1.Mutation.newBuilder().setUpdate(entity.toPb()).build());
}
commitMutation(mutationsPb);
}
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testDelete() {
Iterator<Entity> keys =
DATASTORE.fetch(ENTITY1.getKey(), ENTITY2.getKey(), ENTITY3.getKey()).iterator();
assertEquals(ENTITY1, keys.next());
assertEquals(ENTITY2, keys.next());
assertNull(keys.next());
assertFalse(keys.hasNext());
DATASTORE.delete(ENTITY1.getKey(), ENTITY2.getKey(), ENTITY3.getKey());
keys = DATASTORE.fetch(ENTITY1.getKey(), ENTITY2.getKey(), ENTITY3.getKey()).iterator();
assertNull(keys.next());
assertNull(keys.next());
assertNull(keys.next());
assertFalse(keys.hasNext());
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testDelete() {
Iterator<Entity> keys =
datastore.fetch(ENTITY1.getKey(), ENTITY2.getKey(), ENTITY3.getKey()).iterator();
assertEquals(ENTITY1, keys.next());
assertEquals(ENTITY2, keys.next());
assertNull(keys.next());
assertFalse(keys.hasNext());
datastore.delete(ENTITY1.getKey(), ENTITY2.getKey(), ENTITY3.getKey());
keys = datastore.fetch(ENTITY1.getKey(), ENTITY2.getKey(), ENTITY3.getKey()).iterator();
assertNull(keys.next());
assertNull(keys.next());
assertNull(keys.next());
assertFalse(keys.hasNext());
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testFetchArrayDeferredResults() throws DatastoreException {
List<Entity> foundEntities =
createDatastoreForDeferredLookup().fetch(KEY1, KEY2, KEY3, KEY4, KEY5);
assertEquals(foundEntities.get(0).getKey(), KEY1);
assertEquals(foundEntities.get(1).getKey(), KEY2);
assertEquals(foundEntities.get(2).getKey(), KEY3);
assertEquals(foundEntities.get(3).getKey(), KEY4);
assertEquals(foundEntities.get(4).getKey(), KEY5);
assertEquals(foundEntities.size(), 5);
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testGetArrayDeferredResults() throws DatastoreException {
Set<Key> requestedKeys = new HashSet<>();
requestedKeys.add(KEY1);
requestedKeys.add(KEY2);
requestedKeys.add(KEY3);
requestedKeys.add(KEY4);
requestedKeys.add(KEY5);
Iterator<Entity> iter = createDatastoreForDeferredLookup().get(KEY1, KEY2, KEY3, KEY4, KEY5);
Set<Key> keysOfFoundEntities = new HashSet<>();
while (iter.hasNext()) {
keysOfFoundEntities.add(iter.next().getKey());
}
assertEquals(requestedKeys, keysOfFoundEntities);
}
代码示例来源:origin: googleapis/google-cloud-java
if (entity.getKey() instanceof Key) {
Entity completeEntity = Entity.convert((FullEntity<Key>) entity);
dedupEntities.put(completeEntity.getKey(), completeEntity);
} else {
mutationsPb.add(
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testAddEntity() {
List<Entity> keys = datastore.fetch(ENTITY1.getKey(), ENTITY3.getKey());
assertEquals(ENTITY1, keys.get(0));
assertNull(keys.get(1));
assertEquals(2, keys.size());
try {
datastore.add(ENTITY1);
fail("Expecting a failure");
} catch (DatastoreException expected) {
// expected;
}
List<Entity> entities = datastore.add(ENTITY3, PARTIAL_ENTITY1, PARTIAL_ENTITY2);
assertEquals(ENTITY3, datastore.get(ENTITY3.getKey()));
assertEquals(ENTITY3, entities.get(0));
assertEquals(PARTIAL_ENTITY1.getProperties(), entities.get(1).getProperties());
assertEquals(PARTIAL_ENTITY1.getKey().getAncestors(), entities.get(1).getKey().getAncestors());
assertNotNull(datastore.get(entities.get(1).getKey()));
assertEquals(PARTIAL_ENTITY2.getProperties(), entities.get(2).getProperties());
assertEquals(PARTIAL_ENTITY2.getKey().getAncestors(), entities.get(2).getKey().getAncestors());
assertNotNull(datastore.get(entities.get(2).getKey()));
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testAddEntity() {
List<Entity> keys = DATASTORE.fetch(ENTITY1.getKey(), ENTITY3.getKey());
assertEquals(ENTITY1, keys.get(0));
assertNull(keys.get(1));
assertEquals(2, keys.size());
try {
DATASTORE.add(ENTITY1);
fail("Expecting a failure");
} catch (DatastoreException expected) {
// expected;
}
List<Entity> entities = DATASTORE.add(ENTITY3, PARTIAL_ENTITY1, PARTIAL_ENTITY2);
assertEquals(ENTITY3, DATASTORE.get(ENTITY3.getKey()));
assertEquals(ENTITY3, entities.get(0));
assertEquals(PARTIAL_ENTITY1.getNames(), entities.get(1).getNames());
assertEquals(PARTIAL_ENTITY1.getKey().getAncestors(), entities.get(1).getKey().getAncestors());
assertNotNull(DATASTORE.get(entities.get(1).getKey()));
assertEquals(PARTIAL_ENTITY2.getNames(), entities.get(2).getNames());
assertEquals(PARTIAL_ENTITY2.getKey().getAncestors(), entities.get(2).getKey().getAncestors());
assertNotNull(DATASTORE.get(entities.get(2).getKey()));
for (Entity entity : entities) {
DATASTORE.delete(entity.getKey());
}
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testUpdate() {
List<Entity> keys = DATASTORE.fetch(ENTITY1.getKey(), ENTITY3.getKey());
assertEquals(ENTITY1, keys.get(0));
assertNull(keys.get(1));
assertEquals(2, keys.size());
try {
DATASTORE.update(ENTITY3);
fail("Expecting a failure");
} catch (DatastoreException expected) {
// expected;
}
DATASTORE.add(ENTITY3);
assertEquals(ENTITY3, DATASTORE.get(ENTITY3.getKey()));
Entity entity3 = Entity.newBuilder(ENTITY3).clear().set("bla", new NullValue()).build();
assertNotEquals(ENTITY3, entity3);
DATASTORE.update(entity3);
assertEquals(entity3, DATASTORE.get(ENTITY3.getKey()));
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testUpdate() {
List<Entity> keys = datastore.fetch(ENTITY1.getKey(), ENTITY3.getKey());
assertEquals(ENTITY1, keys.get(0));
assertNull(keys.get(1));
assertEquals(2, keys.size());
try {
datastore.update(ENTITY3);
fail("Expecting a failure");
} catch (DatastoreException expected) {
// expected;
}
datastore.add(ENTITY3);
assertEquals(ENTITY3, datastore.get(ENTITY3.getKey()));
Entity entity3 = Entity.newBuilder(ENTITY3).clear().set("bla", new NullValue()).build();
assertNotEquals(ENTITY3, entity3);
datastore.update(entity3);
assertEquals(entity3, datastore.get(ENTITY3.getKey()));
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testPut() {
Entity updatedEntity = Entity.newBuilder(ENTITY1).set("new_property", 42L).build();
assertEquals(updatedEntity, DATASTORE.put(updatedEntity));
assertEquals(updatedEntity, DATASTORE.get(updatedEntity.getKey()));
Entity entity2 = Entity.newBuilder(ENTITY2).clear().set("bla", new NullValue()).build();
assertNotEquals(ENTITY2, entity2);
List<Entity> entities = DATASTORE.put(ENTITY1, entity2, ENTITY3, PARTIAL_ENTITY1);
assertEquals(ENTITY1, entities.get(0));
assertEquals(entity2, entities.get(1));
assertEquals(ENTITY3, entities.get(2));
assertEquals(PARTIAL_ENTITY1.getNames(), entities.get(3).getNames());
assertEquals(PARTIAL_ENTITY1.getKey().getAncestors(), entities.get(3).getKey().getAncestors());
assertEquals(ENTITY1, DATASTORE.get(ENTITY1.getKey()));
assertEquals(entity2, DATASTORE.get(entity2.getKey()));
assertEquals(ENTITY3, DATASTORE.get(ENTITY3.getKey()));
Entity entity = DATASTORE.get(entities.get(3).getKey());
assertEquals(entities.get(3), entity);
for (Entity entityToDelete : entities) {
DATASTORE.delete(entityToDelete.getKey());
}
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testPut() {
Entity updatedEntity = Entity.newBuilder(ENTITY1).set("new_property", 42L).build();
assertEquals(updatedEntity, datastore.put(updatedEntity));
assertEquals(updatedEntity, datastore.get(updatedEntity.getKey()));
Entity entity2 = Entity.newBuilder(ENTITY2).clear().set("bla", new NullValue()).build();
assertNotEquals(ENTITY2, entity2);
List<Entity> entities = datastore.put(ENTITY1, entity2, ENTITY3, PARTIAL_ENTITY1);
assertEquals(ENTITY1, entities.get(0));
assertEquals(entity2, entities.get(1));
assertEquals(ENTITY3, entities.get(2));
assertEquals(PARTIAL_ENTITY1.getProperties(), entities.get(3).getProperties());
assertEquals(PARTIAL_ENTITY1.getKey().getAncestors(), entities.get(3).getKey().getAncestors());
assertEquals(ENTITY1, datastore.get(ENTITY1.getKey()));
assertEquals(entity2, datastore.get(entity2.getKey()));
assertEquals(ENTITY3, datastore.get(ENTITY3.getKey()));
Entity entity = datastore.get(entities.get(3).getKey());
assertEquals(entities.get(3), entity);
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testEntity() throws Exception {
assertTrue(ENTITY.hasKey());
assertEquals(KEY1, ENTITY.getKey());
assertEquals("bar", ENTITY.getString("foo"));
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testCopyFrom() throws Exception {
Entity.Builder builder = Entity.newBuilder(ENTITY);
assertEquals(ENTITY, builder.build());
Entity entity = builder.setKey(KEY2).build();
assertNotEquals(ENTITY, entity);
assertEquals(KEY2, entity.getKey());
assertEquals(ENTITY.getProperties(), entity.getProperties());
}
代码示例来源:origin: googleapis/google-cloud-java
private void verifyNotUsable(DatastoreWriter writer) {
try {
writer.add(ENTITY3);
fail("Expecting a failure");
} catch (DatastoreException ex) {
// expected to fail
}
try {
writer.put(ENTITY3);
fail("Expecting a failure");
} catch (DatastoreException ex) {
// expected to fail
}
try {
writer.update(ENTITY3);
fail("Expecting a failure");
} catch (DatastoreException ex) {
// expected to fail
}
try {
writer.delete(ENTITY3.getKey());
fail("Expecting a failure");
} catch (DatastoreException ex) {
// expected to fail
}
}
代码示例来源:origin: googleapis/google-cloud-java
QueryResults<Key> results2 = datastore.run(keyOnlyQuery);
assertTrue(results2.hasNext());
assertEquals(ENTITY1.getKey(), results2.next());
assertFalse(results2.hasNext());
assertTrue(results3.hasNext());
ProjectionEntity projectionEntity = results3.next();
assertEquals(ENTITY1.getKey(), projectionEntity.getKey());
assertTrue(projectionEntity.getNames().isEmpty());
assertFalse(results2.hasNext());
assertTrue(results4.hasNext());
ProjectionEntity entity = results4.next();
assertEquals(ENTITY2.getKey(), entity.getKey());
assertEquals(20, entity.getLong("age"));
assertEquals(1, entity.getProperties().size());
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testTransactionWithQuery() {
Query<Entity> query =
Query.newEntityQueryBuilder()
.setKind(KIND2)
.setFilter(PropertyFilter.hasAncestor(KEY2))
.build();
Transaction transaction = datastore.newTransaction();
QueryResults<Entity> results = transaction.run(query);
assertEquals(ENTITY2, results.next());
assertFalse(results.hasNext());
transaction.add(ENTITY3);
transaction.commit();
assertEquals(ENTITY3, datastore.get(KEY3));
transaction = datastore.newTransaction();
results = transaction.run(query);
assertEquals(ENTITY2, results.next());
transaction.delete(ENTITY3.getKey());
// update entity2 during the transaction
datastore.put(Entity.newBuilder(ENTITY2).clear().build());
try {
transaction.commit();
fail("Expecting a failure");
} catch (DatastoreException expected) {
assertEquals("ABORTED", expected.getReason());
}
}
代码示例来源:origin: googleapis/google-cloud-java
assertEquals(ENTITY2, results.next());
assertFalse(results.hasNext());
transaction.delete(ENTITY3.getKey());
内容来源于网络,如有侵权,请联系作者删除!