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
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

//! Data structures stored in Blobs and Logs in serialized form.

// NB: Everything starting with Blob* is directly serialized as a Blob value.
// Ditto for Log* and the Log. The others are used internally in these top-level
// structs.

use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::io::Cursor;
use std::ops::Range;

use bytes::BufMut;
use differential_dataflow::trace::Description;
use ore::cast::CastFrom;
use persist_types::Codec;
use prost::Message;
use semver::Version;
use serde::{Deserialize, Serialize};
use timely::progress::{Antichain, Timestamp};
use timely::PartialOrder;

use crate::error::Error;
use crate::gen::persist::{
    proto_batch_inline, ProtoArrangement, ProtoBatchFormat, ProtoBatchInline, ProtoMeta,
    ProtoStreamRegistration, ProtoTraceBatchInline, ProtoTraceBatchMeta, ProtoU64Antichain,
    ProtoU64Description, ProtoUnsealedBatchInline, ProtoUnsealedBatchMeta,
};
use crate::indexed::columnar::parquet::{
    decode_trace_parquet, decode_unsealed_parquet, encode_trace_parquet, encode_unsealed_parquet,
};
use crate::indexed::columnar::ColumnarRecords;
use crate::storage::SeqNo;

/// An internally unique id for a persisted stream. External users identify
/// streams with a string, which is then mapped internally to this.
#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Id(pub u64);

/// The structure serialized and stored as an entry in a
/// [crate::storage::Log].
///
/// Invariants:
/// - The updates field is non-empty.
#[derive(Debug, Serialize, Deserialize)]
pub struct LogEntry {
    /// Pairs of stream id and the updates themselves.
    //
    // We could require that each Id is included at most once, but at the
    // moment, there's no particular reason we'd need to.
    pub updates: Vec<(Id, Vec<((Vec<u8>, Vec<u8>), u64, isize)>)>,
}

/// The structure serialized and stored as a value in [crate::storage::Blob]
/// storage for metadata keys.
///
/// Invariants:
/// - All strings in id_mapping are unique.
/// - All ids in id_mapping are unique.
/// - All strings in graveyard are unique.
/// - All ids in graveyard are unique.
/// - None of the strings in graveyard are present in any of the (string, id)
///   tuples in id_mapping.
/// - None of the ids in graveyard are present in any of the (string, id) tuples
///   in id_mapping.
/// - The same set of ids are present in id_mapping, unsealeds, and traces.
/// - For each id, the ts_lower in the unsealed is <= the ts_upper in the
///   corresponding trace. (This is less than equals and not strictly equals
///   because truncating the unnecessary elements out of unsealed is fallible, and
///   is allowed to lag behind the migration of new data into trace)
/// - id_mapping.len() + graveyard.len() is == next_stream_id.
/// - All of the keys for trace and unsealed batches are unique across all persisted
///   streams.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BlobMeta {
    /// Which mutations are included in the represented state.
    ///
    /// Persist is a state machine, with all mutating requests modeled as input
    /// state changes sequenced into a log. Periodically those state changes are
    /// applied and the resulting state is written out to blob storage. This
    /// field indicates which prefix of the log (`0..=self.seqno`) has been
    /// included in the state represented by this BlobMeta. SeqNo(0) represents
    /// the initial empty state, the first mutation is SeqNo(1).
    ///
    /// Invariant: For each UnsealedMeta in `unsealeds`, this is >= the last
    /// batch's upper. If they are not equal, there is logically an empty batch
    /// between [last batch's upper, self.seqno).
    pub seqno: SeqNo,
    /// Internal stream id indexed by external stream name.
    ///
    /// Invariant: Each stream name and stream id are in here at most once.
    pub id_mapping: Vec<StreamRegistration>,
    /// Set of deleted streams, indexed by external stream name.
    pub graveyard: Vec<StreamRegistration>,
    /// Arrangements indexed by stream id.
    ///
    /// Invariant: Each stream id is in here at most once.
    pub arrangements: Vec<ArrangementMeta>,
}

/// Registration information for a single stream.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct StreamRegistration {
    /// The external stream name.
    pub name: String,
    /// The internal stream id.
    pub id: Id,
    /// The codec used to encode and decode keys in this stream.
    pub key_codec_name: String,
    /// The codec used to encode and decode values in this stream.
    pub val_codec_name: String,
}

/// The metadata necessary to reconstruct an Arrangement.
///
/// Invariants:
/// - The unsealed_batch SeqNo ranges are sorted and non-overlapping.
/// - The trace_batch Descriptions are sorted, non-overlapping, and contiguous.
/// - Every batch's since frontier is <= the overall trace's since frontier.
/// - The compaction level of trace_batches is weakly decreasing when iterating
///   from oldest to most recent time intervals.
/// - Every trace_batch's upper is <= the overall trace's seal frontier.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ArrangementMeta {
    /// The stream this unsealed belongs to.
    pub id: Id,
    /// Frontier this trace has been sealed up to.
    pub seal: Antichain<u64>,
    /// Compaction frontier for the batches contained in this trace.
    /// There may still be batches containing updates at times < since, but the
    /// the trace only contains correct answers for times at or in advance of this
    /// of this frontier. Readers are expected to advance any updates < since to
    /// since.
    pub since: Antichain<u64>,
    /// The batches that make up the Unsealed.
    pub unsealed_batches: Vec<UnsealedBatchMeta>,
    /// The batches that make up the Trace.
    pub trace_batches: Vec<TraceBatchMeta>,
}

/// The metadata necessary to reconstruct a [BlobUnsealedBatch].
///
/// Invariants:
/// - The [lower, upper) interval of sequence numbers in desc is non-empty.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct UnsealedBatchMeta {
    /// The key to retrieve the [BlobUnsealedBatch] from blob storage.
    pub key: String,
    /// The format of the stored batch data.
    pub format: ProtoBatchFormat,
    /// Half-open interval [lower, upper) of sequence numbers that this batch
    /// contains updates for.
    pub desc: Range<SeqNo>,
    /// The maximum timestamp of any update contained in this batch.
    pub ts_upper: u64,
    /// The minimum timestamp from any update contained in this batch.
    pub ts_lower: u64,
    /// Size of the encoded batch.
    pub size_bytes: u64,
}

/// The metadata necessary to reconstruct a [BlobTraceBatch].
///
/// Invariants:
/// - The Description's time interval is non-empty.
/// - TODO: key invariants?
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TraceBatchMeta {
    /// The key to retrieve the batch's data from the blob store.
    pub key: String,
    /// The format of the stored batch data.
    pub format: ProtoBatchFormat,
    /// The half-open time interval `[lower, upper)` this batch contains data
    /// for.
    pub desc: Description<u64>,
    /// The compaction level of each batch.
    pub level: u64,
    /// Size of the encoded batch.
    pub size_bytes: u64,
}

