-
Notifications
You must be signed in to change notification settings - Fork 12
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
Allow to detect Entities which no longer match subscription criteria #1517
Changes from 9 commits
ab8e45d
fc2c432
0a17c30
5e0cdf7
4914332
9bb9b6f
3af56e8
350bd0c
7534207
0d4a6e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2023, TeamDev. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Redistribution and use in source and/or binary forms, with or without | ||
* modification, must retain the above copyright notice and the following | ||
* disclaimer. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
package io.spine.client; | ||
|
||
import com.google.protobuf.Any; | ||
import io.spine.base.Identifier; | ||
import io.spine.protobuf.AnyPacker; | ||
|
||
import java.util.function.Consumer; | ||
|
||
/** | ||
* Consumer-delegate of entity IDs, which correspond to entities no longer matching | ||
* the subscription criteria. | ||
* | ||
* <p>Unpacks the ID values from the passed {@code Any} instances according | ||
* to the identifier type passed. It is a responsibility of callees to provide | ||
* the relevant type of identifiers. | ||
* | ||
* @param <I> | ||
* the type of entity identifiers | ||
*/ | ||
final class NoLongerMatchingConsumer<I> implements Consumer<Any> { | ||
|
||
private final Class<I> idClass; | ||
private final Consumer<I> delegate; | ||
|
||
/** | ||
* Creates a new instance of {@code IdConsumer}. | ||
* | ||
* @param idClass | ||
* the type of identifiers used to unpack the incoming {@code Any} instances | ||
* @param delegate | ||
* the consumer to delegate the observation to | ||
*/ | ||
NoLongerMatchingConsumer(Class<I> idClass, Consumer<I> delegate) { | ||
this.idClass = idClass; | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public void accept(Any packedId) { | ||
var entityId = AnyPacker.unpack(packedId, EntityId.class); | ||
var any = entityId.getId(); | ||
var unpacked = Identifier.unpack(any, idClass); | ||
delegate.accept(unpacked); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2023, TeamDev. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Redistribution and use in source and/or binary forms, with or without | ||
* modification, must retain the above copyright notice and the following | ||
* disclaimer. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
package io.spine.client; | ||
|
||
import io.grpc.stub.StreamObserver; | ||
|
||
import static io.spine.client.EntityStateUpdate.KindCase.NO_LONGER_MATCHING; | ||
|
||
/** | ||
* An observer taking {@code SubscriptionUpdate} and passing it to the specified | ||
* {@code NoLongerMatchingConsumer} in case the {@code SubscriptionUpdate} tells | ||
* that some entity stopped matching the subscription criteria. | ||
* | ||
* <p>If {@code SubscriptionUpdate} does not correspond to "no longer matching" scenario, | ||
* does nothing. | ||
*/ | ||
final class NoLongerMatchingFilter implements StreamObserver<SubscriptionUpdate> { | ||
|
||
private final NoLongerMatchingConsumer<?> consumer; | ||
|
||
NoLongerMatchingFilter(NoLongerMatchingConsumer<?> consumer) { | ||
this.consumer = consumer; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please check for |
||
} | ||
|
||
@Override | ||
public void onNext(SubscriptionUpdate value) { | ||
if (value.hasEntityUpdates()) { | ||
var updates = value.getEntityUpdates() | ||
.getUpdateList(); | ||
var stream = updates.stream(); | ||
stream.filter(NoLongerMatchingFilter::isNoLongerMatching) | ||
.map(EntityStateUpdate::getId) | ||
.forEach(consumer); | ||
} | ||
} | ||
|
||
private static boolean isNoLongerMatching(EntityStateUpdate update) { | ||
return NO_LONGER_MATCHING == update.getKindCase(); | ||
armiol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Override | ||
public void onError(Throwable t) { | ||
// Do nothing. | ||
} | ||
|
||
@Override | ||
public void onCompleted() { | ||
// Do nothing. | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,8 @@ | |
import com.google.protobuf.Message; | ||
import io.grpc.stub.StreamObserver; | ||
|
||
import java.util.Optional; | ||
|
||
import static io.spine.protobuf.AnyPacker.unpack; | ||
import static io.spine.util.Exceptions.unsupported; | ||
|
||
|
@@ -42,16 +44,61 @@ | |
* and sent to the delegate observer one by one. | ||
* | ||
* @param <M> | ||
* the type of the delegate observer messages, which could be unpacked entity state | ||
* or {@code Event} | ||
* the type of the delegate-observer's messages, which can either be | ||
* an unpacked entity state or an {@code Event} | ||
*/ | ||
final class SubscriptionObserver<M extends Message> | ||
implements StreamObserver<SubscriptionUpdate> { | ||
|
||
/** | ||
* Delegate which would receive the unpacked domain-specific messages, such as | ||
* entity states or {@code Event}s. | ||
*/ | ||
private final StreamObserver<M> delegate; | ||
|
||
|
||
/** | ||
* Optional chained observer of raw {@code SubscriptionUpdate}s, | ||
* which would receive its input within the same subscription. | ||
* | ||
* <p>Such an observer may be handy for descendants which need more details | ||
* than just an "unpacked" domain message. | ||
*/ | ||
@SuppressWarnings("OptionalUsedAsFieldOrParameterType" | ||
/* Could have been `@Nullable`, | ||
but it is always used as `Optional`; | ||
so having `Optional` field is an optimization. */) | ||
private final Optional<StreamObserver<SubscriptionUpdate>> chain; | ||
|
||
/** | ||
* Creates a new instance of {@code SubscriptionObserver}. | ||
* | ||
* <p>Specifies no chained observer. | ||
* | ||
* @param targetObserver a delegate consuming the {@code Entity} state, or an {@code Event} | ||
*/ | ||
SubscriptionObserver(StreamObserver<M> targetObserver) { | ||
this.delegate = targetObserver; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please check for |
||
this.chain = Optional.empty(); | ||
} | ||
|
||
/** | ||
* Creates a new instance of {@code SubscriptionObserver}. | ||
* | ||
* <p>Use this constructor in favour of | ||
* {@link SubscriptionObserver#SubscriptionObserver(StreamObserver) | ||
* SubscriptionObserver(StreamObserver)} to additionally set the observer chain. | ||
* | ||
* @param targetObserver | ||
* a delegate consuming the {@code Entity} state, or an {@code Event} | ||
* @param chain | ||
* chained observer consuming {@code SubscriptionUpdate} obtained within | ||
* the same subscription | ||
*/ | ||
SubscriptionObserver(StreamObserver<M> targetObserver, | ||
StreamObserver<SubscriptionUpdate> chain) { | ||
this.delegate = targetObserver; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please check for |
||
this.chain = Optional.of(chain); | ||
} | ||
|
||
@SuppressWarnings("unchecked") // Logically correct. | ||
|
@@ -79,6 +126,7 @@ public void onNext(SubscriptionUpdate value) { | |
default: | ||
throw unsupported("Unsupported update case `%s`.", updateCase); | ||
} | ||
chain.ifPresent(observer -> observer.onNext(value)); | ||
} | ||
|
||
@Override | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we check for
null
input here, please?