Skip to content

NIFI-4989 Made PutMongo able to use nested lookup keys, a query param…#2560

Closed
MikeThomsen wants to merge 2 commits intoapache:masterfrom
MikeThomsen:NIFI-4989
Closed

NIFI-4989 Made PutMongo able to use nested lookup keys, a query param…#2560
MikeThomsen wants to merge 2 commits intoapache:masterfrom
MikeThomsen:NIFI-4989

Conversation

@MikeThomsen
Copy link
Copy Markdown
Contributor

@MikeThomsen MikeThomsen commented Mar 17, 2018

… 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:

  • Have you ensured that the full suite of tests is executed via mvn -Pcontrib-check clean install at the root nifi folder?
  • Have you written or updated unit tests to verify your changes?
  • If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under ASF 2.0?
  • If applicable, have you updated the LICENSE file, including the main LICENSE file under nifi-assembly?
  • If applicable, have you updated the NOTICE file, including the main NOTICE file found under nifi-assembly?
  • If adding new Properties, have you added .displayName in addition to .name (programmatic access) for each of the new properties?

For documentation related changes:

  • Have you ensured that format looks appropriate for the output in which it is rendered?

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.

Copy link
Copy Markdown
Contributor

@zenfenan zenfenan left a comment

Choose a reason for hiding this comment

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

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")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How about adding description here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

query = parseUpdateKey(updateKey, (Map)doc);
removeUpdateKeys(updateKey, (Map)doc);
} else {
query = Document.parse(updateQuery);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

updateTests(document);
}

@Test
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If possible, would love to see a similar test but with PutMongo.UPDATE_WITH_DOC

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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(".")) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Understood. So this is critical when doing replace i.e. the mode is set to UPDATE_WITH_DOC. Correct?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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
     }
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

TBH, the sanest way to handle such mistakes would be to stop ingestion and run some JavaScript code to update the database properly.

@MikeThomsen
Copy link
Copy Markdown
Contributor Author

@pvillard31 Thoughts on merging this?

@pvillard31
Copy link
Copy Markdown
Contributor

pvillard31 commented Mar 20, 2018

@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?

@MikeThomsen
Copy link
Copy Markdown
Contributor Author

MikeThomsen commented Mar 20, 2018

I think that works.

Copy link
Copy Markdown
Contributor

@zenfenan zenfenan left a comment

Choose a reason for hiding this comment

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

Looks good to me. Thanks @MikeThomsen

@MikeThomsen
Copy link
Copy Markdown
Contributor Author

@pvillard31 Can you merge your change so we can close this out?

@pvillard31
Copy link
Copy Markdown
Contributor

Done, merged to master, thanks @MikeThomsen @zenfenan

@asfgit asfgit closed this in 66590b7 Mar 23, 2018
@MikeThomsen MikeThomsen deleted the NIFI-4989 branch August 14, 2024 21:14
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.

3 participants