datacortex-core 0.6.0

JSON/NDJSON-optimized lossless compression. Schema inference, columnar reorg, typed encoding. Beats zstd-19 by up to 113%
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
//! Format detection and preprocessing pipeline.
//!
//! Phase 0: heuristic detection.
//! Phase 1: format-aware preprocessing (JSON key interning) + detection.

pub mod json;
pub mod json_array;
pub mod ndjson;
pub mod schema;
pub mod transform;
pub mod typed_encoding;
pub mod value_dict;

use std::borrow::Cow;

use crate::dcx::{FormatHint, Mode};
use transform::{
    TRANSFORM_JSON_ARRAY_COLUMNAR, TRANSFORM_JSON_KEY_INTERN, TRANSFORM_NDJSON_COLUMNAR,
    TRANSFORM_NESTED_FLATTEN, TRANSFORM_TYPED_ENCODING, TRANSFORM_VALUE_DICT, TransformChain,
};

/// Detect file format from content bytes.
pub fn detect_format(data: &[u8]) -> FormatHint {
    if data.is_empty() {
        return FormatHint::Generic;
    }

    let trimmed = trim_leading_whitespace(data);

    if starts_with_byte(trimmed, b'{') || starts_with_byte(trimmed, b'[') {
        if is_ndjson(data) {
            return FormatHint::Ndjson;
        }
        return FormatHint::Json;
    }

    FormatHint::Generic
}

/// Detect format from file extension (fallback).
pub fn detect_from_extension(path: &str) -> Option<FormatHint> {
    let ext = path.rsplit('.').next()?.to_lowercase();
    match ext.as_str() {
        "json" => Some(FormatHint::Json),
        "ndjson" | "jsonl" => Some(FormatHint::Ndjson),
        _ => None,
    }
}

