torsh-package 0.1.2

Model packaging and distribution utilities for ToRSh
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
574
//! Delta patching for incremental package updates
//!
//! This module provides functionality for creating and applying delta patches
//! between package versions, enabling efficient incremental updates for large models.

// Framework infrastructure - components designed for future use
#![allow(dead_code)]
use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::Path;

use serde::{Deserialize, Serialize};
use torsh_core::error::{Result, TorshError};

use crate::manifest::PackageManifest;
use crate::package::Package;
use crate::resources::Resource;
use crate::utils::calculate_hash;

/// Delta patch operation types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DeltaOperation {
    /// Add a new resource
    Add {
        /// Name of the resource to add
        resource_name: String,
        /// Resource data content
        resource_data: Vec<u8>,
        /// Resource metadata
        metadata: HashMap<String, String>,
    },
    /// Remove a resource
    Remove {
        /// Name of the resource to remove
        resource_name: String,
    },
    /// Modify an existing resource with binary diff
    Modify {
        /// Name of the resource to modify
        resource_name: String,
        /// Binary diff data
        diff_data: Vec<u8>,
        /// Hash of original resource data
        old_hash: String,
        /// Hash of target resource data
        new_hash: String,
    },
    /// Update manifest metadata
    UpdateManifest {
        /// Manifest field name to update
        field: String,
        /// New value for the field
        new_value: String,
    },
}

/// Delta patch containing a set of operations to transform one package version to another
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeltaPatch {
    /// Source package version
    pub from_version: String,
    /// Target package version
    pub to_version: String,
    /// Patch format version for compatibility
    pub patch_format_version: String,
    /// List of operations to apply
    pub operations: Vec<DeltaOperation>,
    /// Creation timestamp
    pub created_at: chrono::DateTime<chrono::Utc>,
    /// Patch size in bytes
    pub patch_size: u64,
    /// Expected final package hash after applying patch
    pub target_hash: String,
    /// Metadata about the patch
    pub metadata: HashMap<String, String>,
}

/// Delta patch builder for creating patches between packages
pub struct DeltaPatchBuilder {
    compression_level: u32,
    include_metadata_changes: bool,
    max_diff_size_ratio: f64,
}

/// Delta patch applier for applying patches to packages
pub struct DeltaPatchApplier {
    verify_checksums: bool,
    backup_original: bool,
}

impl Default for DeltaPatchBuilder {
    fn default() -> Self {
        Self {
            compression_level: 6,
            include_metadata_changes: true,
            max_diff_size_ratio: 0.8, // Only create binary diff if it's less than 80% of original size
        }
    }
}

impl DeltaPatchBuilder {
    /// Create a new delta patch builder
    pub fn new() -> Self {
        Self::default()
    }

    /// Set compression level for patch data (0-9)
    pub fn with_compression_level(mut self, level: u32) -> Self {
        self.compression_level = level.min(9);
        self
    }

    /// Enable or disable metadata change tracking
    pub fn with_metadata_changes(mut self, include: bool) -> Self {
        self.include_metadata_changes = include;
        self
    }

    /// Set maximum diff size ratio (diff size / original size)
    pub fn with_max_diff_ratio(mut self, ratio: f64) -> Self {
        self.max_diff_size_ratio = ratio.clamp(0.1, 1.0);
        self
    }

