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

Only count a key as an ancestor if there is a separator #141

Merged
merged 5 commits into from
Feb 11, 2020

Conversation

Stebalien
Copy link
Member

@Stebalien Stebalien commented Dec 3, 2019

Also make sure to clean and normalize keys before using them as prefixes.

BREAKING CHANGES:

  • myds.Query(Query{Prefix:"/foo"}) will no longer match "/foobar" (or even "/foo"). This is usually what the user expects, we had a tendency to normalize "/foo/" to "/foo" (when we clean keys), and many datastores can't efficiently search for prefixes that aren't on path-boundaries anyways.
  • Given a mount datastore with mounts ["/foo", "/"], myds.Put("/foo", "bar") will put the value to the datastore mounted at "/", not "/foo", as the key "/" and "" usually doesn't make sense.

While technically breaking, these changes are much more likely to fix bugs than they are to break things.

key.go Outdated Show resolved Hide resolved
}

if strings.HasPrefix(p, key.String()) {
if m.Prefix.IsDescendantOf(key) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm now using proper key functions.

Copy link
Contributor

Choose a reason for hiding this comment

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

Are the comments above this function still accurate? What should happen with /ao/e/ here?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not really sure what that comment is trying to say (what's A, B?). It would return the /ao/e/ mount point, and / as "rest". I've fixed the documentation and added a test.

// We've found an ancestor (or equal) key. We might have
// more general datastores, but they won't contain keys
// with this prefix so there's no point in searching them.
break
Copy link
Member Author

Choose a reason for hiding this comment

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

This is #140 (comment). Thinking back on this, I think it was a bug.

Given the mounts ["/foo", "/"], lookupAll("/foo/bar") would return both (there's now a test for this). However, it should have only returned "/foo" because no keys under "/foo/bar" would end up in "/".

Copy link
Member Author

Choose a reason for hiding this comment

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

Note: this may have been impacting the performance of GC.

Copy link
Contributor

Choose a reason for hiding this comment

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

seems reasonable, this is a contract change but if it increases performance/reliability and no one is utilizing the obscure case then we may as well go for it.

Are we pretty confident that we're not doing queries for things like /blocks even though /blocks is mounted over /?

Copy link
Member Author

Choose a reason for hiding this comment

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

We are, but that should be fine. That is, we're querying for blocks in /blocks, not for blocks in /. As of this change, lookupAll("/blocks"), should return only the datastore mounted at /blocks.


if mountPts[i].Equal(prefix) || suffix.String() != "/" {
return nil
}
Copy link
Member Author

Choose a reason for hiding this comment

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

The fix in lookupAll should make this unnecessary.

if prefix != "/" {
prefix += "/"
}
qr = NaiveFilter(qr, FilterKeyPrefix{prefix})
Copy link
Member Author

Choose a reason for hiding this comment

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

We weren't cleaning/fixing keys.

…arator

Also make sure to clean and normalize keys before using them as prefixes.

BREAKING CHANGES:

* `myds.Query(Query{Prefix:"/foo"})` will no longer match "/foobar" (or even
  "/foo"). This is usually what the user expects, we had a tendency to normalize
  "/foo/" to "/foo" (when we clean keys), and many datastores can't efficiently
  search for prefixes that aren't on path-boundaries anyways.
* Given a mount datastore with mounts `["/foo", "/"]`, `myds.Put("/foo", "bar")`
  will put the value to the datastore mounted at "/", not "/foo", as the key "/"
  and "" usually doesn't make sense.

While technically breaking, these changes are much more likely to fix bugs than
they are to break things.
@Stebalien
Copy link
Member Author

myds.Query(Query{Prefix:"/foo"}) will no longer match "/foobar" (or even "/foo"). This is usually what the user expects, we had a tendency to normalize "/foo/" to "/foo" (when we clean keys), and many datastores can't efficiently search for prefixes that aren't on path-boundaries anyways.

Note: if we don't do this, we need to remember to fix the key transform datastore.

@Stebalien
Copy link
Member Author

@whyrusleeping says

uhhhh, i think normalization seems fine to me

@whyrusleeping
Copy link
Member

Yeah, seems fine to me. We can add more docs around our key model to compensate for the potential weirdness

Copy link
Contributor

@aschmahmann aschmahmann left a comment

Choose a reason for hiding this comment

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

Overall LGTM. Left a few comments and requests for clarification

@@ -88,14 +88,12 @@ func TestNaiveQueryApply(t *testing.T) {

q = Query{
Limit: 3,
Copy link
Contributor

Choose a reason for hiding this comment

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

Limit isn't being tested anymore since we only have one result

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added some more keys to improve the tests.

}

if strings.HasPrefix(p, key.String()) {
if m.Prefix.IsDescendantOf(key) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Are the comments above this function still accurate? What should happen with /ao/e/ here?

// We've found an ancestor (or equal) key. We might have
// more general datastores, but they won't contain keys
// with this prefix so there's no point in searching them.
break
Copy link
Contributor

Choose a reason for hiding this comment

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

seems reasonable, this is a contract change but if it increases performance/reliability and no one is utilizing the obscure case then we may as well go for it.

Are we pretty confident that we're not doing queries for things like /blocks even though /blocks is mounted over /?

@@ -39,7 +39,7 @@ var _ ds.Datastore = (*Datastore)(nil)

func (d *Datastore) lookup(key ds.Key) (ds.Datastore, ds.Key, ds.Key) {
for _, m := range d.mounts {
if m.Prefix.Equal(key) || m.Prefix.IsAncestorOf(key) {
if m.Prefix.IsAncestorOf(key) {
Copy link
Contributor

Choose a reason for hiding this comment

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

To clarify, is the intent here that if I've mounted ["/", "/foo"] and then I do a put to /foo that the key will end up in the / mount instead of the /foo mount?

Is there any reason to support this or would we be safer just throwing an error instead?

@Stebalien Stebalien merged commit 83f9a95 into master Feb 11, 2020
@Stebalien Stebalien deleted the fix/ancestor-sep branch February 11, 2020 00:14
@alanshaw alanshaw requested review from achingbrain and removed request for alanshaw February 24, 2020 09:29
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