runar_node 0.1.0

Runar Node implementation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
use crate::routing::TopicPath;
use std::collections::HashMap;

/// Helper function to check if a segment is a template parameter
fn is_template_param(segment: &str) -> bool {
    segment.starts_with("{") && segment.ends_with("}")
}

/// Helper function to extract parameter name from a template segment
fn extract_param_name(segment: &str) -> String {
    // Remove the { and } from the segment
    segment[1..segment.len() - 1].to_string()
}

/// Result type that includes both the handler and any extracted parameters
#[derive(Clone)]
pub struct PathTrieMatch<T: Clone> {
    /// The handler that matched the path
    pub content: T,
    /// Parameters extracted from template segments
    pub params: HashMap<String, String>,
}

/// Optimized path trie structure for faster matching
///
/// INTENTION: Provide a hierarchical data structure that efficiently matches paths
/// by walking down the segments one at a time. This allows for fast lookup of handlers
/// registered for exact paths, template patterns, and wildcard patterns.
#[derive(Clone)]
pub struct PathTrie<T: Clone> {
    /// Content for this exact path
    content: Vec<T>,

    /// Child nodes for literal segments
    children: HashMap<String, PathTrie<T>>,

    /// Child node for single wildcard (*)
    wildcard_child: Option<Box<PathTrie<T>>>,

    /// Child node for template parameters ({param})
    template_child: Option<Box<PathTrie<T>>>,

    /// Template parameter name at this level (if this is a template node)
    template_param_name: Option<String>,

    /// content for multi-wildcard (>) at this level
    multi_wildcard: Vec<T>,

    /// Network-specific tries - the top level trie is keyed by network_id
    networks: HashMap<String, PathTrie<T>>,

    /// Total count of all values in this trie (cached for efficiency)
    count: usize,
}

impl<T: Clone> Default for PathTrie<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: Clone> PathTrie<T> {
    /// Create a new empty path trie
    pub fn new() -> Self {
        PathTrie {
            content: Vec::new(),
            children: HashMap::new(),
            wildcard_child: None,
            template_child: None,
            template_param_name: None,
            multi_wildcard: Vec::new(),
            networks: HashMap::new(),
            count: 0,
        }
    }

    /// Add a topic path and its handlers to the trie – replaces any existing list stored at that leaf
    pub fn set_values(&mut self, topic: TopicPath, content_list: Vec<T>) {
        let network_id = topic.network_id();
        let network_trie = self.networks.entry(network_id.to_string()).or_default();
        network_trie.set_values_internal(&topic.get_segments(), 0, content_list);
    }

    /// Add a single handler for a topic path – replaces previous list with a single-element list
    pub fn set_value(&mut self, topic: TopicPath, content: T) {
        self.set_values(topic, vec![content]);
    }

    /// Add handlers for a vector of topic paths
    pub fn add_batch_values(&mut self, topics: Vec<TopicPath>, contents: Vec<T>) {
        for topic in topics {
            self.set_values(topic, contents.clone());
        }
    }

    pub fn remove_values(&mut self, topic: &TopicPath) {
        let network_id = topic.network_id();

        // Get or create network-specific trie
        let network_trie = self.networks.entry(network_id.to_string()).or_default();

        // Remove from the network-specific trie
        network_trie.remove_values_internal(&topic.get_segments(), 0);
    }

    /// Internal recursive implementation of remove
    fn remove_values_internal(&mut self, segments: &[String], index: usize) {
        if index >= segments.len() {
            // We've reached the end of the path, remove handlers here
            let old_count = self.content.len();
            self.content.clear();
            self.count -= old_count;
            return;
        }

        let segment = &segments[index];

        if segment == "*" {
            // Single wildcard - remove from wildcard child
            if let Some(child) = &mut self.wildcard_child {
                let old_count = child.count;
                child.remove_values_internal(segments, index + 1);
                self.count -= old_count - child.count;
            }
        } else if is_template_param(segment) {
            // Template parameter - remove from template child
            if let Some(child) = &mut self.template_child {
                let old_count = child.count;
                child.remove_values_internal(segments, index + 1);
                self.count -= old_count - child.count;
            }
        } else {
            // Literal segment - remove from child
            if let Some(child) = self.children.get_mut(segment) {
                let old_count = child.count;
                child.remove_values_internal(segments, index + 1);
                self.count -= old_count - child.count;
            }
        }
    }