/// The structure serialized and stored as a value in [crate::storage::Blob]
/// storage for data keys corresponding to unsealed data.
///
/// Invariants:
/// - The [lower, upper) interval of sequence numbers in desc is non-empty.
/// - The updates field is non-empty.
#[derive(Clone, Debug)]
pub struct BlobUnsealedBatch {
    /// Which updates are included in this batch.
    pub desc: Range<SeqNo>,
    /// The updates themselves.
    pub updates: Vec<ColumnarRecords>,
}

/// The structure serialized and stored as a value in [crate::storage::Blob]
/// storage for data keys corresponding to trace data.
///
/// This batch represents the data that was originally written at some time in
/// [lower, upper) (more precisely !< lower and < upper). The individual record
/// times may have later been advanced by compaction to something <= since.
/// This means the ability to reconstruct the state of the collection at times < since
/// has been lost. However, there may still be records present in the batch whose
/// times are < since. Users iterating through updates must take care to advance
/// records with times < since to since in order to correctly answer queries at
/// times >= since.
///
/// Invariants:
/// - The [lower, upper) interval of times in desc is non-empty.
/// - The timestamp of each update is >= to desc.lower().
/// - The timestamp of each update is < desc.upper() iff desc.upper() > desc.since().
///   Otherwise the timestamp of each update is <= desc.since().
/// - The values in updates are sorted by (key, value, time).
/// - The values in updates are "consolidated", i.e. (key, value, time) is
///   unique.
/// - All entries have a non-zero diff.
/// - (Intentionally no invariant around update non-emptiness because we might
///   need empty batches to make the timestamps line up.)
///
/// TODO: This probably wants to be a different level of abstraction, so we can
/// put multiple small batches in a single blob but also break a very large
/// batch over multiple blobs. We also may want to break the latter into chunks
/// for checksum and encryption?
#[derive(Clone, Debug)]
pub struct BlobTraceBatch {
    /// Which updates are included in this batch.
    pub desc: Description<u64>,
    /// The updates themselves.
    pub updates: Vec<ColumnarRecords>,
}

impl LogEntry {
    /// Asserts Self's documented invariants, returning an error if any are
    /// violated.
    pub fn validate(&self) -> Result<(), Error> {
        // TODO: It's unclear if this invariant is useful/harmful. Feel free to
        // remove it if it ends up not making sense.
        if self.updates.is_empty() {
            return Err("updates is empty".into());
        }
        Ok(())
    }
}

impl Default for BlobMeta {
    fn default() -> Self {
        BlobMeta {
            seqno: SeqNo(0),
            id_mapping: Vec::new(),
            graveyard: Vec::new(),
            arrangements: Vec::new(),
        }
    }
}

impl BlobMeta {
    /// Asserts Self's documented invariants, returning an error if any are
    /// violated.
    pub fn validate(&self) -> Result<(), Error> {
        let mut ids = HashSet::new();
        let mut names = HashSet::new();
        for r in self.id_mapping.iter() {
            if names.contains(&r.name) {
                return Err(format!("duplicate external stream name: {}", r.name).into());
            }
            names.insert(r.name.clone());
            if ids.contains(&r.id) {
                return Err(format!("duplicate internal stream id: {:?}", r.id).into());
            }
            ids.insert(r.id);
        }

        let mut deleted_ids = HashSet::new();
        let mut deleted_names = HashSet::new();

        for r in self.graveyard.iter() {
            if names.contains(&r.name) {
                return Err(format!(
                    "duplicate external stream name {} across deleted and registered streams",
                    r.name
                )
                .into());
            }

            if ids.contains(&r.id) {
                return Err(format!(
                    "duplicate internal stream id {:?} across deleted and registered streams",
                    r.id
                )
                .into());
            }

            if deleted_names.contains(&r.name) {
                return Err(format!("duplicate deleted external stream name: {}", r.name).into());
            }
            deleted_names.insert(r.name.clone());

            if deleted_ids.contains(&r.id) {
                return Err(format!("duplicate deleted internal stream id: {:?}", r.id).into());
            }
            deleted_ids.insert(r.id);
        }

        let next_stream_id = self.next_stream_id();
        if u64::cast_from(deleted_ids.len() + ids.len()) != next_stream_id.0 {
            return Err(format!(
                "next stream {:?}, but only registered {} ids and deleted {} ids",
                next_stream_id,
                ids.len(),
                deleted_ids.len()
            )
            .into());
        }

        let mut arrangements = HashMap::new();
        for f in self.arrangements.iter() {
            if !ids.contains(&f.id) {
                return Err(format!("arrangements id {:?} not present in id_mapping", f.id).into());
            }

            if arrangements.contains_key(&f.id) {
                return Err(format!("duplicate arrangement: {:?}", f.id).into());
            }
            arrangements.insert(f.id, f);

            f.validate()?;
        }

        for id in ids.iter() {
            let arrangement = arrangements.get(id).ok_or_else(|| {
                Error::from(format!(
                    "id_mapping id {:?} not present in arrangements",
                    id
                ))
            })?;
            let unsealed_seqno_upper = arrangement.unsealed_seqno_upper();
            if !unsealed_seqno_upper.less_equal(&self.seqno) {
                return Err(Error::from(format!(
                    "id {:?} unsealed seqno_upper {:?} is not less or equal to the blob's seqno {:?}",
                    id, unsealed_seqno_upper, self.seqno,
                )));
            }
        }

        let mut batch_keys = HashSet::new();
        for a in self.arrangements.iter() {
            for batch in a.unsealed_batches.iter() {
                if batch_keys.contains(&batch.key) {
                    return Err(
                        format!("duplicate batch key found in unsealed: {}", batch.key).into(),
                    );
                }
                batch_keys.insert(batch.key.clone());
            }
            for batch in a.trace_batches.iter() {
                if batch_keys.contains(&batch.key) {
                    return Err(format!("duplicate batch key found in trace: {}", batch.key).into());
                }
                batch_keys.insert(batch.key.clone());
            }
        }

        Ok(())
    }

    /// The next Id to issue for a stream being added to id_mapping.
    pub fn next_stream_id(&self) -> Id {
        let current_highest = self
            .id_mapping
            .iter()
            .chain(self.graveyard.iter())
            .map(|s| s.id)
            .max();
        current_highest.map_or(Id(0), |id| Id(id.0 + 1))
    }
}

impl Default for ArrangementMeta {
    fn default() -> Self {
        ArrangementMeta {
            id: Id(0),
            since: Antichain::from_elem(Timestamp::minimum()),
            seal: Antichain::from_elem(Timestamp::minimum()),
            unsealed_batches: Vec::new(),
            trace_batches: Vec::new(),
        }
    }
}

impl ArrangementMeta {
    /// Create a new [ArrangementMeta] belonging to `id`.
    pub fn new(id: Id) -> Self {
        ArrangementMeta {
            id,
            ..Default::default()
        }
    }