    /// Generate a delta patch between two packages
    pub fn create_patch(&self, old_package: &Package, new_package: &Package) -> Result<DeltaPatch> {
        let mut operations = Vec::new();

        // Compare versions
        let from_version = old_package.get_version().to_string();
        let to_version = new_package.get_version().to_string();

        // Find resource differences
        let old_resources: HashSet<String> = old_package.resources.keys().cloned().collect();
        let new_resources: HashSet<String> = new_package.resources.keys().cloned().collect();

        // Find added resources
        for resource_name in new_resources.difference(&old_resources) {
            if let Some(resource) = new_package.resources.get(resource_name) {
                operations.push(DeltaOperation::Add {
                    resource_name: resource_name.clone(),
                    resource_data: resource.data.clone(),
                    metadata: resource.metadata.clone(),
                });
            }
        }

        // Find removed resources
        for resource_name in old_resources.difference(&new_resources) {
            operations.push(DeltaOperation::Remove {
                resource_name: resource_name.clone(),
            });
        }

        // Find modified resources
        for resource_name in old_resources.intersection(&new_resources) {
            if let (Some(old_resource), Some(new_resource)) = (
                old_package.resources.get(resource_name),
                new_package.resources.get(resource_name),
            ) {
                let old_hash = calculate_hash(&old_resource.data);
                let new_hash = calculate_hash(&new_resource.data);

                if old_hash != new_hash {
                    // Create binary diff
                    let diff_data =
                        self.create_binary_diff(&old_resource.data, &new_resource.data)?;

                    // Only use diff if it's smaller than the threshold
                    let diff_ratio = diff_data.len() as f64 / new_resource.data.len() as f64;

                    if diff_ratio <= self.max_diff_size_ratio {
                        operations.push(DeltaOperation::Modify {
                            resource_name: resource_name.clone(),
                            diff_data,
                            old_hash,
                            new_hash,
                        });
                    } else {
                        // If diff is too large, just replace the entire resource
                        operations.push(DeltaOperation::Remove {
                            resource_name: resource_name.clone(),
                        });
                        operations.push(DeltaOperation::Add {
                            resource_name: resource_name.clone(),
                            resource_data: new_resource.data.clone(),
                            metadata: new_resource.metadata.clone(),
                        });
                    }
                }
            }
        }

        // Check for manifest changes if enabled
        if self.include_metadata_changes {
            self.add_manifest_changes(
                &mut operations,
                &old_package.manifest,
                &new_package.manifest,
            )?;
        }

        // Calculate patch size
        let patch_size = self.calculate_patch_size(&operations);

        // Generate target hash (simplified - just hash the new package manifest)
        let target_hash = calculate_hash(
            &oxicode::serde::encode_to_vec(&new_package.manifest, oxicode::config::standard())
                .expect("manifest serialization should succeed"),
        );

        let mut metadata = HashMap::new();
        metadata.insert(
            "compression_level".to_string(),
            self.compression_level.to_string(),
        );
        metadata.insert("operations_count".to_string(), operations.len().to_string());

        Ok(DeltaPatch {
            from_version,
            to_version,
            patch_format_version: "1.0.0".to_string(),
            operations,
            created_at: chrono::Utc::now(),
            patch_size,
            target_hash,
            metadata,
        })
    }

    /// Create binary diff between two byte arrays
    fn create_binary_diff(&self, _old_data: &[u8], new_data: &[u8]) -> Result<Vec<u8>> {
        // Simple implementation using basic compression
        // In a production system, you might want to use more sophisticated algorithms like bsdiff
        use oxiarc_deflate::gzip::gzip_compress;

        // For simplicity, we'll create a compressed representation of the new data
        // A more sophisticated implementation would create actual binary diffs
        gzip_compress(new_data, self.compression_level as u8)
            .map_err(|e| TorshError::SerializationError(format!("Compression failed: {}", e)))
    }

    /// Add manifest change operations
    fn add_manifest_changes(
        &self,
        operations: &mut Vec<DeltaOperation>,
        old_manifest: &PackageManifest,
        new_manifest: &PackageManifest,
    ) -> Result<()> {
        // Check for version change
        if old_manifest.version != new_manifest.version {
            operations.push(DeltaOperation::UpdateManifest {
                field: "version".to_string(),
                new_value: new_manifest.version.clone(),
            });
        }

        // Check for description change
        if old_manifest.description != new_manifest.description {
            let new_value = new_manifest.description.clone().unwrap_or_default();
            operations.push(DeltaOperation::UpdateManifest {
                field: "description".to_string(),
                new_value,
            });
        }

        // Check for author change
        if old_manifest.author != new_manifest.author {
            let new_value = new_manifest.author.clone().unwrap_or_default();
            operations.push(DeltaOperation::UpdateManifest {
                field: "author".to_string(),
                new_value,
            });
        }

        Ok(())
    }

    /// Calculate total patch size
    fn calculate_patch_size(&self, operations: &[DeltaOperation]) -> u64 {
        operations
            .iter()
            .map(|op| match op {
                DeltaOperation::Add { resource_data, .. } => resource_data.len() as u64,
                DeltaOperation::Modify { diff_data, .. } => diff_data.len() as u64,
                DeltaOperation::Remove { .. } => 0,
                DeltaOperation::UpdateManifest { new_value, .. } => new_value.len() as u64,
            })
            .sum()
    }
}

