radix_route_matcher
A high-performance route matching library based on Radix Tree (compressed trie) data structure.
Features
- High Performance: Based on Redis's
raximplementation in C - Multiple Match Modes: Exact match, longest prefix match, and iterator-based matching
- Safe Rust API: High-level safe wrapper around C implementation
- C ABI Compatible: Can be used from C/C++ or other FFI-capable languages
- Unicode Support: Full UTF-8 support including Chinese characters
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Usage
Basic Usage
use RadixTree;
let mut tree = new.expect;
// Insert routes
tree.insert.unwrap;
tree.insert.unwrap;
tree.insert.unwrap;
// Create an iterator for prefix matching operations
let iter = tree.create_iter.unwrap;
// Exact match
assert_eq!;
// Longest prefix match
assert_eq!;
// Get all matching prefixes
let matches = tree.find_all_prefixes;
assert_eq!; // ["/api/users", "/api"]
Iterator-Style Matching
use RadixTree;
let mut tree = new.unwrap;
tree.insert.unwrap;
tree.insert.unwrap;
tree.insert.unwrap;
let iter = tree.create_iter.unwrap;
let path = "/api/v1/users";
if tree.search
Performance
- Insert: O(k) where k is the key length (~447ns per route)
- Query: O(k) where k is the key length (~226ns per query)
- Space: Efficient prefix compression, shared prefixes stored once
API Reference
RadixTree
| Method | Description |
|---|---|
new() |
Creates a new empty Radix Tree |
insert(path, idx) |
Inserts a path with an associated index |
find_exact(path) |
Finds the exact match for a path |
remove(path) |
Removes a path from the tree |
create_iter() |
Creates a new iterator for prefix operations |
longest_prefix(iter, path) |
Finds the longest prefix match |
search(iter, path) |
Initializes iterator for prefix searching |
next_prefix(iter, path) |
Gets the next prefix match |
find_all_prefixes(iter, path) |
Returns all matching prefixes |
C API
This library also exports a C-compatible API for use from other languages:
void* ;
int ;
int ;
void* ;
int ;
void* ;
void* ;
int ;
License
This project is licensed under the MIT License - see the LICENSE file for details.
This project includes code from the Redis rax implementation by Salvatore Sanfilippo, which is also under a BSD-style license.
Acknowledgments
- Redis rax - The underlying radix tree implementation