NIFI-4989 Made PutMongo able to use nested lookup keys, a query param…#2560
NIFI-4989 Made PutMongo able to use nested lookup keys, a query param…#2560MikeThomsen wants to merge 2 commits intoapache:masterfrom
Conversation
… and multiple lookup keys.
zenfenan
left a comment
There was a problem hiding this comment.
Requesting changes only for the description to be added in the new UPDATE_QUERY PropertyDescriptor.
| .defaultValue("_id") | ||
| .build(); | ||
| static final PropertyDescriptor UPDATE_QUERY = new PropertyDescriptor.Builder() | ||
| .name("putmongo-update-query") |
There was a problem hiding this comment.
How about adding description here?
| query = parseUpdateKey(updateKey, (Map)doc); | ||
| removeUpdateKeys(updateKey, (Map)doc); | ||
| } else { | ||
| query = Document.parse(updateQuery); |
There was a problem hiding this comment.
Shall we change it to filterQuery ? Purely a cosmetic change, feel free to ignore it but I think it follows a better naming convention since it's actually a filter query based on which the update is done.
| updateTests(document); | ||
| } | ||
|
|
||
| @Test |
There was a problem hiding this comment.
If possible, would love to see a similar test but with PutMongo.UPDATE_WITH_DOC
There was a problem hiding this comment.
Yeah. I'm adding 2 at the moment. One for update by keys and one for query.
| .required(true) | ||
| .addValidator(StandardValidators.NON_EMPTY_VALIDATOR) | ||
| .required(false) | ||
| .addValidator(Validator.VALID) |
There was a problem hiding this comment.
Since required is set to false, you can still use the original validor: it'll be used only if a value is set. No? Unless you really want someone to be able to set an empty string as a possible value.
There was a problem hiding this comment.
Wasn't sure if that's the case.
| List<ValidationResult> problems = new ArrayList<>(); | ||
|
|
||
| final boolean queryKey = validationContext.getProperty(UPDATE_QUERY_KEY).isSet() | ||
| && !StringUtils.isBlank(validationContext.getProperty(UPDATE_QUERY_KEY).getValue()); |
There was a problem hiding this comment.
You don't need to check that if my previous comment is valid (since, if a value is set, the validator will check that's the value is not empty)
There was a problem hiding this comment.
I just remembered why I did this. When you call removeProperty to unset the property, the getProperty call here will return the default value. So maybe I need to remove the default value. What do you think? Is this a problem with the test helpers or my understanding of how properties should work?
| private void removeUpdateKeys(String updateKeyParam, Map doc) { | ||
| String[] parts = updateKeyParam.split(",[\\s]*"); | ||
| for (String part : parts) { | ||
| if (part.contains(".")) { |
There was a problem hiding this comment.
Forgot to raise this earlier. Why are we doing a check if it contains a . ? It will work only for complex keys and simple key will be ignored i.e. not removed, right?
There was a problem hiding this comment.
For this input:
{
"name": "John Smith",
"department": "Engineering"
}
It makes no sense to remove name if we're doing a full document update using the `name key.
Now consider this complex document:
{
"name": "John Smith",
"department": "Engineering",
"contacts": {
"email": "john.smith@test.com"
}
}
To search on email, we have to submit this payload with the lookup key being contacts.email:
{
"contacts.email": "john.smith@test.com",
"name": "John Smith",
"department": "Engineering",
"contacts": {
"email": "john.smith@test.com"
}
}
Mongo cannot do a lookup using this: { "contacts": { "email": "john.smith@test.com" }}
So if we don't remove the complex lookup key, we are leaving extraneous information in the document that almost certainly has no value to the user.
Now maybe we'll get an angry ticket complaining that they can't do periods in the key names, but I've never seen normal use cases where developers do that. The whole idea of creating complex key names for real data using periods and such flies in the face of how JSON is supposed to work.
There was a problem hiding this comment.
No.. what I meant was, regardless of whether we have a complex key or simple key, we don't need them to exist in the parsed filter query, right? So why don't we simply remove this if (part.contains(".")) check, basically meaning, remove the key from the doc. No?
There was a problem hiding this comment.
we don't need them to exist in the parsed filter query, right?
We do when replacing one document with another. Say you have a user account document, and the update key is username. If the user is doing full document replacement, and we remove the update key, the username field is going to be removed from the document that is used in the substitution. So then the poor end users will be searching through an accounts collection looking for documents with no usernames.
There was a problem hiding this comment.
Also, again, complex key names simply wouldn't have any value to over 99% of Mongo users. As I said, I'm sure there's a few random users that want to chuck something like this in Mongo:
{
"user.name": "john.smith",
"user.contact.email": "john.smith@test.com"
}
Accommodating that would be detrimental to providing a consistent tool for the vast majority of Mongo users.
There was a problem hiding this comment.
Understood. So this is critical when doing replace i.e. the mode is set to UPDATE_WITH_DOC. Correct?
There was a problem hiding this comment.
Yes. The only time it wouldn't apply is if a user wanted to remove a field like they misnamed the username field as yusername. They can fix that with:
{
"yusername": "....",
"$set": {
"username": ".....",
},
"$unset": {
"yusername": 1
}
}
There was a problem hiding this comment.
TBH, the sanest way to handle such mistakes would be to stop ingestion and run some JavaScript code to update the database properly.
|
@pvillard31 Thoughts on merging this? |
|
@MikeThomsen - can you have a look at this commit: pvillard31@5a67aa1 I reworked a bit the tests to avoid "removing" a property while executing the tests. Basically, instead of having a shared instance of the TestRunner for all the test suite, I initialize one each time (to be sure there is no conflict between the tests and to avoid issues in cases tests are not executed in the same order each time). Regarding your comment about the validation failing, it's because we're providing a default value "_id". Because of that the property will always return true to isSet(). Since we're adding a new property and the user needs to use one of the two, I suggest removing the default value: it does not break anything, and it "forces" the user to understand the choice the user is doing. Does that make sense? |
|
I think that works. |
zenfenan
left a comment
There was a problem hiding this comment.
Looks good to me. Thanks @MikeThomsen
|
@pvillard31 Can you merge your change so we can close this out? |
|
Done, merged to master, thanks @MikeThomsen @zenfenan |
… and multiple lookup keys.
Thank you for submitting a contribution to Apache NiFi.
In order to streamline the review of the contribution we ask you
to ensure the following steps have been taken:
For all changes:
Is there a JIRA ticket associated with this PR? Is it referenced
in the commit message?
Does your PR title start with NIFI-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
Has your PR been rebased against the latest commit within the target branch (typically master)?
Is your initial contribution a single, squashed commit?
For code changes:
For documentation related changes:
Note:
Please ensure that once the PR is submitted, you check travis-ci for build issues and submit an update to your PR as soon as possible.