-
Notifications
You must be signed in to change notification settings - Fork 16
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
Conversation
UpdateOperator.INC, | ||
(ObjectNode) objectMapper.readTree("{\"root.x\":-7, \"root.inc\":-3}"), | ||
UpdateOperator.MUL, | ||
(ObjectNode) objectMapper.readTree("{\"root.mul\":3, \"root.x\":2}")))); |
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.
Just to confirm, would updateOne({}, { $inc: { 'root.x': 2 }, $mul: { 'root.mul': 3 } })
still work?
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.
Yes. I should add a test case to verify.
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.
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" |
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.
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.
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.
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.
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.
@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.
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.
@vkarpov15 Ah. Ok, let me think this over. Good catch.
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.
I can make sorting work with some more work. Two obvious extensions:
- Use (implement) modified String comparison where
.
is sorted likenull
byte (value 0) - Sort split segments (which Locator already has)
both would work; I may go with (2).
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.
Added segment-aware sorting, testing to verify functioning.
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.
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.
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.
@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.
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