/// Apply format-aware preprocessing transforms.
/// Returns (preprocessed_data, transform_chain).
///
/// NDJSON columnar: ALL modes (grouping similar values helps both zstd and CM).
/// Key interning: Balanced/Max only (hurts Fast mode due to zstd redundancy).
/// For NDJSON, columnar is applied FIRST — if it succeeds, key interning is skipped
/// (keys are already removed from the data stream by the columnar transform).
pub fn preprocess(data: &[u8], format: FormatHint, mode: Mode) -> (Vec<u8>, TransformChain) {
    let mut chain = TransformChain::new();
    let mut current: Cow<'_, [u8]> = Cow::Borrowed(data);

    // Track whether a uniform columnar transform was applied (for value dict chaining).
    // Uniform columnar = data is \x00/\x01-separated, downstream transforms are compatible.
    let mut columnar_applied = false;
    // Track whether ANY ndjson transform was applied (uniform or grouped).
    let mut ndjson_transform_applied = false;

    // NDJSON columnar reorg: ALL modes (dramatic improvement for uniform NDJSON).
    // Strategy 1 (uniform, version=1) produces \x00/\x01 separated columnar data.
    // Strategy 2 (grouped, version=2) produces a different format with per-group data.
    // Only Strategy 1 output is compatible with downstream typed_encoding/value_dict.
    if format == FormatHint::Ndjson {
        if let Some(result) = ndjson::preprocess(&current) {
            let is_uniform_columnar = !result.metadata.is_empty() && result.metadata[0] == 1;
            chain.push(TRANSFORM_NDJSON_COLUMNAR, result.metadata);
            current = Cow::Owned(result.data);
            ndjson_transform_applied = true;
            columnar_applied = is_uniform_columnar;
        }
    }

    // JSON array columnar reorg: ALL modes.
    // Strategy 1 (uniform, version=1) produces \x00/\x01 separated columnar data.
    // Strategy 2 (grouped, version=2) produces a different format with per-group data.
    // Only Strategy 1 output is compatible with downstream typed_encoding/value_dict/nested_flatten.
    let mut json_array_applied = false;
    if !columnar_applied && !ndjson_transform_applied && format == FormatHint::Json {
        if let Some(result) = json_array::preprocess(&current) {
            let is_uniform = !result.metadata.is_empty() && result.metadata[0] == 1;
            chain.push(TRANSFORM_JSON_ARRAY_COLUMNAR, result.metadata);
            current = Cow::Owned(result.data);
            json_array_applied = true;
            columnar_applied = is_uniform;
        }
    }

    // Nested flatten: decompose nested objects into sub-columns.
    // Works on any \x00/\x01 columnar data. Only for non-NDJSON paths because
    // the NDJSON uniform path already handles its own nested flatten internally.
    if columnar_applied && !ndjson_transform_applied {
        // Extract num_rows from the json_array metadata (offset 1, u32 LE).
        let ja_meta = &chain.records.last().unwrap().metadata;
        if ja_meta.len() >= 5 {
            let num_rows = u32::from_le_bytes(ja_meta[1..5].try_into().unwrap()) as usize;
            if let Some((flat_data, nested_groups)) =
                ndjson::flatten_nested_columns(&current, num_rows)
            {
                // Build metadata: num_rows + total_flat_cols + serialized nested info.
                let total_flat_cols = flat_data.split(|&b| b == 0x00).count() as u16;

                // Verify roundtrip: unflatten must produce the exact original columnar
                // data. Nested objects with varying sub-key sets or key ordering can
                // cause the compact reconstruction to reorder keys, breaking byte-exact
                // roundtrip. Only apply if the unflatten is provably lossless.
                let unflattened = ndjson::unflatten_nested_columns(
                    &flat_data,
                    &nested_groups,
                    num_rows,
                    total_flat_cols as usize,
                );
                if unflattened == current.as_ref() {
                    let mut nested_meta = Vec::new();
                    nested_meta.extend_from_slice(&(num_rows as u32).to_le_bytes());
                    nested_meta.extend_from_slice(&total_flat_cols.to_le_bytes());
                    nested_meta.extend_from_slice(&ndjson::serialize_nested_info(&nested_groups));
                    chain.push(TRANSFORM_NESTED_FLATTEN, nested_meta);
                    current = Cow::Owned(flat_data);
                }
                // else: roundtrip not exact — skip nested flatten (data stays columnar
                // without sub-column decomposition, still benefits from typed encoding
                // and value dict on the outer columns).
            }
        }
    }

    // Typed encoding: Fast mode ONLY. CM mode doesn't benefit (gotcha #35 confirmed).
    // Binary encoding disrupts CM's learned text patterns. But zstd benefits from
    // smaller raw data (delta varints, boolean bitmaps).
    if columnar_applied && mode == Mode::Fast {
        if let Some(result) = typed_encoding::preprocess(&current) {
            chain.push(TRANSFORM_TYPED_ENCODING, result.metadata);
            current = Cow::Owned(result.data);
        }
    }

    // Value dictionary: chain AFTER any columnar transform.
    // Replaces repeated multi-byte values with single-byte codes.
    // Only applies to columnar data (uses \x00/\x01 separators).
    // NOTE: value dict only operates on \x00/\x01-separated data.
    // If typed encoding was applied, the data is now binary (no separators),
    // so value dict will naturally not apply (it won't find separators to split on,
    // or its size check will fail).
    if columnar_applied {
        if let Some(result) = value_dict::preprocess(&current) {
            chain.push(TRANSFORM_VALUE_DICT, result.metadata);
            current = Cow::Owned(result.data);
        }
    }

    if columnar_applied || ndjson_transform_applied || json_array_applied {
        return (current.into_owned(), chain);
    }

    // JSON key interning: Balanced/Max only (hurts Fast mode due to zstd redundancy).
    if matches!(mode, Mode::Balanced | Mode::Max)
        && matches!(format, FormatHint::Json | FormatHint::Ndjson)
        && let Some(result) = json::preprocess(&current)
    {
        chain.push(TRANSFORM_JSON_KEY_INTERN, result.metadata);
        current = Cow::Owned(result.data);
    }

    (current.into_owned(), chain)
}

