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

Add checks to make sure Update Operators do not use conflicting/overlapping paths #267

Merged
merged 9 commits into from
Mar 16, 2023

Conversation

tatu-at-datastax
Copy link
Contributor

What this PR does:

Improves checking of dotted paths used with update operator to ensure overlapping paths are not allowed (same as with other mongoose backends).

Which issue(s) this PR fixes:
Fixes #232

Checklist

  • Changes manually tested
  • Automated Tests added/updated
  • Documentation added/updated
  • CLA Signed: DataStax CLA

@tatu-at-datastax tatu-at-datastax self-assigned this Mar 16, 2023
UpdateOperator.INC,
(ObjectNode) objectMapper.readTree("{\"root.x\":-7, \"root.inc\":-3}"),
UpdateOperator.MUL,
(ObjectNode) objectMapper.readTree("{\"root.mul\":3, \"root.x\":2}"))));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just to confirm, would updateOne({}, { $inc: { 'root.x': 2 }, $mul: { 'root.mul': 3 } }) still work?

Copy link
Contributor Author

@tatu-at-datastax tatu-at-datastax Mar 16, 2023

Choose a reason for hiding this comment

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

Yes. I should add a test case to verify.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added another IT that checks nested.old ($unset) and nested.new ($set) (same logic between all operators).

// Second: check for sub-paths -- efficiently done by using alphabetic-sorted paths
// and comparing adjacent ones; for each pair see if later one is longer one and
// starts with dot.
// For example: "path.x" vs "path.x.some.more" we notice latter has suffix ".some.more"
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't remember off the top of my head which characters we allow in field names, but I don't think this will work if we allow spaces, $, or + in field names. Because "path.x$foo" comes before "path.x.foo" in lexicographical order.

Is there any way we can use a different approach here? I'd rather not add code that blocks us from supporting "$" in field names.

Copy link
Contributor Author

@tatu-at-datastax tatu-at-datastax Mar 16, 2023

Choose a reason for hiding this comment

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

I am not sure why dollar sign or other characters would change logic here: . is separator character and nothing else? Or are you saying paths path.x$foo and path.x.foo conflict? (I wouldn't think they do if "$" is part of path segment?)

That is, sorting for purpose of finding parent/child paths seems to work as far as I can see.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@tatu-at-datastax the issue is your alphabetical sorting assumption. The assumption here is that path.x.some.more would immediately follow path.x in the sort order, unless there's some other conflicting path. But in ASCII, $ is less than .. So conventional string sorting would put fields with dollar paths before conflicting paths. Below is an example in JavaScript

> ['path.x', 'path.x.some.more', 'path.x$foo'].sort()
[ 'path.x', 'path.x$foo', 'path.x.some.more' ]

Not sure if that applies here, just figured I'd call that out.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@vkarpov15 Ah. Ok, let me think this over. Good catch.

Copy link
Contributor Author

@tatu-at-datastax tatu-at-datastax Mar 16, 2023

Choose a reason for hiding this comment

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

I can make sorting work with some more work. Two obvious extensions:

  1. Use (implement) modified String comparison where . is sorted like null byte (value 0)
  2. Sort split segments (which Locator already has)

both would work; I may go with (2).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added segment-aware sorting, testing to verify functioning.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice, thanks. It looks like the reason why this works though is because you've included a loop over path segments in the compare() method - any worries about this being potentially slow? You'll have to iterate over path segments O(nlogn) times, creating a map of all parent paths will likely be faster but more memory intensive. I don't think this is a big deal either way, just food for thought.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@vkarpov15 that's a reasonable question. I guess it all depends on depth of document. My thinking was that for deeper documents most differences are at first segment(s) anyway so that it evens out, compared to String comparison. But my main consideration was, I guess, between straight String comparison (just changing sort value of .) vs segmented, where I think performance is similar. I did not consider possibility of building lookup for covered paths: in that case it does come back to both memory usage added and (more importantly), construction of new String instances to reconstruct parent paths. I guess it can go either way wrt overall performance, in the end.

Fundamentally I guess it also comes down to how many updates do we usually have: I suspect differences are not meaningful until into thousands of operations.

Since this logic is fairly isolated, I think I'll keep the question in mind and can reconsider re-implementation if we find a hotspot.

@tatu-at-datastax tatu-at-datastax marked this pull request as ready for review March 16, 2023 17:46
@tatu-at-datastax tatu-at-datastax requested a review from a team as a code owner March 16, 2023 17:46
@tatu-at-datastax tatu-at-datastax changed the title (WIP) Add checks to make sure Update Operators do not use conflicting/overlapping paths Add checks to make sure Update Operators do not use conflicting/overlapping paths Mar 16, 2023
@tatu-at-datastax tatu-at-datastax marked this pull request as draft March 16, 2023 19:38
@tatu-at-datastax tatu-at-datastax marked this pull request as ready for review March 16, 2023 21:44
@tatu-at-datastax tatu-at-datastax merged commit fe9f4d2 into main Mar 16, 2023
@tatu-at-datastax tatu-at-datastax deleted the tatu/232-overlapping-path-check branch March 16, 2023 21:49
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.

Check that different update operators do not target same field (conflict)
3 participants