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

Mongo: unify condition handling #7093

Merged
merged 1 commit into from
Jun 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
import org.agrona.collections.Hashing;
import org.agrona.collections.Object2IntHashMap;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.Binary;
import org.projectnessie.nessie.relocated.protobuf.ByteString;
import org.projectnessie.versioned.storage.common.config.StoreConfig;
Expand Down Expand Up @@ -252,14 +253,7 @@ public Reference markReferenceAsDeleted(@Nonnull @jakarta.annotation.Nonnull Ref
throws RefNotFoundException, RefConditionFailedException {
reference = reference.withDeleted(false);
UpdateResult result =
backend
.refs()
.updateOne(
and(
eq(ID_PROPERTY_NAME, idRefDoc(reference)),
eq(COL_REFERENCES_POINTER, objIdToBinary(reference.pointer())),
eq(COL_REFERENCES_DELETED, false)),
set(COL_REFERENCES_DELETED, true));
backend.refs().updateOne(referenceCondition(reference), set(COL_REFERENCES_DELETED, true));
if (result.getModifiedCount() != 1) {
Reference ex = fetchReference(reference.name());
if (ex == null) {
Expand All @@ -271,6 +265,15 @@ public Reference markReferenceAsDeleted(@Nonnull @jakarta.annotation.Nonnull Ref
return reference.withDeleted(true);
}

private Bson referenceCondition(Reference reference) {
List<Bson> filters = new ArrayList<>(5);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
List<Bson> filters = new ArrayList<>(5);
List<Bson> filters = new ArrayList<>(3);

Copy link
Member Author

Choose a reason for hiding this comment

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

That number will be correct in the future ;)

filters.add(eq(ID_PROPERTY_NAME, idRefDoc(reference)));
filters.add(eq(COL_REFERENCES_POINTER, objIdToBinary(reference.pointer())));
filters.add(eq(COL_REFERENCES_DELETED, reference.deleted()));

return and(filters);
}

@Nonnull
@jakarta.annotation.Nonnull
@Override
Expand All @@ -284,10 +287,7 @@ public Reference updateReferencePointer(
backend
.refs()
.updateOne(
and(
eq(ID_PROPERTY_NAME, idRefDoc(reference)),
eq(COL_REFERENCES_POINTER, objIdToBinary(reference.pointer())),
eq(COL_REFERENCES_DELETED, false)),
referenceCondition(reference),
set(COL_REFERENCES_POINTER, objIdToBinary(newPointer)));
if (result.getModifiedCount() != 1) {
if (result.getMatchedCount() == 1) {
Expand All @@ -309,14 +309,7 @@ public Reference updateReferencePointer(
public void purgeReference(@Nonnull @jakarta.annotation.Nonnull Reference reference)
throws RefNotFoundException, RefConditionFailedException {
reference = reference.withDeleted(true);
DeleteResult result =
backend
.refs()
.deleteOne(
and(
eq(ID_PROPERTY_NAME, idRefDoc(reference)),
eq(COL_REFERENCES_POINTER, objIdToBinary(reference.pointer())),
eq(COL_REFERENCES_DELETED, true)));
DeleteResult result = backend.refs().deleteOne(referenceCondition(reference));
if (result.getDeletedCount() != 1) {
Reference ex = fetchReference(reference.name());
if (ex == null) {
Expand Down