/// Reverse preprocessing transforms (applied in reverse order).
pub fn reverse_preprocess(data: &[u8], chain: &TransformChain) -> Vec<u8> {
    let mut current: Cow<'_, [u8]> = Cow::Borrowed(data);

    // Apply in reverse order.
    for record in chain.records.iter().rev() {
        match record.id {
            TRANSFORM_JSON_KEY_INTERN => {
                current = Cow::Owned(json::reverse(&current, &record.metadata));
            }
            TRANSFORM_NDJSON_COLUMNAR => {
                current = Cow::Owned(ndjson::reverse(&current, &record.metadata));
            }
            TRANSFORM_JSON_ARRAY_COLUMNAR => {
                current = Cow::Owned(json_array::reverse(&current, &record.metadata));
            }
            TRANSFORM_VALUE_DICT => {
                current = Cow::Owned(value_dict::reverse(&current, &record.metadata));
            }
            TRANSFORM_TYPED_ENCODING => {
                current = Cow::Owned(typed_encoding::reverse(&current, &record.metadata));
            }
            TRANSFORM_NESTED_FLATTEN => {
                // Metadata: num_rows (u32 LE) + total_flat_cols (u16 LE) + nested_info.
                if record.metadata.len() >= 6 {
                    let num_rows =
                        u32::from_le_bytes(record.metadata[0..4].try_into().unwrap()) as usize;
                    let total_flat_cols =
                        u16::from_le_bytes(record.metadata[4..6].try_into().unwrap()) as usize;
                    if let Some((nested_groups, _)) =
                        ndjson::deserialize_nested_info(&record.metadata[6..])
                    {
                        current = Cow::Owned(ndjson::unflatten_nested_columns(
                            &current,
                            &nested_groups,
                            num_rows,
                            total_flat_cols,
                        ));
                    }
                }
            }
            _ => {} // Unknown/legacy transform — skip.
        }
    }

    current.into_owned()
}

// --- Detection helpers (unchanged from Phase 0) ---

fn trim_leading_whitespace(data: &[u8]) -> &[u8] {
    let start = data
        .iter()
        .position(|&b| !b.is_ascii_whitespace())
        .unwrap_or(data.len());
    &data[start..]
}

fn starts_with_byte(data: &[u8], byte: u8) -> bool {
    data.first() == Some(&byte)
}

