oximedia-io 0.1.2

I/O layer for OxiMedia
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
#![allow(dead_code)]
//! Parallel file copy engine with chunked I/O and progress tracking.
//!
//! Provides efficient parallel copy operations by splitting files into
//! chunks and copying them concurrently, with real-time progress feedback.

use std::collections::VecDeque;

/// Default chunk size for parallel copy (1 MiB).
const DEFAULT_CHUNK_SIZE: usize = 1024 * 1024;

/// Maximum number of concurrent copy workers.
const MAX_WORKERS: usize = 16;

/// Copy progress information.
#[derive(Debug, Clone)]
pub struct CopyProgress {
    /// Total bytes to copy.
    pub total_bytes: u64,
    /// Bytes copied so far.
    pub copied_bytes: u64,
    /// Number of chunks completed.
    pub chunks_completed: usize,
    /// Total number of chunks.
    pub total_chunks: usize,
    /// Whether the copy is finished.
    pub finished: bool,
}

impl CopyProgress {
    /// Create a new copy progress tracker.
    pub fn new(total_bytes: u64, total_chunks: usize) -> Self {
        Self {
            total_bytes,
            copied_bytes: 0,
            chunks_completed: 0,
            total_chunks,
            finished: false,
        }
    }

    /// Get the progress fraction (0.0 to 1.0).
    #[allow(clippy::cast_precision_loss)]
    pub fn fraction(&self) -> f64 {
        if self.total_bytes == 0 {
            return 1.0;
        }
        self.copied_bytes as f64 / self.total_bytes as f64
    }

    /// Get progress as a percentage (0 to 100).
    #[allow(clippy::cast_precision_loss)]
    #[allow(clippy::cast_possible_truncation)]
    #[allow(clippy::cast_sign_loss)]
    pub fn percentage(&self) -> u8 {
        (self.fraction() * 100.0).min(100.0) as u8
    }

    /// Record completion of a chunk.
    pub fn complete_chunk(&mut self, bytes: u64) {
        self.copied_bytes = self.copied_bytes.saturating_add(bytes);
        self.chunks_completed += 1;
        if self.chunks_completed >= self.total_chunks {
            self.finished = true;
        }
    }
}

/// A chunk descriptor for parallel copy.
#[derive(Debug, Clone)]
pub struct CopyChunk {
    /// Offset within the source.
    pub offset: u64,
    /// Length of this chunk.
    pub length: usize,
    /// Index of this chunk.
    pub index: usize,
}

/// Configuration for the parallel copy engine.
#[derive(Debug, Clone)]
pub struct ParallelCopyConfig {
    /// Size of each chunk in bytes.
    pub chunk_size: usize,
    /// Number of concurrent workers.
    pub workers: usize,
    /// Whether to verify after copy.
    pub verify: bool,
    /// Whether to preserve file metadata.
    pub preserve_metadata: bool,
}

impl Default for ParallelCopyConfig {
    fn default() -> Self {
        Self {
            chunk_size: DEFAULT_CHUNK_SIZE,
            workers: 4,
            verify: false,
            preserve_metadata: true,
        }
    }
}

impl ParallelCopyConfig {
    /// Create a new config with the given chunk size.
    pub fn with_chunk_size(mut self, size: usize) -> Self {
        self.chunk_size = size.max(4096);
        self
    }

    /// Set the number of workers.
    pub fn with_workers(mut self, workers: usize) -> Self {
        self.workers = workers.clamp(1, MAX_WORKERS);
        self
    }

    /// Enable or disable verification.
    pub fn with_verify(mut self, verify: bool) -> Self {
        self.verify = verify;
        self
    }

    /// Validate the configuration.
    pub fn validate(&self) -> Result<(), String> {
        if self.chunk_size < 512 {
            return Err("Chunk size must be at least 512 bytes".to_string());
        }
        if self.workers == 0 || self.workers > MAX_WORKERS {
            return Err(format!("Workers must be between 1 and {MAX_WORKERS}"));
        }
        Ok(())
    }
}

/// Plan for a parallel copy operation.
#[derive(Debug, Clone)]
pub struct CopyPlan {
    /// Source path.
    pub source: String,
    /// Destination path.
    pub destination: String,
    /// File size in bytes.
    pub file_size: u64,
    /// Chunks to copy.
    pub chunks: Vec<CopyChunk>,
    /// Configuration used.
    pub config: ParallelCopyConfig,
}

impl CopyPlan {
    /// Create a copy plan for a given file size.
    #[allow(clippy::cast_possible_truncation)]
    pub fn create(source: &str, destination: &str, file_size: u64, config: ParallelCopyConfig) -> Self {
        let chunk_size = config.chunk_size;
        let mut chunks = Vec::new();
        let mut offset = 0u64;
        let mut index = 0;

        while offset < file_size {
            let remaining = file_size - offset;
            let length = if remaining > chunk_size as u64 {
                chunk_size
            } else {
                remaining as usize
            };
            chunks.push(CopyChunk {
                offset,
                length,
                index,
            });
            offset += length as u64;
            index += 1;
        }

        Self {
            source: source.to_string(),
            destination: destination.to_string(),
            file_size,
            chunks,
            config,
        }
    }

