ipfrs-tensorlogic 0.2.0

Zero-copy tensor operations and logic programming for content-addressed 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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
//! Codec Registry — compression/encoding codec selection and peer negotiation
//!
//! Tensor data flows through multiple compression/encoding stages. This module
//! manages codec selection and negotiation between peers so that both sides
//! agree on a common codec before data is transmitted.
//!
//! # Overview
//!
//! [`CodecRegistry`] is the central store of [`CodecDescriptor`] entries, each
//! keyed by a [`CodecId`].  Seven built-in codecs are pre-registered on
//! construction:
//!
//! | Id | Name       | Speed class  | Ratio est. |
//! |----|------------|--------------|------------|
//! | 0  | none       | VeryFast     | 1.00       |
//! | 1  | zstd       | Balanced     | 0.30       |
//! | 2  | lz4        | VeryFast     | 0.55       |
//! | 3  | snappy     | VeryFast     | 0.60       |
//! | 4  | brotli     | Slow         | 0.25       |
//! | 10 | arrow_ipc  | Fast         | 0.70       |
//! | 11 | garw       | Fast         | 0.40       |
//!
//! Codec negotiation follows a "first-match" policy: the first codec in the
//! *local* preference list that is also present in the *remote* list wins.
//!
//! # Examples
//!
//! ```
//! use ipfrs_tensorlogic::codec_registry::{CodecId, CodecRegistry};
//!
//! let registry = CodecRegistry::new();
//!
//! // Look up ZSTD
//! let desc = registry.get(CodecId::ZSTD).expect("example: should succeed in docs");
//! assert_eq!(desc.name, "zstd");
//!
//! // Negotiate between two peers
//! let local  = vec![CodecId::ZSTD, CodecId::LZ4, CodecId::NONE];
//! let remote = vec![CodecId::LZ4,  CodecId::NONE];
//! let agreed = CodecRegistry::negotiate(&local, &remote);
//! assert_eq!(agreed, Some(CodecId::LZ4));
//! ```

use std::collections::HashMap;
use std::fmt;
use std::time::Instant;
use thiserror::Error;

// ─── CodecId ─────────────────────────────────────────────────────────────────

/// Opaque identifier for a compression/encoding codec.
///
/// Use the associated constants (`NONE`, `ZSTD`, …) for well-known codecs.
/// Custom codecs should use ids ≥ 1 000 to avoid conflicts with future
/// built-in ids.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct CodecId(pub u32);

impl CodecId {
    /// No codec — data is passed through unmodified.
    pub const NONE: Self = Self(0);
    /// Zstandard (zstd) compression.
    pub const ZSTD: Self = Self(1);
    /// LZ4 block compression.
    pub const LZ4: Self = Self(2);
    /// Snappy compression.
    pub const SNAPPY: Self = Self(3);
    /// Brotli compression.
    pub const BROTLI: Self = Self(4);
    /// Apache Arrow IPC framing/encoding.
    pub const ARROW_IPC: Self = Self(10);
    /// GARW (Generic Arrow Row-Wire) encoding.
    pub const GARW: Self = Self(11);

    /// Return the raw numeric value of this id.
    #[inline]
    pub fn value(self) -> u32 {
        self.0
    }
}

impl fmt::Display for CodecId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "CodecId({})", self.0)
    }
}

impl From<u32> for CodecId {
    fn from(v: u32) -> Self {
        Self(v)
    }
}

impl From<CodecId> for u32 {
    fn from(c: CodecId) -> Self {
        c.0
    }
}

// ─── SpeedClass ──────────────────────────────────────────────────────────────

/// Qualitative speed tier for a codec.
///
/// The ordering is `VeryFast < Fast < Balanced < Slow < VerySlow`, i.e.
/// a *smaller* variant is *faster*.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum SpeedClass {
    /// Negligible encoding/decoding overhead (e.g. pass-through, LZ4).
    VeryFast,
    /// Low overhead; suitable for latency-sensitive paths.
    Fast,
    /// Moderate overhead; good balance of speed and compression.
    Balanced,
    /// High overhead; use when CPU is not the bottleneck.
    Slow,
    /// Very high overhead; only for batch/offline scenarios.
    VerySlow,
}

// ─── CodecDescriptor ─────────────────────────────────────────────────────────