    /// Asserts Self's documented invariants, returning an error if any are
    /// violated.
    pub fn validate(&self) -> Result<(), Error> {
        let mut unsealed_prev: Option<&UnsealedBatchMeta> = None;
        for meta in self.unsealed_batches.iter() {
            meta.validate()?;
            if let Some(prev) = unsealed_prev {
                if prev.desc.end > meta.desc.start {
                    return Err(format!(
                        "invalid batch sequence: {:?} followed by {:?}",
                        prev.desc, meta.desc
                    )
                    .into());
                }
            }
            unsealed_prev = Some(&meta)
        }

        let mut trace_prev: Option<&TraceBatchMeta> = None;
        for meta in self.trace_batches.iter() {
            if !PartialOrder::less_equal(meta.desc.since(), &self.since) {
                return Err(format!(
                    "invalid batch since: {:?} in advance of trace since {:?}",
                    meta.desc, self.since
                )
                .into());
            }

            if !PartialOrder::less_equal(meta.desc.upper(), &self.seal) {
                return Err(format!(
                    "invalid batch upper: {:?} in advance of trace seal {:?}",
                    meta.desc, self.seal,
                )
                .into());
            }

            meta.validate()?;

            if let Some(prev) = trace_prev {
                if prev.desc.upper() != meta.desc.lower() {
                    return Err(format!(
                        "invalid batch sequence: {:?} followed by {:?}",
                        prev.desc, meta.desc,
                    )
                    .into());
                }

                if prev.level < meta.level {
                    return Err(format!(
                        "invalid batch sequence: compaction level {} followed by {}",
                        prev.level, meta.level
                    )
                    .into());
                }
            }
            trace_prev = Some(&meta)
        }

        Ok(())
    }

    /// Returns an open upper bound on the seqnos contained in this unsealed.
    pub fn unsealed_seqno_upper(&self) -> SeqNo {
        self.unsealed_batches
            .last()
            .map_or_else(|| SeqNo(0), |meta| meta.desc.end)
    }

    /// Returns an open upper bound on the timestamps of data contained in this
    /// trace.
    pub fn trace_ts_upper(&self) -> Antichain<u64> {
        self.trace_batches.last().map_or_else(
            || Antichain::from_elem(Timestamp::minimum()),
            |meta| meta.desc.upper().clone(),
        )
    }
}

impl UnsealedBatchMeta {
    /// Asserts Self's documented invariants, returning an error if any are
    /// violated.
    pub fn validate(&self) -> Result<(), Error> {
        // TODO: It's unclear if the equal case (an empty desc) is
        // useful/harmful. Feel free to make this a less_than if empty descs end
        // up making sense.
        if self.desc.end <= self.desc.start {
            return Err(format!("invalid desc: {:?}", &self.desc).into());
        }

        Ok(())
    }
}

impl TraceBatchMeta {
    /// Asserts Self's documented invariants, returning an error if any are
    /// violated.
    pub fn validate(&self) -> Result<(), Error> {
        // TODO: It's unclear if the equal case (an empty desc) is
        // useful/harmful. Feel free to make this a less_than if empty descs end
        // up making sense.
        if PartialOrder::less_equal(self.desc.upper(), &self.desc.lower()) {
            return Err(format!("invalid desc: {:?}", &self.desc).into());
        }

        Ok(())
    }
}

impl BlobUnsealedBatch {
    /// Asserts Self's documented invariants, returning an error if any are
    /// violated.
    pub fn validate(&self) -> Result<(), Error> {
        // TODO: It's unclear if the equal case (an empty desc) is
        // useful/harmful. Feel free to make this a less_than if empty descs end
        // up making sense.
        if self.desc.end <= self.desc.start {
            return Err(format!("invalid desc: {:?}", &self.desc).into());
        }
        // TODO: It's unclear if this invariant is useful/harmful. Feel free to
        // remove it if it ends up not making sense.
        if self.updates.is_empty() {
            return Err("updates is empty".into());
        }

        Ok(())
    }
}

// BlobUnsealedBatch doesn't really need to implement Codec (it's never stored
// as a key or value in a persisted record) but it's nice to have a common
// interface for this.
impl Codec for BlobUnsealedBatch {
    fn codec_name() -> String {
        "parquet[UnsealedBatch]".into()
    }

    fn encode<B>(&self, buf: &mut B)
    where
        B: BufMut,
    {
        encode_unsealed_parquet(&mut buf.writer(), &self).expect("writes to BufMut are infallible");
    }

    fn decode<'a>(buf: &'a [u8]) -> Result<Self, String> {
        decode_unsealed_parquet(&mut Cursor::new(&buf)).map_err(|err| err.to_string())
    }
}

impl BlobTraceBatch {
    /// Asserts the documented invariants, returning an error if any are
    /// violated.
    pub fn validate(&self) -> Result<(), Error> {
        // TODO: It's unclear if the equal case (an empty desc) is
        // useful/harmful. Feel free to make this a less_than if empty descs end
        // up making sense.
        if PartialOrder::less_equal(self.desc.upper(), &self.desc.lower()) {
            return Err(format!("invalid desc: {:?}", &self.desc).into());
        }

        let mut prev: Option<(PrettyBytes<'_>, PrettyBytes<'_>, u64)> = None;
        for update in self.updates.iter().flat_map(|u| u.iter()) {
            let ((key, val), ts, diff) = update;
            // Check ts against desc.
            if !self.desc.lower().less_equal(&ts) {
                return Err(format!(
                    "timestamp {} is less than the batch lower: {:?}",
                    ts, self.desc
                )
                .into());
            }

            if PartialOrder::less_than(self.desc.since(), self.desc.upper()) {
                if self.desc.upper().less_equal(&ts) {
                    return Err(format!(
                        "timestamp {} is greater than or equal to the batch upper: {:?}",
                        ts, self.desc
                    )
                    .into());
                }
            } else if self.desc.since().less_than(&ts) {
                return Err(format!(
                    "timestamp {} is greater than the batch since: {:?}",
                    ts, self.desc,
                )
                .into());
            }

            // Check ordering.
            let this = (PrettyBytes(&key), PrettyBytes(&val), ts);
            if let Some(prev) = prev {
                match prev.cmp(&this) {
                    Ordering::Less => {} // Correct.
                    Ordering::Equal => return Err(format!("unconsolidated: {:?}", this).into()),
                    Ordering::Greater => {
                        return Err(format!("unsorted: {:?} was before {:?}", prev, this).into())
                    }
                }
            }
            prev = Some(this);

            // Check data invariants.
            if diff == 0 {
                return Err(format!("update with 0 diff: {:?}", PrettyRecord(update)).into());
            }
        }
        Ok(())
    }
}

// BlobTraceBatch doesn't really need to implement Codec (it's never stored as a
// key or value in a persisted record) but it's nice to have a common interface
// for this.
impl Codec for BlobTraceBatch {
    fn codec_name() -> String {
        "parquet[TraceBatch]".into()
    }

