• Stars
    star
    102
  • Rank 335,584 (Top 7 %)
  • Language
    Crystal
  • License
    MIT License
  • Created almost 9 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Radix Tree implementation for Crystal

Radix Tree

Radix tree implementation for Crystal language

CI Latest Release

Installation

Add this to your application's shard.yml:

dependencies:
  radix:
    github: luislavena/radix

Usage

Building Trees

You can associate a payload with each path added to the tree:

require "radix"

tree = Radix::Tree(Symbol).new
tree.add "/products", :products
tree.add "/products/featured", :featured

result = tree.find "/products/featured"

if result.found?
  puts result.payload # => :featured
end

The types allowed for payload are defined on Tree definition:

tree = Radix::Tree(Symbol).new

# Good, since Symbol is allowed as payload
tree.add "/", :root

# Compilation error, Int32 is not allowed
tree.add "/meaning-of-life", 42

Can combine multiple types if needed:

tree = Radix::Tree(Int32 | String | Symbol).new

tree.add "/", :root
tree.add "/meaning-of-life", 42
tree.add "/hello", "world"

Lookup and placeholders

You can also extract values from placeholders (as named segments or globbing):

tree.add "/products/:id", :product

result = tree.find "/products/1234"

if result.found?
  puts result.params["id"]? # => "1234"
end

Please see Radix::Tree#add documentation for more usage examples.

Caveats

Pretty much all Radix implementations have their limitations and this project is no exception.

When designing and adding paths to a Tree, please consider that two different named parameters cannot share the same level:

tree.add "/", :root
tree.add "/:post", :post
tree.add "/:category/:post", :category_post # => Radix::Tree::SharedKeyError

This is because different named parameters at the same level will result in incorrect params when lookup is performed, and sometimes the value for post or category parameters will not be stored as expected.

To avoid this issue, usage of explicit keys that differentiate each path is recommended.

For example, following a good SEO practice will be consider /:post as absolute permalink for the post and have a list of categories which links to the permalinks of the posts under that category:

tree.add "/", :root
tree.add "/:post", :post                    # this is post permalink
tree.add "/categories", :categories         # list of categories
tree.add "/categories/:category", :category # listing of posts under each category

Implementation

This project has been inspired and adapted from julienschmidt/httprouter and spriet2000/vertx-http-router Go and Java implementations, respectively.

Changes to logic and optimizations have been made to take advantage of Crystal's features.

Contributing

  1. Fork it ( https://github.com/luislavena/radix/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

More Repositories

1

bench-micro

Benchmark some Ruby web microframeworks, just for fun
Ruby
244
star
2

gem-compiler

A RubyGems plugin that generates binary gems
Ruby
153
star
3

mysql-gem

MySQL/Ruby Bindings, wrapped as Gem with improved cross-platform support
C
53
star
4

brooklyn

Brooklyn - Small web tool on top of Rack
Ruby
23
star
5

win32console

DEPRECATED: Mirror of Win32::Console Gem project with improved MinGW support
Ruby
22
star
6

service_wrapper

Wrap any command-line tool as Windows service (WiP)
Visual Basic
21
star
7

beryl

Action-focused HTTP routing library for Crystal
Crystal
20
star
8

homelab-headscale

Making it easy (for me) to deploy a production-ready Headscale setup to Fly.io
Dockerfile
17
star
9

hydrofoil-crystal

Opinionated, Alpine-based development container for Crystal
Dockerfile
12
star
10

mongrel_service

Mongrel::Service
Visual Basic
12
star
11

binfiles

Personal scripts Tools
Visual Basic
11
star
12

drift

SQL-driven schema migration tool and library for Crystal
Crystal
10
star
13

crystal-state_machine

State Machine for Crystal
Crystal
9
star
14

fenix

OBSOLETE: Rebirth and renewal of Ruby (see README)
Ruby
6
star
15

test-ruby-c-extension

Test ground of rake-compiler extension compilation. Useful for reporting bugs during cross-compilation issues
Ruby
6
star
16

template-laravel-twill

An empty Laravel + Twill project template using Docker and Docker compose for local development
PHP
6
star
17

nekro

Ruby to Neko (playground)
Ruby
5
star
18

testly

Testly is a simple, minimal testing library aimed to programmers using FreeBASIC to implement TDD development techniques
Visual Basic
5
star
19

magic-haversack

Facilitate Crystal cross-compilation
Shell
5
star
20

learn-chef

Explore ideas and try to understand how Chef works
Ruby
4
star
21

simple_logger

SimpleLogger is a simple, minimal logger library for programmers using FreeBASIC inspired in ruby logger or log4cpp.
4
star
22

learn-ansible

Explore some playbooks organization with Ansible
Shell
4
star
23

exp-crystal-hello-binary

๐Ÿงช Compile and package static binaries with Crystal
Crystal
4
star
24

hydrofoil-php

Opinionated, PHP environment tailored for development
Dockerfile
3
star
25

isolate-lockdown

Lockdown your isolated gems, give them speed.
Ruby
3
star
26

simple-bench-ruby-io

Provide a simple test ground to benchmark Ruby require bottlenecks
Ruby
2
star
27

sinatra-hello

Testing Cloud deployment with Sinatra
Ruby
2
star
28

experiments

Place to put all those little experiments that are too small for their own repository.
C
2
star
29

servicefb

ServiceFB simplify programmers work when creating NT Services using FreeBASIC as programming language. It also encapsulate most of the Win32 API and provide a clean event-driven like interface.
2
star
30

autotest-snarl

Clean and stolen easy integration of Net::Snarl and autotest
Ruby
1
star
31

autobuild-rubies

Automated Ruby builds using Travis-CI
Shell
1
star
32

test-crystal-cross-compiling

A container image that combines Zig toolchain and packages for different platforms to facilitate compiling Crystal apps
Dockerfile
1
star
33

test-unicode

Sample files for testing unicode capabilities of Windows consoles
Ruby
1
star
34

mini_service

Create Windows Services, and just that
Ruby
1
star
35

net-snarl

Basic Snarl SNP implementation in Ruby
Ruby
1
star
36

test-crystal-sqlite3-db

Repo showcasing multi-thread issues with crystal-sqlite3 (crystal-lang/crystal-sqlite3#87)
Crystal
1
star
37

dockerfiles

Experimental Docker containers for development (DEPRECATED)
Shell
1
star
38

crystal-xbuild-container

Container image to ease cross-compilation of Crystal applications
Dockerfile
1
star
39

greek-steam

Debian-based PHP container image for production usage, tailored for Laravel Vapor & Twill CMS
Dockerfile
1
star