    /// Internal recursive implementation of add
    fn set_values_internal(&mut self, segments: &[String], index: usize, handlers: Vec<T>) {
        if index >= segments.len() {
            // We've reached the end of the path, replace handlers here
            let old_count = self.content.len();
            self.content.extend(handlers);
            self.count += self.content.len() - old_count;
            return;
        }

        let segment = &segments[index];

        if segment == ">" {
            // Multi-wildcard - adds handlers here and matches everything below
            let old_count = self.multi_wildcard.len();
            self.multi_wildcard.extend(handlers);
            self.count += self.multi_wildcard.len() - old_count;
        } else if segment == "*" {
            // Single wildcard - create/get wildcard child and continue
            if self.wildcard_child.is_none() {
                self.wildcard_child = Some(Box::new(PathTrie::new()));
            }

            if let Some(child) = &mut self.wildcard_child {
                let old_count = child.count;
                child.set_values_internal(segments, index + 1, handlers);
                self.count += child.count - old_count;
            }
        } else if is_template_param(segment) {
            // Template parameter - create/get template child and continue
            if self.template_child.is_none() {
                self.template_child = Some(Box::new(PathTrie::new()));
                // Store the parameter name
                self.template_param_name = Some(extract_param_name(segment));
            }

            if let Some(child) = &mut self.template_child {
                let old_count = child.count;
                child.set_values_internal(segments, index + 1, handlers);
                self.count += child.count - old_count;
            }
        } else {
            // Literal segment - create/get child and continue
            let child = self.children.entry(segment.clone()).or_default();
            let old_count = child.count;
            child.set_values_internal(segments, index + 1, handlers);
            self.count += child.count - old_count;
        }
    }

    /// Find all handlers that match the given topic, including any extracted parameters
    pub fn find_matches(&self, topic: &TopicPath) -> Vec<PathTrieMatch<T>> {
        // If the topic contains wildcards, use the wildcard search
        if topic.is_pattern() {
            return self.find_wildcard_matches(topic);
        }

        let network_id = topic.network_id();
        let segments = topic.get_segments();
        let mut results = Vec::new();

        // Only look in the network-specific trie
        if let Some(network_trie) = self.networks.get(&network_id.to_string()) {
            // Start with empty parameters map
            let mut params = HashMap::new();
            network_trie.find_matches_internal(&segments, 0, &mut results, &mut params);
        }

        results
    }

    /// Find all handlers that match a wildcard topic pattern
    ///
    /// INTENTION: Support searching with wildcard patterns, allowing clients to find
    /// all handlers that match a specific pattern (e.g., "serviceA/*" to find all actions
    /// for serviceA).
    pub fn find_wildcard_matches(&self, pattern: &TopicPath) -> Vec<PathTrieMatch<T>> {
        let network_id = pattern.network_id();
        let pattern_segments = pattern.get_segments();
        let mut results = Vec::new();

        // Only look in the network-specific trie
        if let Some(network_trie) = self.networks.get(&network_id.to_string()) {
            // Collect all handlers from the network trie that match the pattern
            network_trie.collect_wildcard_matches(&pattern_segments, 0, &mut results);
        }

        results
    }

    /// Recursively collect all handlers that match a wildcard pattern
    fn collect_wildcard_matches(
        &self,
        pattern_segments: &[String],
        index: usize,
        results: &mut Vec<PathTrieMatch<T>>,
    ) {
        // If we've reached the end of the pattern, add all handlers at this level
        if index >= pattern_segments.len() {
            for handler in &self.content {
                results.push(PathTrieMatch {
                    content: handler.clone(),
                    params: HashMap::new(), // No parameter extraction for wildcard searches
                });
            }
            return;
        }

        let segment = &pattern_segments[index];

        // Handle different segment types
        if segment == "*" {
            // Single wildcard - match any single segment
            // For * wildcard, we need to collect ALL handlers from this node and ALL descendants
            // This is equivalent to a multi-wildcard (>) at this level
            self.collect_all_handlers(results);
        } else if segment == ">" {
            // Multi-wildcard - match everything below this level
            // Add all handlers from this level and all children recursively
            self.collect_all_handlers(results);
        } else {
            // Literal segment - only check the matching child
            if let Some(child) = self.children.get(segment) {
                child.collect_wildcard_matches(pattern_segments, index + 1, results);
            }
        }
    }

    /// Recursively collect all handlers from this node and all its children
    fn collect_all_handlers(&self, results: &mut Vec<PathTrieMatch<T>>) {
        // Add handlers at this level
        for handler in &self.content {
            results.push(PathTrieMatch {
                content: handler.clone(),
                params: HashMap::new(), // No parameter extraction for wildcard searches
            });
        }

        // Add multi-wildcard handlers
        for handler in &self.multi_wildcard {
            results.push(PathTrieMatch {
                content: handler.clone(),
                params: HashMap::new(),
            });
        }

        // Recursively add handlers from all children
        for child in self.children.values() {
            child.collect_all_handlers(results);
        }

        // Check wildcard and template children if they exist
        if let Some(child) = &self.wildcard_child {
            child.collect_all_handlers(results);
        }

        if let Some(child) = &self.template_child {
            child.collect_all_handlers(results);
        }
    }

    /// Internal recursive implementation of find_matches
    fn find_matches_internal(
        &self,
        segments: &[String],
        index: usize,
        results: &mut Vec<PathTrieMatch<T>>,
        current_params: &mut HashMap<String, String>,
    ) {
        if index >= segments.len() {
            // We've reached the end of the path
            // Add all handlers at this level with the current parameters
            for handler in &self.content {
                results.push(PathTrieMatch {
                    content: handler.clone(),
                    params: current_params.clone(),
                });
            }
            return;
        }

        // Add any multi-wildcard handlers at this level
        for handler in &self.multi_wildcard {
            results.push(PathTrieMatch {
                content: handler.clone(),
                params: current_params.clone(),
            });
        }

        let segment = &segments[index];

        // Check literal children
        if let Some(child) = self.children.get(segment) {
            child.find_matches_internal(segments, index + 1, results, current_params);
        }

        // Check wildcard child
        if let Some(child) = &self.wildcard_child {
            child.find_matches_internal(segments, index + 1, results, current_params);
        }

        // Check template child
        if let Some(child) = &self.template_child {
            // If this is a template node, extract and store the parameter
            if let Some(param_name) = &self.template_param_name {
                // Save old value in case we need to restore it
                let old_value = current_params.get(param_name).cloned();

                // Set the parameter value from the current segment
                current_params.insert(param_name.clone(), segment.clone());

                // Continue matching with the updated parameters
                child.find_matches_internal(segments, index + 1, results, current_params);

                // Restore old value or remove the parameter
                match old_value {
                    Some(value) => {
                        current_params.insert(param_name.clone(), value);
                    }
                    None => {
                        current_params.remove(param_name);
                    }
                }
            } else {
                // No parameter name defined, just continue matching
                child.find_matches_internal(segments, index + 1, results, current_params);
            }
        }
    }

    /// For backward compatibility - get just the handlers without parameters
    pub fn find(&self, topic: &TopicPath) -> Vec<T> {
        self.find_matches(topic)
            .into_iter()
            .map(|m| m.content)
            .collect()
    }

    /// Remove handlers that match a predicate for a specific topic path
    pub fn remove_handler<F>(&mut self, topic: &TopicPath, predicate: F) -> bool
    where
        F: Fn(&T) -> bool + Copy,
    {
        let network_id = topic.network_id();

        // Only remove from the specific network trie
        if let Some(network_trie) = self.networks.get_mut(&network_id.to_string()) {
            return network_trie.remove_handler_internal(&topic.get_segments(), 0, predicate);
        }

        false
    }

    /// Internal recursive implementation of remove_handler
    fn remove_handler_internal<F>(
        &mut self,
        segments: &[String],
        index: usize,
        predicate: F,
    ) -> bool
    where
        F: Fn(&T) -> bool + Copy,
    {
        if index >= segments.len() {
            // We've reached the end of the path
            let initial_len = self.content.len();
            self.content.retain(|h| !predicate(h));
            return initial_len > self.content.len();
        }

        let segment = &segments[index];
        let mut removed = false;

        if segment == ">" {
            // Multi-wildcard - remove from multi_wildcard_handlers
            let initial_len = self.multi_wildcard.len();
            self.multi_wildcard.retain(|h| !predicate(h));
            removed = initial_len > self.multi_wildcard.len();
        } else if segment == "*" {
            // Single wildcard - delegate to wildcard child if it exists
            if let Some(child) = &mut self.wildcard_child {
                removed = child.remove_handler_internal(segments, index + 1, predicate);
            }
        } else if is_template_param(segment) {
            // Template parameter - delegate to template child if it exists
            if let Some(child) = &mut self.template_child {
                removed = child.remove_handler_internal(segments, index + 1, predicate);
            }
        } else {
            // Literal segment - delegate to the appropriate child if it exists
            if let Some(child) = self.children.get_mut(segment) {
                removed = child.remove_handler_internal(segments, index + 1, predicate);
            }
        }

        removed
    }

    /// Check if this trie is empty (has no handlers or children)
    pub fn is_empty(&self) -> bool {
        self.content.is_empty()
            && self.multi_wildcard.is_empty()
            && self.children.is_empty()
            && self.wildcard_child.is_none()
            && self.template_child.is_none()
            && self.networks.is_empty()
    }

    /// Get the total number of handlers in the trie
    pub fn handler_count(&self) -> usize {
        self.count
    }

    /// Get all values from the trie
    pub fn get_all_values(&self) -> Vec<T> {
        let mut results = Vec::new();
        for network_trie in self.networks.values() {
            network_trie.collect_all_values_internal(&mut results);
        }
        results
    }

    /// Internal method to collect all values from this trie and its children
    fn collect_all_values_internal(&self, results: &mut Vec<T>) {
        // Add content from this node
        results.extend(self.content.clone());
        results.extend(self.multi_wildcard.clone());

        // Recursively collect from children
        for child in self.children.values() {
            child.collect_all_values_internal(results);
        }

        if let Some(wildcard) = &self.wildcard_child {
            wildcard.collect_all_values_internal(results);
        }

        if let Some(template) = &self.template_child {
            template.collect_all_values_internal(results);
        }
    }
}