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

RATIS-8: Avoid using Optional for data fields in TransactionContex #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ public class TransactionContext {
private final StateMachine stateMachine;

/** Original request from the client */
private Optional<RaftClientRequest> clientRequest = Optional.empty();
private RaftClientRequest clientRequest;

/** Exception from the {@link StateMachine} or from the log */
private Optional<Exception> exception = Optional.empty();
private Exception exception;

/** Data from the {@link StateMachine} */
private Optional<SMLogEntryProto> smLogEntryProto = Optional.empty();
private SMLogEntryProto smLogEntryProto;

/**
* Context specific to the state machine.
* The {@link StateMachine} can use this object to carry state between
* {@link StateMachine#startTransaction(RaftClientRequest)} and
* {@link StateMachine#applyTransaction(TransactionContext)}.
*/
private Optional<Object> stateMachineContext = Optional.empty();
private Object stateMachineContext;

/**
* Whether to commit the transaction to the RAFT Log.
Expand All @@ -73,7 +73,7 @@ public class TransactionContext {
private boolean shouldCommit = true;

/** Committed LogEntry. */
private Optional<LogEntryProto> logEntry = Optional.empty();
private LogEntryProto logEntry;

private TransactionContext(StateMachine stateMachine) {
this.stateMachine = stateMachine;
Expand All @@ -96,9 +96,9 @@ public TransactionContext(
StateMachine stateMachine, RaftClientRequest clientRequest,
SMLogEntryProto smLogEntryProto, Object stateMachineContext) {
this(stateMachine);
this.clientRequest = Optional.of(clientRequest);
this.smLogEntryProto = Optional.ofNullable(smLogEntryProto);
this.stateMachineContext = Optional.ofNullable(stateMachineContext);
this.clientRequest = clientRequest;
this.smLogEntryProto = smLogEntryProto;
this.stateMachineContext = stateMachineContext;
}

/** The same as this(stateMachine, clientRequest, exception, null). */
Expand All @@ -117,9 +117,9 @@ public TransactionContext(
StateMachine stateMachine, RaftClientRequest clientRequest,
Exception exception, Object stateMachineContext) {
this(stateMachine);
this.clientRequest = Optional.of(clientRequest);
this.exception = Optional.of(exception);
this.stateMachineContext = Optional.ofNullable(stateMachineContext);
this.clientRequest = clientRequest;
this.exception = exception;
this.stateMachineContext = stateMachineContext;
}

/**
Expand All @@ -129,48 +129,48 @@ public TransactionContext(
*/
public TransactionContext(StateMachine stateMachine, LogEntryProto logEntry) {
this(stateMachine);
this.smLogEntryProto = Optional.of(logEntry.getSmLogEntry());
this.logEntry = Optional.of(logEntry);
this.smLogEntryProto = logEntry.getSmLogEntry();
this.logEntry = logEntry;
}

public Optional<RaftClientRequest> getClientRequest() {
return this.clientRequest;
return Optional.ofNullable(clientRequest);
}

public Optional<SMLogEntryProto> getSMLogEntry() {
return this.smLogEntryProto;
return Optional.ofNullable(smLogEntryProto);
}

public Optional<Exception> getException() {
return this.exception;
return Optional.ofNullable(exception);
}

public TransactionContext setStateMachineContext(Object stateMachineContext) {
this.stateMachineContext = Optional.ofNullable(stateMachineContext);
this.stateMachineContext = stateMachineContext;
return this;
}

public Optional<Object> getStateMachineContext() {
return stateMachineContext;
return Optional.ofNullable(stateMachineContext);
}

public TransactionContext setLogEntry(LogEntryProto logEntry) {
this.logEntry = Optional.of(logEntry);
this.logEntry = logEntry;
return this;
}

public TransactionContext setSmLogEntryProto(SMLogEntryProto smLogEntryProto) {
this.smLogEntryProto = Optional.of(smLogEntryProto);
this.smLogEntryProto = smLogEntryProto;
return this;
}

public Optional<LogEntryProto> getLogEntry() {
return logEntry;
return Optional.ofNullable(logEntry);
}

private TransactionContext setException(IOException ioe) {
assert !this.exception.isPresent();
this.exception = Optional.of(ioe);
assert exception != null;
this.exception = ioe;
return this;
}

Expand Down