Skip to main content

radixip/
lib.rs

1//! RadixIP - High-performance IP subnet caching engine
2//!
3//! This library provides three trees, a binary trie, a radix tree and Adaptive Radix Tree (ART),for
4//! longest-prefix matching of IP addresses against CIDR blocks.
5
6pub mod art;
7pub mod atomic;
8pub mod cache;
9pub mod config;
10pub mod engine;
11pub mod engine_art;
12pub mod errors;
13pub mod hybrid;
14pub mod lpm;
15pub mod node;
16pub mod traits;
17pub mod tree;
18pub mod types;
19
20#[cfg(feature = "redis")]
21pub use radixip_cache as redis;
22
23#[cfg(feature = "ffi")]
24pub mod ffi;
25
26#[cfg(feature = "pyo3")]
27pub mod python;
28
29#[cfg(feature = "node")]
30pub mod nodejs;
31
32pub use engine::{EngineWrapper, ShardedEngine, StandardEngine};
33pub use engine_art::ARTEngineAdapter;
34pub use errors::{RadixError, Result};
35pub use hybrid::HybridEngine;
36pub use lpm::LPM;
37// Uncompressed node types
38pub use node::{AtomicTrieNode, LockFreeTrieNode, NodeWrapper, NormalTrieNode, PaddedTrieNode};
39pub use node::NodeBuilder;
40// Compressed (Patricia/Radix) node types
41pub use node::{
42    AtomicRadixNode, LockFreeRadixNode, NormalRadixNode, PaddedRadixNode,
43};
44pub use types::{Metadata, SubnetRule};
45
46pub use cache::{CacheConfig, CachedEngine};
47pub use config::RadixConfig;
48pub use traits::*;
49
50use std::sync::Arc;
51
52/// Create a new RadixIP engine with the given configuration
53pub async fn new(config: RadixConfig) -> Box<dyn RadixEngine> {
54    if config.enable_split_plane {
55        return Box::new(
56            HybridEngine::new(&config)
57                .await
58                .expect("Failed to initialize HybridEngine"),
59        );
60    }
61
62    let engine = EngineWrapper::new(
63        config.engine_variant,
64        config.node_variant,
65        config.read_compressed,
66        config.num_shards,
67    );
68
69    if config.cache_enabled {
70        let cache_config = CacheConfig {
71            max_entries: config.cache_max_entries,
72            ttl_seconds: config.cache_ttl_seconds,
73        };
74        #[cfg(feature = "redis")]
75        {
76            let redis = if let Some(redis_config) = config.redis.clone() {
77                Some(
78                    radixip_cache::RedisClient::new(redis_config)
79                        .await
80                        .expect("Failed to initialize Redis cache client"),
81                )
82            } else {
83                None
84            };
85
86            Box::new(CachedEngine::new(Arc::new(engine), cache_config, redis))
87        }
88        #[cfg(not(feature = "redis"))]
89        {
90            Box::new(CachedEngine::new(Arc::new(engine), cache_config))
91        }
92    } else {
93        Box::new(engine)
94    }
95}
96
97/// Create a high-performance RadixIP engine
98pub async fn new_high_performance() -> Box<dyn RadixEngine> {
99    new(RadixConfig::high_performance()).await
100}
101
102#[cfg(test)]
103mod tests {
104    use super::*;
105
106    #[test]
107    fn redis_config_builder_tracks_cache_and_channel() {
108        let mut config = RadixConfig::new();
109        config.cache_enabled = true;
110        config.redis_channel = "radixip:test".to_string();
111        config.redis = Some(radixip_cache::RedisConfig {
112            url: "redis://127.0.0.1:6379".to_string(),
113            pool_size: 4,
114            connect_timeout: std::time::Duration::from_secs(2),
115            max_retries: 1,
116        });
117
118        assert!(config.cache_enabled);
119        assert_eq!(config.redis_channel, "radixip:test");
120        assert!(config.redis.is_some());
121    }
122}
123
124/// Create a memory-efficient RadixIP engine
125pub async fn new_memory_efficient() -> Box<dyn RadixEngine> {
126    new(RadixConfig::memory_efficient()).await
127}
128
129/// Create a balanced RadixIP engine
130pub async fn new_balanced() -> Box<dyn RadixEngine> {
131    new(RadixConfig::balanced()).await
132}
133
134/// Library version
135pub const VERSION: &str = env!("CARGO_PKG_VERSION");