Skip to main content

BatchExecutor

Struct BatchExecutor 

Source
pub struct BatchExecutor { /* private fields */ }
Expand description

High-performance batch processor for HEDL files.

Orchestrates parallel or serial processing based on configuration and workload. Provides progress tracking and comprehensive error collection.

§Thread Safety

BatchExecutor is thread-safe and can be shared across threads via Arc.

§Examples

use hedl_cli::batch::{BatchExecutor, BatchConfig, ValidationOperation};
use std::path::PathBuf;

let processor = BatchExecutor::new(BatchConfig {
    parallel_threshold: 5,
    verbose: true,
    ..Default::default()
});

let files: Vec<PathBuf> = vec![
    "file1.hedl".into(),
    "file2.hedl".into(),
];

let results = processor.process(
    &files,
    ValidationOperation { strict: false },
    true,
)?;

if results.has_failures() {
    eprintln!("Some files failed validation");
    for failure in results.failures() {
        eprintln!("  - {}: {:?}", failure.path.display(), failure.result);
    }
}

Implementations§

Source§

impl BatchExecutor

Source

pub fn new(config: BatchConfig) -> Self

Create a new batch processor with the given configuration.

Source

pub fn default_config() -> Self

Create a batch processor with default configuration.

Source

pub fn process<O>( &self, files: &[PathBuf], operation: O, show_progress: bool, ) -> Result<BatchResults<O::Output>, CliError>
where O: BatchOperation,

Process multiple files with the given operation.

Automatically selects parallel or serial processing based on configuration and file count. Provides progress reporting and collects all results.

§Arguments
  • files - Slice of file paths to process
  • operation - The operation to perform on each file
  • show_progress - Whether to show progress updates
§Returns
  • Ok(BatchResults) - Successfully processed all files (individual failures collected in results)
  • Err(CliError::ThreadPoolError) - Failed to create thread pool with requested configuration
§Thread Pool Selection

The method uses different thread pool strategies based on configuration:

  1. Serial Processing: If files.len() < parallel_threshold, processes serially (no thread pool)
  2. Local Thread Pool: If max_threads is Some(n), creates isolated pool with n threads
  3. Global Thread Pool: If max_threads is None, uses Rayon’s global pool
§Error Handling

Thread pool creation can fail if:

  • max_threads is 0 (invalid configuration)
  • System cannot allocate thread resources
  • Thread stack allocation fails

Individual file processing errors are collected in BatchResults, not returned as errors.

§Performance
  • Serial processing for small batches to avoid thread pool overhead
  • Local thread pool: ~0.5-1ms creation overhead, ~2-8MB per thread
  • Global thread pool: zero overhead
  • Lock-free progress tracking using atomic counters
§Examples
use hedl_cli::batch::{BatchExecutor, BatchConfig, FormatOperation};
use hedl_cli::error::CliError;
use std::path::PathBuf;

let processor = BatchExecutor::new(BatchConfig {
    max_threads: Some(4),
    ..Default::default()
});

let files = vec![PathBuf::from("a.hedl"), PathBuf::from("b.hedl")];

match processor.process(
    &files,
    FormatOperation {
        check: false,
        ditto: true,
        with_counts: false,
    },
    true,
) {
    Ok(results) => {
        println!("Formatted {} files", results.success_count());
        if results.has_failures() {
            // Handle individual file failures
        }
    }
    Err(CliError::ThreadPoolError { message, requested_threads }) => {
        eprintln!("Failed to create thread pool: {}", message);
    }
    Err(e) => {
        eprintln!("Unexpected error: {}", e);
    }
}
Source

pub fn process_streaming<O>( &self, files: &[PathBuf], operation: O, show_progress: bool, ) -> Result<BatchResults<O::Output>, CliError>

Process files using streaming operations for memory efficiency.

This method uses the streaming parser from hedl-stream to process files with constant memory usage regardless of file size. Ideal for:

  • Files larger than 100MB
  • Memory-constrained environments
  • Processing thousands of files
§Arguments
  • files - Slice of file paths to process
  • operation - The streaming operation to perform
  • show_progress - Whether to show progress updates
§Returns
  • Ok(BatchResults) - Always succeeds and collects all individual results
  • Err(CliError) - Only on catastrophic failures
§Memory Usage

Peak memory = buffer_size (8KB) × num_threads + ID tracking set

§Examples
use hedl_cli::batch::{BatchExecutor, StreamingValidationOperation, BatchConfig};
use std::path::PathBuf;

let processor = BatchExecutor::default_config();
let files = vec![PathBuf::from("large-file.hedl")];
let operation = StreamingValidationOperation { strict: false };

let results = processor.process_streaming(&files, operation, true)?;
println!("Processed {} files with constant memory", results.success_count());
Source

pub fn process_auto<O, SO>( &self, files: &[PathBuf], standard_op: O, streaming_op: SO, show_progress: bool, ) -> Result<BatchResults<O::Output>, CliError>
where O: BatchOperation<Output = SO::Output>, SO: StreamingBatchOperation,

Automatically choose between standard and streaming based on file size.

Files larger than 100MB use streaming mode for memory efficiency, while smaller files use standard mode for better performance.

§Arguments
  • files - Slice of file paths to process
  • standard_op - Standard operation for small files
  • streaming_op - Streaming operation for large files
  • show_progress - Whether to show progress updates
§Returns
  • Ok(BatchResults) - Combined results from both modes
  • Err(CliError) - On catastrophic failures
§Examples
use hedl_cli::batch::{BatchExecutor, ValidationOperation, StreamingValidationOperation};
use std::path::PathBuf;

let processor = BatchExecutor::default_config();
let files = vec![
    PathBuf::from("small.hedl"),
    PathBuf::from("large-200mb.hedl"),
];

let results = processor.process_auto(
    &files,
    ValidationOperation { strict: false },
    StreamingValidationOperation { strict: false },
    true,
)?;
println!("Processed {} files", results.results.len());

Trait Implementations§

Source§

impl Clone for BatchExecutor

Source§

fn clone(&self) -> BatchExecutor

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BatchExecutor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,