/// Metadata about a single codec.
#[derive(Debug, Clone)]
pub struct CodecDescriptor {
    /// Stable numeric identifier.
    pub id: CodecId,
    /// Human-readable name (e.g. `"zstd"`).
    pub name: String,
    /// Estimated output-to-input byte ratio after compression.
    ///
    /// A value of `0.3` means the compressed output is 30 % of the original
    /// size (70 % reduction).  `1.0` means no compression.
    pub compression_ratio_estimate: f32,
    /// Qualitative speed tier.
    pub speed_class: SpeedClass,
    /// Whether the codec is bit-for-bit lossless.
    pub is_lossless: bool,
}

// ─── CodecError ──────────────────────────────────────────────────────────────

/// Errors that can occur while working with [`CodecRegistry`].
#[derive(Debug, Error)]
pub enum CodecError {
    /// A codec with the given id is already registered.
    #[error("codec id {0} is already registered")]
    AlreadyRegistered(u32),
    /// No codec with the given id exists in the registry.
    #[error("unknown codec id {0}")]
    UnknownCodec(u32),
}

// ─── CodecNegotiationRecord ───────────────────────────────────────────────────

/// Record of a single codec negotiation between the local peer and a remote
/// peer.
#[derive(Debug)]
pub struct CodecNegotiationRecord {
    /// Codec ids offered by the local side, in preference order.
    pub local_offered: Vec<CodecId>,
    /// Codec ids offered by the remote side, in any order.
    pub remote_offered: Vec<CodecId>,
    /// The codec that was agreed upon, or `None` if no common codec was found.
    pub agreed: Option<CodecId>,
    /// Wall-clock time at which negotiation completed.
    pub negotiated_at: Instant,
    /// Duration of the negotiation in milliseconds.
    pub negotiation_ms: u64,
}

// ─── CodecRegistry ───────────────────────────────────────────────────────────

/// Central registry of compression/encoding codecs.
///
/// On construction the seven built-in codecs are pre-registered.  Custom codecs
/// can be added via [`CodecRegistry::register`]; duplicate ids are rejected.
pub struct CodecRegistry {
    /// Internal map from `CodecId` value to descriptor.
    codecs: HashMap<u32, CodecDescriptor>,
}

impl CodecRegistry {
    /// Construct a new registry with all seven built-in codecs pre-registered.
    pub fn new() -> Self {
        let mut registry = Self {
            codecs: HashMap::new(),
        };

        let built_ins: &[CodecDescriptor] = &[
            CodecDescriptor {
                id: CodecId::NONE,
                name: "none".to_string(),
                compression_ratio_estimate: 1.0,
                speed_class: SpeedClass::VeryFast,
                is_lossless: true,
            },
            CodecDescriptor {
                id: CodecId::ZSTD,
                name: "zstd".to_string(),
                compression_ratio_estimate: 0.30,
                speed_class: SpeedClass::Balanced,
                is_lossless: true,
            },
            CodecDescriptor {
                id: CodecId::LZ4,
                name: "lz4".to_string(),
                compression_ratio_estimate: 0.55,
                speed_class: SpeedClass::VeryFast,
                is_lossless: true,
            },
            CodecDescriptor {
                id: CodecId::SNAPPY,
                name: "snappy".to_string(),
                compression_ratio_estimate: 0.60,
                speed_class: SpeedClass::VeryFast,
                is_lossless: true,
            },
            CodecDescriptor {
                id: CodecId::BROTLI,
                name: "brotli".to_string(),
                compression_ratio_estimate: 0.25,
                speed_class: SpeedClass::Slow,
                is_lossless: true,
            },
            CodecDescriptor {
                id: CodecId::ARROW_IPC,
                name: "arrow_ipc".to_string(),
                compression_ratio_estimate: 0.70,
                speed_class: SpeedClass::Fast,
                is_lossless: true,
            },
            CodecDescriptor {
                id: CodecId::GARW,
                name: "garw".to_string(),
                compression_ratio_estimate: 0.40,
                speed_class: SpeedClass::Fast,
                is_lossless: true,
            },
        ];

        for desc in built_ins {
            // These are known-unique; insertion cannot fail at construction time.
            registry.codecs.insert(desc.id.value(), desc.clone());
        }

        registry
    }