    /// Get the total number of chunks.
    pub fn chunk_count(&self) -> usize {
        self.chunks.len()
    }

    /// Estimate throughput needed for a target duration (bytes/sec).
    #[allow(clippy::cast_precision_loss)]
    pub fn required_throughput(&self, target_seconds: f64) -> f64 {
        if target_seconds <= 0.0 {
            return f64::INFINITY;
        }
        self.file_size as f64 / target_seconds
    }
}

/// Copy work queue for scheduling chunks across workers.
#[derive(Debug)]
pub struct CopyWorkQueue {
    /// Pending chunks.
    pending: VecDeque<CopyChunk>,
    /// In-progress chunk indices.
    in_progress: Vec<usize>,
    /// Completed chunk indices.
    completed: Vec<usize>,
}

impl CopyWorkQueue {
    /// Create a new work queue from a copy plan.
    pub fn from_plan(plan: &CopyPlan) -> Self {
        let pending: VecDeque<CopyChunk> = plan.chunks.iter().cloned().collect();
        Self {
            pending,
            in_progress: Vec::new(),
            completed: Vec::new(),
        }
    }

    /// Take the next chunk to process.
    pub fn take_next(&mut self) -> Option<CopyChunk> {
        if let Some(chunk) = self.pending.pop_front() {
            self.in_progress.push(chunk.index);
            Some(chunk)
        } else {
            None
        }
    }

    /// Mark a chunk as completed.
    pub fn mark_completed(&mut self, index: usize) {
        self.in_progress.retain(|&i| i != index);
        self.completed.push(index);
    }

    /// Check if all chunks are completed.
    pub fn is_finished(&self) -> bool {
        self.pending.is_empty() && self.in_progress.is_empty()
    }

    /// Get the number of remaining chunks.
    pub fn remaining(&self) -> usize {
        self.pending.len() + self.in_progress.len()
    }

    /// Get the number of completed chunks.
    pub fn completed_count(&self) -> usize {
        self.completed.len()
    }
}

/// Throughput calculator for copy operations.
#[derive(Debug, Clone)]
pub struct ThroughputCalculator {
    /// Samples of (bytes, elapsed_nanos).
    samples: Vec<(u64, u64)>,
    /// Maximum number of samples to keep.
    max_samples: usize,
}

impl ThroughputCalculator {
    /// Create a new throughput calculator.
    pub fn new(max_samples: usize) -> Self {
        Self {
            samples: Vec::with_capacity(max_samples),
            max_samples: max_samples.max(1),
        }
    }

    /// Add a sample.
    pub fn add_sample(&mut self, bytes: u64, elapsed_nanos: u64) {
        if self.samples.len() >= self.max_samples {
            self.samples.remove(0);
        }
        self.samples.push((bytes, elapsed_nanos));
    }

    /// Get average throughput in bytes/sec.
    #[allow(clippy::cast_precision_loss)]
    pub fn average_throughput(&self) -> f64 {
        if self.samples.is_empty() {
            return 0.0;
        }
        let total_bytes: u64 = self.samples.iter().map(|(b, _)| b).sum();
        let total_nanos: u64 = self.samples.iter().map(|(_, n)| n).sum();
        if total_nanos == 0 {
            return 0.0;
        }
        total_bytes as f64 / (total_nanos as f64 / 1_000_000_000.0)
    }

    /// Get the number of samples recorded.
    pub fn sample_count(&self) -> usize {
        self.samples.len()
    }