impl Default for DeltaPatchApplier {
    fn default() -> Self {
        Self {
            verify_checksums: true,
            backup_original: true,
        }
    }
}

impl DeltaPatchApplier {
    /// Create a new delta patch applier
    pub fn new() -> Self {
        Self::default()
    }

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

    /// Enable or disable backup creation before applying patch
    pub fn with_backup(mut self, backup: bool) -> Self {
        self.backup_original = backup;
        self
    }

    /// Apply a delta patch to a package
    pub fn apply_patch(&self, package: &mut Package, patch: &DeltaPatch) -> Result<()> {
        // Verify version compatibility
        if package.get_version() != patch.from_version {
            return Err(TorshError::InvalidArgument(format!(
                "Version mismatch: package version {} doesn't match patch from_version {}",
                package.get_version(),
                patch.from_version
            )));
        }

        // Apply operations
        for operation in &patch.operations {
            self.apply_operation(package, operation)?;
        }

        // Verify final hash if enabled
        if self.verify_checksums {
            let current_hash = calculate_hash(
                &oxicode::serde::encode_to_vec(&package.manifest, oxicode::config::standard())
                    .expect("manifest serialization should succeed"),
            );
            if current_hash != patch.target_hash {
                return Err(TorshError::InvalidArgument(
                    "Package hash doesn't match expected target hash after applying patch"
                        .to_string(),
                ));
            }
        }

        Ok(())
    }

    /// Apply a single delta operation
    fn apply_operation(&self, package: &mut Package, operation: &DeltaOperation) -> Result<()> {
        match operation {
            DeltaOperation::Add {
                resource_name,
                resource_data,
                metadata,
            } => {
                let resource = Resource {
                    name: resource_name.clone(),
                    resource_type: crate::resources::ResourceType::Data, // Default type
                    data: resource_data.clone(),
                    metadata: metadata.clone(),
                };
                package.resources.insert(resource_name.clone(), resource);
            }
            DeltaOperation::Remove { resource_name } => {
                package.resources.remove(resource_name);
            }
            DeltaOperation::Modify {
                resource_name,
                diff_data,
                old_hash,
                new_hash,
            } => {
                if let Some(resource) = package.resources.get_mut(resource_name) {
                    // Verify old hash if enabled
                    if self.verify_checksums {
                        let current_hash = calculate_hash(&resource.data);
                        if current_hash != *old_hash {
                            return Err(TorshError::InvalidArgument(format!(
                                "Resource {} hash mismatch before applying diff",
                                resource_name
                            )));
                        }
                    }

                    // Apply diff (decompress for our simple implementation)
                    let new_data = self.apply_binary_diff(&resource.data, diff_data)?;

                    // Verify new hash if enabled
                    if self.verify_checksums {
                        let result_hash = calculate_hash(&new_data);
                        if result_hash != *new_hash {
                            return Err(TorshError::InvalidArgument(format!(
                                "Resource {} hash mismatch after applying diff",
                                resource_name
                            )));
                        }
                    }

                    resource.data = new_data;
                }
            }
            DeltaOperation::UpdateManifest { field, new_value } => {
                self.update_manifest_field(&mut package.manifest, field, new_value)?;
            }
        }
        Ok(())
    }

    /// Apply binary diff (decompress for our simple implementation)
    fn apply_binary_diff(&self, _old_data: &[u8], diff_data: &[u8]) -> Result<Vec<u8>> {
        use oxiarc_deflate::gzip::gzip_decompress;

        gzip_decompress(diff_data)
            .map_err(|e| TorshError::SerializationError(format!("Decompression failed: {}", e)))
    }

    /// Update a manifest field
    fn update_manifest_field(
        &self,
        manifest: &mut PackageManifest,
        field: &str,
        new_value: &str,
    ) -> Result<()> {
        match field {
            "version" => manifest.version = new_value.to_string(),
            "description" => manifest.description = Some(new_value.to_string()),
            "author" => manifest.author = Some(new_value.to_string()),
            _ => {
                // For unknown fields, store in metadata
                manifest
                    .metadata
                    .insert(field.to_string(), new_value.to_string());
            }
        }
        Ok(())
    }