    /// Register a custom [`CodecDescriptor`].
    ///
    /// Returns [`CodecError::AlreadyRegistered`] if a codec with the same id
    /// already exists in the registry.
    pub fn register(&mut self, descriptor: CodecDescriptor) -> Result<(), CodecError> {
        let key = descriptor.id.value();
        if self.codecs.contains_key(&key) {
            return Err(CodecError::AlreadyRegistered(key));
        }
        self.codecs.insert(key, descriptor);
        Ok(())
    }

    /// Look up a codec by its id.
    ///
    /// Returns `None` when the id is not registered.
    pub fn get(&self, id: CodecId) -> Option<&CodecDescriptor> {
        self.codecs.get(&id.value())
    }

    /// Return all registered codecs, sorted in ascending [`CodecId`] order.
    pub fn list_all(&self) -> Vec<&CodecDescriptor> {
        let mut entries: Vec<&CodecDescriptor> = self.codecs.values().collect();
        entries.sort_by_key(|d| d.id);
        entries
    }

    /// Negotiate a codec between two peers.
    ///
    /// Returns the first id in `local` that also appears in `remote`, or `None`
    /// if there is no common codec.  This is a static method because negotiation
    /// does not require access to a registry instance — it operates purely on
    /// the id sets.
    pub fn negotiate(local: &[CodecId], remote: &[CodecId]) -> Option<CodecId> {
        // Build a hash-set from the remote list for O(1) lookup.
        let remote_set: std::collections::HashSet<CodecId> = remote.iter().copied().collect();
        local.iter().find(|id| remote_set.contains(id)).copied()
    }

    /// Return a reference to the fastest registered codec.
    ///
    /// When multiple codecs share the best speed class the one with the
    /// numerically smallest [`CodecId`] is returned for determinism.
    ///
    /// Returns `None` only if the registry is empty (not possible after
    /// `new()` unless all entries were somehow replaced).
    pub fn best_for_speed(&self) -> Option<&CodecDescriptor> {
        self.codecs.values().min_by(|a, b| {
            a.speed_class
                .cmp(&b.speed_class)
                .then_with(|| a.id.cmp(&b.id))
        })
    }

    /// Return a reference to the codec with the lowest
    /// `compression_ratio_estimate` (best compression).
    ///
    /// When multiple codecs share the same ratio the one with the numerically
    /// smallest [`CodecId`] is returned for determinism.  NaN ratio values are
    /// sorted last.
    ///
    /// Returns `None` only if the registry is empty.
    pub fn best_for_compression(&self) -> Option<&CodecDescriptor> {
        self.codecs.values().min_by(|a, b| {
            a.compression_ratio_estimate
                .partial_cmp(&b.compression_ratio_estimate)
                .unwrap_or(std::cmp::Ordering::Equal)
                .then_with(|| a.id.cmp(&b.id))
        })
    }
}

impl Default for CodecRegistry {
    fn default() -> Self {
        Self::new()
    }
}

