Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide new option for "if-equal" header: "skip-minimizing-merge" #1772

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,18 @@ public enum IfEqual {
* Option which will skip the update of a twin if the new value is the same (via {@code equal()}) than the value
* before.
*/
SKIP("skip");
SKIP("skip"),

/**
* Option which will skip the update of a twin if the new value is the same (via {@code equal()}) than the value
* before.
* And additionally minimizes a "Merge" command to only the actually changed fields compared to the current state
* of the entity. This can be beneficial to reduce (persisted and emitted) events to the minimum of what actually
* did change.
*
* @since 3.4.0
*/
SKIP_MINIMIZING_MERGE("skip-minimizing-merge");

private final String option;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

import java.util.Optional;

import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.json.JsonValue;

/**
* Implementations of this interface are associated to an entity returned from {@link #getEntity(org.eclipse.ditto.base.model.json.JsonSchemaVersion)} .
* Implementations of this interface are associated to an entity returned from {@link #getEntity(JsonSchemaVersion)} .
*
* @param <T> the type of the implementing class.
*/
public interface WithOptionalEntity {
public interface WithOptionalEntity<T extends WithOptionalEntity<T>> {

/**
* Returns the entity as JSON.
Expand All @@ -41,4 +43,14 @@ default Optional<JsonValue> getEntity(final JsonSchemaVersion schemaVersion) {
return Optional.empty();
}

/**
* Sets the entity and returns a new object.
*
* @param entity the entity to set.
* @return the newly created object with the set entity.
* @throws NullPointerException if the passed {@code entity} is null.
* @since 3.4.0
*/
T setEntity(JsonValue entity);

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
*/
@JsonParsableCommandResponse(type = Acknowledgement.TYPE)
@Immutable
public final class Acknowledgement implements CommandResponse<Acknowledgement>, WithOptionalEntity, WithEntityType,
SignalWithEntityId<Acknowledgement> {
public final class Acknowledgement implements CommandResponse<Acknowledgement>, WithOptionalEntity<Acknowledgement>,
WithEntityType, SignalWithEntityId<Acknowledgement> {

/**
* The type of {@code Acknowledgement} signals.
Expand Down Expand Up @@ -231,6 +231,7 @@ public Optional<JsonValue> getEntity(final JsonSchemaVersion schemaVersion) {
* @return the Acknowledgement with set payload.
* @since 1.2.0
*/
@Override
public Acknowledgement setEntity(final @Nullable JsonValue payload) {
if (payload != null) {
return of(label, entityId, httpStatus, dittoHeaders, payload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@
@JsonParsableCommandResponse(type = Acknowledgements.TYPE)
@Immutable
public final class Acknowledgements
implements Iterable<Acknowledgement>, CommandResponse<Acknowledgements>, WithOptionalEntity, WithEntityType,
SignalWithEntityId<Acknowledgements> {
implements Iterable<Acknowledgement>, CommandResponse<Acknowledgements>, WithOptionalEntity<Acknowledgements>,
WithEntityType, SignalWithEntityId<Acknowledgements> {

static final String TYPE = "acknowledgements";
private final EntityId entityId;
Expand All @@ -81,7 +81,7 @@ public final class Acknowledgements
private final DittoHeaders dittoHeaders;

private Acknowledgements(final EntityId entityId,
final Collection<? extends Acknowledgement> acknowledgements,
final Collection<Acknowledgement> acknowledgements,
final HttpStatus httpStatus,
final DittoHeaders dittoHeaders) {

Expand All @@ -107,7 +107,7 @@ private Acknowledgements(final EntityId entityId,
* @throws IllegalArgumentException if the given {@code acknowledgements} are empty or if the entity IDs or entity
* types of the given acknowledgements are not equal.
*/
public static Acknowledgements of(final Collection<? extends Acknowledgement> acknowledgements,
public static Acknowledgements of(final Collection<Acknowledgement> acknowledgements,
final DittoHeaders dittoHeaders) {

argumentNotEmpty(acknowledgements, "acknowledgements");
Expand All @@ -130,7 +130,7 @@ public static Acknowledgements of(final Collection<? extends Acknowledgement> ac
* @since 2.0.0
*/
public static Acknowledgements of(final EntityId entityId,
final Collection<? extends Acknowledgement> acknowledgements,
final Collection<Acknowledgement> acknowledgements,
final HttpStatus httpStatus,
final DittoHeaders dittoHeaders) {

Expand All @@ -142,8 +142,8 @@ public static Acknowledgements of(final EntityId entityId,
return new Acknowledgements(entityId, acknowledgements, httpStatus, dittoHeaders);
}

private static EntityId getEntityId(final Iterable<? extends Acknowledgement> acknowledgements) {
final Iterator<? extends Acknowledgement> acknowledgementIterator = acknowledgements.iterator();
private static EntityId getEntityId(final Iterable<Acknowledgement> acknowledgements) {
final Iterator<Acknowledgement> acknowledgementIterator = acknowledgements.iterator();
Acknowledgement acknowledgement = acknowledgementIterator.next();
final EntityId entityId = acknowledgement.getEntityId();
while (acknowledgementIterator.hasNext()) {
Expand All @@ -157,15 +157,15 @@ private static EntityId getEntityId(final Iterable<? extends Acknowledgement> ac
return entityId;
}

private static HttpStatus getCombinedHttpStatus(final Collection<? extends Acknowledgement> acknowledgements) {
private static HttpStatus getCombinedHttpStatus(final Collection<Acknowledgement> acknowledgements) {
final HttpStatus result;
if (1 == acknowledgements.size()) {
result = acknowledgements.stream()
.findFirst()
.map(Acknowledgement::getHttpStatus)
.orElse(HttpStatus.INTERNAL_SERVER_ERROR);
} else {
final Stream<? extends Acknowledgement> acknowledgementStream = acknowledgements.stream();
final Stream<Acknowledgement> acknowledgementStream = acknowledgements.stream();
final boolean allAcknowledgementsSuccessful = acknowledgementStream.allMatch(Acknowledgement::isSuccess);
if (allAcknowledgementsSuccessful) {
result = HttpStatus.OK;
Expand Down Expand Up @@ -364,6 +364,11 @@ public Optional<JsonValue> getEntity(final JsonSchemaVersion schemaVersion) {
return result;
}

@Override
public Acknowledgements setEntity(final JsonValue entity) {
return this;
}

private JsonObject acknowledgementsEntitiesToJson(final JsonSchemaVersion schemaVersion) {
return acknowledgementsToJsonWithDisambiguation(schemaVersion, FieldType.all(), (ack, version, predicate) -> {
final JsonObjectBuilder jsonObjectBuilder = JsonObject.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* @param <T> the type of the implementing class.
*/
@IndexSubclasses
public interface Event<T extends Event<T>> extends Signal<T>, WithOptionalEntity {
public interface Event<T extends Event<T>> extends Signal<T>, WithOptionalEntity<T> {

/**
* Type qualifier of events.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;

/**
* This event is emitted after all items of a subscription are sent.
Expand Down Expand Up @@ -93,6 +94,11 @@ public StreamingSubscriptionComplete setDittoHeaders(final DittoHeaders dittoHea
return new StreamingSubscriptionComplete(getSubscriptionId(), getEntityId(), dittoHeaders);
}

@Override
public StreamingSubscriptionComplete setEntity(final JsonValue entity) {
return this;
}

@Override
protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder) {
// nothing to add
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;

/**
* This event is emitted after a stream is established for items to be streamed in the back-end.
Expand Down Expand Up @@ -95,6 +96,11 @@ public StreamingSubscriptionCreated setDittoHeaders(final DittoHeaders dittoHead
return new StreamingSubscriptionCreated(getSubscriptionId(), getEntityId(), dittoHeaders);
}

@Override
public StreamingSubscriptionCreated setEntity(final JsonValue entity) {
return this;
}

@Override
protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder) {
// nothing to add
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;

/**
* This event is emitted after a stream failed.
Expand Down Expand Up @@ -128,6 +129,11 @@ public StreamingSubscriptionFailed setDittoHeaders(final DittoHeaders dittoHeade
return new StreamingSubscriptionFailed(getSubscriptionId(), getEntityId(), error, dittoHeaders);
}

@Override
public StreamingSubscriptionFailed setEntity(final JsonValue entity) {
return this;
}

@Override
protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder) {
jsonObjectBuilder.set(JsonFields.ERROR, error.toJson());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ public StreamingSubscriptionHasNext setDittoHeaders(final DittoHeaders dittoHead
return new StreamingSubscriptionHasNext(getSubscriptionId(), getEntityId(), item, dittoHeaders);
}

@Override
public StreamingSubscriptionHasNext setEntity(final JsonValue entity) {
return this;
}

@Override
protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder) {
jsonObjectBuilder.set(JsonFields.ITEM, item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@

import javax.annotation.Nonnull;

import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.base.model.entity.metadata.Metadata;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.JsonParsableEvent;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.base.model.signals.events.Event;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.utils.jsr305.annotations.AllValuesAreNonnullByDefault;

@JsonParsableEvent(name = TestEvent.NAME, typePrefix = TestEvent.TYPE_PREFIX)
Expand Down Expand Up @@ -91,4 +92,9 @@ public String getManifest() {
public JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> predicate) {
return JsonObject.empty();
}

@Override
public TestEvent setEntity(final JsonValue entity) {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,22 @@
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import org.eclipse.ditto.connectivity.model.signals.commands.ConnectivityCommand;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.eclipse.ditto.json.JsonParseException;
import org.eclipse.ditto.base.model.exceptions.DittoJsonException;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.FieldType;
import org.eclipse.ditto.base.model.json.JsonParsableCommand;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.connectivity.model.ConnectionId;
import org.eclipse.ditto.base.model.signals.commands.AbstractCommand;
import org.eclipse.ditto.base.model.signals.commands.CommandJsonDeserializer;
import org.eclipse.ditto.connectivity.model.ConnectionId;
import org.eclipse.ditto.connectivity.model.signals.commands.ConnectivityCommand;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.eclipse.ditto.json.JsonParseException;
import org.eclipse.ditto.json.JsonValue;

/**
* Command that will enable logging in a {@link org.eclipse.ditto.connectivity.model.Connection}.
Expand All @@ -56,7 +57,7 @@ public final class CheckConnectionLogsActive extends AbstractCommand<CheckConnec
*/
public static final String TYPE = TYPE_PREFIX + NAME;

protected static final JsonFieldDefinition<String> JSON_TIMESTAMP =
static final JsonFieldDefinition<String> JSON_TIMESTAMP =
JsonFactory.newStringFieldDefinition("timestamp", FieldType.REGULAR,
JsonSchemaVersion.V_2);

Expand Down Expand Up @@ -191,6 +192,11 @@ public CheckConnectionLogsActive setDittoHeaders(final DittoHeaders dittoHeaders
return of(connectionId, timestamp, dittoHeaders);
}

@Override
public CheckConnectionLogsActive setEntity(final JsonValue entity) {
return this;
}

@Override
protected boolean canEqual(@Nullable final Object other) {
return (other instanceof CheckConnectionLogsActive);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.JsonParsableCommand;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.base.model.signals.commands.AbstractCommand;
import org.eclipse.ditto.base.model.signals.commands.CommandJsonDeserializer;
import org.eclipse.ditto.connectivity.model.ConnectionId;
import org.eclipse.ditto.connectivity.model.signals.commands.ConnectivityCommand;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.JsonParsableCommand;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.connectivity.model.ConnectionId;
import org.eclipse.ditto.base.model.signals.commands.AbstractCommand;
import org.eclipse.ditto.base.model.signals.commands.CommandJsonDeserializer;
import org.eclipse.ditto.json.JsonValue;

/**
* Command which closes a {@link org.eclipse.ditto.connectivity.model.Connection}.
Expand Down Expand Up @@ -133,6 +134,11 @@ public CloseConnection setDittoHeaders(final DittoHeaders dittoHeaders) {
return of(connectionId, dittoHeaders);
}

@Override
public CloseConnection setEntity(final JsonValue entity) {
return this;
}

@Override
protected boolean canEqual(@Nullable final Object other) {
return (other instanceof CloseConnection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;

/**
* Response to a {@link CloseConnection} command.
Expand Down Expand Up @@ -142,6 +143,11 @@ public CloseConnectionResponse setDittoHeaders(final DittoHeaders dittoHeaders)
return of(connectionId, dittoHeaders);
}

@Override
public CloseConnectionResponse setEntity(final JsonValue entity) {
return this;
}

@Override
protected boolean canEqual(@Nullable final Object other) {
return other instanceof CloseConnectionResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
*/
package org.eclipse.ditto.connectivity.model.signals.commands.modify;

import org.eclipse.ditto.connectivity.model.WithConnectionId;
import org.eclipse.ditto.base.model.signals.SignalWithEntityId;
import org.eclipse.ditto.base.model.signals.WithOptionalEntity;
import org.eclipse.ditto.connectivity.model.WithConnectionId;
import org.eclipse.ditto.connectivity.model.signals.commands.ConnectivityCommand;

/**
Expand All @@ -23,5 +23,5 @@
* @param <T> the type of the implementing class.
*/
public interface ConnectivityModifyCommand<T extends ConnectivityModifyCommand<T>>
extends ConnectivityCommand<T>, WithOptionalEntity, WithConnectionId, SignalWithEntityId<T> {
extends ConnectivityCommand<T>, WithOptionalEntity<T>, WithConnectionId, SignalWithEntityId<T> {
}