Skip to content

Commit

Permalink
Mongo can now properly access ID from nested object, which prevents c…
Browse files Browse the repository at this point in the history
…reation of new ID and solves mismatch problem
  • Loading branch information
luk-kaminski committed Feb 14, 2025
1 parent 036f3a0 commit eba183c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.mongojack.Id;
import org.mongojack.ObjectId;

import javax.annotation.Nullable;
import java.util.Map;
Expand All @@ -48,6 +47,7 @@ public abstract class SearchJobState implements MongoEntity {
public static final String ERRORS_FIELD = "error_message";

@JsonUnwrapped
@Id
public abstract SearchJobIdentifier identifier();

@JsonProperty(STATUS_FIELD)
Expand Down Expand Up @@ -144,8 +144,6 @@ public static SearchJobState createDoneJobFrom(final SearchJobState existingSear
}

@Override
@ObjectId
@Id
@JsonIgnore
public String id() {
return identifier().id();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@ public long expireOlderThan(final DateTime dateTime) {
}

public SearchJobState create(final SearchJobState searchJobState) {
// if (searchJobState.identifier().id() != null) {
// throw new IllegalStateException("ID should be null for a call to create a new SearchJobState");
// } //TODO: check mismatch between IDs in Mongo and inmemory cache
final InsertOneResult insertOneResult = this.collection.insertOne(searchJobState);
return get(MongoUtils.insertedIdAsString(insertOneResult)).orElseThrow(() -> new IllegalStateException("Unable to retrieve saved search job state!"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,18 @@ public void setUp() {
@Test
public void testSaveAndGet() {
final SearchJobState toBeSaved = SearchJobState.builder()
.identifier(new SearchJobIdentifier(null, "677fd86ae6db8b71a8e10e3e", "john", "dcae52e4-777e-4e3f-8e69-61df7a607016"))
.identifier(new SearchJobIdentifier("777fd86ae6db8b71a8e10000", "677fd86ae6db8b71a8e10e3e", "john", "dcae52e4-777e-4e3f-8e69-61df7a607016"))
.result(noResult())
.status(SearchJobStatus.RUNNING)
.progress(42)
.createdAt(DateTime.now(DateTimeZone.UTC))
.updatedAt(DateTime.now(DateTimeZone.UTC))
.build();
final SearchJobState saved = toTest.create(toBeSaved);
final Optional<SearchJobState> retrieved = toTest.get(saved.id());
toTest.create(toBeSaved);
final Optional<SearchJobState> retrieved = toTest.get("777fd86ae6db8b71a8e10000");
assertTrue(retrieved.isPresent());
assertEquals(toBeSaved.toBuilder()
.identifier(new SearchJobIdentifier(saved.id(), "677fd86ae6db8b71a8e10e3e", "john", "dcae52e4-777e-4e3f-8e69-61df7a607016"))
.identifier(new SearchJobIdentifier("777fd86ae6db8b71a8e10000", "677fd86ae6db8b71a8e10e3e", "john", "dcae52e4-777e-4e3f-8e69-61df7a607016"))
.build(), retrieved.get());
}

Expand Down

0 comments on commit eba183c

Please sign in to comment.