Skip to content

feat: implement multi stream append. #324

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

Draft
wants to merge 11 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ dependencies {
implementation "org.slf4j:slf4j-api:2.0.17"
implementation "org.bouncycastle:bcprov-jdk18on:1.80"
implementation "org.bouncycastle:bcpkix-jdk18on:1.80"
implementation "org.apache.commons:commons-lang3:3.17.0"

implementation platform("io.opentelemetry:opentelemetry-bom:${openTelemetryVersion}")
implementation "io.opentelemetry:opentelemetry-api"
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/io/kurrent/dbclient/v2/AppendStreamFailure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.kurrent.dbclient.v2;

import javax.validation.constraints.NotNull;
import java.util.Objects;

/**
* Represents a failed append to a stream.
*/
public class AppendStreamFailure {
private final String stream;
private final Exception error;

/**
* Initializes a new instance of the AppendStreamFailure class.
*
* @param stream The stream name.
* @param error The error that occurred.
*/
public AppendStreamFailure(@NotNull String stream, Exception error) {
this.stream = stream;
this.error = error;
}

/**
* Gets the stream name.
*
* @return The stream name.
*/
public String getStream() {
return stream;
}

/**
* Gets the error that occurred.
*
* @return The error that occurred.
*/
public Exception getError() {
return error;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AppendStreamFailure that = (AppendStreamFailure) o;
return Objects.equals(stream, that.stream) &&
Objects.equals(error, that.error);
}

@Override
public int hashCode() {
return Objects.hash(stream, error);
}

@Override
public String toString() {
return "AppendStreamFailure{" +
"stream='" + stream + '\'' +
", error=" + error +
'}';
}
}
26 changes: 26 additions & 0 deletions src/main/java/io/kurrent/dbclient/v2/AppendStreamFailures.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.kurrent.dbclient.v2;

import java.util.ArrayList;
import java.util.Collection;

/**
* Represents a collection of failed appends to streams.
*/
public class AppendStreamFailures extends ArrayList<AppendStreamFailure> {

/**
* Initializes a new instance of the AppendStreamFailures class.
*/
public AppendStreamFailures() {
super();
}

/**
* Initializes a new instance of the AppendStreamFailures class with the specified collection of AppendStreamFailure objects.
*
* @param input The collection of AppendStreamFailure objects.
*/
public AppendStreamFailures(Collection<AppendStreamFailure> input) {
super(input);
}
}
80 changes: 80 additions & 0 deletions src/main/java/io/kurrent/dbclient/v2/AppendStreamRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package io.kurrent.dbclient.v2;

import io.kurrent.dbclient.StreamState;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* Represents a request to append messages to a stream.
*/
public class AppendStreamRequest {
private final String stream;
private final List<Message> messages;
private final StreamState expectedState;
/**
* Initializes a new instance of the AppendStreamRequest class.
*
* @param stream The stream name.
* @param messages The messages to append.
* @param expectedState The expected state of the stream.
*/
public AppendStreamRequest(@NotNull String stream, @NotNull List<Message> messages, @NotNull StreamState expectedState) {
this.stream = stream;
this.messages = messages;
this.expectedState = expectedState;
}

/**
* Gets the stream name.
*
* @return The stream name.
*/
public String getStream() {
return stream;
}

/**
* Gets the messages to append.
*
* @return The messages to append.
*/
public List<Message> getMessages() {
return messages;
}

/**
* Gets the expected state of the stream.
*
* @return The expected state of the stream.
*/
public StreamState getExpectedState() {
return expectedState;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AppendStreamRequest that = (AppendStreamRequest) o;
return Objects.equals(stream, that.stream) &&
Objects.equals(messages, that.messages) &&
Objects.equals(expectedState, that.expectedState);
}

@Override
public int hashCode() {
return Objects.hash(stream, messages, expectedState);
}

@Override
public String toString() {
return "AppendStreamRequest{" +
"stream='" + stream + '\'' +
", messages=" + messages +
", expectedState=" + expectedState +
'}';
}
}
125 changes: 125 additions & 0 deletions src/main/java/io/kurrent/dbclient/v2/AppendStreamResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package io.kurrent.dbclient.v2;

import java.util.Objects;
import java.util.function.Function;

/**
* Represents the result of appending to a stream, which can be either a success or a failure.
*/
public class AppendStreamResult {
private final AppendStreamSuccess success;
private final AppendStreamFailure failure;

private AppendStreamResult(AppendStreamSuccess success, AppendStreamFailure failure) {
this.success = success;
this.failure = failure;
}

/**
* Creates a new AppendStreamResult from a success.
*
* @param success The success result.
* @return A new AppendStreamResult.
*/
public static AppendStreamResult fromSuccess(AppendStreamSuccess success) {
if (success == null)
throw new IllegalArgumentException("Success cannot be null");

return new AppendStreamResult(success, null);
}

/**
* Creates a new AppendStreamResult from a failure.
*
* @param failure The failure result.
* @return A new AppendStreamResult.
*/
public static AppendStreamResult fromFailure(AppendStreamFailure failure) {
if (failure == null)
throw new IllegalArgumentException("Failure cannot be null");

return new AppendStreamResult(null, failure);
}

/**
* Checks if this result is a success.
*
* @return true if this result is a success, false otherwise.
*/
public boolean isSuccess() {
return success != null;
}

/**
* Checks if this result is a failure.
*
* @return true if this result is a failure, false otherwise.
*/
public boolean isFailure() {
return failure != null;
}

/**
* Gets the success result.
*
* @return The success result.
* @throws IllegalStateException if this result is not a success.
*/
public AppendStreamSuccess getSuccess() {
if (!isSuccess())
throw new IllegalStateException("Result is not a success");

return success;
}

/**
* Gets the failure result.
*
* @return The failure result.
* @throws IllegalStateException if this result is not a failure.
*/
public AppendStreamFailure getFailure() {
if (!isFailure())
throw new IllegalStateException("Result is not a failure");

return failure;
}

/**
* Folds the result to the appropriate action.
*
* @param successAction The action to perform if this result is a success.
* @param failureAction The action to perform if this result is a failure.
* @param <T> The type of the result.
* @return The result of the action.
*/
public <T> T fold(Function<AppendStreamSuccess, T> successAction,
Function<AppendStreamFailure, T> failureAction) {
if (isSuccess())
return successAction.apply(success);
else
return failureAction.apply(failure);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AppendStreamResult that = (AppendStreamResult) o;
return Objects.equals(success, that.success) &&
Objects.equals(failure, that.failure);
}

@Override
public int hashCode() {
return Objects.hash(success, failure);
}

@Override
public String toString() {
if (isSuccess())
return "AppendStreamResult{success=" + success + '}';
else
return "AppendStreamResult{failure=" + failure + '}';
}
}
76 changes: 76 additions & 0 deletions src/main/java/io/kurrent/dbclient/v2/AppendStreamSuccess.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package io.kurrent.dbclient.v2;

import java.util.Objects;

/**
* Represents a successful append to a stream.
*/
public class AppendStreamSuccess {
private final String stream;
private final long position;
private final long streamRevision;

/**
* Initializes a new instance of the AppendStreamSuccess class.
*
* @param stream The stream name.
* @param position The position in the log.
* @param streamRevision The stream revision.
*/
public AppendStreamSuccess(String stream, long position, long streamRevision) {
this.stream = stream != null ? stream : "";
this.position = position;
this.streamRevision = streamRevision;
}

/**
* Gets the stream name.
*
* @return The stream name.
*/
public String getStream() {
return stream;
}

/**
* Gets the position in the log.
*
* @return The position in the log.
*/
public long getPosition() {
return position;
}

/**
* Gets the stream revision.
*
* @return The stream revision.
*/
public long getStreamRevision() {
return streamRevision;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AppendStreamSuccess that = (AppendStreamSuccess) o;
return position == that.position &&
streamRevision == that.streamRevision &&
Objects.equals(stream, that.stream);
}

@Override
public int hashCode() {
return Objects.hash(stream, position, streamRevision);
}

@Override
public String toString() {
return "AppendStreamSuccess{" +
"stream='" + stream + '\'' +
", position=" + position +
", streamRevision=" + streamRevision +
'}';
}
}
Loading
Loading