-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10b0fc1
commit fe83148
Showing
24 changed files
with
841 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/io/stargate/sgv3/docsapi/api/model/command/impl/DeleteOneCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.stargate.sgv3.docsapi.api.model.command.impl; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonTypeName; | ||
import io.stargate.sgv3.docsapi.api.model.command.Command; | ||
import io.stargate.sgv3.docsapi.api.model.command.Filterable; | ||
import io.stargate.sgv3.docsapi.api.model.command.ModifyCommand; | ||
import io.stargate.sgv3.docsapi.api.model.command.clause.filter.FilterClause; | ||
import javax.validation.Valid; | ||
import javax.validation.constraints.NotNull; | ||
import org.eclipse.microprofile.openapi.annotations.media.Schema; | ||
|
||
/** | ||
* Representation of the deleteOne API {@link Command}. | ||
* | ||
* @param filterClause {@link FilterClause} used to identify the document. | ||
*/ | ||
@Schema(description = "Command that finds a single document and deletes it from a collection") | ||
@JsonTypeName("deleteOne") | ||
public record DeleteOneCommand( | ||
@NotNull | ||
@Schema( | ||
description = "Filter clause based on which document is identified", | ||
implementation = FilterClause.class) | ||
@Valid | ||
@JsonProperty("filter") | ||
FilterClause filterClause) | ||
implements ModifyCommand, Filterable {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/java/io/stargate/sgv3/docsapi/service/operation/model/ModifyOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package io.stargate.sgv3.docsapi.service.operation.model; | ||
|
||
/** Interface for operations that modify data, insert, delete, update. */ | ||
public interface ModifyOperation {} | ||
public interface ModifyOperation extends Operation {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/main/java/io/stargate/sgv3/docsapi/service/operation/model/impl/DeleteOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package io.stargate.sgv3.docsapi.service.operation.model.impl; | ||
|
||
import io.smallrye.mutiny.Multi; | ||
import io.smallrye.mutiny.Uni; | ||
import io.stargate.bridge.grpc.Values; | ||
import io.stargate.bridge.proto.QueryOuterClass; | ||
import io.stargate.sgv3.docsapi.api.model.command.CommandContext; | ||
import io.stargate.sgv3.docsapi.api.model.command.CommandResult; | ||
import io.stargate.sgv3.docsapi.service.bridge.executor.QueryExecutor; | ||
import io.stargate.sgv3.docsapi.service.operation.model.ModifyOperation; | ||
import io.stargate.sgv3.docsapi.service.operation.model.ReadOperation; | ||
import io.stargate.sgv3.docsapi.service.operation.model.ReadOperation.FindResponse; | ||
import java.util.List; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Executes readOperation to get the documents ids based on filter condition. All the ids are | ||
* deleted as LWT based on the id and tx_id. | ||
*/ | ||
public record DeleteOperation(CommandContext commandContext, ReadOperation readOperation) | ||
implements ModifyOperation { | ||
@Override | ||
public Uni<Supplier<CommandResult>> execute(QueryExecutor queryExecutor) { | ||
Uni<FindResponse> docsToDelete = readOperation().getDocuments(queryExecutor); | ||
final QueryOuterClass.Query delete = buildDeleteQuery(); | ||
final Uni<List<String>> ids = | ||
docsToDelete | ||
.onItem() | ||
.transformToMulti( | ||
findResponse -> Multi.createFrom().items(findResponse.docs().stream())) | ||
.onItem() | ||
.transformToUniAndConcatenate( | ||
readDocument -> deleteDocument(queryExecutor, delete, readDocument)) | ||
.collect() | ||
.asList(); | ||
return ids.onItem().transform(DeleteOperationPage::new); | ||
} | ||
|
||
private QueryOuterClass.Query buildDeleteQuery() { | ||
String delete = "DELETE FROM \"%s\".\"%s\" WHERE key = ? IF tx_id = ?"; | ||
return QueryOuterClass.Query.newBuilder() | ||
.setCql(String.format(delete, commandContext.database(), commandContext.collection())) | ||
.build(); | ||
} | ||
|
||
/** | ||
* When delete is run with LWT, applied field is always the first field and in case the | ||
* transaction id mismatch the latest transaction id is returned as second field Eg: | ||
* cassandra@cqlsh:docsapi> delete from docsapi.test1 where key = 'doc2' IF tx_id = | ||
* 13659a90-9361-11ed-92df-515ba7f99655 ; | ||
* | ||
* <p>[applied] | tx_id -----------+-------------------------------------- False | | ||
* 13659a90-9361-11ed-92df-515ba7f99654 | ||
* | ||
* <p>cassandra@cqlsh:docsapi> delete from docsapi.test1 where key = 'doc2' IF tx_id = | ||
* 13659a90-9361-11ed-92df-515ba7f99654 ; | ||
* | ||
* <p>[applied] ----------- True | ||
* | ||
* @param queryExecutor | ||
* @param query | ||
* @param doc | ||
* @return | ||
*/ | ||
private static Uni<String> deleteDocument( | ||
QueryExecutor queryExecutor, QueryOuterClass.Query query, ReadDocument doc) { | ||
query = bindDeleteQuery(query, doc); | ||
return queryExecutor | ||
.executeWrite(query) | ||
.onItem() | ||
.transformToUni( | ||
result -> { | ||
if (result.getRows(0).getValues(0).getBoolean()) { | ||
return Uni.createFrom().item(doc.id()); | ||
} else { | ||
return Uni.createFrom().nothing(); | ||
} | ||
}); | ||
} | ||
|
||
private static QueryOuterClass.Query bindDeleteQuery( | ||
QueryOuterClass.Query builtQuery, ReadDocument doc) { | ||
QueryOuterClass.Values.Builder values = | ||
QueryOuterClass.Values.newBuilder() | ||
.addValues(Values.of(doc.id())) | ||
.addValues(Values.of(doc.txnId())); | ||
return QueryOuterClass.Query.newBuilder(builtQuery).setValues(values).build(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/io/stargate/sgv3/docsapi/service/operation/model/impl/DeleteOperationPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.stargate.sgv3.docsapi.service.operation.model.impl; | ||
|
||
import io.stargate.sgv3.docsapi.api.model.command.CommandResult; | ||
import io.stargate.sgv3.docsapi.api.model.command.CommandStatus; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* This represents the response for a delete operation. . | ||
* | ||
* @param deletedIds - document ids deleted | ||
*/ | ||
public record DeleteOperationPage(List<String> deletedIds) implements Supplier<CommandResult> { | ||
@Override | ||
public CommandResult get() { | ||
return new CommandResult(Map.of(CommandStatus.DELETED_IDS, deletedIds)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.