Entity(FullEntity<Key> from) { super(from); Preconditions.checkArgument(from.getKey() != null); }
private void putInternal(FullEntity<Key> entity) { Key key = entity.getKey(); toAdd.remove(key); toUpdate.remove(key); toDelete.remove(key); toPut.put(key, entity); }
private void addInternal(FullEntity<Key> entity) { Key key = entity.getKey(); if (toAdd.containsKey(key) || toUpdate.containsKey(key) || toPut.containsKey(key)) { throw newInvalidRequest( "Entity with the key %s was already added or updated in this %s", entity.getKey(), name); } if (toDelete.remove(key)) { toPut.put(key, entity); } else { toAdd.put(key, entity); } }
@SuppressWarnings("unchecked") @Override public final void addWithDeferredIdAllocation(FullEntity<?>... entities) { validateActive(); for (FullEntity<?> entity : entities) { IncompleteKey key = entity.getKey(); Preconditions.checkArgument(key != null, "Entity must have a key"); if (key instanceof Key) { addInternal((FullEntity<Key>) entity); } else { toAddAutoId.add((FullEntity<IncompleteKey>) entity); } } }
@SuppressWarnings("unchecked") @Override public final void putWithDeferredIdAllocation(FullEntity<?>... entities) { validateActive(); for (FullEntity<?> entity : entities) { IncompleteKey key = entity.getKey(); Preconditions.checkArgument(key != null, "Entity must have a key"); if (key instanceof Key) { putInternal(Entity.convert((FullEntity<Key>) entity)); } else { toAddAutoId.add((FullEntity<IncompleteKey>) entity); } } }
List<IncompleteKey> incompleteKeys = Lists.newArrayListWithExpectedSize(entities.length); for (FullEntity<?> entity : entities) { IncompleteKey key = entity.getKey(); Preconditions.checkArgument(key != null, "Entity must have a key"); if (!(key instanceof Key)) { if (entity.getKey() instanceof Key) { putInternal((FullEntity<Key>) entity); answer.add(Entity.convert((FullEntity<Key>) entity));
List<IncompleteKey> incompleteKeys = Lists.newArrayListWithExpectedSize(entities.length); for (FullEntity<?> entity : entities) { IncompleteKey key = entity.getKey(); Preconditions.checkArgument(key != null, "Entity must have a key"); if (!(key instanceof Key)) { if (entity.getKey() instanceof Key) { addInternal((FullEntity<Key>) entity); answer.add(Entity.convert((FullEntity<Key>) entity));
for (FullEntity<?> entity : entities) { Entity completeEntity = null; if (entity.getKey() instanceof Key) { completeEntity = Entity.convert((FullEntity<Key>) entity); if (completeEntities.put(completeEntity.getKey(), completeEntity) != null) { throw DatastoreException.throwInvalidRequest( "Duplicate entity with the key %s", entity.getKey()); ImmutableList.Builder<Entity> responseBuilder = ImmutableList.builder(); for (FullEntity<?> entity : entities) { Entity completeEntity = completeEntities.get(entity.getKey()); if (completeEntity != null) { responseBuilder.add(completeEntity);
for (FullEntity<?> entity : entities) { Preconditions.checkArgument(entity.hasKey(), "Entity %s is missing a key", entity); if (entity.getKey() instanceof Key) { Entity completeEntity = Entity.convert((FullEntity<Key>) entity); dedupEntities.put(completeEntity.getKey(), completeEntity); ImmutableList.Builder<Entity> responseBuilder = ImmutableList.builder(); for (FullEntity<?> entity : entities) { Entity completeEntity = dedupEntities.get(entity.getKey()); if (completeEntity != null) { responseBuilder.add(completeEntity);
@Test public void testFullEntity() throws Exception { assertTrue(COMPLETE_ENTITY1.hasKey()); assertEquals(KEY1, COMPLETE_ENTITY1.getKey()); assertEquals("bar", COMPLETE_ENTITY1.getString("foo")); assertTrue(COMPLETE_ENTITY2.hasKey()); assertEquals(KEY2, COMPLETE_ENTITY2.getKey()); assertEquals("bar", COMPLETE_ENTITY2.getString("foo")); }
@Test public void testNoKey() throws Exception { FullEntity<IncompleteKey> entity = FullEntity.newBuilder().set("foo", "bar").build(); assertFalse(entity.hasKey()); assertNull(entity.getKey()); assertEquals("bar", entity.getString("foo")); entity = FullEntity.newBuilder((IncompleteKey) null).build(); assertFalse(entity.hasKey()); assertNull(entity.getKey()); }
@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())); }
@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()); } }
assertSame(entity4, entities.get(0)); assertEquals(PARTIAL_ENTITY2.getNames(), entity6.getNames()); assertEquals(PARTIAL_ENTITY2.getKey().getProjectId(), entity6.getKey().getProjectId()); assertEquals(PARTIAL_ENTITY2.getKey().getNamespace(), entity6.getKey().getNamespace()); assertEquals(PARTIAL_ENTITY2.getKey().getAncestors(), entity6.getKey().getAncestors()); assertEquals(PARTIAL_ENTITY2.getKey().getKind(), entity6.getKey().getKind()); assertEquals(PARTIAL_ENTITY2.getKey(), IncompleteKey.newBuilder(entity6.getKey()).build()); assertEquals(PARTIAL_ENTITY2.getKey().getAncestors(), entity6.getKey().getAncestors()); assertNotEquals(PARTIAL_ENTITY2.getKey(), entity6.getKey()); assertSame(entity5, entities.get(2)); batch.addWithDeferredIdAllocation(PARTIAL_ENTITY3); assertEquals(1, generatedKeys.size()); assertEquals(PARTIAL_ENTITY3.getNames(), DATASTORE.get(generatedKeys.get(0)).getNames()); assertEquals(PARTIAL_ENTITY3.getKey(), IncompleteKey.newBuilder(generatedKeys.get(0)).build());
assertSame(entity4, entities.get(0)); assertEquals(PARTIAL_ENTITY2.getProperties(), entity6.getProperties()); assertEquals(PARTIAL_ENTITY2.getKey().getProjectId(), entity6.getKey().getProjectId()); assertEquals(PARTIAL_ENTITY2.getKey().getNamespace(), entity6.getKey().getNamespace()); assertEquals(PARTIAL_ENTITY2.getKey().getAncestors(), entity6.getKey().getAncestors()); assertEquals(PARTIAL_ENTITY2.getKey().getKind(), entity6.getKey().getKind()); assertEquals(PARTIAL_ENTITY2.getKey(), IncompleteKey.newBuilder(entity6.getKey()).build()); assertNotEquals(PARTIAL_ENTITY2.getKey().getPath(), entity6.getKey().getPath()); assertNotEquals(PARTIAL_ENTITY2.getKey(), entity6.getKey()); assertSame(entity5, entities.get(2)); batch.addWithDeferredIdAllocation(PARTIAL_ENTITY3); assertEquals( PARTIAL_ENTITY3.getProperties(), datastore.get(generatedKeys.get(0)).getProperties()); assertEquals(PARTIAL_ENTITY3.getKey(), IncompleteKey.newBuilder(generatedKeys.get(0)).build());
@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); }
@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()); } }
/** * @return the native datastore key for a registered pojo entity (or FullEntity). */ public com.google.cloud.datastore.Key rawKeyOf(final Object pojo) { if (pojo instanceof FullEntity<?>) { return (com.google.cloud.datastore.Key)((FullEntity)pojo).getKey(); } else { return getMetadataSafe(pojo).getCompleteKey(pojo); } }
@Override public Object load(final Value<FullEntity<?>> container, final LoadContext ctx, final Path path) throws SkipException { final IncompleteKey key = container.get().getKey(); final EntityMetadata<?> meta = registrar.getMetadataSafe(key.getKind()); return meta.getTranslator().load(container, ctx, path); }
/** * Sets the key (from the container) onto the POJO id/parent fields. Also doublechecks to make sure * that the key fields aren't present in the container, which means we're in a very bad state. */ public void setKey(final P pojo, final FullEntity<?> container, final LoadContext ctx, final Path containerPath) { if (container.contains(idMeta.getProperty().getName())) throw new IllegalStateException("Datastore has a property present for the id field " + idMeta.getProperty() + " which would conflict with the key: " + container); if (parentMeta != null && container.contains(parentMeta.getProperty().getName())) throw new IllegalStateException("Datastore has a property present for the parent field " + parentMeta.getProperty() + " which would conflict with the key: " + container); this.setKey(pojo, container.getKey(), ctx, containerPath); }