execute_parallel_match

Function execute_parallel_match 

Source
pub async fn execute_parallel_match(
    directory: &Path,
    recursive: bool,
    output: Option<&Path>,
    config_service: &dyn ConfigService,
) -> Result<()>
Expand description

Execute parallel matching operations across multiple files and directories.

This function provides high-performance batch processing capabilities for large collections of video and subtitle files. It leverages the parallel processing system to efficiently handle multiple matching operations simultaneously while maintaining proper resource management.

§Parallel Processing Benefits

  • Performance: Multiple files processed simultaneously
  • Efficiency: Optimal CPU and I/O resource utilization
  • Scalability: Handles large file collections effectively
  • Progress Tracking: Real-time progress across all operations
  • Error Isolation: Individual file failures don’t stop other operations

§Resource Management

The parallel system automatically manages:

  • Worker Threads: Optimal thread pool sizing based on system capabilities
  • Memory Usage: Streaming processing to handle large datasets
  • API Rate Limits: Automatic throttling for AI service calls
  • Disk I/O: Efficient file system access patterns
  • Network Resources: Connection pooling and retry logic

§Task Scheduling

Files are processed using intelligent task scheduling:

  • Priority Queue: Important files processed first
  • Dependency Management: Related files processed together
  • Load Balancing: Work distributed evenly across workers
  • Failure Recovery: Automatic retry for transient failures

§Arguments

  • directory - Root directory to scan for media files
  • recursive - Whether to include subdirectories in the scan
  • output - Optional output directory for processed files

§Returns

Returns Ok(()) on successful completion of all tasks, or an error if critical failures prevent processing from continuing.

§File Discovery Process

  1. Directory Scanning: Recursively scan specified directories
  2. File Classification: Identify video and subtitle files
  3. Pairing Logic: Match video files with potential subtitle candidates
  4. Priority Assignment: Assign processing priority based on file characteristics
  5. Task Creation: Generate processing tasks for the scheduler

§Error Handling

  • Individual Failures: Single file errors don’t stop batch processing
  • Critical Errors: System-level failures halt all processing
  • Partial Completion: Successfully processed files are preserved
  • Progress Reporting: Clear indication of which files succeeded/failed

§Performance Optimization

  • Batching: Related operations grouped for efficiency
  • Caching: Shared cache across all parallel operations
  • Memory Pooling: Reuse of allocated resources
  • I/O Optimization: Sequential disk access patterns where possible

§Examples

use subx_cli::commands::match_command;
use std::path::Path;

// Process all files in a directory tree
match_command::execute_parallel_match(
    Path::new("/path/to/media"),
    true,  // recursive
    Some(Path::new("/path/to/output"))
).await?;

// Process single directory without recursion
match_command::execute_parallel_match(
    Path::new("./current_dir"),
    false, // not recursive
    None   // output to same directory
).await?;

§System Requirements

For optimal performance with parallel processing:

  • CPU: Multi-core processor recommended
  • Memory: Sufficient RAM for concurrent operations (4GB+ recommended)
  • Disk: SSD storage for improved I/O performance
  • Network: Stable connection for AI service calls