return; if (user.contains("contact")) { FullEntity<IncompleteKey> contact = user.getEntity("contact"); String email = contact.getString("email");
@Test public void testGetArrayNoDeferredResults() { datastore.put(ENTITY3); Iterator<Entity> result = datastore.fetch(KEY1, Key.newBuilder(KEY1).setName("bla").build(), KEY2, KEY3).iterator(); assertEquals(ENTITY1, result.next()); assertNull(result.next()); assertEquals(ENTITY2, result.next()); Entity entity3 = result.next(); assertEquals(ENTITY3, entity3); assertTrue(entity3.isNull("null")); assertFalse(entity3.getBoolean("bool")); assertEquals(LIST_VALUE2.get(), entity3.getList("list")); FullEntity<IncompleteKey> partial1 = entity3.getEntity("partial1"); FullEntity<IncompleteKey> partial2 = entity3.getEntity("partial2"); assertEquals(PARTIAL_ENTITY2, partial1); assertEquals(ENTITY2, partial2); assertEquals(ValueType.BOOLEAN, entity3.getValue("bool").getType()); assertEquals(LAT_LNG_VALUE, entity3.getValue("latLng")); assertEquals(EMPTY_LIST_VALUE, entity3.getValue("emptyList")); assertEquals(8, entity3.getNames().size()); assertFalse(entity3.contains("bla")); try { entity3.getString("str"); fail("Expecting a failure"); } catch (DatastoreException expected) { // expected - no such property } assertFalse(result.hasNext()); }
@Test public void testGetArrayNoDeferredResults() { DATASTORE.put(ENTITY3); Iterator<Entity> result = DATASTORE.fetch(KEY1, Key.newBuilder(KEY1).setName("bla").build(), KEY2, KEY3).iterator(); assertEquals(ENTITY1, result.next()); assertNull(result.next()); assertEquals(ENTITY2, result.next()); Entity entity3 = result.next(); assertEquals(ENTITY3, entity3); assertTrue(entity3.isNull("null")); assertFalse(entity3.getBoolean("bool")); assertEquals(LIST_VALUE2.get(), entity3.getList("list")); FullEntity<IncompleteKey> partial1 = entity3.getEntity("partial1"); FullEntity<IncompleteKey> partial2 = entity3.getEntity("partial2"); assertEquals(PARTIAL_ENTITY2, partial1); assertEquals(ENTITY2, partial2); assertEquals(ValueType.BOOLEAN, entity3.getValue("bool").getType()); assertEquals(LAT_LNG_VALUE, entity3.getValue("latLng")); assertEquals(EMPTY_LIST_VALUE, entity3.getValue("emptyList")); assertEquals(8, entity3.getNames().size()); assertFalse(entity3.contains("bla")); try { entity3.getString("str"); fail("Expecting a failure"); } catch (DatastoreException expected) { // expected - no such property } assertFalse(result.hasNext()); }
@Test public void testGet() { Entity entity = DATASTORE.get(KEY3); assertNull(entity); entity = DATASTORE.get(KEY1); assertEquals(ENTITY1, entity); StringValue value1 = entity.getValue("str"); assertEquals(STR_VALUE, value1); BooleanValue value2 = entity.getValue("bool"); assertEquals(BOOL_VALUE, value2); ListValue value3 = entity.getValue("list"); assertEquals(LIST_VALUE2, value3); TimestampValue value4 = entity.getValue("date"); assertEquals(TIMESTAMP_VALUE, value4); LatLngValue value5 = entity.getValue("latLng"); assertEquals(LAT_LNG_VALUE, value5); FullEntity<IncompleteKey> value6 = entity.getEntity("partial1"); assertEquals(PARTIAL_ENTITY1, value6); ListValue value7 = entity.getValue("emptyList"); assertEquals(EMPTY_LIST_VALUE, value7); assertEquals(7, entity.getNames().size()); assertFalse(entity.contains("bla")); }
@Test public void testGet() { Entity entity = datastore.get(KEY3); assertNull(entity); entity = datastore.get(KEY1); assertEquals(ENTITY1, entity); StringValue value1 = entity.getValue("str"); assertEquals(STR_VALUE, value1); BooleanValue value2 = entity.getValue("bool"); assertEquals(BOOL_VALUE, value2); ListValue value3 = entity.getValue("list"); assertEquals(LIST_VALUE2, value3); TimestampValue value4 = entity.getValue("date"); assertEquals(TIMESTAMP_VALUE, value4); LatLngValue value5 = entity.getValue("latLng"); assertEquals(LAT_LNG_VALUE, value5); FullEntity<IncompleteKey> value6 = entity.getEntity("partial1"); assertEquals(PARTIAL_ENTITY1, value6); ListValue value7 = entity.getValue("emptyList"); assertEquals(EMPTY_LIST_VALUE, value7); assertEquals(7, entity.getNames().size()); assertFalse(entity.contains("bla")); }
private String getStrategyId(Entity featureEntity) { if (featureEntity.contains(STRATEGY_ID)) { final String strategyId = featureEntity.getString(STRATEGY_ID); if (!Strings.isNullOrEmpty(strategyId)) { return strategyId.trim(); } } return null; }
static <T> Optional<T> readOptJson(Entity entity, String property, Class<T> cls) throws IOException { return entity.contains(property) ? Optional.of(OBJECT_MAPPER.readValue(entity.getString(property), cls)) : Optional.empty(); }
static <T> Optional<T> readOptJson(Entity entity, String property, TypeReference valueTypeRef) throws IOException { return entity.contains(property) ? Optional.of(OBJECT_MAPPER.readValue(entity.getString(property), valueTypeRef)) : Optional.empty(); }
private List<Value<String>> valuesList(Entity entity, String propertyName) { return entity.contains(propertyName) ? entity.<Value<String>>getList(propertyName) : Collections.<Value<String>>emptyList(); }
static <T> Optional<T> readOpt(Entity entity, String property) { return entity.contains(property) ? Optional.of(entity.<Value<T>>getValue(property).get()) : Optional.empty(); }
/** * Optionally get an {@link Instant} value for an {@link Entity}'s property. * * @return an optional containing the property value if it existed, empty otherwise. */ static Optional<Instant> getOptInstantProperty(Optional<Entity> entity, String property) { return entity .filter(w -> w.contains(property)) .map(workflow -> timestampToInstant(workflow.getTimestamp(property))); }
Optional<Workflow> workflow(WorkflowId workflowId) throws IOException { final Optional<Entity> entityOptional = getOpt(datastore, workflowKey(datastore.newKeyFactory(), workflowId)) .filter(e -> e.contains(PROPERTY_WORKFLOW_JSON)); if (entityOptional.isPresent()) { return Optional.of(parseWorkflowJson(entityOptional.get(), workflowId)); } else { return Optional.empty(); } }
boolean enabled(WorkflowId workflowId) throws IOException { final Key workflowKey = workflowKey(datastore.newKeyFactory(), workflowId); return getOpt(datastore, workflowKey) .filter(w -> w.contains(PROPERTY_WORKFLOW_ENABLED)) .map(workflow -> workflow.getBoolean(PROPERTY_WORKFLOW_ENABLED)) .orElse(DEFAULT_WORKFLOW_ENABLED); }
public WorkflowState workflowState(WorkflowId workflowId) throws IOException { final WorkflowState.Builder builder = WorkflowState.builder(); final Optional<Entity> workflowEntity = getOpt(datastore, workflowKey(datastore.newKeyFactory(), workflowId)); builder.enabled(workflowEntity.filter(w -> w.contains(PROPERTY_WORKFLOW_ENABLED)) .map(workflow -> workflow.getBoolean(PROPERTY_WORKFLOW_ENABLED)) .orElse(DEFAULT_WORKFLOW_ENABLED)); getOptInstantProperty(workflowEntity, PROPERTY_NEXT_NATURAL_TRIGGER) .ifPresent(builder::nextNaturalTrigger); getOptInstantProperty(workflowEntity, PROPERTY_NEXT_NATURAL_OFFSET_TRIGGER) .ifPresent(builder::nextNaturalOffsetTrigger); return builder.build(); }
Set<WorkflowId> enabled() throws IOException { final EntityQuery queryWorkflows = EntityQuery.newEntityQueryBuilder().setKind(KIND_WORKFLOW).build(); final Set<WorkflowId> enabledWorkflows = Sets.newHashSet(); datastore.query(queryWorkflows, workflow -> { final boolean enabled = workflow.contains(PROPERTY_WORKFLOW_ENABLED) && workflow.getBoolean(PROPERTY_WORKFLOW_ENABLED); if (enabled) { enabledWorkflows.add(parseWorkflowId(workflow)); } }); return enabledWorkflows; }
public List<Workflow> workflows(String componentId) throws IOException { final Key componentKey = componentKey(datastore.newKeyFactory(), componentId); final List<Workflow> workflows = Lists.newArrayList(); final EntityQuery query = Query.newEntityQueryBuilder() .setKind(KIND_WORKFLOW) .setFilter(PropertyFilter.hasAncestor(componentKey)) .build(); datastore.query(query, entity -> { final Workflow workflow; if (entity.contains(PROPERTY_WORKFLOW_JSON)) { try { workflow = OBJECT_MAPPER.readValue(entity.getString(PROPERTY_WORKFLOW_JSON), Workflow.class); } catch (IOException e) { LOG.warn("Failed to read workflow {}.", entity.getKey(), e); return; } workflows.add(workflow); } }); return workflows; }
static Backfill entityToBackfill(Entity entity) throws IOException { final WorkflowId workflowId = WorkflowId.create(entity.getString(PROPERTY_COMPONENT), entity.getString(PROPERTY_WORKFLOW)); final BackfillBuilder builder = Backfill.newBuilder() .id(entity.getKey().getName()) .start(timestampToInstant(entity.getTimestamp(PROPERTY_START))) .end(timestampToInstant(entity.getTimestamp(PROPERTY_END))) .workflowId(workflowId) .concurrency((int) entity.getLong(PROPERTY_CONCURRENCY)) .nextTrigger(timestampToInstant(entity.getTimestamp(PROPERTY_NEXT_TRIGGER))) .schedule(Schedule.parse(entity.getString(PROPERTY_SCHEDULE))) .allTriggered(entity.getBoolean(PROPERTY_ALL_TRIGGERED)) .halted(entity.getBoolean(PROPERTY_HALTED)) .reverse(read(entity, PROPERTY_REVERSE, Boolean.FALSE)); if (entity.contains(PROPERTY_DESCRIPTION)) { builder.description(entity.getString(PROPERTY_DESCRIPTION)); } if (entity.contains(PROPERTY_TRIGGER_PARAMETERS)) { builder.triggerParameters(OBJECT_MAPPER.readValue( entity.getString(PROPERTY_TRIGGER_PARAMETERS), TriggerParameters.class)); } return builder.build(); }