Skip to content

Commit

Permalink
Merge pull request #184 from fauna/BT-5321-eventsource
Browse files Browse the repository at this point in the history
[BT-5321-eventsource] Refactor: Delete EventSourceResponse.java, as it duplicates EventSource.java
  • Loading branch information
pnwpedro authored Nov 13, 2024
2 parents 953f8e4 + 1555cc3 commit 17ee17a
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 129 deletions.
15 changes: 6 additions & 9 deletions src/main/java/com/fauna/client/FaunaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.fauna.codec.DefaultCodecRegistry;
import com.fauna.codec.ParameterizedOf;
import com.fauna.event.EventSource;
import com.fauna.event.EventSourceResponse;
import com.fauna.event.FaunaStream;
import com.fauna.event.FeedIterator;
import com.fauna.event.FeedOptions;
Expand Down Expand Up @@ -612,11 +611,9 @@ public <E> FaunaStream<E> stream(final EventSource eventSource,
*/
public <E> CompletableFuture<FaunaStream<E>> asyncStream(final Query fql,
final Class<E> elementClass) {
return this.asyncQuery(fql, EventSourceResponse.class)
return this.asyncQuery(fql, EventSource.class)
.thenApply(queryResponse ->
this.stream(EventSource.fromResponse(
queryResponse.getData()),
StreamOptions.builder().build(), elementClass));
this.stream(queryResponse.getData(), StreamOptions.builder().build(), elementClass));
}

