-
Notifications
You must be signed in to change notification settings - Fork 65
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
Changes from 2 commits
5598edf
d009ffb
bffc5fd
ae428d8
2fe8b68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
s := strings.TrimPrefix(key.String(), m.Prefix.String()) | ||
k := ds.NewKey(s) | ||
return m.Datastore, m.Prefix, k | ||
|
@@ -159,21 +159,21 @@ func (h *querySet) next() (query.Result, bool) { | |
// /aoe/ not matching | ||
func (d *Datastore) lookupAll(key ds.Key) (dst []ds.Datastore, mountpoint, rest []ds.Key) { | ||
for _, m := range d.mounts { | ||
p := m.Prefix.String() | ||
if len(p) > 1 { | ||
p = p + "/" | ||
} | ||
|
||
if strings.HasPrefix(p, key.String()) { | ||
if m.Prefix.IsDescendantOf(key) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm now using proper key functions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are the comments above this function still accurate? What should happen with There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
dst = append(dst, m.Datastore) | ||
mountpoint = append(mountpoint, m.Prefix) | ||
rest = append(rest, ds.NewKey("/")) | ||
} else if strings.HasPrefix(key.String(), p) { | ||
} else if m.Prefix.Equal(key) || m.Prefix.IsAncestorOf(key) { | ||
r := strings.TrimPrefix(key.String(), m.Prefix.String()) | ||
|
||
dst = append(dst, m.Datastore) | ||
mountpoint = append(mountpoint, m.Prefix) | ||
rest = append(rest, ds.NewKey(r)) | ||
|
||
// 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: this may have been impacting the performance of GC. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, |
||
} | ||
} | ||
return dst, mountpoint, rest | ||
|
@@ -191,15 +191,11 @@ func (d *Datastore) Put(key ds.Key, value []byte) error { | |
func (d *Datastore) Sync(prefix ds.Key) error { | ||
// Sync all mount points below the prefix | ||
// Sync the mount point right at (or above) the prefix | ||
dstores, mountPts, rest := d.lookupAll(prefix) | ||
dstores, _, rest := d.lookupAll(prefix) | ||
for i, suffix := range rest { | ||
if err := dstores[i].Sync(suffix); err != nil { | ||
return err | ||
} | ||
|
||
if mountPts[i].Equal(prefix) || suffix.String() != "/" { | ||
return nil | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fix in |
||
} | ||
|
||
return nil | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package query | ||
|
||
import ( | ||
"path" | ||
|
||
goprocess "github.com/jbenet/goprocess" | ||
) | ||
|
||
|
@@ -116,7 +118,23 @@ func NaiveOrder(qr Results, orders ...Order) Results { | |
|
||
func NaiveQueryApply(q Query, qr Results) Results { | ||
if q.Prefix != "" { | ||
qr = NaiveFilter(qr, FilterKeyPrefix{q.Prefix}) | ||
// Clean the prefix as a key and append / so a prefix of /bar | ||
// only finds /bar/baz, not /barbaz. | ||
prefix := q.Prefix | ||
if len(prefix) == 0 { | ||
prefix = "/" | ||
} else { | ||
if prefix[0] != '/' { | ||
prefix = "/" + prefix | ||
} | ||
prefix = path.Clean(prefix) | ||
} | ||
// If the prefix isn't "/", end it in a "/" so we only find keys | ||
// _under_ the prefix. | ||
if prefix != "/" { | ||
prefix += "/" | ||
} | ||
qr = NaiveFilter(qr, FilterKeyPrefix{prefix}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We weren't cleaning/fixing keys. |
||
} | ||
for _, f := range q.Filters { | ||
qr = NaiveFilter(qr, f) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ var sampleKeys = []string{ | |
} | ||
|
||
func testResults(t *testing.T, res Results, expect []string) { | ||
t.Helper() | ||
|
||
actualE, err := res.Rest() | ||
if err != nil { | ||
t.Fatal(err) | ||
|
@@ -37,6 +39,7 @@ func testResults(t *testing.T, res Results, expect []string) { | |
|
||
func TestNaiveQueryApply(t *testing.T) { | ||
testNaiveQueryApply := func(t *testing.T, query Query, keys []string, expect []string) { | ||
t.Helper() | ||
e := make([]Entry, len(keys)) | ||
for i, k := range keys { | ||
e[i] = Entry{Key: k} | ||
|
@@ -71,9 +74,6 @@ func TestNaiveQueryApply(t *testing.T) { | |
testNaiveQueryApply(t, q, sampleKeys, []string{ | ||
"/ab/c", | ||
"/ab/cd", | ||
"/abce", | ||
"/abcf", | ||
"/ab", | ||
}) | ||
|
||
q = Query{Orders: []Order{OrderByKeyDescending{}}} | ||
|
@@ -88,14 +88,12 @@ func TestNaiveQueryApply(t *testing.T) { | |
|
||
q = Query{ | ||
Limit: 3, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Limit isn't being tested anymore since we only have one result There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added some more keys to improve the tests. |
||
Offset: 2, | ||
Offset: 1, | ||
Prefix: "/ab", | ||
Orders: []Order{OrderByKey{}}, | ||
} | ||
testNaiveQueryApply(t, q, sampleKeys, []string{ | ||
"/ab/cd", | ||
"/abce", | ||
"/abcf", | ||
}) | ||
} | ||
|
||
|
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.
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?