RadixTarget
RadixTarget is a performant radix implementation designed for quick lookups of IP addresses/networks and DNS hostnames.
RadixTarget is:
- Written in Rust with Python bindings
- Capable of ~1,000,000 lookups per second regardless of database size
- 100% test coverage
- Available as both a Rust crate and Python package
- Used by:
Python
Installation
Usage
=
# IPv4
# IPv4Network("192.168.1.0/24")
# None
# IPv6
# IPv6Network("dead::/64")
# None
# DNS
# "net"
# "net"
# "www.example.com"
# "test.www.example.com"
# None
# Custom data nodes
# "custom_data"
# Insertion returns unique identifier
=
assert ==
# Store custom data with any entry
=
# result contains your custom data: {"tags": ["production"], "priority": 1}
API Differences: Python vs Rust
Both APIs provide an insert() method that returns a unique string identifier for each entry (a normalized version of the hostname or IP address). However, the Python API includes additional functionality:
- Rust:
insert()returns a string that uniquely identifies the insertion - Python:
insert()also returns this unique identifier string, but additionally supports adata=parameter that allows you to store custom data of any Python type alongside the entry. When you later callget(), it returns your custom data instead of just the matched target string.
This makes the Python API more flexible for applications that need to associate metadata with targets, while the Rust API focuses on pure performance for lookups.
Rust
Installation
Usage
use ;
use HashSet;
// Create a new RadixTarget
let mut rt = new;
// IPv4 networks and addresses
rt.insert;
assert_eq!;
assert_eq!;
// IPv6 networks and addresses
rt.insert;
assert_eq!;
assert_eq!;
// DNS hostnames
rt.insert;
rt.insert;
assert_eq!;
assert_eq!;
// Check if target contains a value
assert!;
assert!;
assert!;
// Get all hosts
let hosts: = rt.hosts;
println!;
// Delete targets
assert!;
assert!; // false - already deleted
// Utility operations
println!;
println!;
// Prune dead nodes (returns number of pruned nodes)
let pruned_count = rt.prune;
// Defragment overlapping networks (returns (cleaned, new) hosts)
let = rt.defrag;
Scope Modes
RadixTarget supports different scope modes for DNS matching:
use ;
// Normal mode: standard radix tree behavior (default)
let mut rt_normal = new;
rt_normal.insert;
assert_eq!;
// Strict mode: exact matching only
let mut rt_strict = new;
rt_strict.insert;
assert_eq!;
assert_eq!; // No subdomain matching
// ACL mode: Same behavior as normal, but keeps only the highest parent subnet for efficiency
let mut rt_acl = new;
rt_acl.insert;
rt_acl.insert;
// Least specific match is returned instead of most specific
assert_eq!;
Initialization with Hosts
use ;
// Initialize with existing hosts
let hosts = vec!;
let rt = new;
assert!;
assert!;
assert!;