private static Map<String, Object> getProperties(Entity entity) throws IOException, ClassNotFoundException { if (entity == null) { return null; } ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>(); for (String property : entity.getNames()) { // builder.put(property, entity.getValue(property)); if (entity.getValue(property) instanceof StringValue) { builder.put(property, (String) entity.getString(property)); } else if (entity.getValue(property) instanceof LongValue) { // This conversion is safe because of integer to long conversion above builder.put(property, new Long(entity.getLong(property)).intValue()); } else if (entity.getValue(property) instanceof DoubleValue) { builder.put(property, (Double) entity.getDouble(property)); } else if (entity.getValue(property) instanceof BooleanValue) { builder.put(property, (Boolean) entity.getBoolean(property)); } else if (entity.getValue(property) instanceof TimestampValue) { builder.put(property, (Timestamp) entity.getTimestamp(property)); } else { Blob blob = entity.getBlob(property); Object obj = null; try (ObjectInputStream in = new ObjectInputStream(blob.asInputStream())) { obj = in.readObject(); } builder.put(property, obj); // BlobValue } } return builder.build(); }
@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()); }
/** * Converts a list of task entities to a list of formatted task strings. * * @param tasks An iterator over task entities * @return A list of tasks strings, one per entity */ static List<String> formatTasks(Iterator<Entity> tasks) { List<String> strings = new ArrayList<>(); while (tasks.hasNext()) { Entity task = tasks.next(); if (task.getBoolean("done")) { strings.add( String.format("%d : %s (done)", task.getKey().getId(), task.getString("description"))); } else { strings.add(String.format("%d : %s (created %s)", task.getKey().getId(), task.getString("description"), task.getTimestamp("created"))); } } return strings; }
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(); }
private FeatureState createFeatureState(final Feature feature, final Entity featureEntity) { if (featureEntity == null) { return null; } final Boolean enabled = featureEntity.getBoolean(ENABLED); final FeatureState state = new FeatureState(feature, enabled); state.setStrategyId(getStrategyId(featureEntity)); List<Value<String>> names = valuesList(featureEntity, STRATEGY_PARAMS_NAMES); List<Value<String>> values = valuesList(featureEntity, STRATEGY_PARAMS_VALUES); Preconditions.checkState(names.size() == values.size()); for (int i = 0; i < names.size(); i++) { String name = names.get(i).get(); String value = values.get(i).get(); state.setParameter(name, value); } return state; }
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; }
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(); }