// ─── Tests ───────────────────────────────────────────────────────────────────

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

    // ── 1. All pre-registered codecs are present ──────────────────────────────

    #[test]
    fn test_builtin_codecs_all_present() {
        let r = CodecRegistry::new();
        let ids = [
            CodecId::NONE,
            CodecId::ZSTD,
            CodecId::LZ4,
            CodecId::SNAPPY,
            CodecId::BROTLI,
            CodecId::ARROW_IPC,
            CodecId::GARW,
        ];
        for id in &ids {
            assert!(
                r.get(*id).is_some(),
                "codec {} should be pre-registered",
                id
            );
        }
    }

    // ── 2. Correct codec count ────────────────────────────────────────────────

    #[test]
    fn test_builtin_codec_count() {
        let r = CodecRegistry::new();
        assert_eq!(r.list_all().len(), 7);
    }

    // ── 3. `get` returns the correct descriptor ────────────────────────────────

    #[test]
    fn test_get_zstd_descriptor() {
        let r = CodecRegistry::new();
        let desc = r.get(CodecId::ZSTD).expect("ZSTD should exist");
        assert_eq!(desc.id, CodecId::ZSTD);
        assert_eq!(desc.name, "zstd");
        assert!(desc.is_lossless);
        assert_eq!(desc.speed_class, SpeedClass::Balanced);
    }

    #[test]
    fn test_get_none_descriptor() {
        let r = CodecRegistry::new();
        let desc = r.get(CodecId::NONE).expect("NONE should exist");
        assert_eq!(desc.compression_ratio_estimate, 1.0);
        assert_eq!(desc.speed_class, SpeedClass::VeryFast);
    }

    // ── 4. `get` returns None for unknown id ─────────────────────────────────

    #[test]
    fn test_get_unknown_returns_none() {
        let r = CodecRegistry::new();
        assert!(r.get(CodecId::from(9999)).is_none());
    }

    // ── 5. `negotiate` finds intersection ────────────────────────────────────

    #[test]
    fn test_negotiate_finds_common_codec() {
        let local = vec![CodecId::ZSTD, CodecId::LZ4, CodecId::NONE];
        let remote = vec![CodecId::LZ4, CodecId::NONE];
        let agreed = CodecRegistry::negotiate(&local, &remote);
        assert_eq!(agreed, Some(CodecId::LZ4));
    }

    #[test]
    fn test_negotiate_respects_local_preference_order() {
        // local prefers ZSTD; remote supports all — ZSTD wins
        let local = vec![CodecId::ZSTD, CodecId::LZ4, CodecId::NONE];
        let remote = vec![CodecId::NONE, CodecId::LZ4, CodecId::ZSTD];
        let agreed = CodecRegistry::negotiate(&local, &remote);
        assert_eq!(agreed, Some(CodecId::ZSTD));
    }

    // ── 6. `negotiate` returns None when no common codec ──────────────────────

    #[test]
    fn test_negotiate_no_common_returns_none() {
        let local = vec![CodecId::ZSTD, CodecId::BROTLI];
        let remote = vec![CodecId::LZ4, CodecId::SNAPPY];
        assert_eq!(CodecRegistry::negotiate(&local, &remote), None);
    }

    #[test]
    fn test_negotiate_empty_lists_returns_none() {
        assert_eq!(CodecRegistry::negotiate(&[], &[]), None);
        assert_eq!(CodecRegistry::negotiate(&[CodecId::ZSTD], &[]), None);
        assert_eq!(CodecRegistry::negotiate(&[], &[CodecId::ZSTD]), None);
    }

    // ── 7. `best_for_speed` returns a VeryFast codec ─────────────────────────

    #[test]
    fn test_best_for_speed_is_very_fast() {
        let r = CodecRegistry::new();
        let best = r.best_for_speed().expect("registry is non-empty");
        assert_eq!(
            best.speed_class,
            SpeedClass::VeryFast,
            "expected VeryFast, got {:?}",
            best.speed_class
        );
    }

    // ── 8. `best_for_compression` returns the lowest ratio ────────────────────

    #[test]
    fn test_best_for_compression_is_lowest_ratio() {
        let r = CodecRegistry::new();
        let best = r.best_for_compression().expect("registry is non-empty");
        // Brotli has ratio 0.25, which is the lowest among built-ins
        assert_eq!(
            best.id,
            CodecId::BROTLI,
            "expected BROTLI (0.25), got {} ({})",
            best.name,
            best.compression_ratio_estimate
        );
    }

    // ── 9. `register` custom codec succeeds ───────────────────────────────────

    #[test]
    fn test_register_custom_codec() {
        let mut r = CodecRegistry::new();
        let custom = CodecDescriptor {
            id: CodecId::from(1000),
            name: "my_codec".to_string(),
            compression_ratio_estimate: 0.45,
            speed_class: SpeedClass::Fast,
            is_lossless: true,
        };
        r.register(custom)
            .expect("custom registration should succeed");
        let found = r
            .get(CodecId::from(1000))
            .expect("custom codec should exist");
        assert_eq!(found.name, "my_codec");
    }

    // ── 10. Duplicate registration returns AlreadyRegistered error ───────────

    #[test]
    fn test_register_duplicate_returns_error() {
        let mut r = CodecRegistry::new();
        let dup = CodecDescriptor {
            id: CodecId::ZSTD, // already registered
            name: "duplicate_zstd".to_string(),
            compression_ratio_estimate: 0.30,
            speed_class: SpeedClass::Balanced,
            is_lossless: true,
        };
        let err = r.register(dup).expect_err("duplicate should be rejected");
        match err {
            CodecError::AlreadyRegistered(id) => assert_eq!(id, CodecId::ZSTD.value()),
            other => panic!("unexpected error: {}", other),
        }
    }

    // ── 11. `list_all` returns entries in ascending CodecId order ─────────────

    #[test]
    fn test_list_all_sorted_order() {
        let r = CodecRegistry::new();
        let list = r.list_all();
        let ids: Vec<u32> = list.iter().map(|d| d.id.value()).collect();
        let mut sorted = ids.clone();
        sorted.sort_unstable();
        assert_eq!(ids, sorted, "list_all() must be sorted by CodecId");
    }

    #[test]
    fn test_list_all_first_is_none() {
        let r = CodecRegistry::new();
        let list = r.list_all();
        assert_eq!(list[0].id, CodecId::NONE);
    }

    // ── 12. CodecId constants have correct numeric values ────────────────────

    #[test]
    fn test_codec_id_constants() {
        assert_eq!(CodecId::NONE.value(), 0);
        assert_eq!(CodecId::ZSTD.value(), 1);
        assert_eq!(CodecId::LZ4.value(), 2);
        assert_eq!(CodecId::SNAPPY.value(), 3);
        assert_eq!(CodecId::BROTLI.value(), 4);
        assert_eq!(CodecId::ARROW_IPC.value(), 10);
        assert_eq!(CodecId::GARW.value(), 11);
    }

    // ── 13. CodecId Display ───────────────────────────────────────────────────

    #[test]
    fn test_codec_id_display() {
        assert_eq!(CodecId::ZSTD.to_string(), "CodecId(1)");
        assert_eq!(CodecId::NONE.to_string(), "CodecId(0)");
    }

    // ── 14. CodecNegotiationRecord stores correct data ────────────────────────

    #[test]
    fn test_codec_negotiation_record() {
        let local = vec![CodecId::ZSTD, CodecId::LZ4];
        let remote = vec![CodecId::LZ4];
        let start = Instant::now();
        let agreed = CodecRegistry::negotiate(&local, &remote);
        let elapsed_ms = start.elapsed().as_millis() as u64;

        let record = CodecNegotiationRecord {
            local_offered: local.clone(),
            remote_offered: remote.clone(),
            agreed,
            negotiated_at: Instant::now(),
            negotiation_ms: elapsed_ms,
        };

        assert_eq!(record.agreed, Some(CodecId::LZ4));
        assert_eq!(record.local_offered.len(), 2);
        assert_eq!(record.remote_offered.len(), 1);
    }

    // ── 15. SpeedClass ordering ────────────────────────────────────────────────

    #[test]
    fn test_speed_class_ordering() {
        assert!(SpeedClass::VeryFast < SpeedClass::Fast);
        assert!(SpeedClass::Fast < SpeedClass::Balanced);
        assert!(SpeedClass::Balanced < SpeedClass::Slow);
        assert!(SpeedClass::Slow < SpeedClass::VerySlow);
    }

    // ── 16. best_for_speed with registry containing only slow codecs ──────────

    #[test]
    fn test_best_for_speed_custom_only_slow() {
        let mut r = CodecRegistry::new();
        // Add an even slower custom codec; the result should still be the
        // built-in VeryFast codec.
        let custom = CodecDescriptor {
            id: CodecId::from(2000),
            name: "super_slow".to_string(),
            compression_ratio_estimate: 0.10,
            speed_class: SpeedClass::VerySlow,
            is_lossless: true,
        };
        r.register(custom).expect("should register");
        let best = r.best_for_speed().expect("non-empty");
        assert_eq!(best.speed_class, SpeedClass::VeryFast);
    }

    // ── 17. UnknownCodec error variant Display ────────────────────────────────

    #[test]
    fn test_unknown_codec_error_display() {
        let err = CodecError::UnknownCodec(42);
        assert!(err.to_string().contains("42"));
    }

    // ── 18. Default impl matches new() ────────────────────────────────────────

    #[test]
    fn test_default_matches_new() {
        let a = CodecRegistry::new();
        let b = CodecRegistry::default();
        assert_eq!(a.list_all().len(), b.list_all().len());
    }
}