final com.google.datastore.v1.Entity toPb() { com.google.datastore.v1.Entity.Builder entityPb = com.google.datastore.v1.Entity.newBuilder(); for (Map.Entry<String, Value<?>> entry : properties.entrySet()) { entityPb.putProperties(entry.getKey(), entry.getValue().toPb()); } if (key != null) { entityPb.setKey(key.toPb()); } return entityPb.build(); } }
/** * * * <pre> * The resulting entity. * </pre> * * <code>.google.datastore.v1.Entity entity = 1;</code> */ public Builder setEntity(com.google.datastore.v1.Entity.Builder builderForValue) { if (entityBuilder_ == null) { entity_ = builderForValue.build(); onChanged(); } else { entityBuilder_.setMessage(builderForValue.build()); } return this; } /**
/** * * * <pre> * The entity to upsert. The entity may or may not already exist. * The entity key's final path element may be incomplete. * </pre> * * <code>.google.datastore.v1.Entity upsert = 6;</code> */ public Builder setUpsert(com.google.datastore.v1.Entity.Builder builderForValue) { if (upsertBuilder_ == null) { operation_ = builderForValue.build(); onChanged(); } else { upsertBuilder_.setMessage(builderForValue.build()); } operationCase_ = 6; return this; } /**
/** * <pre> * The resulting entity. * </pre> * * <code>optional .google.datastore.v1.Entity entity = 1;</code> */ public Builder setEntity( com.google.datastore.v1.Entity.Builder builderForValue) { if (entityBuilder_ == null) { entity_ = builderForValue.build(); onChanged(); } else { entityBuilder_.setMessage(builderForValue.build()); } return this; } /**
@ProcessElement public void processElement(ProcessContext c) throws InvalidProtocolBufferException { String entityJson = c.element(); Entity.Builder entityBuilder = Entity.newBuilder(); entityJsonParser.merge(entityJson, entityBuilder); // Build entity who's key has an empty project Id. // This allows DatastoreIO to handle what project Entities are loaded into Key k = entityBuilder.build().getKey(); entityBuilder.setKey(Key.newBuilder() .addAllPath(k.getPathList()) .setPartitionId(PartitionId.newBuilder() .setProjectId("") .setNamespaceId(k.getPartitionId().getNamespaceId()))); c.output(entityBuilder.build()); } }
/** * Creates a new {@code Entity}. * * @return an immutable entity. */ public Entity build() { entity.getMutableProperties().clear(); entity.putAllProperties( properties .entrySet() .stream() .collect(Collectors.toMap( Map.Entry::getKey, e -> e.getValue().getPb().toBuilder().build()))); return new Entity(entity.build()); }
com.google.datastore.v1.Entity getPb(final String namespace) { final Map<String, com.google.datastore.v1.Value> propertiesLocal = entity.getProperties() .entrySet() .stream() .collect(Collectors.toMap( Map.Entry::getKey, e -> Value.builder(e.getValue()).build().getPb(namespace))); return com.google.datastore.v1.Entity .newBuilder() .putAllProperties(propertiesLocal) .setKey(getKey().getPb(namespace)) .build(); } }
/** * Add a greeting to the specified guestbook. */ private void addGreeting(String guestbookName, String user, String message) throws DatastoreException { Entity.Builder greeting = Entity.newBuilder(); greeting.setKey(makeKey(GUESTBOOK_KIND, guestbookName, GREETING_KIND)); greeting.getMutableProperties().put(USER_PROPERTY, makeValue(user).build()); greeting.getMutableProperties().put(MESSAGE_PROPERTY, makeValue(message).build()); greeting.getMutableProperties().put(DATE_PROPERTY, makeValue(new Date()).build()); Key greetingKey = insert(greeting.build()); System.out.println("greeting key is: " + greetingKey); }
/** * Make a entity value. */ public static Value.Builder makeValue(Entity.Builder entity) { return makeValue(entity.build()); }
/** * Make a entity value. */ public static Value.Builder makeValue(Entity.Builder entity) { return makeValue(entity.build()); }
/** Test that entities with incomplete keys cannot be deleted. */ @Test public void testDeleteEntitiesWithIncompleteKeys() throws Exception { Key key = makeKey("bird").build(); Entity entity = Entity.newBuilder().setKey(key).build(); DeleteEntityFn deleteEntityFn = new DeleteEntityFn(); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Entities to be deleted from the Cloud Datastore must have complete keys"); deleteEntityFn.apply(entity); }
/** Test that entities with valid keys are transformed to delete mutations. */ @Test public void testDeleteEntities() throws Exception { Key key = makeKey("bird", "finch").build(); Entity entity = Entity.newBuilder().setKey(key).build(); DeleteEntityFn deleteEntityFn = new DeleteEntityFn(); Mutation exceptedMutation = makeDelete(entity.getKey()).build(); assertEquals(deleteEntityFn.apply(entity), exceptedMutation); }
@Test /** Test that entities with valid keys are transformed to upsert mutations. */ public 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); }
/** Test that entities with incomplete keys cannot be updated. */ @Test public void testAddEntitiesWithIncompleteKeys() throws Exception { Key key = makeKey("bird").build(); Entity entity = Entity.newBuilder().setKey(key).build(); UpsertFn upsertFn = new UpsertFn(); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Entities to be written to the Cloud Datastore must have complete keys"); upsertFn.apply(entity); }
final com.google.datastore.v1.Entity toPb() { com.google.datastore.v1.Entity.Builder entityPb = com.google.datastore.v1.Entity.newBuilder(); for (Map.Entry<String, Value<?>> entry : properties.entrySet()) { entityPb.putProperties(entry.getKey(), entry.getValue().toPb()); } if (key != null) { entityPb.setKey(key.toPb()); } return entityPb.build(); } }
public Entity parse(String json) throws InvalidProtocolBufferException { Entity.Builder entityBuilter = Entity.newBuilder(); merge(json, entityBuilter); return entityBuilter.build(); }
.setExcludeFromIndexes(skipIndex).build()); Entity entity = entityBuilder.build(); logger.debug("entity built as: " + entity.toString());
/** * * * <pre> * An entity value. * - May have no key. * - May have a key with an incomplete key path. * - May have a reserved/read-only key. * </pre> * * <code>.google.datastore.v1.Entity entity_value = 6;</code> */ public Builder setEntityValue(com.google.datastore.v1.Entity.Builder builderForValue) { if (entityValueBuilder_ == null) { valueType_ = builderForValue.build(); onChanged(); } else { entityValueBuilder_.setMessage(builderForValue.build()); } valueTypeCase_ = 6; return this; } /**
/** * * * <pre> * The entity to insert. The entity must not already exist. * The entity key's final path element may be incomplete. * </pre> * * <code>.google.datastore.v1.Entity insert = 4;</code> */ public Builder setInsert(com.google.datastore.v1.Entity.Builder builderForValue) { if (insertBuilder_ == null) { operation_ = builderForValue.build(); onChanged(); } else { insertBuilder_.setMessage(builderForValue.build()); } operationCase_ = 4; return this; } /**
/** * * * <pre> * The entity to update. The entity must already exist. * Must have a complete key path. * </pre> * * <code>.google.datastore.v1.Entity update = 5;</code> */ public Builder setUpdate(com.google.datastore.v1.Entity.Builder builderForValue) { if (updateBuilder_ == null) { operation_ = builderForValue.build(); onChanged(); } else { updateBuilder_.setMessage(builderForValue.build()); } operationCase_ = 5; return this; } /**