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

ReadAndUpdateOperation changes for native driver #588

Merged
merged 3 commits into from
Oct 24, 2023
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
@@ -1,15 +1,14 @@
package io.stargate.sgv2.jsonapi.service.operation.model.impl;

import com.datastax.oss.driver.api.core.cql.SimpleStatement;
import com.fasterxml.jackson.databind.JsonNode;
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.sgv2.jsonapi.api.model.command.CommandContext;
import io.stargate.sgv2.jsonapi.api.model.command.CommandResult;
import io.stargate.sgv2.jsonapi.exception.ErrorCode;
import io.stargate.sgv2.jsonapi.service.bridge.executor.QueryExecutor;
import io.stargate.sgv2.jsonapi.service.bridge.serializer.CustomValueSerializers;
import io.stargate.sgv2.jsonapi.service.bridge.serializer.CQLBindValues;
import io.stargate.sgv2.jsonapi.service.operation.model.ModifyOperation;
import io.stargate.sgv2.jsonapi.service.operation.model.ReadOperation;
import io.stargate.sgv2.jsonapi.service.projection.DocumentProjector;
Expand Down Expand Up @@ -209,7 +208,7 @@ private Uni<UpdatedDocument> processUpdate(

private Uni<DocumentId> updatedDocument(
QueryExecutor queryExecutor, WritableShreddedDocument writableShreddedDocument) {
final QueryOuterClass.Query updateQuery =
final SimpleStatement updateQuery =
bindUpdateValues(
buildUpdateQuery(commandContext().isVectorEnabled()),
writableShreddedDocument,
Expand All @@ -219,15 +218,15 @@ private Uni<DocumentId> updatedDocument(
.onItem()
.transformToUni(
result -> {
if (result.getRows(0).getValues(0).getBoolean()) {
if (result.wasApplied()) {
return Uni.createFrom().item(writableShreddedDocument.id());
} else {
throw new LWTException(ErrorCode.CONCURRENCY_FAILURE);
}
});
}

private QueryOuterClass.Query buildUpdateQuery(boolean vectorEnabled) {
private String buildUpdateQuery(boolean vectorEnabled) {
if (vectorEnabled) {
String update =
"UPDATE \"%s\".\"%s\" "
Expand All @@ -247,9 +246,7 @@ private QueryOuterClass.Query buildUpdateQuery(boolean vectorEnabled) {
+ " key = ?"
+ " IF "
+ " tx_id = ?";
return QueryOuterClass.Query.newBuilder()
.setCql(String.format(update, commandContext.namespace(), commandContext.collection()))
.build();
return String.format(update, commandContext.namespace(), commandContext.collection());
} else {
String update =
"UPDATE \"%s\".\"%s\" "
Expand All @@ -268,56 +265,42 @@ private QueryOuterClass.Query buildUpdateQuery(boolean vectorEnabled) {
+ " key = ?"
+ " IF "
+ " tx_id = ?";
return QueryOuterClass.Query.newBuilder()
.setCql(String.format(update, commandContext.namespace(), commandContext.collection()))
.build();
return String.format(update, commandContext.namespace(), commandContext.collection());
}
}

protected static QueryOuterClass.Query bindUpdateValues(
QueryOuterClass.Query builtQuery, WritableShreddedDocument doc, boolean vectorEnabled) {
protected static SimpleStatement bindUpdateValues(
String builtQuery, WritableShreddedDocument doc, boolean vectorEnabled) {
// respect the order in the DocsApiConstants.ALL_COLUMNS_NAMES
if (vectorEnabled) {
QueryOuterClass.Values.Builder values =
QueryOuterClass.Values.newBuilder()
.addValues(Values.of(CustomValueSerializers.getSetValue(doc.existKeys())))
.addValues(Values.of(CustomValueSerializers.getIntegerMapValues(doc.arraySize())))
.addValues(Values.of(CustomValueSerializers.getStringSetValue(doc.arrayContains())))
.addValues(
Values.of(CustomValueSerializers.getBooleanMapValues(doc.queryBoolValues())))
.addValues(
Values.of(CustomValueSerializers.getDoubleMapValues(doc.queryNumberValues())))
.addValues(
Values.of(CustomValueSerializers.getStringMapValues(doc.queryTextValues())))
.addValues(Values.of(CustomValueSerializers.getSetValue(doc.queryNullValues())))
.addValues(
Values.of(
CustomValueSerializers.getTimestampMapValues(doc.queryTimestampValues())))
.addValues(CustomValueSerializers.getVectorValue(doc.queryVectorValues()))
.addValues(Values.of(doc.docJson()))
.addValues(Values.of(CustomValueSerializers.getDocumentIdValue(doc.id())))
.addValues(doc.txID() == null ? Values.NULL : Values.of(doc.txID()));
return QueryOuterClass.Query.newBuilder(builtQuery).setValues(values).build();
return SimpleStatement.newInstance(
builtQuery,
CQLBindValues.getSetValue(doc.existKeys()),
CQLBindValues.getIntegerMapValues(doc.arraySize()),
CQLBindValues.getStringSetValue(doc.arrayContains()),
CQLBindValues.getBooleanMapValues(doc.queryBoolValues()),
CQLBindValues.getDoubleMapValues(doc.queryNumberValues()),
CQLBindValues.getStringMapValues(doc.queryTextValues()),
CQLBindValues.getSetValue(doc.queryNullValues()),
CQLBindValues.getTimestampMapValues(doc.queryTimestampValues()),
CQLBindValues.getVectorValue(doc.queryVectorValues()),
doc.docJson(),
CQLBindValues.getDocumentIdValue(doc.id()),
doc.txID());
} else {
QueryOuterClass.Values.Builder values =
QueryOuterClass.Values.newBuilder()
.addValues(Values.of(CustomValueSerializers.getSetValue(doc.existKeys())))
.addValues(Values.of(CustomValueSerializers.getIntegerMapValues(doc.arraySize())))
.addValues(Values.of(CustomValueSerializers.getStringSetValue(doc.arrayContains())))
.addValues(
Values.of(CustomValueSerializers.getBooleanMapValues(doc.queryBoolValues())))
.addValues(
Values.of(CustomValueSerializers.getDoubleMapValues(doc.queryNumberValues())))
.addValues(
Values.of(CustomValueSerializers.getStringMapValues(doc.queryTextValues())))
.addValues(Values.of(CustomValueSerializers.getSetValue(doc.queryNullValues())))
.addValues(
Values.of(
CustomValueSerializers.getTimestampMapValues(doc.queryTimestampValues())))
.addValues(Values.of(doc.docJson()))
.addValues(Values.of(CustomValueSerializers.getDocumentIdValue(doc.id())))
.addValues(doc.txID() == null ? Values.NULL : Values.of(doc.txID()));
return QueryOuterClass.Query.newBuilder(builtQuery).setValues(values).build();
return SimpleStatement.newInstance(
builtQuery,
CQLBindValues.getSetValue(doc.existKeys()),
CQLBindValues.getIntegerMapValues(doc.arraySize()),
CQLBindValues.getStringSetValue(doc.arrayContains()),
CQLBindValues.getBooleanMapValues(doc.queryBoolValues()),
CQLBindValues.getDoubleMapValues(doc.queryNumberValues()),
CQLBindValues.getStringMapValues(doc.queryTextValues()),
CQLBindValues.getSetValue(doc.queryNullValues()),
CQLBindValues.getTimestampMapValues(doc.queryTimestampValues()),
doc.docJson(),
CQLBindValues.getDocumentIdValue(doc.id()),
doc.txID());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
import java.util.UUID;
import java.util.function.Supplier;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

@QuarkusTest
@Disabled
@TestProfile(NoGlobalResourcesTestProfile.Impl.class)
public class ReadAndUpdateOperationRetryTest extends AbstractValidatingStargateBridgeTest {
private static final String KEYSPACE_NAME = RandomStringUtils.randomAlphanumeric(16);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@
import java.util.UUID;
import java.util.function.Supplier;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

@QuarkusTest
@Disabled
@TestProfile(NoGlobalResourcesTestProfile.Impl.class)
public class ReadAndUpdateOperationTest extends AbstractValidatingStargateBridgeTest {
private static final String KEYSPACE_NAME = RandomStringUtils.randomAlphanumeric(16);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.UUID;
import java.util.function.Supplier;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -152,6 +153,7 @@ class Insert {
+ " (?, now(), ?, ?, ?, ?, ?, ?, ?, ?, ?) IF NOT EXISTS";

@Test
@Disabled
public void insert() throws Exception {
String document =
"""
Expand Down Expand Up @@ -223,6 +225,7 @@ public void insert() throws Exception {
class ReadAndUpdate {

@Test
@Disabled
public void readAndUpdate() throws Exception {
String collectionReadCql =
"SELECT key, tx_id, doc_json FROM \"%s\".\"%s\" WHERE key = ? LIMIT 1"
Expand Down