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

feat: adding upsert api with for conditial insert and update #5

Draft
wants to merge 1 commit into
base: code-review-exercise-base
Choose a base branch
from

Conversation

aaron-steinfeld
Copy link
Owner

No description provided.

* 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)
Copy link
Owner Author

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)

Copy link
Owner Author

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);
Copy link
Owner Author

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);
Copy link
Owner Author

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()) {
Copy link
Owner Author

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 =
Copy link
Owner Author

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)
Copy link
Owner Author

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 {
Copy link
Owner Author

Choose a reason for hiding this comment

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

method level comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant