matchy 2.0.1

Fast database for IP address and pattern matching with rich data storage
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// Arc is passed by value intentionally - Arc::clone() is cheap (atomic increment)
#![allow(clippy::needless_pass_by_value)]

use anyhow::{Context, Result};
use crossbeam_channel::{bounded, Receiver, Sender};
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufRead, BufReader, Read, Seek, SeekFrom};
use std::path::{Path, PathBuf};
use std::sync::{
    atomic::{AtomicBool, Ordering},
    Arc,
};
use std::thread;
use std::time::{Duration, Instant};

use super::sequential::process_line_matches;
use super::stats::ProcessingStats;
use super::thread_utils::set_thread_name;

const POLL_INTERVAL_MS: u64 = 100;

#[allow(clippy::too_many_arguments)]
pub fn follow_files(
    inputs: &[PathBuf],
    db: &matchy::Database,
    extractor: &matchy::extractor::Extractor,
    output_format: &str,
    show_stats: bool,
    show_progress: bool,
    overall_start: Instant,
    shutdown: Arc<AtomicBool>,
) -> Result<ProcessingStats> {
    if inputs.iter().any(|p| p.to_str() == Some("-")) {
        anyhow::bail!("--follow mode not supported with stdin");
    }

    let mut aggregate_stats = ProcessingStats::new();

    let mut progress = if show_progress {
        Some(super::stats::ProgressReporter::new())
    } else {
        None
    };

    if show_stats {
        eprintln!("[INFO] Processing existing file content...");
    }

    for input_path in inputs {
        let stats = process_existing_content(
            input_path,
            db,
            extractor,
            output_format,
            show_stats,
            false,
            overall_start,
        )?;
        aggregate_stats.add(&stats);
    }

    if show_stats {
        eprintln!("[INFO] Watching for new content (Ctrl+C to stop)...");
    }

    let mut file_positions: Vec<(PathBuf, u64)> = Vec::new();
    for input_path in inputs {
        let file = File::open(input_path)
            .with_context(|| format!("Failed to open {}", input_path.display()))?;
        let pos = file.metadata()?.len();
        file_positions.push((input_path.clone(), pos));
    }

    while !shutdown.load(Ordering::Relaxed) {
        let mut had_changes = false;

        for (path, last_pos) in &mut file_positions {
            match std::fs::metadata(path.as_path()) {
                Ok(meta) => {
                    let current_size = meta.len();
                    if current_size > *last_pos {
                        let stats = process_new_content(
                            path.as_path(),
                            last_pos,
                            db,
                            extractor,
                            output_format,
                            show_stats,
                            overall_start,
                        )?;
                        aggregate_stats.add(&stats);
                        had_changes = true;
                    } else if current_size < *last_pos {
                        if show_stats {
                            eprintln!(
                                "[INFO] File truncated, resetting position: {}",
                                path.display()
                            );
                        }
                        *last_pos = 0;
                    }
                }
                Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                    if show_stats {
                        eprintln!(
                            "[WARN] File not found (deleted/rotated?): {}",
                            path.display()
                        );
                    }
                    *last_pos = 0;
                }
                Err(e) => {
                    eprintln!("[WARN] Error checking file {}: {}", path.display(), e);
                }
            }
        }

        if had_changes {
            if let Some(ref mut prog) = progress {
                if prog.should_update() {
                    prog.show(&aggregate_stats, overall_start.elapsed());
                }
            }
        }

        thread::sleep(Duration::from_millis(POLL_INTERVAL_MS));
    }

    if show_stats {
        eprintln!("[INFO] Follow mode stopped");
    }

    if progress.is_some() {
        eprintln!();
    }

    Ok(aggregate_stats)
}

fn process_existing_content(
    input_path: &Path,
    db: &matchy::Database,
    extractor: &matchy::extractor::Extractor,
    output_format: &str,
    show_stats: bool,
    show_progress: bool,
    overall_start: Instant,
) -> Result<ProcessingStats> {
    super::sequential::process_file(
        input_path,
        db,
        extractor,
        output_format,
        show_stats,
        show_progress,
        overall_start,
    )
}

fn process_new_content(
    input_path: &Path,
    last_pos: &mut u64,
    db: &matchy::Database,
    extractor: &matchy::extractor::Extractor,
    output_format: &str,
    _show_stats: bool,
    _overall_start: Instant,
) -> Result<ProcessingStats> {
    let mut file = File::open(input_path)
        .with_context(|| format!("Failed to open {}", input_path.display()))?;

    let current_size = file.metadata()?.len();

    if current_size < *last_pos {
        *last_pos = 0;
    }

    file.seek(SeekFrom::Start(*last_pos))?;

    let mut stats = ProcessingStats::new();
    let output_json = output_format == "json";

    let reader = BufReader::new(file);
    for line_result in reader.lines() {
        let line = line_result?;
        let line_bytes = line.as_bytes();

        stats.lines_processed += 1;
        stats.total_bytes += line_bytes.len();

        let timestamp = if output_json {
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_secs_f64()
        } else {
            0.0
        };

        process_line_matches(
            line_bytes,
            input_path,
            timestamp,
            db,
            extractor,
            output_json,
            &mut stats,
        )?;
    }

    let new_file = File::open(input_path)?;
    *last_pos = new_file.metadata()?.len();

    Ok(stats)
}

#[allow(clippy::too_many_arguments)]
pub fn follow_files_parallel(
    inputs: &[PathBuf],
    db: Arc<matchy::Database>,
    num_threads: usize,
    output_format: &str,
    show_stats: bool,
    show_progress: bool,
    overall_start: Instant,
    shutdown: Arc<AtomicBool>,
    extractor_config: super::parallel::ExtractorConfig,
) -> Result<ProcessingStats> {
    if inputs.iter().any(|p| p.to_str() == Some("-")) {
        anyhow::bail!("--follow mode not supported with stdin");
    }

    let output_json = output_format == "json";

    let work_queue_capacity = num_threads * 4;
    let result_queue_capacity = 1000;

    let (work_tx, work_rx) = bounded::<Option<super::parallel::DataBatch>>(work_queue_capacity);
    let (result_tx, result_rx) =
        bounded::<Option<super::parallel::WorkerMessage>>(result_queue_capacity);

    let shutdown_output = Arc::clone(&shutdown);
    let output_handle = {
        thread::spawn(move || {
            set_thread_name("matchy-follow-output");
            output_thread_follow(
                result_rx,
                output_json,
                show_progress,
                overall_start,
                shutdown_output,
            )
        })
    };

    let mut worker_handles = Vec::new();
    for worker_id in 0..num_threads {
        let work_rx = work_rx.clone();
        let result_tx = result_tx.clone();
        let db_clone = Arc::clone(&db);
        let extractor_config = extractor_config.clone();

        let handle = thread::spawn(move || {
            set_thread_name(&format!("matchy-follow-worker-{worker_id}"));
            worker_thread_follow(
                worker_id,
                work_rx,
                result_tx,
                db_clone,
                show_stats,
                extractor_config,
            )
        });
        worker_handles.push(handle);
    }

    drop(result_tx);

    let reader_handle = {
        let inputs = inputs.to_vec();
        let shutdown_reader = Arc::clone(&shutdown);
        thread::spawn(move || {
            set_thread_name("matchy-follow-reader");
            reader_watcher_thread(inputs, work_tx, overall_start, shutdown_reader, show_stats);
        })
    };

    reader_handle.join().expect("Reader thread panicked");

    let mut worker_stats = Vec::new();
    for handle in worker_handles {
        match handle.join() {
            Ok(stats) => worker_stats.push(stats),
            Err(_) => eprintln!("[ERROR] Worker thread panicked"),
        }
    }

    let _output_stats = output_handle.join().expect("Output thread panicked");

    let mut aggregate = ProcessingStats::new();
    for stats in worker_stats {
        aggregate.lines_processed += stats.lines_processed;
        aggregate.candidates_tested += stats.candidates_tested;
        aggregate.total_matches += stats.matches_found;
        aggregate.total_bytes += stats.total_bytes;
        aggregate.ipv4_count += stats.ipv4_count;
        aggregate.ipv6_count += stats.ipv6_count;
        aggregate.domain_count += stats.domain_count;
        aggregate.email_count += stats.email_count;
    }

    Ok(aggregate)
}

fn reader_watcher_thread(
    inputs: Vec<PathBuf>,
    work_tx: Sender<Option<super::parallel::DataBatch>>,
    _overall_start: Instant,
    shutdown: Arc<AtomicBool>,
    show_stats: bool,
) {
    if show_stats {
        eprintln!("[INFO] Processing existing file content...");
    }

    let mut file_positions: HashMap<PathBuf, u64> = HashMap::new();

    for input_path in &inputs {
        if let Ok(file) = File::open(input_path) {
            if let Ok(metadata) = file.metadata() {
                let size = metadata.len();
                if size > 0 {
                    let mut content = Vec::new();
                    if let Ok(mut f) = File::open(input_path) {
                        if f.read_to_end(&mut content).is_ok() {
                            let batch = super::parallel::DataBatch {
                                source: input_path.clone(),
                                data: Arc::new(content),
                            };
                            let _ = work_tx.send(Some(batch));
                        }
                    }
                }
                file_positions.insert(input_path.clone(), size);
            }
        }
    }

    if show_stats {
        eprintln!("[INFO] Watching for new content (Ctrl+C to stop)...");
    }

    while !shutdown.load(Ordering::Relaxed) {
        for (path, last_pos) in &mut file_positions {
            match std::fs::metadata(path) {
                Ok(meta) => {
                    let current_size = meta.len();
                    if current_size < *last_pos {
                        if show_stats {
                            eprintln!(
                                "[INFO] File truncated, resetting position: {}",
                                path.display()
                            );
                        }
                        *last_pos = 0;
                    }

                    if current_size > *last_pos {
                        if let Ok(mut file) = File::open(path) {
                            if file.seek(SeekFrom::Start(*last_pos)).is_ok() {
                                let mut new_content = Vec::new();
                                if file.read_to_end(&mut new_content).is_ok()
                                    && !new_content.is_empty()
                                {
                                    let batch = super::parallel::DataBatch {
                                        source: path.clone(),
                                        data: Arc::new(new_content),
                                    };
                                    let _ = work_tx.send(Some(batch));
                                    *last_pos = current_size;
                                }
                            }
                        }
                    }
                }
                Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                    if show_stats {
                        eprintln!(
                            "[WARN] File not found (deleted/rotated?): {}",
                            path.display()
                        );
                    }
                    *last_pos = 0;
                }
                Err(e) => {
                    eprintln!("[WARN] Error checking file {}: {}", path.display(), e);
                }
            }
        }

        thread::sleep(Duration::from_millis(POLL_INTERVAL_MS));
    }

    drop(work_tx);
}

fn worker_thread_follow(
    worker_id: usize,
    work_rx: Receiver<Option<super::parallel::DataBatch>>,
    result_tx: Sender<Option<super::parallel::WorkerMessage>>,
    db: Arc<matchy::Database>,
    _show_stats: bool,
    extractor_config: super::parallel::ExtractorConfig,
) -> super::parallel::WorkerStats {
    use super::parallel::{
        build_match_result, create_extractor_for_db, MatchBuffers, WorkerMessage, WorkerStats,
    };

    let extractor = match create_extractor_for_db(&db, &extractor_config) {
        Ok(ext) => ext,
        Err(e) => {
            eprintln!("[ERROR] Worker {worker_id} failed to create extractor: {e}");
            return WorkerStats::default();
        }
    };

    let mut worker = matchy::processing::Worker::builder()
        .extractor(extractor)
        .add_database("default", db)
        .build();
    let mut last_progress_update = Instant::now();
    let progress_interval = Duration::from_millis(100);

    let mut match_buffers = MatchBuffers::new();

    loop {
        let batch_opt = work_rx.recv();

        match batch_opt {
            Ok(Some(batch)) => {
                match worker.process_batch(&batch) {
                    Ok(matches) => {
                        for m in matches {
                            if let Some(match_result) = build_match_result(&m, &mut match_buffers) {
                                let _ = result_tx.send(Some(WorkerMessage::Match(match_result)));
                            }
                        }
                    }
                    Err(e) => {
                        eprintln!("[ERROR] Worker {worker_id} batch processing failed: {e}");
                    }
                }

                let now = Instant::now();
                if now.duration_since(last_progress_update) >= progress_interval {
                    let _ = result_tx.send(Some(WorkerMessage::Stats {
                        worker_id,
                        stats: worker.stats().clone(),
                    }));
                    last_progress_update = now;
                }
            }
            Ok(None) | Err(_) => break,
        }
    }

    let final_stats = worker.stats().clone();
    let _ = result_tx.send(Some(WorkerMessage::Stats {
        worker_id,
        stats: final_stats.clone(),
    }));
    final_stats
}

fn output_thread_follow(
    result_rx: crossbeam_channel::Receiver<Option<super::parallel::WorkerMessage>>,
    output_json: bool,
    show_progress: bool,
    overall_start: Instant,
    shutdown: Arc<AtomicBool>,
) -> ProcessingStats {
    use super::parallel::WorkerMessage;
    use serde_json::json;

    let mut stats = ProcessingStats::new();
    let mut worker_stats_map: HashMap<usize, super::parallel::WorkerStats> = HashMap::new();

    let mut progress = if show_progress {
        Some(super::stats::ProgressReporter::new())
    } else {
        None
    };

    loop {
        if shutdown.load(Ordering::Relaxed) {
            break;
        }

        match result_rx.recv_timeout(Duration::from_millis(100)) {
            Ok(Some(msg)) => match msg {
                WorkerMessage::Match(result) => {
                    if output_json {
                        let mut match_obj = json!({
                            "timestamp": format!("{:.3}", result.timestamp),
                            "source": result.source_file.display().to_string(),
                            "matched_text": result.matched_text,
                            "match_type": result.match_type,
                        });

                        if let Some(pattern_count) = result.pattern_count {
                            match_obj["pattern_count"] = json!(pattern_count);
                        }
                        if let Some(data) = result.data {
                            match_obj["data"] = data;
                        }
                        if let Some(prefix_len) = result.prefix_len {
                            match_obj["prefix_len"] = json!(prefix_len);
                        }
                        if let Some(cidr) = result.cidr {
                            match_obj["cidr"] = json!(cidr);
                        }

                        if let Ok(json_str) = serde_json::to_string(&match_obj) {
                            println!("{json_str}");
                        }
                    }

                    stats.total_matches += 1;
                }
                WorkerMessage::Stats {
                    worker_id,
                    stats: worker_stats_msg,
                } => {
                    worker_stats_map.insert(worker_id, worker_stats_msg);

                    let mut aggregate = ProcessingStats::new();
                    for stats in worker_stats_map.values() {
                        aggregate.lines_processed += stats.lines_processed;
                        aggregate.candidates_tested += stats.candidates_tested;
                        aggregate.total_matches += stats.matches_found;
                        aggregate.total_bytes += stats.total_bytes;
                        aggregate.ipv4_count += stats.ipv4_count;
                        aggregate.ipv6_count += stats.ipv6_count;
                        aggregate.domain_count += stats.domain_count;
                        aggregate.email_count += stats.email_count;
                    }

                    if let Some(ref mut prog) = progress {
                        if prog.should_update() {
                            prog.show(&aggregate, overall_start.elapsed());
                        }
                    }
                }
            },
            Ok(None) => {
                break;
            }
            Err(crossbeam_channel::RecvTimeoutError::Timeout) => {
                continue;
            }
            Err(crossbeam_channel::RecvTimeoutError::Disconnected) => {
                break;
            }
        }
    }

    if progress.is_some() {
        eprintln!();
    }

    stats
}