    /// Save delta patch to file
    pub fn save_patch<P: AsRef<Path>>(patch: &DeltaPatch, path: P) -> Result<()> {
        let serialized = oxicode::serde::encode_to_vec(patch, oxicode::config::standard())
            .map_err(|e| TorshError::SerializationError(e.to_string()))?;

        fs::write(path, serialized).map_err(|e| TorshError::IoError(e.to_string()))?;

        Ok(())
    }

    /// Load delta patch from file
    pub fn load_patch<P: AsRef<Path>>(path: P) -> Result<DeltaPatch> {
        let data = fs::read(path).map_err(|e| TorshError::IoError(e.to_string()))?;

        let (patch, _): (DeltaPatch, usize) =
            oxicode::serde::decode_from_slice(&data, oxicode::config::standard())
                .map_err(|e| TorshError::SerializationError(e.to_string()))?;

        Ok(patch)
    }
}

/// Extension trait for Package to add delta functionality
pub trait DeltaPackageExt {
    /// Create a delta patch from this package to another
    fn create_delta_to(&self, target: &Package) -> Result<DeltaPatch>;

    /// Apply a delta patch to this package
    fn apply_delta(&mut self, patch: &DeltaPatch) -> Result<()>;

    /// Get package hash for delta verification
    fn get_package_hash(&self) -> String;
}

impl DeltaPackageExt for Package {
    fn create_delta_to(&self, target: &Package) -> Result<DeltaPatch> {
        let builder = DeltaPatchBuilder::new();
        builder.create_patch(self, target)
    }

    fn apply_delta(&mut self, patch: &DeltaPatch) -> Result<()> {
        let applier = DeltaPatchApplier::new();
        applier.apply_patch(self, patch)
    }

    fn get_package_hash(&self) -> String {
        calculate_hash(
            &oxicode::serde::encode_to_vec(&self.manifest, oxicode::config::standard())
                .expect("manifest serialization should succeed"),
        )
    }
}

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

    #[test]
    fn test_delta_patch_builder() {
        let builder = DeltaPatchBuilder::new()
            .with_compression_level(9)
            .with_metadata_changes(true)
            .with_max_diff_ratio(0.5);

        assert_eq!(builder.compression_level, 9);
        assert!(builder.include_metadata_changes);
        assert!((builder.max_diff_size_ratio - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn test_delta_patch_applier() {
        let applier = DeltaPatchApplier::new()
            .with_checksum_verification(false)
            .with_backup(false);

        assert!(!applier.verify_checksums);
        assert!(!applier.backup_original);
    }

    #[test]
    fn test_simple_delta_creation() {
        let old_package = Package::new("test".to_string(), "1.0.0".to_string());
        let mut new_package = Package::new("test".to_string(), "1.1.0".to_string());

        // Add a resource to the new package
        let resource = Resource {
            name: "test.txt".to_string(),
            resource_type: ResourceType::Data,
            data: b"Hello, World!".to_vec(),
            metadata: HashMap::new(),
        };
        new_package
            .resources
            .insert("test.txt".to_string(), resource);

        let builder = DeltaPatchBuilder::new();
        let patch = builder.create_patch(&old_package, &new_package).unwrap();

        assert_eq!(patch.from_version, "1.0.0");
        assert_eq!(patch.to_version, "1.1.0");
        assert!(!patch.operations.is_empty());
    }

    #[test]
    fn test_patch_serialization() {
        let patch = DeltaPatch {
            from_version: "1.0.0".to_string(),
            to_version: "1.1.0".to_string(),
            patch_format_version: "1.0.0".to_string(),
            operations: vec![DeltaOperation::Add {
                resource_name: "test.txt".to_string(),
                resource_data: b"Hello".to_vec(),
                metadata: HashMap::new(),
            }],
            created_at: chrono::Utc::now(),
            patch_size: 5,
            target_hash: "abc123".to_string(),
            metadata: HashMap::new(),
        };

        // Test serialization and deserialization
        let serialized =
            oxicode::serde::encode_to_vec(&patch, oxicode::config::standard()).unwrap();
        let (deserialized, _): (DeltaPatch, usize) =
            oxicode::serde::decode_from_slice(&serialized, oxicode::config::standard()).unwrap();

        assert_eq!(patch.from_version, deserialized.from_version);
        assert_eq!(patch.to_version, deserialized.to_version);
        assert_eq!(patch.operations.len(), deserialized.operations.len());
    }
}