    fn encode<B>(&self, buf: &mut B)
    where
        B: BufMut,
    {
        encode_trace_parquet(&mut buf.writer(), self).expect("writes to BufMut are infallible");
    }

    fn decode<'a>(buf: &'a [u8]) -> Result<Self, String> {
        decode_trace_parquet(&mut Cursor::new(&buf)).map_err(|err| err.to_string())
    }
}

#[derive(PartialOrd, Ord, PartialEq, Eq)]
struct PrettyBytes<'a>(&'a [u8]);

impl fmt::Debug for PrettyBytes<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match std::str::from_utf8(self.0) {
            Ok(x) => fmt::Debug::fmt(x, f),
            Err(_) => fmt::Debug::fmt(self.0, f),
        }
    }
}

struct PrettyRecord<'a>(((&'a [u8], &'a [u8]), u64, isize));

impl fmt::Debug for PrettyRecord<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let ((k, v), ts, diff) = &self.0;
        fmt::Debug::fmt(&((PrettyBytes(&k), PrettyBytes(&v)), ts, diff), f)
    }
}

impl From<ProtoMeta> for BlobMeta {
    fn from(x: ProtoMeta) -> Self {
        let mut meta = BlobMeta {
            seqno: SeqNo(x.seqno),
            id_mapping: x.id_mapping.into_iter().map(|x| x.into()).collect(),
            graveyard: x.graveyard.into_iter().map(|x| x.into()).collect(),
            arrangements: x.arrangements.into_iter().map(|x| x.into()).collect(),
        };
        // TODO: Make the types on BlobMeta be HashMaps and remove this sort.
        meta.id_mapping.sort_by_key(|x| x.id);
        meta.graveyard.sort_by_key(|x| x.id);
        meta.arrangements.sort_by_key(|x| x.id);
        meta
    }
}

impl From<(u64, ProtoArrangement)> for ArrangementMeta {
    fn from(x: (u64, ProtoArrangement)) -> Self {
        let (id, x) = x;
        ArrangementMeta {
            id: Id(id),
            seal: x
                .seal
                .map_or_else(|| Antichain::from_elem(u64::minimum()), |x| x.into()),
            since: x
                .since
                .map_or_else(|| Antichain::from_elem(u64::minimum()), |x| x.into()),
            unsealed_batches: x.unsealed_batches.into_iter().map(|x| x.into()).collect(),
            trace_batches: x.trace_batches.into_iter().map(|x| x.into()).collect(),
        }
    }
}

impl From<(u64, ProtoStreamRegistration)> for StreamRegistration {
    fn from(x: (u64, ProtoStreamRegistration)) -> Self {
        let (id, x) = x;
        StreamRegistration {
            id: Id(id),
            name: x.name,
            key_codec_name: x.key_codec_name,
            val_codec_name: x.val_codec_name,
        }
    }
}

impl From<ProtoUnsealedBatchMeta> for UnsealedBatchMeta {
    fn from(x: ProtoUnsealedBatchMeta) -> Self {
        UnsealedBatchMeta {
            format: x.format(),
            key: x.key,
            desc: SeqNo(x.seqno_lower)..SeqNo(x.seqno_upper),
            ts_upper: x.ts_upper,
            ts_lower: x.ts_lower,
            size_bytes: x.size_bytes,
        }
    }
}

impl From<ProtoTraceBatchMeta> for TraceBatchMeta {
    fn from(x: ProtoTraceBatchMeta) -> Self {
        TraceBatchMeta {
            format: x.format(),
            key: x.key,
            desc: x.desc.map_or_else(
                || {
                    Description::new(
                        Antichain::from_elem(u64::minimum()),
                        Antichain::from_elem(u64::minimum()),
                        Antichain::from_elem(u64::minimum()),
                    )
                },
                |x| x.into(),
            ),
            level: x.level,
            size_bytes: x.size_bytes,
        }
    }
}

impl From<ProtoU64Description> for Description<u64> {
    fn from(x: ProtoU64Description) -> Self {
        Description::new(
            x.lower
                .map_or_else(|| Antichain::from_elem(u64::minimum()), |x| x.into()),
            x.upper
                .map_or_else(|| Antichain::from_elem(u64::minimum()), |x| x.into()),
            x.since
                .map_or_else(|| Antichain::from_elem(u64::minimum()), |x| x.into()),
        )
    }
}

impl From<ProtoU64Antichain> for Antichain<u64> {
    fn from(x: ProtoU64Antichain) -> Self {
        Antichain::from(x.elements)
    }
}

impl From<(&BlobMeta, &Version)> for ProtoMeta {
    fn from(x: (&BlobMeta, &Version)) -> Self {
        let (x, b) = x;
        ProtoMeta {
            version: b.to_string(),
            seqno: x.seqno.0,
            id_mapping: x.id_mapping.iter().map(|x| (x.id.0, x.into())).collect(),
            graveyard: x.graveyard.iter().map(|x| (x.id.0, x.into())).collect(),
            arrangements: x.arrangements.iter().map(|x| (x.id.0, x.into())).collect(),
        }
    }
}

impl From<&ArrangementMeta> for ProtoArrangement {
    fn from(x: &ArrangementMeta) -> Self {
        ProtoArrangement {
            since: Some((&x.since).into()),
            seal: Some((&x.seal).into()),
            unsealed_batches: x.unsealed_batches.iter().map(|x| x.into()).collect(),
            trace_batches: x.trace_batches.iter().map(|x| x.into()).collect(),
        }
    }
}

impl From<&StreamRegistration> for ProtoStreamRegistration {
    fn from(x: &StreamRegistration) -> Self {
        ProtoStreamRegistration {
            name: x.name.clone(),
            key_codec_name: x.key_codec_name.clone(),
            val_codec_name: x.val_codec_name.clone(),
        }
    }
}

impl From<&UnsealedBatchMeta> for ProtoUnsealedBatchMeta {
    fn from(x: &UnsealedBatchMeta) -> Self {
        ProtoUnsealedBatchMeta {
            key: x.key.clone(),
            format: x.format.into(),
            seqno_upper: x.desc.end.0,
            seqno_lower: x.desc.start.0,
            ts_upper: x.ts_upper,
            ts_lower: x.ts_lower,
            size_bytes: x.size_bytes,
        }
    }
}

impl From<&TraceBatchMeta> for ProtoTraceBatchMeta {
    fn from(x: &TraceBatchMeta) -> Self {
        ProtoTraceBatchMeta {
            key: x.key.clone(),
            format: x.format.into(),
            desc: Some((&x.desc).into()),
            level: x.level,
            size_bytes: x.size_bytes,
        }
    }
}

impl From<&Antichain<u64>> for ProtoU64Antichain {
    fn from(x: &Antichain<u64>) -> Self {
        ProtoU64Antichain {
            elements: x.elements().to_vec(),
        }
    }
}