/**
Expand All @@ -625,8 +622,8 @@ public <E> CompletableFuture<FaunaStream<E>> asyncStream(final Query fql,
*
* <p>
* Query = fql("Product.all().eventSource()");
* QuerySuccess&lt;EventSourceResponse&gt; querySuccess = client.query(fql, EventSourceResponse.class);
* EventSource source = EventSource.fromResponse(querySuccess.getData());
* QuerySuccess&lt;EventSource&gt; querySuccess = client.query(fql, EventSource.class);
* EventSource source = querySuccess.getData();
* FaunaStream&lt;Product&gt; faunaStream = client.stream(source, StreamOptions.DEFAULT, Product.class)
*
* @param fql The FQL query to be executed. It must return a stream, e.g. ends in `.toStream()`.
Expand Down Expand Up @@ -678,9 +675,9 @@ public <E> CompletableFuture<FeedPage<E>> poll(final EventSource eventSource,
public <E> CompletableFuture<FeedIterator<E>> asyncFeed(final Query fql,
final FeedOptions feedOptions,
final Class<E> elementClass) {
return this.asyncQuery(fql, EventSourceResponse.class).thenApply(
return this.asyncQuery(fql, EventSource.class).thenApply(
success -> this.feed(
EventSource.fromResponse(success.getData()),
success.getData(),
feedOptions, elementClass));
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/fauna/codec/DefaultCodecProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.fauna.codec.codecs.ClassCodec;
import com.fauna.codec.codecs.DynamicCodec;
import com.fauna.codec.codecs.EnumCodec;
import com.fauna.codec.codecs.EventSourceResponseCodec;
import com.fauna.codec.codecs.EventSourceCodec;
import com.fauna.codec.codecs.ListCodec;
import com.fauna.codec.codecs.MapCodec;
import com.fauna.codec.codecs.NullableDocumentCodec;
Expand All @@ -15,7 +15,7 @@
import com.fauna.codec.codecs.QueryLiteralCodec;
import com.fauna.codec.codecs.QueryObjCodec;
import com.fauna.codec.codecs.QueryValCodec;
import com.fauna.event.EventSourceResponse;
import com.fauna.event.EventSource;
import com.fauna.query.builder.Query;
import com.fauna.query.builder.QueryArr;
import com.fauna.query.builder.QueryLiteral;
Expand Down Expand Up @@ -62,7 +62,7 @@ public DefaultCodecProvider(final CodecRegistry registry) {
registry.put(CodecRegistryKey.from(QueryVal.class), new QueryValCodec(this));
registry.put(CodecRegistryKey.from(QueryLiteral.class), new QueryLiteralCodec());

registry.put(CodecRegistryKey.from(EventSourceResponse.class), new EventSourceResponseCodec());
registry.put(CodecRegistryKey.from(EventSource.class), new EventSourceCodec());

var bdc = new BaseDocumentCodec(this);
registry.put(CodecRegistryKey.from(BaseDocument.class), bdc);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/fauna/codec/codecs/DynamicCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.fauna.codec.FaunaType;
import com.fauna.codec.UTF8FaunaGenerator;
import com.fauna.codec.UTF8FaunaParser;
import com.fauna.event.EventSourceResponse;
import com.fauna.event.EventSource;
import com.fauna.exception.CodecException;
import com.fauna.types.Document;
import com.fauna.types.DocumentRef;
Expand Down Expand Up @@ -52,7 +52,7 @@ public Object decode(final UTF8FaunaParser parser) throws CodecException {
case START_DOCUMENT:
return provider.get(Document.class).decode(parser);
case STREAM:
return provider.get(EventSourceResponse.class).decode(parser);
return provider.get(EventSource.class).decode(parser);
case MODULE:
return parser.getValueAsModule();
case INT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
import com.fauna.codec.FaunaType;
import com.fauna.codec.UTF8FaunaGenerator;
import com.fauna.codec.UTF8FaunaParser;
import com.fauna.event.EventSourceResponse;
import com.fauna.event.EventSource;
import com.fauna.exception.CodecException;

/**
* Codec for encoding and decoding {@link EventSourceResponse} instances in the Fauna tagged format.
* Codec for encoding and decoding {@link EventSource} instances in the Fauna tagged format.
*/
public final class EventSourceResponseCodec extends BaseCodec<EventSourceResponse> {
public final class EventSourceCodec extends BaseCodec<EventSource> {

@Override
public EventSourceResponse decode(final UTF8FaunaParser parser)
public EventSource decode(final UTF8FaunaParser parser)
throws CodecException {
if (parser.getCurrentTokenType() == FaunaTokenType.STREAM) {
return new EventSourceResponse(parser.getTaggedValueAsString());
return new EventSource(parser.getTaggedValueAsString());
} else {
throw new CodecException(this.unsupportedTypeDecodingMessage(
parser.getCurrentTokenType().getFaunaType(),
Expand All @@ -25,14 +25,14 @@ public EventSourceResponse decode(final UTF8FaunaParser parser)
}

@Override
public void encode(final UTF8FaunaGenerator gen, final EventSourceResponse obj)
public void encode(final UTF8FaunaGenerator gen, final EventSource obj)
throws CodecException {
throw new CodecException("Cannot encode StreamTokenResponse");
throw new CodecException("Cannot encode EventSource");
}

@Override
public Class<?> getCodecClass() {
return EventSourceResponse.class;
return EventSource.class;
}

@Override
Expand Down
36 changes: 31 additions & 5 deletions src/main/java/com/fauna/event/EventSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.fauna.query.builder.Query;

import java.util.Objects;

/**
* Represents an <a href="https://docs.fauna.com/fauna/current/reference/cdc/#event-source">event source</a>. You can consume event sources
as <a href="https://docs.fauna.com/fauna/current/reference/cdc/#event-feeds">Event Feeds</a> by
Expand Down Expand Up @@ -41,14 +43,38 @@ public String getToken() {
public static EventSource fromToken(final String token) {
return new EventSource(token);
}
/**
* Compares this {@code EventSource} with another object for equality.
*
* @param o The object to compare with.
* @return {@code true} if the specified object is equal to this {@code EventSource}; {@code false} otherwise.
*/
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}

if (o == null) {
return false;
}

if (getClass() != o.getClass()) {
return false;
}

EventSource c = (EventSource) o;

return Objects.equals(token, c.token);
}

/**
* Creates an {@code EventSource} from an {@code EventSourceResponse}.
* Returns the hash code for this {@code EventSource}.
*
* @param response An {@code EventSourceResponse} containing the token for the event source.
* @return A new {@code EventSource} instance.
* @return An {@code int} representing the hash code of this object.
*/
public static EventSource fromResponse(final EventSourceResponse response) {
return new EventSource(response.getToken());
@Override
public int hashCode() {
return Objects.hash(token);
}
}
72 changes: 0 additions & 72 deletions src/main/java/com/fauna/event/EventSourceResponse.java

This file was deleted.

8 changes: 4 additions & 4 deletions src/main/java/com/fauna/event/FeedRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ public String serialize() throws IOException {
}

/**
* Creates a new {@code FeedRequest} from an {@link EventSourceResponse}.
* Creates a new {@code FeedRequest} from an {@link EventSource}.
*
* @param resp The {@link EventSourceResponse} containing the event source token.
* @param resp The {@link EventSource} containing the event source token.
* @param options The {@link FeedOptions} specifying additional feed request options.
* @return A new {@code FeedRequest} instance based on the response and options.
*/
public static FeedRequest fromResponse(final EventSourceResponse resp, final FeedOptions options) {
return new FeedRequest(EventSource.fromToken(resp.getToken()), options);
public static FeedRequest fromResponse(final EventSource resp, final FeedOptions options) {
return new FeedRequest(resp, options);
}
}
1 change: 0 additions & 1 deletion src/main/java/com/fauna/event/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*
* <ul>
* <li>{@link com.fauna.event.EventSource} - Represents an <a href="https://docs.fauna.com/fauna/current/reference/cdc/#event-source">event source</a>.</li>
* <li>{@link com.fauna.event.EventSourceResponse} - Encapsulates the response containing an event source token.</li>
* <li>{@link com.fauna.event.FaunaEvent} - Defines an <a href="https://docs.fauna.com/fauna/current/reference/cdc/#events">event</a>.</li>
* <li>{@link com.fauna.event.FaunaStream} - Processes events from an <a href="https://docs.fauna.com/fauna/current/reference/cdc/#event-streaming">Event Stream</a>, decoding them into {@code FaunaEvent} instances.</li>
* <li>{@link com.fauna.event.FeedIterator} - Enables iteration through pages of events in an <a href="https://docs.fauna.com/fauna/current/reference/cdc/#event-feeds">Event Feed</a>.</li>
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/fauna/beans/InventorySource.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.fauna.beans;

import com.fauna.e2e.beans.Product;
import com.fauna.event.EventSourceResponse;
import com.fauna.event.EventSource;
import com.fauna.types.Page;

public class InventorySource {
public Page<Product> firstPage;
public EventSourceResponse eventSource;
public EventSource eventSource;
}
4 changes: 2 additions & 2 deletions src/test/java/com/fauna/codec/codecs/DynamicCodecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.fauna.codec.Codec;
import com.fauna.codec.DefaultCodecProvider;
import com.fauna.codec.FaunaType;
import com.fauna.event.EventSourceResponse;
import com.fauna.event.EventSource;
import com.fauna.exception.NullDocumentException;
import com.fauna.types.Document;
import com.fauna.types.DocumentRef;
Expand Down Expand Up @@ -84,7 +84,7 @@ public static Stream<Arguments> testCases() {
null, NULL_DOC_EXCEPTION),
Arguments.of(TestType.Decode, DYNAMIC_CODEC,
"{\"@stream\":\"token\"}",
new EventSourceResponse("token"), null),
new EventSource("token"), null),
Arguments.of(TestType.Decode, DYNAMIC_CODEC,
"{\"@bytes\": \"RmF1bmE=\"}",
new byte[] {70, 97, 117, 110, 97}, null)
Expand Down
16 changes: 6 additions & 10 deletions src/test/java/com/fauna/e2e/E2EFeedsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.fauna.client.FaunaConfig;
import com.fauna.e2e.beans.Product;
import com.fauna.event.EventSource;
import com.fauna.event.EventSourceResponse;
import com.fauna.event.FaunaEvent;
import com.fauna.event.FeedIterator;
import com.fauna.event.FeedOptions;
Expand Down Expand Up @@ -69,10 +68,10 @@ public void feedOfAll() {
@Test
public void manualFeed() {
// Use the feeds API with complete (i.e. manual) control of the calls made to Fauna.
QuerySuccess<EventSourceResponse> sourceQuery =
QuerySuccess<EventSource> sourceQuery =
client.query(fql("Product.all().eventSource()"),
EventSourceResponse.class);
EventSource source = EventSource.fromResponse(sourceQuery.getData());
EventSource.class);
EventSource source = sourceQuery.getData();
List<FaunaEvent<Product>> productUpdates = new ArrayList<>();
FeedOptions initialOptions =
FeedOptions.builder().startTs(productCollectionTs).pageSize(2)
Expand Down Expand Up @@ -140,12 +139,9 @@ public void feedError() {
public void feedEventError() {
// Fauna can also return a valid feed page, with HTTP 200, but an "error" event type.
FeedOptions options = FeedOptions.builder().startTs(0L).build();
QuerySuccess<EventSourceResponse> sourceQuery =
client.query(fql("Product.all().eventSource()"),
EventSourceResponse.class);
FeedIterator<Product> iter =
client.feed(EventSource.fromResponse(sourceQuery.getData()),
options, Product.class);
QuerySuccess<EventSource> sourceQuery =
client.query(fql("Product.all().eventSource()"), EventSource.class);
FeedIterator<Product> iter = client.feed(sourceQuery.getData(), options, Product.class);
FeedPage<Product> pageOne = iter.next();
assertFalse(pageOne.hasNext());
assertEquals(1, pageOne.getEvents().size());
Expand Down
Loading

0 comments on commit 17ee17a

Please sign in to comment.