    /// Estimate time remaining for given bytes.
    #[allow(clippy::cast_precision_loss)]
    pub fn estimate_remaining_secs(&self, remaining_bytes: u64) -> Option<f64> {
        let throughput = self.average_throughput();
        if throughput <= 0.0 {
            return None;
        }
        Some(remaining_bytes as f64 / throughput)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_copy_progress_new() {
        let p = CopyProgress::new(1000, 10);
        assert_eq!(p.total_bytes, 1000);
        assert_eq!(p.copied_bytes, 0);
        assert_eq!(p.chunks_completed, 0);
        assert!(!p.finished);
    }

    #[test]
    fn test_copy_progress_fraction() {
        let mut p = CopyProgress::new(1000, 2);
        assert!((p.fraction() - 0.0).abs() < f64::EPSILON);
        p.complete_chunk(500);
        assert!((p.fraction() - 0.5).abs() < f64::EPSILON);
        p.complete_chunk(500);
        assert!((p.fraction() - 1.0).abs() < f64::EPSILON);
        assert!(p.finished);
    }

    #[test]
    fn test_copy_progress_zero_size() {
        let p = CopyProgress::new(0, 0);
        assert!((p.fraction() - 1.0).abs() < f64::EPSILON);
        assert_eq!(p.percentage(), 100);
    }

    #[test]
    fn test_copy_progress_percentage() {
        let mut p = CopyProgress::new(200, 4);
        p.complete_chunk(50);
        assert_eq!(p.percentage(), 25);
    }

    #[test]
    fn test_parallel_copy_config_default() {
        let cfg = ParallelCopyConfig::default();
        assert_eq!(cfg.chunk_size, DEFAULT_CHUNK_SIZE);
        assert_eq!(cfg.workers, 4);
        assert!(!cfg.verify);
        assert!(cfg.preserve_metadata);
    }

    #[test]
    fn test_config_builders() {
        let cfg = ParallelCopyConfig::default()
            .with_chunk_size(8192)
            .with_workers(8)
            .with_verify(true);
        assert_eq!(cfg.chunk_size, 8192);
        assert_eq!(cfg.workers, 8);
        assert!(cfg.verify);
    }

    #[test]
    fn test_config_clamp_workers() {
        let cfg = ParallelCopyConfig::default().with_workers(100);
        assert_eq!(cfg.workers, MAX_WORKERS);
        let cfg2 = ParallelCopyConfig::default().with_workers(0);
        assert_eq!(cfg2.workers, 1);
    }

    #[test]
    fn test_config_validate() {
        let cfg = ParallelCopyConfig::default();
        assert!(cfg.validate().is_ok());

        let mut bad = ParallelCopyConfig::default();
        bad.chunk_size = 100;
        assert!(bad.validate().is_err());
    }

    #[test]
    fn test_copy_plan_create() {
        let plan = CopyPlan::create("src.bin", "dst.bin", 3000, ParallelCopyConfig::default().with_chunk_size(1000));
        assert_eq!(plan.chunk_count(), 3);
        assert_eq!(plan.chunks[0].offset, 0);
        assert_eq!(plan.chunks[0].length, 1000);
        assert_eq!(plan.chunks[2].offset, 2000);
        assert_eq!(plan.chunks[2].length, 1000);
    }

    #[test]
    fn test_copy_plan_uneven() {
        let plan = CopyPlan::create("a", "b", 2500, ParallelCopyConfig::default().with_chunk_size(1000));
        assert_eq!(plan.chunk_count(), 3);
        assert_eq!(plan.chunks[2].length, 500);
    }

    #[test]
    fn test_copy_plan_empty() {
        let plan = CopyPlan::create("a", "b", 0, ParallelCopyConfig::default());
        assert_eq!(plan.chunk_count(), 0);
    }

    #[test]
    fn test_work_queue_lifecycle() {
        let plan = CopyPlan::create("a", "b", 3000, ParallelCopyConfig::default().with_chunk_size(1000));
        let mut queue = CopyWorkQueue::from_plan(&plan);
        assert_eq!(queue.remaining(), 3);
        assert!(!queue.is_finished());

        let c1 = queue.take_next().expect("take_next should return chunk");
        assert_eq!(c1.index, 0);
        assert_eq!(queue.remaining(), 3); // 2 pending + 1 in progress

        queue.mark_completed(0);
        assert_eq!(queue.remaining(), 2);
        assert_eq!(queue.completed_count(), 1);

        let _ = queue.take_next().expect("take_next should return chunk");
        let _ = queue.take_next().expect("take_next should return chunk");
        queue.mark_completed(1);
        queue.mark_completed(2);
        assert!(queue.is_finished());
    }

    #[test]
    fn test_throughput_calculator() {
        let mut calc = ThroughputCalculator::new(10);
        assert_eq!(calc.sample_count(), 0);
        assert!((calc.average_throughput() - 0.0).abs() < f64::EPSILON);

        // 1000 bytes in 1 second (1_000_000_000 nanos)
        calc.add_sample(1000, 1_000_000_000);
        assert!((calc.average_throughput() - 1000.0).abs() < 1.0);
    }

    #[test]
    fn test_throughput_estimate_remaining() {
        let mut calc = ThroughputCalculator::new(10);
        assert!(calc.estimate_remaining_secs(1000).is_none());

        calc.add_sample(1000, 1_000_000_000);
        let est = calc.estimate_remaining_secs(5000).expect("estimate should succeed");
        assert!((est - 5.0).abs() < 0.01);
    }

    #[test]
    fn test_throughput_max_samples() {
        let mut calc = ThroughputCalculator::new(3);
        calc.add_sample(100, 100);
        calc.add_sample(200, 200);
        calc.add_sample(300, 300);
        calc.add_sample(400, 400);
        assert_eq!(calc.sample_count(), 3);
    }

    #[test]
    fn test_required_throughput() {
        let plan = CopyPlan::create("a", "b", 10_000_000, ParallelCopyConfig::default());
        let throughput = plan.required_throughput(10.0);
        assert!((throughput - 1_000_000.0).abs() < 1.0);
    }
}