-
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?
feat: adding upsert api with for conditial insert and update #5
Conversation
* False indicates if the document exists, update it otherwise don't do anything. | ||
* @return True if success. False otherwise. | ||
*/ | ||
boolean upsert(Key key, Document document, Filter condition, @Nullable Boolean isUpsert) |
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
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Null check on write result to be defensive
public boolean upsert(Key key, Document document, Filter condition, @Nullable Boolean isUpsert) | ||
throws IOException { | ||
try { | ||
Map<String, Object> map = parseQuery(condition); |
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.
null check
UpdateOptions options = new UpdateOptions().upsert(isUpsert != null ? isUpsert : true); | ||
UpdateResult writeResult = | ||
collection.updateOne(conditionObject, this.prepareUpsertWithCondition(key, document), options); | ||
if (LOGGER.isDebugEnabled()) { |
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.
This isn't needed
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 comment
The reason will be displayed to describe this comment to others. Learn more.
null check
* 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Can any of these args be null? check them
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
method level comments
No description provided.