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

Makes node prioritization insertion-independent #19

Merged
merged 1 commit into from
Feb 4, 2017

Conversation

luislavena
Copy link
Owner

@luislavena luislavena commented Feb 4, 2017

The order in which nodes were inserted into a tree might produce failures in lookup mechanism.

tree = Radix::Tree(Symbol).new
tree.add "/one/:id", :one
tree.add "/one-longer/:id", :two

result = tree.find "/one-longer/10"

# expected `true`
result.found? # => false

In above example, reversing the order of insertion solved the issue, but exposed the naive sorting/prioritization mechanism used.

This change improves that by properly identifying the kind of node being evaluated and compared against others of the same kind.

It is now possible to know in advance if a node contains an special condition (named parameter or globbing) or is a normal one:

node = Radix::Node(Nil).new("a")
node.normal? # => true

node = Radix::Node(Nil).new(":query")
node.named? # => true

node = Radix::Node(Nil).new("*filepath")
node.glob? # => true

Which helps out with prioritization of nodes:

  • A normal node ranks higher than a named one
  • A named node ranks higher than a glob one
  • On two of same kind, ranks are based on priority

With this change in place, above example works as expected:

tree = Radix::Tree(Symbol).new
tree.add "/one/:id", :one
tree.add "/one-longer/:id", :two

result = tree.find "/one-longer/10"

result.found? # => true

Fixes #18

# * A named parameter key will receive priority above catch all (`-1`)
# * Any other type of key will receive priority based on its size.
# This value will be directly associated to the key size up until a
# special elements are found.
Copy link
Owner Author

Choose a reason for hiding this comment

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

is found

The order in which nodes were inserted into a tree might produce
failures in lookup mechanism.

    tree = Radix::Tree(Symbol).new
    tree.add "/one/:id", :one
    tree.add "/one-longer/:id", :two

    result = tree.find "/one-longer/10"

    # expected `true`
    result.found? # => false

In above example, reversing the order of insertion solved the issue,
but exposed the naive sorting/prioritization mechanism used.

This change improves that by properly identifying the kind of node
being evaluated and compared against others of the same kind.

It is now possible to know in advance if a node contains an special
condition (named parameter or globbing) or is a normal one:

    node = Radix::Node(Nil).new("a")
    node.normal? # => true

    node = Radix::Node(Nil).new(":query")
    node.named? # => true

    node = Radix::Node(Nil).new("*filepath")
    node.glob? # => true

Which helps out with prioritization of nodes:

- A normal node ranks higher than a named one
- A named node ranks higher than a glob one
- On two of same kind, ranks are based on priority

With this change in place, above example works as expected:

    tree = Radix::Tree(Symbol).new
    tree.add "/one/:id", :one
    tree.add "/one-longer/:id", :two

    result = tree.find "/one-longer/10"

    result.found? # => true

Fixes #18
@luislavena luislavena force-pushed the improve-node-sorting branch from faab80c to 1764332 Compare February 4, 2017 15:36
@luislavena luislavena merged commit 68e21bc into master Feb 4, 2017
@luislavena luislavena deleted the improve-node-sorting branch February 20, 2017 20:46
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.

1 participant