-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: adding upsert api with for conditial insert and update #5
base: code-review-exercise-base
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ | |
import java.util.Set; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import javax.annotation.Nullable; | ||
import net.jodah.failsafe.Failsafe; | ||
import net.jodah.failsafe.RetryPolicy; | ||
import org.bson.conversions.Bson; | ||
|
@@ -123,13 +124,36 @@ public boolean upsert(Key key, Document document) throws IOException { | |
LOGGER.debug("Write result: " + writeResult.toString()); | ||
} | ||
|
||
return writeResult.getModifiedCount() > 0; | ||
return (writeResult.getUpsertedId() != null || writeResult.getModifiedCount() > 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Null check on write result to be defensive |
||
} catch (IOException e) { | ||
LOGGER.error("Exception upserting document. key: {} content:{}", key, document, e); | ||
throw e; | ||
} | ||
} | ||
|
||
/** | ||
* Same as existing upsert method, however, extends the support with condition and optional | ||
* parameter for explicitly controlling insert and update. | ||
* */ | ||
@Override | ||
public boolean upsert(Key key, Document document, Filter condition, @Nullable Boolean isUpsert) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can any of these args be null? check them |
||
throws IOException { | ||
try { | ||
Map<String, Object> map = parseQuery(condition); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. null check |
||
map.put(ID_KEY, key.toString()); | ||
BasicDBObject conditionObject = new BasicDBObject(map); | ||
UpdateOptions options = new UpdateOptions().upsert(isUpsert != null ? isUpsert : true); | ||
UpdateResult writeResult = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. null check |
||
collection.updateOne(conditionObject, this.prepareUpsertWithCondition(key, document), options); | ||
if (LOGGER.isDebugEnabled()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't needed |
||
LOGGER.debug("Write result: " + writeResult.toString()); | ||
} | ||
return (writeResult.getUpsertedId() == null || writeResult.getModifiedCount() > 0); | ||
} catch (Exception e) { | ||
LOGGER.error("Exception upserting document. key: {} content:{}", key, document, e); | ||
throw new IOException(e); | ||
} | ||
} | ||
/** | ||
* Adds the following fields automatically: _id, _lastUpdateTime, lastUpdatedTime and created Time | ||
*/ | ||
|
@@ -152,6 +176,21 @@ public Document upsertAndReturn(Key key, Document document) throws IOException { | |
return this.dbObjectToDocument(upsertResult); | ||
} | ||
|
||
private BasicDBObject prepareUpsertWithCondition(Key key, Document document) throws JsonProcessingException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. method level comments |
||
String jsonString = document.toJson(); | ||
JsonNode jsonNode = MAPPER.readTree(jsonString); | ||
|
||
// escape "." and "$" in field names since Mongo DB does not like them | ||
JsonNode sanitizedJsonNode = recursiveClone(jsonNode, this::encodeKey); | ||
BasicDBObject setObject = BasicDBObject.parse(MAPPER.writeValueAsString(sanitizedJsonNode)); | ||
long now = System.currentTimeMillis(); | ||
setObject.put(ID_KEY, key.toString()); | ||
setObject.put(LAST_UPDATED_TIME, now); | ||
return new BasicDBObject("$set", setObject) | ||
.append("$currentDate", new BasicDBObject(LAST_UPDATE_TIME, true)) | ||
.append("$setOnInsert", new BasicDBObject(CREATED_TIME, now)); | ||
} | ||
|
||
private BasicDBObject prepareUpsert(Key key, Document document) throws JsonProcessingException { | ||
String jsonString = document.toJson(); | ||
JsonNode jsonNode = MAPPER.readTree(jsonString); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Final args (could be at impl level)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documentation is unclear