impl From<&Description<u64>> for ProtoU64Description {
    fn from(x: &Description<u64>) -> Self {
        ProtoU64Description {
            lower: Some(x.lower().into()),
            upper: Some(x.upper().into()),
            since: Some(x.since().into()),
        }
    }
}

/// Encodes the inline metadata for an unsealed batch into a base64 string.
pub fn encode_unsealed_inline_meta(batch: &BlobUnsealedBatch, format: ProtoBatchFormat) -> String {
    let inline = ProtoBatchInline {
        batch_type: Some(proto_batch_inline::BatchType::Unsealed(
            ProtoUnsealedBatchInline {
                format: format.into(),
                seqno_lower: batch.desc.start.0,
                seqno_upper: batch.desc.end.0,
            },
        )),
    };
    let inline_encoded = inline.encode_to_vec();
    base64::encode(inline_encoded)
}

/// Encodes the inline metadata for a trace batch into a base64 string.
pub fn encode_trace_inline_meta(batch: &BlobTraceBatch, format: ProtoBatchFormat) -> String {
    let inline = ProtoBatchInline {
        batch_type: Some(proto_batch_inline::BatchType::Trace(
            ProtoTraceBatchInline {
                format: format.into(),
                desc: Some((&batch.desc).into()),
            },
        )),
    };
    let inline_encoded = inline.encode_to_vec();
    base64::encode(inline_encoded)
}

/// Decodes the inline metadata for an unsealed batch from a base64 string.
pub fn decode_unsealed_inline_meta(
    inline_base64: Option<&String>,
) -> Result<(ProtoBatchFormat, ProtoUnsealedBatchInline), Error> {
    let inline_base64 = inline_base64.ok_or("missing batch metadata")?;
    let inline_encoded = base64::decode(&inline_base64).map_err(|err| err.to_string())?;
    let inline = ProtoBatchInline::decode(&*inline_encoded).map_err(|err| err.to_string())?;
    match inline.batch_type {
        Some(proto_batch_inline::BatchType::Unsealed(x)) => {
            let format = ProtoBatchFormat::from_i32(x.format)
                .ok_or_else(|| Error::from(format!("unknown format: {}", x.format)))?;
            Ok((format, x))
        }
        x => return Err(format!("incorrect batch type: {:?}", x).into()),
    }
}

/// Decodes the inline metadata for a trace batch from a base64 string.
pub fn decode_trace_inline_meta(
    inline_base64: Option<&String>,
) -> Result<(ProtoBatchFormat, ProtoTraceBatchInline), Error> {
    let inline_base64 = inline_base64.ok_or("missing batch metadata")?;
    let inline_encoded = base64::decode(&inline_base64).map_err(|err| err.to_string())?;
    let inline = ProtoBatchInline::decode(&*inline_encoded).map_err(|err| err.to_string())?;
    match inline.batch_type {
        Some(proto_batch_inline::BatchType::Trace(x)) => {
            let format = ProtoBatchFormat::from_i32(x.format)
                .ok_or_else(|| Error::from(format!("unknown format: {}", x.format)))?;
            Ok((format, x))
        }
        x => return Err(format!("incorrect batch type: {:?}", x).into()),
    }
}

#[cfg(test)]
mod tests {
    use crate::error::Error;
    use crate::indexed::columnar::ColumnarRecordsVec;
    use crate::workload::DataGenerator;

    use super::*;

    fn update_with_ts(ts: u64) -> ((Vec<u8>, Vec<u8>), u64, isize) {
        (("".into(), "".into()), ts, 1)
    }

    fn update_with_key(ts: u64, key: &'static str) -> ((Vec<u8>, Vec<u8>), u64, isize) {
        ((key.into(), "".into()), ts, 1)
    }

    fn u64_desc(lower: u64, upper: u64) -> Description<u64> {
        Description::new(
            Antichain::from_elem(lower),
            Antichain::from_elem(upper),
            Antichain::from_elem(0),
        )
    }

    fn batch_meta(lower: u64, upper: u64) -> TraceBatchMeta {
        TraceBatchMeta {
            key: "".to_string(),
            format: ProtoBatchFormat::Unknown,
            desc: u64_desc(lower, upper),
            level: 1,
            size_bytes: 0,
        }
    }

    fn batch_meta_full(lower: u64, upper: u64, since: u64, level: u64) -> TraceBatchMeta {
        TraceBatchMeta {
            key: "".to_string(),
            format: ProtoBatchFormat::Unknown,
            desc: u64_desc_since(lower, upper, since),
            level,
            size_bytes: 0,
        }
    }

    fn u64_desc_since(lower: u64, upper: u64, since: u64) -> Description<u64> {
        Description::new(
            Antichain::from_elem(lower),
            Antichain::from_elem(upper),
            Antichain::from_elem(since),
        )
    }

    fn unsealed_batch_meta(lower: u64, upper: u64) -> UnsealedBatchMeta {
        UnsealedBatchMeta {
            key: "".to_string(),
            format: ProtoBatchFormat::Unknown,
            desc: SeqNo(lower)..SeqNo(upper),
            ts_upper: 0,
            ts_lower: 0,
            size_bytes: 0,
        }
    }

    fn columnar_records(updates: Vec<((Vec<u8>, Vec<u8>), u64, isize)>) -> Vec<ColumnarRecords> {
        updates.iter().collect::<ColumnarRecordsVec>().into_inner()
    }

