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

Bug fix for upsert if filter is based on non id filter #280

Merged
merged 1 commit into from
Mar 20, 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
Expand Up @@ -153,10 +153,12 @@ private Uni<UpdatedDocument> processUpdate(
documentUpdater().applyUpdates(readDocument.document().deepCopy(), isInsert);
JsonNode updatedDocument = documentUpdaterResponse.document();
Uni<DocumentId> updated = Uni.createFrom().nullItem();
final WritableShreddedDocument writableShreddedDocument;
if (documentUpdaterResponse.modified()) {
WritableShreddedDocument writableShreddedDocument =
shredder().shred(updatedDocument, readDocument.txnId());
writableShreddedDocument = shredder().shred(updatedDocument, readDocument.txnId());
updated = updatedDocument(queryExecutor, writableShreddedDocument);
} else {
writableShreddedDocument = null;
}
final JsonNode documentToReturn =
returnUpdatedDocument ? updatedDocument : originalDocument;
Expand All @@ -168,7 +170,7 @@ private Uni<UpdatedDocument> processUpdate(
v -> {
if (readDocument.txnId() != null) modifiedCount.incrementAndGet();
return new UpdatedDocument(
readDocument.id(),
writableShreddedDocument.id(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So how is this a fix and wouldn't this end up in the NPE in that else case? Something is off here..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default updated Document id (updated) is set as Uni.nullValue() when writableShreddedDocument is null. This handled in the below code as
updated
.onItem()
.ifNotNull()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I adapted this a bit in #281 We need to think about readbility of the code, and in general simplification where possible..

readDocument.txnId() == null,
returnDocumentInResponse ? documentToReturn : null,
null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;

import io.quarkus.test.common.QuarkusTestResource;
Expand Down Expand Up @@ -175,24 +176,71 @@ public void findByIdReturnDocumentAfter() {
}

@Test
public void findByIdUpsert() {
public void findByColumnUpsert() {
String json =
"""
{
"findOneAndUpdate": {
"filter" : {"_id" : "afterDoc4"},
"filter" : {"location" : "my_city"},
"update" : {"$set" : {"active_user": false}},
"options" : {"returnDocument" : "after", "upsert" : true}
}
}
""";
String expected =

given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(json)
.when()
.post(CollectionResource.BASE_PATH, keyspaceId.asInternal(), collectionName)
.then()
.statusCode(200)
.body("data.docs[0]", is(notNullValue()))
.body("status.upsertedId", is(notNullValue()))
.body("status.matchedCount", is(0))
.body("status.modifiedCount", is(0))
.body("errors", is(nullValue()));

// assert state after update
json =
"""
{
"_id":"afterDoc4",
"active_user":false
"find": {
"filter" : {"location" : "my_city"}
}
}
""";
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(json)
.when()
.post(CollectionResource.BASE_PATH, keyspaceId.asInternal(), collectionName)
.then()
.statusCode(200)
.body("data.docs[0]", is(notNullValue()));
}

@Test
public void findByIdUpsert() {
String json =
"""
{
"findOneAndUpdate": {
"filter" : {"_id" : "afterDoc4"},
"update" : {"$set" : {"active_user": false}},
"options" : {"returnDocument" : "after", "upsert" : true}
}
}
""";
String expected =
"""
{
"_id":"afterDoc4",
"active_user":false
}
""";

given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
Expand All @@ -211,12 +259,12 @@ public void findByIdUpsert() {
// assert state after update
json =
"""
{
"find": {
"filter" : {"_id" : "afterDoc4"}
}
}
""";
{
"find": {
"filter" : {"_id" : "afterDoc4"}
}
}
""";
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;

import io.quarkus.test.common.QuarkusTestResource;
Expand Down Expand Up @@ -359,6 +360,53 @@ public void updateManyByIdNoChange() {
.body("data.docs[0]", jsonEquals(expected));
}

@Test
public void upsertManyByColumnUpsert() {
String json =
"""
{
"updateMany": {
"filter" : {"location" : "my_city"},
"update" : {"$set" : {"active_user": false}},
"options" : {"upsert" : true}
}
}
""";

given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(json)
.when()
.post(CollectionResource.BASE_PATH, keyspaceId.asInternal(), collectionName)
.then()
.statusCode(200)
.body("status.upsertedId", is(notNullValue()))
.body("status.matchedCount", is(0))
.body("status.modifiedCount", is(0))
.body("errors", is(nullValue()));

// assert state after update
json =
"""
{
"find": {
"filter" : {"location" : "my_city"}
}
}
""";

given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(json)
.when()
.post(CollectionResource.BASE_PATH, keyspaceId.asInternal(), collectionName)
.then()
.statusCode(200)
.body("data.docs[0]", is(notNullValue()));
}

@Test
public void updateManyUpsertAddFilterColumn() {
insert(5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static io.stargate.sgv2.common.IntegrationTestUtils.getAuthToken;
import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;

import io.quarkus.test.common.QuarkusTestResource;
Expand Down Expand Up @@ -149,6 +150,53 @@ public void findByIdUpsert() {
.body("data.docs[0]", jsonEquals(expected));
}

@Test
public void findByColumnUpsert() {
String json =
"""
{
"updateOne": {
"filter" : {"location" : "my_city"},
"update" : {"$set" : {"active_user": false}},
"options" : {"upsert" : true}
}
}
""";

given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(json)
.when()
.post(CollectionResource.BASE_PATH, keyspaceId.asInternal(), collectionName)
.then()
.statusCode(200)
.body("status.upsertedId", is(notNullValue()))
.body("status.matchedCount", is(0))
.body("status.modifiedCount", is(0))
.body("errors", is(nullValue()));

// assert state after update
json =
"""
{
"find": {
"filter" : {"location" : "my_city"}
}
}
""";

given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(json)
.when()
.post(CollectionResource.BASE_PATH, keyspaceId.asInternal(), collectionName)
.then()
.statusCode(200)
.body("data.docs[0]", is(notNullValue()));
}

@Test
public void findByIdAndColumnUpsert() {
String json =
Expand Down