fn is_ndjson(data: &[u8]) -> bool {
    let mut json_lines = 0;
    let mut total_lines = 0;

    for line in data.split(|&b| b == b'\n') {
        let trimmed = trim_leading_whitespace(line);
        if trimmed.is_empty() {
            continue;
        }
        total_lines += 1;
        if starts_with_byte(trimmed, b'{') {
            json_lines += 1;
        }
    }

    total_lines >= 2 && json_lines as f64 / total_lines as f64 > 0.8
}

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

    #[test]
    fn detect_json() {
        assert_eq!(detect_format(b"  {\"key\": \"value\"}"), FormatHint::Json);
        assert_eq!(detect_format(b"[1, 2, 3]"), FormatHint::Json);
    }

    #[test]
    fn detect_ndjson() {
        let data = b"{\"a\":1}\n{\"b\":2}\n{\"c\":3}\n";
        assert_eq!(detect_format(data), FormatHint::Ndjson);
    }

    #[test]
    fn detect_generic_fallback() {
        assert_eq!(detect_format(b""), FormatHint::Generic);
        assert_eq!(detect_format(b"just some random text"), FormatHint::Generic);
    }

    #[test]
    fn extension_detection() {
        assert_eq!(detect_from_extension("test.json"), Some(FormatHint::Json));
        assert_eq!(
            detect_from_extension("data.ndjson"),
            Some(FormatHint::Ndjson)
        );
        assert_eq!(detect_from_extension("file.txt"), None);
    }

    #[test]
    fn preprocess_json_key_interning() {
        let data = br#"{"name":"Alice","age":30,"name":"Bob","age":25}"#;
        let (preprocessed, chain) = preprocess(data, FormatHint::Json, Mode::Balanced);
        assert!(!chain.is_empty(), "should have applied key interning");
        assert!(
            preprocessed.len() < data.len(),
            "preprocessed should be smaller"
        );

        // Reverse and verify.
        let restored = reverse_preprocess(&preprocessed, &chain);
        assert_eq!(restored, data.to_vec());
    }

    #[test]
    fn preprocess_ndjson_columnar() {
        let data = br#"{"ts":"a","val":1}
{"ts":"b","val":2}
{"ts":"c","val":3}
"#;
        let (preprocessed, chain) = preprocess(data, FormatHint::Ndjson, Mode::Balanced);
        assert!(!chain.is_empty());
        // Should use columnar transform (ID 2), not key interning.
        assert_eq!(
            chain.records[0].id,
            transform::TRANSFORM_NDJSON_COLUMNAR,
            "NDJSON should use columnar transform"
        );

        let restored = reverse_preprocess(&preprocessed, &chain);
        assert_eq!(restored, data.to_vec());
    }

    #[test]
    fn preprocess_ndjson_columnar_fast_mode() {
        // Columnar should apply for ALL modes, including Fast.
        let data = br#"{"ts":"a","val":1}
{"ts":"b","val":2}
{"ts":"c","val":3}
"#;
        let (preprocessed, chain) = preprocess(data, FormatHint::Ndjson, Mode::Fast);
        assert!(!chain.is_empty());
        assert_eq!(chain.records[0].id, transform::TRANSFORM_NDJSON_COLUMNAR);

        let restored = reverse_preprocess(&preprocessed, &chain);
        assert_eq!(restored, data.to_vec());

        // Verify columnar data groups values.
        let cols: Vec<&[u8]> = preprocessed.split(|&b| b == 0x00).collect();
        assert_eq!(cols.len(), 2, "should have 2 columns");
    }

    #[test]
    fn preprocess_json_array_columnar() {
        let data = br#"{"data": [{"id": 1, "type": "a"}, {"id": 2, "type": "b"}, {"id": 3, "type": "c"}, {"id": 4, "type": "d"}, {"id": 5, "type": "e"}], "meta": {"count": 5}}"#;
        let (preprocessed, chain) = preprocess(data, FormatHint::Json, Mode::Balanced);
        assert!(!chain.is_empty());
        assert_eq!(
            chain.records[0].id,
            transform::TRANSFORM_JSON_ARRAY_COLUMNAR,
            "JSON with array should use array columnar transform"
        );

        let restored = reverse_preprocess(&preprocessed, &chain);
        assert_eq!(restored, data.to_vec());
    }

    #[test]
    fn preprocess_json_array_too_few_falls_through() {
        // Only 3 elements — below MIN_ROWS, should fall through to key interning.
        let data = br#"{"data": [{"id": 1, "type": "a"}, {"id": 2, "type": "a"}, {"id": 3, "type": "a"}], "meta": {"count": 3}, "data2": [{"id": 1, "type": "a"}, {"id": 2, "type": "a"}, {"id": 3, "type": "a"}]}"#;
        let (preprocessed, chain) = preprocess(data, FormatHint::Json, Mode::Balanced);
        // Should fall through to key interning (not array columnar).
        if !chain.is_empty() {
            assert_ne!(
                chain.records[0].id,
                transform::TRANSFORM_JSON_ARRAY_COLUMNAR,
                "3 elements should NOT trigger array columnar"
            );
        }

        let restored = reverse_preprocess(&preprocessed, &chain);
        assert_eq!(restored, data.to_vec());
    }

    #[test]
    fn preprocess_non_json_passthrough() {
        let data = b"just some plain text with no JSON keys";
        let (preprocessed, chain) = preprocess(data, FormatHint::Generic, Mode::Fast);
        assert!(chain.is_empty());
        assert_eq!(preprocessed, data.to_vec());
    }

    #[test]
    fn test_json_array_nested_flatten_roundtrip() {
        // JSON array with nested objects — should apply json_array columnar + nested flatten.
        let mut json = String::from(r#"{"data": ["#);
        for i in 0..10 {
            if i > 0 {
                json.push_str(", ");
            }
            json.push_str(&format!(
                r#"{{"id": {}, "name": "item_{}", "meta": {{"score": {}, "active": {}, "tag": "t{}"}}}}"#,
                i, i, i * 10, if i % 2 == 0 { "true" } else { "false" }, i
            ));
        }
        json.push_str(r#"], "total": 10}"#);

        let data = json.as_bytes();
        let (preprocessed, chain) = preprocess(data, FormatHint::Json, Mode::Fast);
        assert!(!chain.is_empty());
        assert_eq!(
            chain.records[0].id,
            transform::TRANSFORM_JSON_ARRAY_COLUMNAR,
            "should apply json_array columnar first"
        );

        // Check that nested flatten was applied.
        let has_nested_flatten = chain
            .records
            .iter()
            .any(|r| r.id == transform::TRANSFORM_NESTED_FLATTEN);
        assert!(
            has_nested_flatten,
            "should apply nested flatten for objects with nested fields"
        );

        // Verify byte-exact roundtrip.
        let restored = reverse_preprocess(&preprocessed, &chain);
        assert_eq!(
            String::from_utf8_lossy(&restored),
            String::from_utf8_lossy(data),
        );
        assert_eq!(restored, data.to_vec());
    }

    #[test]
    fn test_json_array_nested_flatten_improves_ratio() {
        // Build a dataset where nested flatten demonstrably helps:
        // many rows with a nested object having repeated/similar values.
        let mut json = String::from(r#"{"items": ["#);
        for i in 0..50 {
            if i > 0 {
                json.push_str(", ");
            }
            json.push_str(&format!(
                r#"{{"id": {}, "user": {{"name": "user_{}", "role": "admin", "level": {}, "verified": true, "email": "user_{}@test.com"}}}}"#,
                i, i, i % 5, i
            ));
        }
        json.push_str(r"]}");

        let data = json.as_bytes();

        // Preprocess WITH nested flatten (current code).
        let (preprocessed_with, chain_with) = preprocess(data, FormatHint::Json, Mode::Fast);
        assert!(
            chain_with
                .records
                .iter()
                .any(|r| r.id == transform::TRANSFORM_NESTED_FLATTEN),
            "nested flatten should activate"
        );

        // Verify roundtrip.
        let restored = reverse_preprocess(&preprocessed_with, &chain_with);
        assert_eq!(restored, data.to_vec());

        // The preprocessed data should have more columns (sub-columns from nested objects).
        let num_cols_with = preprocessed_with.split(|&b| b == 0x00).count();
        // Without nested flatten, json_array produces 2 columns (id, user).
        // With nested flatten, user is decomposed into 5 sub-columns, so total = 1 + 5 = 6.
        assert!(
            num_cols_with > 2,
            "nested flatten should produce more columns: got {}",
            num_cols_with
        );
    }

    #[test]
    fn test_ndjson_unaffected() {
        // NDJSON with nested objects — should use NDJSON path, NOT the standalone nested flatten.
        let mut ndjson = String::new();
        for i in 0..10 {
            ndjson.push_str(&format!(
                r#"{{"id":{},"user":{{"name":"u{}","level":{}}}}}"#,
                i,
                i,
                i % 3
            ));
            ndjson.push('\n');
        }

        let data = ndjson.as_bytes();
        let (preprocessed, chain) = preprocess(data, FormatHint::Ndjson, Mode::Fast);
        assert!(!chain.is_empty());
        assert_eq!(
            chain.records[0].id,
            transform::TRANSFORM_NDJSON_COLUMNAR,
            "NDJSON should use its own columnar transform"
        );

        // Should NOT have standalone TRANSFORM_NESTED_FLATTEN in chain.
        let has_standalone_nested = chain
            .records
            .iter()
            .any(|r| r.id == transform::TRANSFORM_NESTED_FLATTEN);
        assert!(
            !has_standalone_nested,
            "NDJSON path should NOT use standalone nested flatten (it handles nesting internally)"
        );

        // Verify roundtrip.
        let restored = reverse_preprocess(&preprocessed, &chain);
        assert_eq!(restored, data.to_vec());
    }

    #[test]
    fn test_ndjson_large_delta_integer_roundtrip() {
        // Regression: NDJSON with integers spanning the epoch-timestamp range
        // (e.g. 2147483647 = i32::MAX) caused schema misclassification and
        // CRC-32 mismatch on decompression in Fast mode.
        let edges: &[i64] = &[
            0,
            -1,
            1,
            -2147483648,
            2147483647,
            -9007199254740991,
            9007199254740991,
        ];
        let mut ndjson = String::new();
        for i in 0..203 {
            ndjson.push_str(&format!("{{\"val\":{},\"idx\":{}}}\n", edges[i % 7], i));
        }

        let data = ndjson.as_bytes();

        // Full pipeline roundtrip (ndjson columnar + typed encoding in Fast mode).
        let (preprocessed, chain) = preprocess(data, FormatHint::Ndjson, Mode::Fast);

        // Verify typed encoding was applied.
        assert!(
            chain
                .records
                .iter()
                .any(|r| r.id == transform::TRANSFORM_TYPED_ENCODING),
            "typed encoding should be applied in Fast mode"
        );

        let restored = reverse_preprocess(&preprocessed, &chain);
        assert_eq!(restored, data.to_vec(), "byte-exact roundtrip failed");
    }

    #[test]
    fn test_nested_flatten_varying_subkeys_roundtrip() {
        // Regression test for npm_search.json roundtrip bug:
        // JSON array of uniform objects where nested dicts have VARYING sub-keys
        // across rows (e.g., some rows have "license", some don't; "links" has
        // 5 different schemas). The nested flatten must verify its roundtrip is
        // byte-exact before applying, because compact reconstruction reorders
        // keys to discovery order instead of preserving the original order.
        let mut json = String::from(r#"{"objects":["#);
        for i in 0..250 {
            if i > 0 {
                json.push(',');
            }
            // Nested dict with optional key (missing for first 6 rows)
            let license = if i >= 6 { r#","license":"MIT""# } else { "" };
            // Nested dict with varying key sets across rows
            let links = match i % 5 {
                0 => format!(
                    r#"{{"homepage":"h{i}","repository":"r{i}","bugs":"b{i}","npm":"n{i}"}}"#
                ),
                1 => format!(r#"{{"homepage":"h{i}","npm":"n{i}","repository":"r{i}"}}"#),
                2 => format!(r#"{{"npm":"n{i}"}}"#),
                3 => format!(r#"{{"bugs":"b{i}","homepage":"h{i}","npm":"n{i}"}}"#),
                _ => format!(r#"{{"npm":"n{i}","repository":"r{i}"}}"#),
            };
            let publisher = if i % 3 == 0 {
                format!(r#"{{"email":"u{i}@t.com","username":"u{i}","actor":"a{i}"}}"#)
            } else {
                format!(r#"{{"email":"u{i}@t.com","username":"u{i}"}}"#)
            };
            json.push_str(&format!(
                r#"{{"dl":{{"m":{},"w":{}}},"dep":"{}","sc":{},"pkg":{{"name":"p{i}","kw":["j","t"],"ver":"{i}.0","pub":{publisher},"mnt":[{{"u":"u{i}"}}]{license},"links":{links}}},"score":{{"f":0.5,"d":{{"q":0.8}}}},"flags":{{"x":0}}}}"#,
                1000 * (i + 1),
                250 * (i + 1),
                i * 5,
                1697.0894 + i as f64 * 0.1,
            ));
        }
        json.push_str(r#"],"total":250}"#);

        let data = json.as_bytes();

        for mode in [Mode::Fast, Mode::Balanced] {
            let (preprocessed, chain) = preprocess(data, FormatHint::Json, mode);
            assert!(!chain.is_empty(), "should apply transforms in {mode} mode");
            let restored = reverse_preprocess(&preprocessed, &chain);
            assert_eq!(restored.len(), data.len(), "length mismatch in {mode} mode",);
            assert_eq!(restored, data.to_vec(), "roundtrip failed in {mode} mode");
        }
    }
}