    impl From<(&'_ str, Id)> for StreamRegistration {
        fn from(x: (&'_ str, Id)) -> Self {
            let (name, id) = x;
            StreamRegistration {
                name: name.to_owned(),
                id,
                key_codec_name: "".into(),
                val_codec_name: "".into(),
            }
        }
    }

    #[test]
    fn log_entry_validate() {
        // Normal case
        let b = LogEntry {
            updates: vec![(Id(0), vec![update_with_key(0, "0")])],
        };
        assert_eq!(b.validate(), Ok(()));

        // Empty
        let b: LogEntry = LogEntry { updates: vec![] };
        assert_eq!(b.validate(), Err("updates is empty".into()));
    }

    #[test]
    fn unsealed_batch_validate() {
        // Normal case
        let b = BlobUnsealedBatch {
            desc: SeqNo(0)..SeqNo(2),
            updates: columnar_records(vec![update_with_ts(0), update_with_ts(1)]),
        };
        assert_eq!(b.validate(), Ok(()));

        // Empty
        let b: BlobUnsealedBatch = BlobUnsealedBatch {
            desc: SeqNo(0)..SeqNo(2),
            updates: vec![],
        };
        assert_eq!(b.validate(), Err("updates is empty".into()));

        // Invalid desc
        let b: BlobUnsealedBatch = BlobUnsealedBatch {
            desc: SeqNo(2)..SeqNo(0),
            updates: vec![],
        };
        assert_eq!(
            b.validate(),
            Err(Error::from("invalid desc: SeqNo(2)..SeqNo(0)"))
        );

        // Empty desc
        let b: BlobUnsealedBatch = BlobUnsealedBatch {
            desc: SeqNo(0)..SeqNo(0),
            updates: vec![],
        };
        assert_eq!(
            b.validate(),
            Err(Error::from("invalid desc: SeqNo(0)..SeqNo(0)"))
        );
    }

    #[test]
    fn trace_batch_validate() {
        // Normal case
        let b = BlobTraceBatch {
            desc: u64_desc(0, 2),
            updates: columnar_records(vec![update_with_key(0, "0"), update_with_key(1, "1")]),
        };
        assert_eq!(b.validate(), Ok(()));

        // Empty
        let b: BlobTraceBatch = BlobTraceBatch {
            desc: u64_desc(0, 2),
            updates: columnar_records(vec![]),
        };
        assert_eq!(b.validate(), Ok(()));

        // Invalid desc
        let b: BlobTraceBatch = BlobTraceBatch {
            desc: u64_desc(2, 0),
            updates: columnar_records(vec![]),
        };
        assert_eq!(
            b.validate(),
            Err(Error::from(
                "invalid desc: Description { lower: Antichain { elements: [2] }, upper: Antichain { elements: [0] }, since: Antichain { elements: [0] } }"
            ))
        );

        // Empty desc
        let b: BlobTraceBatch = BlobTraceBatch {
            desc: u64_desc(0, 0),
            updates: columnar_records(vec![]),
        };
        assert_eq!(
            b.validate(),
            Err(Error::from(
                "invalid desc: Description { lower: Antichain { elements: [0] }, upper: Antichain { elements: [0] }, since: Antichain { elements: [0] } }"
            ))
        );

        // Not sorted by key
        let b = BlobTraceBatch {
            desc: u64_desc(0, 2),
            updates: columnar_records(vec![update_with_key(0, "1"), update_with_key(1, "0")]),
        };
        assert_eq!(
            b.validate(),
            Err(Error::from(
                "unsorted: (\"1\", \"\", 0) was before (\"0\", \"\", 1)"
            ))
        );

        // Not consolidated
        let b = BlobTraceBatch {
            desc: u64_desc(0, 2),
            updates: columnar_records(vec![update_with_key(0, "0"), update_with_key(0, "0")]),
        };
        assert_eq!(
            b.validate(),
            Err(Error::from("unconsolidated: (\"0\", \"\", 0)"))
        );

        // Update "before" desc
        let b = BlobTraceBatch {
            desc: u64_desc(1, 2),
            updates: columnar_records(vec![update_with_key(0, "0")]),
        };
        assert_eq!(b.validate(), Err(Error::from("timestamp 0 is less than the batch lower: Description { lower: Antichain { elements: [1] }, upper: Antichain { elements: [2] }, since: Antichain { elements: [0] } }")));

        // Update "after" desc
        let b = BlobTraceBatch {
            desc: u64_desc(1, 2),
            updates: columnar_records(vec![update_with_key(2, "0")]),
        };
        assert_eq!(b.validate(), Err(Error::from("timestamp 2 is greater than or equal to the batch upper: Description { lower: Antichain { elements: [1] }, upper: Antichain { elements: [2] }, since: Antichain { elements: [0] } }")));

        // Normal case: update "after" desc and within since
        let b = BlobTraceBatch {
            desc: u64_desc_since(1, 2, 4),
            updates: columnar_records(vec![update_with_key(2, "0")]),
        };
        assert_eq!(b.validate(), Ok(()));

        // Normal case: update "after" desc and at since
        let b = BlobTraceBatch {
            desc: u64_desc_since(1, 2, 4),
            updates: columnar_records(vec![update_with_key(4, "0")]),
        };
        assert_eq!(b.validate(), Ok(()));

        // Update "after" desc since
        let b = BlobTraceBatch {
            desc: u64_desc_since(1, 2, 4),
            updates: columnar_records(vec![update_with_key(5, "0")]),
        };
        assert_eq!(b.validate(), Err(Error::from("timestamp 5 is greater than the batch since: Description { lower: Antichain { elements: [1] }, upper: Antichain { elements: [2] }, since: Antichain { elements: [4] } }")));

        // Invalid update
        let b: BlobTraceBatch = BlobTraceBatch {
            desc: u64_desc(0, 1),
            updates: columnar_records(vec![(("0".into(), "0".into()), 0, 0)]),
        };
        assert_eq!(
            b.validate(),
            Err(Error::from("update with 0 diff: ((\"0\", \"0\"), 0, 0)"))
        );
    }

    #[test]
    fn trace_batch_meta_validate() {
        // Normal case
        let b = batch_meta(0, 1);
        assert_eq!(b.validate(), Ok(()));

        // Empty interval
        let b = batch_meta(0, 0);
        assert_eq!(b.validate(),
            Err(Error::from(
                "invalid desc: Description { lower: Antichain { elements: [0] }, upper: Antichain { elements: [0] }, since: Antichain { elements: [0] } }"
            )),
        );

        // Invalid interval
        let b = batch_meta(2, 0);
        assert_eq!(b.validate(),
            Err(Error::from(
                "invalid desc: Description { lower: Antichain { elements: [2] }, upper: Antichain { elements: [0] }, since: Antichain { elements: [0] } }"
            )),
        );
    }

    #[test]
    fn trace_meta_validate() {
        // Empty
        let b = ArrangementMeta {
            id: Id(0),
            trace_batches: vec![],
            since: Antichain::from_elem(0),
            seal: Antichain::from_elem(0),
            ..Default::default()
        };
        assert_eq!(b.validate(), Ok(()));

        // Normal case
        let b = ArrangementMeta {
            id: Id(0),
            trace_batches: vec![batch_meta(0, 1), batch_meta(1, 2)],
            since: Antichain::from_elem(0),
            seal: Antichain::from_elem(2),
            ..Default::default()
        };
        assert_eq!(b.validate(), Ok(()));

        // Gap
        let b = ArrangementMeta {
            id: Id(0),
            trace_batches: vec![batch_meta(0, 1), batch_meta(2, 3)],
            since: Antichain::from_elem(0),
            seal: Antichain::from_elem(3),
            ..Default::default()
        };
        assert_eq!(b.validate(), Err(Error::from("invalid batch sequence: Description { lower: Antichain { elements: [0] }, upper: Antichain { elements: [1] }, since: Antichain { elements: [0] } } followed by Description { lower: Antichain { elements: [2] }, upper: Antichain { elements: [3] }, since: Antichain { elements: [0] } }")));

        // Overlapping
        let b = ArrangementMeta {
            id: Id(0),
            trace_batches: vec![batch_meta(0, 2), batch_meta(1, 3)],
            since: Antichain::from_elem(0),
            seal: Antichain::from_elem(3),
            ..Default::default()
        };
        assert_eq!(b.validate(), Err(Error::from("invalid batch sequence: Description { lower: Antichain { elements: [0] }, upper: Antichain { elements: [2] }, since: Antichain { elements: [0] } } followed by Description { lower: Antichain { elements: [1] }, upper: Antichain { elements: [3] }, since: Antichain { elements: [0] } }")));

        // Normal case: trace since before nonzero trace upper
        let b = ArrangementMeta {
            id: Id(0),
            trace_batches: vec![batch_meta(0, 1), batch_meta(1, 2)],
            since: Antichain::from_elem(1),
            seal: Antichain::from_elem(2),
            ..Default::default()
        };
        assert_eq!(b.validate(), Ok(()));

        // Trace since at nonzero trace seal
        let b = ArrangementMeta {
            id: Id(0),
            trace_batches: vec![batch_meta(0, 2), batch_meta(2, 3)],
            since: Antichain::from_elem(3),
            seal: Antichain::from_elem(3),
            ..Default::default()
        };
        assert_eq!(b.validate(), Ok(()));

        // Trace since in advance of nonzero trace seal
        let b = ArrangementMeta {
            id: Id(0),
            trace_batches: vec![batch_meta(0, 2), batch_meta(2, 3)],
            since: Antichain::from_elem(4),
            seal: Antichain::from_elem(3),
            ..Default::default()
        };
        assert_eq!(b.validate(), Ok(()));

        // Normal case: batch since at or before trace since
        let b = ArrangementMeta {
            id: Id(0),
            trace_batches: vec![batch_meta(0, 1), batch_meta_full(1, 2, 1, 1)],
            since: Antichain::from_elem(1),
            seal: Antichain::from_elem(2),
            ..Default::default()
        };
        assert_eq!(b.validate(), Ok(()));

        // Batch since in advance of trace since
        let b = ArrangementMeta {
            id: Id(0),
            trace_batches: vec![batch_meta(0, 1), batch_meta_full(1, 2, 2, 1)],
            since: Antichain::from_elem(1),
            seal: Antichain::from_elem(2),
            ..Default::default()
        };
        assert_eq!(b.validate(), Err(Error::from("invalid batch since: Description { lower: Antichain { elements: [1] }, upper: Antichain { elements: [2] }, since: Antichain { elements: [2] } } in advance of trace since Antichain { elements: [1] }")));

        // Normal case: decreasing or constant compaction levels
        let b = ArrangementMeta {
            id: Id(0),
            trace_batches: vec![
                batch_meta_full(0, 1, 0, 2),
                batch_meta_full(1, 2, 0, 2),
                batch_meta_full(2, 3, 0, 1),
            ],
            since: Antichain::from_elem(0),
            seal: Antichain::from_elem(3),
            ..Default::default()
        };
        assert_eq!(b.validate(), Ok(()));

        // Increasing compaction level.
        let b = ArrangementMeta {
            id: Id(0),
            trace_batches: vec![batch_meta_full(0, 1, 0, 1), batch_meta_full(1, 2, 0, 2)],
            since: Antichain::from_elem(0),
            seal: Antichain::from_elem(2),
            ..Default::default()
        };
        assert_eq!(
            b.validate(),
            Err(Error::from(
                "invalid batch sequence: compaction level 1 followed by 2"
            ))
        );
    }

    #[test]
    fn unsealed_batch_meta_validate() {
        // Normal case
        let b = unsealed_batch_meta(0, 1);
        assert_eq!(b.validate(), Ok(()));

        // Empty interval
        let b = unsealed_batch_meta(0, 0);
        assert_eq!(
            b.validate(),
            Err(Error::from("invalid desc: SeqNo(0)..SeqNo(0)"))
        );

        // Invalid desc
        let b = unsealed_batch_meta(1, 0);
        assert_eq!(
            b.validate(),
            Err(Error::from("invalid desc: SeqNo(1)..SeqNo(0)"))
        );
    }

    #[test]
    fn unsealed_meta_validate() {
        // Empty
        let b = ArrangementMeta {
            id: Id(0),
            unsealed_batches: vec![],
            ..Default::default()
        };
        assert_eq!(b.validate(), Ok(()));

        // Normal case
        let b = ArrangementMeta {
            id: Id(0),
            unsealed_batches: vec![unsealed_batch_meta(0, 1), unsealed_batch_meta(1, 2)],
            ..Default::default()
        };
        assert_eq!(b.validate(), Ok(()));

        // Normal case: gap between sequence number ranges.
        let b = ArrangementMeta {
            id: Id(0),
            unsealed_batches: vec![unsealed_batch_meta(0, 1), unsealed_batch_meta(2, 3)],
            ..Default::default()
        };
        assert_eq!(b.validate(), Ok(()),);

        // Overlapping
        let b = ArrangementMeta {
            id: Id(0),
            unsealed_batches: vec![unsealed_batch_meta(0, 2), unsealed_batch_meta(1, 3)],
            ..Default::default()
        };
        assert_eq!(
            b.validate(),
            Err(Error::from(
                "invalid batch sequence: SeqNo(0)..SeqNo(2) followed by SeqNo(1)..SeqNo(3)"
            ))
        );
    }

    #[test]
    fn blob_meta_validate() {
        // Empty
        let b = BlobMeta::default();
        assert_eq!(b.validate(), Ok(()));

        // Normal case
        let b = BlobMeta {
            id_mapping: vec![("0", Id(0)).into(), ("1", Id(1)).into()],
            arrangements: vec![ArrangementMeta::new(Id(0)), ArrangementMeta::new(Id(1))],
            ..Default::default()
        };
        assert_eq!(b.validate(), Ok(()));

        // Duplicate external stream id
        let b = BlobMeta {
            id_mapping: vec![("1", Id(0)).into(), ("1", Id(1)).into()],
            arrangements: vec![ArrangementMeta::new(Id(0)), ArrangementMeta::new(Id(1))],
            ..Default::default()
        };
        assert_eq!(
            b.validate(),
            Err(Error::from("duplicate external stream name: 1"))
        );

        // Duplicate internal stream id
        let b = BlobMeta {
            id_mapping: vec![("0", Id(1)).into(), ("1", Id(1)).into()],
            arrangements: vec![ArrangementMeta::new(Id(0)), ArrangementMeta::new(Id(1))],
            ..Default::default()
        };
        assert_eq!(
            b.validate(),
            Err(Error::from("duplicate internal stream id: Id(1)"))
        );

        // Missing arrangement
        let b = BlobMeta {
            id_mapping: vec![("0", Id(0)).into()],
            arrangements: vec![],
            ..Default::default()
        };
        assert_eq!(
            b.validate(),
            Err(Error::from(
                "id_mapping id Id(0) not present in arrangements"
            ))
        );

        // Extra arrangement
        let b = BlobMeta {
            id_mapping: vec![],
            arrangements: vec![ArrangementMeta::new(Id(0))],
            ..Default::default()
        };
        assert_eq!(
            b.validate(),
            Err(Error::from(
                "arrangements id Id(0) not present in id_mapping"
            ))
        );

        // Duplicate in arrangements
        let b = BlobMeta {
            id_mapping: vec![("0", Id(0)).into()],
            arrangements: vec![ArrangementMeta::new(Id(0)), ArrangementMeta::new(Id(0))],
            ..Default::default()
        };
        assert_eq!(
            b.validate(),
            Err(Error::from("duplicate arrangement: Id(0)"))
        );

        // Normal case: unsealed ts_lower < ts_upper
        let b = BlobMeta {
            id_mapping: vec![("0", Id(0)).into()],
            arrangements: vec![ArrangementMeta {
                id: Id(0),
                unsealed_batches: vec![],
                trace_batches: vec![batch_meta(0, 1)],
                since: Antichain::from_elem(0),
                seal: Antichain::from_elem(1),
            }],
            ..Default::default()
        };
        assert_eq!(b.validate(), Ok(()),);

        // Normal case: unsealed ts_lower at ts_upper
        let b = BlobMeta {
            id_mapping: vec![("0", Id(0)).into()],
            arrangements: vec![ArrangementMeta {
                id: Id(0),
                unsealed_batches: vec![],
                trace_batches: vec![batch_meta(0, 1)],
                since: Antichain::from_elem(0),
                seal: Antichain::from_elem(1),
            }],
            ..Default::default()
        };
        assert_eq!(b.validate(), Ok(()),);

        // seqno less than one of the unsealed seqno uppers
        let b = BlobMeta {
            id_mapping: vec![("0", Id(0)).into()],
            seqno: SeqNo(2),
            arrangements: vec![ArrangementMeta {
                id: Id(0),
                unsealed_batches: vec![unsealed_batch_meta(0, 3)],
                ..Default::default()
            }],
            ..Default::default()
        };
        assert_eq!(
            b.validate(),
            Err(Error::from(
                "id Id(0) unsealed seqno_upper SeqNo(3) is not less or equal to the blob's seqno SeqNo(2)"
            ))
        );

        // Duplicate id in graveyard.
        let b = BlobMeta {
            graveyard: vec![("deleted", Id(0)).into(), ("1", Id(0)).into()],
            ..Default::default()
        };

        assert_eq!(
            b.validate(),
            Err(Error::from("duplicate deleted internal stream id: Id(0)"))
        );

        // Duplicate stream name in graveyard.
        let b = BlobMeta {
            graveyard: vec![("deleted", Id(0)).into(), ("deleted", Id(1)).into()],
            ..Default::default()
        };

        assert_eq!(
            b.validate(),
            Err(Error::from(
                "duplicate deleted external stream name: deleted"
            ))
        );

        // Duplicate id across graveyard and id_mapping.
        let b = BlobMeta {
            id_mapping: vec![("deleted", Id(0)).into()],
            graveyard: vec![("1", Id(0)).into()],
            ..Default::default()
        };

        assert_eq!(
            b.validate(),
            Err(Error::from(
                "duplicate internal stream id Id(0) across deleted and registered streams"
            ))
        );

        // Duplicate stream name across graveyard and id_mapping.
        let b = BlobMeta {
            id_mapping: vec![("name", Id(1)).into()],
            graveyard: vec![("name", Id(0)).into()],
            ..Default::default()
        };

        assert_eq!(
            b.validate(),
            Err(Error::from(
                "duplicate external stream name name across deleted and registered streams"
            ))
        );

        // Next stream id != id_mapping + deleted
        let b = BlobMeta {
            id_mapping: vec![("name", Id(1)).into()],
            ..Default::default()
        };

        assert_eq!(
            b.validate(),
            Err(Error::from(
                "next stream Id(2), but only registered 1 ids and deleted 0 ids"
            ))
        );

        let b = BlobMeta {
            id_mapping: vec![("0", Id(0)).into(), ("1", Id(1)).into()],
            seqno: SeqNo(2),
            arrangements: vec![
                ArrangementMeta {
                    id: Id(0),
                    unsealed_batches: vec![unsealed_batch_meta(0, 1)],
                    ..Default::default()
                },
                ArrangementMeta {
                    id: Id(1),
                    unsealed_batches: vec![unsealed_batch_meta(0, 1)],
                    ..Default::default()
                },
            ],
            ..Default::default()
        };

        assert_eq!(
            b.validate(),
            Err(Error::from("duplicate batch key found in unsealed: "))
        );

        let b = BlobMeta {
            id_mapping: vec![("0", Id(0)).into(), ("1", Id(1)).into()],
            arrangements: vec![
                ArrangementMeta {
                    id: Id(0),
                    trace_batches: vec![batch_meta(0, 1)],
                    since: Antichain::from_elem(0),
                    seal: Antichain::from_elem(1),
                    ..Default::default()
                },
                ArrangementMeta {
                    id: Id(1),
                    trace_batches: vec![batch_meta(0, 1)],
                    since: Antichain::from_elem(0),
                    seal: Antichain::from_elem(1),
                    ..Default::default()
                },
            ],
            ..Default::default()
        };

        assert_eq!(
            b.validate(),
            Err(Error::from("duplicate batch key found in trace: "))
        );
    }

    #[test]
    fn encoded_batch_sizes() {
        fn sizes(data: DataGenerator) -> (usize, usize) {
            let unsealed = BlobUnsealedBatch {
                desc: SeqNo(0)..SeqNo(1),
                updates: data.batches().collect(),
            };
            let trace = BlobTraceBatch {
                desc: Description::new(
                    Antichain::from_elem(0),
                    Antichain::from_elem(1),
                    Antichain::from_elem(0),
                ),
                updates: data.batches().collect(),
            };
            let (mut unsealed_buf, mut trace_buf) = (Vec::new(), Vec::new());
            unsealed.encode(&mut unsealed_buf);
            trace.encode(&mut trace_buf);
            (unsealed_buf.len(), trace_buf.len())
        }

        let record_size_bytes = DataGenerator::default().record_size_bytes;
        // Print all the sizes into one assert so we only have to update one
        // place if sizes change.
        assert_eq!(
            format!(
                "1/1={:?} 25/1={:?} 1000/1={:?} 1000/100={:?}",
                sizes(DataGenerator::new(1, record_size_bytes, 1)),
                sizes(DataGenerator::new(25, record_size_bytes, 25)),
                sizes(DataGenerator::new(1_000, record_size_bytes, 1_000)),
                sizes(DataGenerator::new(1_000, record_size_bytes, 1_000 / 100)),
            ),
            "1/1=(481, 501) 25/1=(2229, 2249) 1000/1=(72468, 72488) 1000/100=(106557, 106577)"
        );
    }
}