Skip to content

feat: add option to indicate that a statement is the last in a transaction #3644

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

Merged
merged 3 commits into from
Feb 13, 2025
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.
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 @@ -698,6 +698,9 @@ ExecuteSqlRequest.Builder getExecuteSqlRequestBuilder(
if (!isReadOnly()) {
builder.setSeqno(getSeqNo());
}
if (options.hasLastStatementSet()) {
builder.setLastStatement(options.lastStatementSet());
}
builder.setQueryOptions(buildQueryOptions(statement.getQueryOptions()));
builder.setRequestOptions(buildRequestOptions(options));
return builder;
Expand Down Expand Up @@ -743,6 +746,9 @@ ExecuteBatchDmlRequest.Builder getExecuteBatchDmlRequestBuilder(
if (selector != null) {
builder.setTransaction(selector);
}
if (options.hasLastStatementSet()) {
builder.setLastStatements(options.lastStatementSet());
}
builder.setSeqno(getSeqNo());
builder.setRequestOptions(buildRequestOptions(options));
return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,20 @@ public static DataBoostQueryOption dataBoostEnabled(Boolean dataBoostEnabled) {
return new DataBoostQueryOption(dataBoostEnabled);
}

/**
* If set to true, this option marks the end of the transaction. The transaction should be
* committed or aborted after this statement executes, and attempts to execute any other requests
* against this transaction (including reads and queries) will be rejected. Mixing mutations with
* statements that are marked as the last statement is not allowed.
*
* <p>For DML statements, setting this option may cause some error reporting to be deferred until
* commit time (e.g. validation of unique constraints). Given this, successful execution of a DML
* statement should not be assumed until the transaction commits.
*/
public static LastStatementUpdateOption lastStatementSet(Boolean lastStatementSet) {
return new LastStatementUpdateOption(lastStatementSet);
}

/**
* Specifying this will cause the list operation to start fetching the record from this onwards.
*/
Expand Down Expand Up @@ -495,6 +509,8 @@ void appendToOptions(Options options) {
private RpcOrderBy orderBy;
private RpcLockHint lockHint;

private Boolean lastStatementSet;

// Construction is via factory methods below.
private Options() {}

Expand Down Expand Up @@ -630,6 +646,14 @@ OrderBy orderBy() {
return orderBy == null ? null : orderBy.proto;
}

boolean hasLastStatementSet() {
return lastStatementSet != null;
}

Boolean lastStatementSet() {
return lastStatementSet;
}

boolean hasLockHint() {
return lockHint != null;
}
Expand Down Expand Up @@ -694,6 +718,9 @@ public String toString() {
if (orderBy != null) {
b.append("orderBy: ").append(orderBy).append(' ');
}
if (lastStatementSet != null) {
b.append("lastStatementSet: ").append(lastStatementSet).append(' ');
}
if (lockHint != null) {
b.append("lockHint: ").append(lockHint).append(' ');
}
Expand Down Expand Up @@ -737,6 +764,7 @@ public boolean equals(Object o) {
&& Objects.equals(dataBoostEnabled(), that.dataBoostEnabled())
&& Objects.equals(directedReadOptions(), that.directedReadOptions())
&& Objects.equals(orderBy(), that.orderBy())
&& Objects.equals(lastStatementSet(), that.lastStatementSet());
&& Objects.equals(lockHint(), that.lockHint());
}

Expand Down Expand Up @@ -797,6 +825,9 @@ public int hashCode() {
if (orderBy != null) {
result = 31 * result + orderBy.hashCode();
}
if (lastStatementSet != null) {
result = 31 * result + lastStatementSet.hashCode();
}
if (lockHint != null) {
result = 31 * result + lockHint.hashCode();
}
Expand Down Expand Up @@ -965,4 +996,18 @@ public boolean equals(Object o) {
return Objects.equals(filter, ((FilterOption) o).filter);
}
}

static final class LastStatementUpdateOption extends InternalOption implements UpdateOption {

private final Boolean lastStatementSet;

LastStatementUpdateOption(Boolean lastStatementSet) {
this.lastStatementSet = lastStatementSet;
}

@Override
void appendToOptions(Options options) {
options.lastStatementSet = lastStatementSet;
}
}
}
Loading