Skip to content

Commit

Permalink
fixed adding missing "_created" field for ThingCreated events
Browse files Browse the repository at this point in the history
* undid "hidden field filtering", this must not be done
* remove a cache json when last event was ThingDeleted

Signed-off-by: Thomas Jaeckle <thomas.jaeckle@bosch.io>
  • Loading branch information
thjaeckle committed Nov 2, 2021
1 parent 6079bc5 commit d3108d7
Showing 1 changed file with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@

import org.eclipse.ditto.base.model.entity.id.EntityId;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.FieldType;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.base.model.signals.WithResource;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.akka.logging.ThreadSafeDittoLogger;
import org.eclipse.ditto.internal.utils.cache.Cache;
import org.eclipse.ditto.internal.utils.cache.CacheFactory;
import org.eclipse.ditto.internal.utils.cache.config.CacheConfig;
import org.eclipse.ditto.json.JsonCollectors;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonObject;
Expand Down Expand Up @@ -360,8 +358,13 @@ private CompletionStage<JsonObject> handleNextExpectedThingEvents(
}
}
final var enhancedJsonObject = enhanceJsonObject(jsonObject, concernedSignals, enhancedFieldSelector);
// update local cache with enhanced object:
extraFieldsCache.put(cacheKey, enhancedJsonObject);
final ThingEvent<?> last = getLast(concernedSignals);
if (last instanceof ThingDeleted) {
extraFieldsCache.invalidate(cacheKey);
} else {
// update local cache with enhanced object:
extraFieldsCache.put(cacheKey, enhancedJsonObject);
}
return CompletableFuture.completedFuture(enhancedJsonObject);
}

Expand All @@ -388,19 +391,16 @@ private static JsonObject getDeleteJsonObject(final JsonObject jsonObject, final

private static JsonObject getDefaultJsonObject(final JsonObject jsonObject, final ThingEvent<?> thingEvent) {
final var resourcePath = thingEvent.getResourcePath();
final var nonHiddenFieldsJsonObjectBuilder = jsonObject.stream()
.filter(field -> !field.isMarkedAs(FieldType.HIDDEN))
.collect(JsonCollectors.fieldsToObject())
.toBuilder();
final var jsonObjectBuilder = jsonObject.toBuilder();
final Optional<JsonValue> optEntity = thingEvent.getEntity();
if (resourcePath.isEmpty() && optEntity.filter(JsonValue::isObject).isPresent()) {
optEntity.map(JsonValue::asObject).ifPresent(nonHiddenFieldsJsonObjectBuilder::setAll);
optEntity.map(JsonValue::asObject).ifPresent(jsonObjectBuilder::setAll);
} else {
optEntity.ifPresent(entity -> nonHiddenFieldsJsonObjectBuilder
optEntity.ifPresent(entity -> jsonObjectBuilder
.set(resourcePath.toString(), entity)
);
}
return nonHiddenFieldsJsonObjectBuilder.build();
return jsonObjectBuilder.build();
}

private Optional<CompletionStage<JsonObject>> invalidateCacheOnPolicyChange(final SignalEnrichmentCacheKey cacheKey,
Expand Down Expand Up @@ -428,6 +428,11 @@ private static JsonObject enhanceJsonObject(final JsonObject jsonObject, final L
final ThingEvent<?> last = getLast(concernedSignals);
final var jsonObjectBuilder = jsonObject.toBuilder()
.set(Thing.JsonFields.REVISION, last.getRevision());
concernedSignals.stream()
.filter(ThingCreated.class::isInstance)
.map(ThingCreated.class::cast)
.forEach(thingCreated -> thingCreated.getTimestamp().ifPresent(timestamp ->
jsonObjectBuilder.set(Thing.JsonFields.CREATED, timestamp.toString())));
last.getTimestamp().ifPresent(timestamp ->
jsonObjectBuilder.set(Thing.JsonFields.MODIFIED, timestamp.toString()));
return enhancedFieldSelector == null
Expand Down

0 comments on commit d3108d7

Please sign in to comment.