paimon 0.1.0

The rust implementation of Apache Paimon
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

//! SST block infrastructure compatible with Java Paimon's sst package.
//!
//! Block layout:
//! ```text
//!     +---------------+
//!     | Block Trailer |  (5 bytes: 1 byte compression type + 4 bytes CRC32)
//!     +---------------+
//!     |  Block Data   |
//!     +---------------+--------------------------------+----+
//!     | key len | key bytes | value len | value bytes  |    |
//!     +------------------------------------------------+    |
//!     | key len | key bytes | value len | value bytes  |    +-> Key-Value pairs
//!     +------------------------------------------------+    |
//!     |                  ... ...                       |    |
//!     +------------------------------------------------+----+
//!     | entry pos | entry pos |     ...    | entry pos |    +-> optional, for unaligned block
//!     +------------------------------------------------+----+
//!     |   entry num  /  entry size   |   aligned type  |
//!     +------------------------------------------------+
//! ```

use crate::btree::var_len::{
    decode_var_int, decode_var_int_from_slice, encode_var_int, encode_var_int_to_slice,
};
use std::cmp::Ordering;
use std::io::{self, Cursor, Read, Write};

/// Block compression type, compatible with Java's BlockCompressionType.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum BlockCompressionType {
    None = 0,
    Zstd = 1,
    Lz4 = 2,
    Lzo = 3,
}

impl BlockCompressionType {
    pub fn from_persistent_id(id: u8) -> io::Result<Self> {
        match id {
            0 => Ok(Self::None),
            1 => Ok(Self::Zstd),
            2 => Ok(Self::Lz4),
            3 => Ok(Self::Lzo),
            _ => Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("Unknown compression type: {id}"),
            )),
        }
    }
}

/// Block aligned type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum BlockAlignedType {
    Aligned = 0,
    Unaligned = 1,
}

impl BlockAlignedType {
    pub fn from_byte(b: u8) -> io::Result<Self> {
        match b {
            0 => Ok(Self::Aligned),
            1 => Ok(Self::Unaligned),
            _ => Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("Unknown aligned type: {b}"),
            )),
        }
    }
}

/// Block trailer: compression type (1 byte) + CRC32 (4 bytes) = 5 bytes.
pub const BLOCK_TRAILER_LENGTH: usize = 5;

pub struct BlockTrailer {
    pub compression_type: BlockCompressionType,
    pub crc32c: u32,
}

impl BlockTrailer {
    #[allow(dead_code)]
    pub fn write_to(&self, out: &mut impl Write) -> io::Result<()> {
        out.write_all(&[self.compression_type as u8])?;
        out.write_all(&(self.crc32c as i32).to_le_bytes())?;
        Ok(())
    }

    /// Encode trailer to a fixed-size stack array.
    pub fn to_bytes(&self) -> [u8; BLOCK_TRAILER_LENGTH] {
        let crc_bytes = (self.crc32c as i32).to_le_bytes();
        [
            self.compression_type as u8,
            crc_bytes[0],
            crc_bytes[1],
            crc_bytes[2],
            crc_bytes[3],
        ]
    }

    pub fn read_from(data: &[u8]) -> io::Result<Self> {
        if data.len() < BLOCK_TRAILER_LENGTH {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "Block trailer too short",
            ));
        }
        let compression_type = BlockCompressionType::from_persistent_id(data[0])?;
        let crc32c = u32::from_le_bytes([data[1], data[2], data[3], data[4]]);
        Ok(Self {
            compression_type,
            crc32c,
        })
    }
}

/// Compute CRC32 of block data + compression type byte (compatible with Java).
pub fn compute_crc32(data: &[u8], compression_type: BlockCompressionType) -> u32 {
    let mut hasher = crc32fast::Hasher::new();
    hasher.update(data);
    hasher.update(&[compression_type as u8]);
    hasher.finalize()
}

/// BlockHandle: offset + size of a block in the file.
pub const BLOCK_HANDLE_MAX_ENCODED_LENGTH: usize = 14; // 9 (var long) + 5 (var int)

#[derive(Debug, Clone, Copy, Default)]
pub struct BlockHandle {
    pub offset: u64,
    pub size: u32,
}

impl BlockHandle {
    pub fn new(offset: u64, size: u32) -> Self {
        Self { offset, size }
    }

    pub fn full_block_size(&self) -> u32 {
        self.size + BLOCK_TRAILER_LENGTH as u32
    }

    pub fn write_to(&self, out: &mut impl Write) -> io::Result<()> {
        crate::btree::var_len::encode_var_long(out, self.offset as i64)?;
        encode_var_int(out, self.size as i32)?;
        Ok(())
    }

    pub fn read_from(input: &mut impl Read) -> io::Result<Self> {
        let offset = crate::btree::var_len::decode_var_long(input)? as u64;
        let size = decode_var_int(input)? as u32;
        Ok(Self { offset, size })
    }

    pub fn encode(&self) -> Vec<u8> {
        let mut buf = Vec::with_capacity(BLOCK_HANDLE_MAX_ENCODED_LENGTH);
        self.write_to(&mut buf).unwrap();
        buf
    }

    /// Encode into a stack buffer, returning (buffer, length).
    pub fn encode_to_buf(&self) -> ([u8; BLOCK_HANDLE_MAX_ENCODED_LENGTH], usize) {
        let mut buf = [0u8; BLOCK_HANDLE_MAX_ENCODED_LENGTH];
        let mut cursor = io::Cursor::new(&mut buf[..]);
        self.write_to(&mut cursor).unwrap();
        let len = cursor.position() as usize;
        (buf, len)
    }

    pub fn decode(data: &[u8]) -> io::Result<Self> {
        Self::read_from(&mut Cursor::new(data))
    }
}

/// BlockWriter: builds a block of key-value pairs.
pub struct BlockWriter {
    data: Vec<u8>,
    positions: Vec<i32>,
    aligned_size: i32,
    aligned: bool,
}

impl BlockWriter {
    pub fn new(capacity: usize) -> Self {
        Self {
            data: Vec::with_capacity(capacity),
            positions: Vec::with_capacity(32),
            aligned_size: 0,
            aligned: true,
        }
    }

    pub fn add(&mut self, key: &[u8], value: &[u8]) {
        let start = self.data.len() as i32;

        // key_len (var int) + key + value_len (var int) + value
        let mut var_buf = [0u8; 5];
        let n = encode_var_int_to_slice(&mut var_buf, 0, key.len() as i32);
        self.data.extend_from_slice(&var_buf[..n]);
        self.data.extend_from_slice(key);

        let n = encode_var_int_to_slice(&mut var_buf, 0, value.len() as i32);
        self.data.extend_from_slice(&var_buf[..n]);
        self.data.extend_from_slice(value);

        let end = self.data.len() as i32;
        self.positions.push(start);

        if self.aligned {
            let current_size = end - start;
            if self.aligned_size == 0 {
                self.aligned_size = current_size;
            } else {
                self.aligned = self.aligned_size == current_size;
            }
        }
    }

    pub fn entry_count(&self) -> usize {
        self.positions.len()
    }

    /// Estimated memory usage of the block.
    pub fn memory(&self) -> usize {
        let mut mem = self.data.len() + 5;
        if !self.aligned {
            mem += self.positions.len() * 4;
        }
        mem
    }

    /// Finish building the block, returning the raw block bytes.
    pub fn finish(&mut self) -> Vec<u8> {
        if self.positions.is_empty() {
            self.aligned = false;
        }

        if self.aligned {
            self.data
                .extend_from_slice(&(self.aligned_size).to_le_bytes());
        } else {
            for &pos in &self.positions {
                self.data.extend_from_slice(&pos.to_le_bytes());
            }
            self.data
                .extend_from_slice(&(self.positions.len() as i32).to_le_bytes());
        }
        self.data.push(if self.aligned {
            BlockAlignedType::Aligned as u8
        } else {
            BlockAlignedType::Unaligned as u8
        });

        let result = std::mem::take(&mut self.data);
        self.reset();
        result
    }

    pub fn reset(&mut self) {
        self.data.clear();
        self.positions.clear();
        self.aligned_size = 0;
        self.aligned = true;
    }
}

/// BlockReader: reads a block and supports binary search.
pub struct BlockReader {
    /// The raw data portion (key-value pairs only, no index/footer).
    pub(crate) data: Vec<u8>,
    record_count: usize,
    /// For aligned blocks: record_size; for unaligned: index offsets.
    seek_info: SeekInfo,
}

enum SeekInfo {
    Aligned { record_size: usize },
    Unaligned { offsets: Vec<i32> },
}

impl BlockReader {
    /// Create a BlockReader from raw (decompressed) block bytes (borrows and copies).
    #[allow(dead_code)]
    pub fn create(block: &[u8]) -> io::Result<Self> {
        Self::create_from_vec(block.to_vec())
    }

    /// Create a BlockReader taking ownership of the decompressed block bytes (zero-copy).
    pub fn create_from_vec(mut block: Vec<u8>) -> io::Result<Self> {
        if block.is_empty() {
            return Ok(Self {
                data: Vec::new(),
                record_count: 0,
                seek_info: SeekInfo::Unaligned {
                    offsets: Vec::new(),
                },
            });
        }

        let aligned_type = BlockAlignedType::from_byte(block[block.len() - 1])?;
        let int_value = i32::from_le_bytes([
            block[block.len() - 5],
            block[block.len() - 4],
            block[block.len() - 3],
            block[block.len() - 2],
        ]);

        match aligned_type {
            BlockAlignedType::Aligned => {
                let record_size = int_value as usize;
                let data_len = block.len() - 5;
                let record_count = if record_size > 0 {
                    data_len / record_size
                } else {
                    0
                };
                block.truncate(data_len);
                Ok(Self {
                    data: block,
                    record_count,
                    seek_info: SeekInfo::Aligned { record_size },
                })
            }
            BlockAlignedType::Unaligned => {
                let num_entries = int_value as usize;
                let index_len = num_entries * 4;
                let data_end = block.len() - 5 - index_len;
                let index_start = data_end;

                let mut offsets = Vec::with_capacity(num_entries);
                for i in 0..num_entries {
                    let pos = index_start + i * 4;
                    let off = i32::from_le_bytes([
                        block[pos],
                        block[pos + 1],
                        block[pos + 2],
                        block[pos + 3],
                    ]);
                    offsets.push(off);
                }

                block.truncate(data_end);
                Ok(Self {
                    data: block,
                    record_count: num_entries,
                    seek_info: SeekInfo::Unaligned { offsets },
                })
            }
        }
    }

    #[allow(dead_code)]
    pub fn record_count(&self) -> usize {
        self.record_count
    }

    fn seek_to_position(&self, record_pos: usize) -> usize {
        match &self.seek_info {
            SeekInfo::Aligned { record_size } => record_pos * record_size,
            SeekInfo::Unaligned { offsets } => offsets[record_pos] as usize,
        }
    }

    /// Read only the key at the given byte offset, skipping value parsing.
    /// Returns (key_slice, next_entry_offset).
    fn read_key_at(&self, offset: usize) -> (&[u8], usize) {
        let (key_len, consumed) = decode_var_int_from_slice(&self.data, offset);
        let key_start = offset + consumed;
        let key = &self.data[key_start..key_start + key_len as usize];

        let val_offset = key_start + key_len as usize;
        let (val_len, consumed2) = decode_var_int_from_slice(&self.data, val_offset);
        let next_offset = val_offset + consumed2 + val_len as usize;

        (key, next_offset)
    }

    /// Read a key-value entry at the given byte offset, returning (key, value, next_offset).
    /// Returns slices into the block's data buffer (zero-copy).
    pub(crate) fn read_entry_at(&self, offset: usize) -> (&[u8], &[u8], usize) {
        let (key_len, consumed) = decode_var_int_from_slice(&self.data, offset);
        let key_start = offset + consumed;
        let key = &self.data[key_start..key_start + key_len as usize];

        let val_offset = key_start + key_len as usize;
        let (val_len, consumed2) = decode_var_int_from_slice(&self.data, val_offset);
        let val_start = val_offset + consumed2;
        let value = &self.data[val_start..val_start + val_len as usize];

        (key, value, val_start + val_len as usize)
    }

    /// Create a sequential iterator over all entries.
    #[cfg(test)]
    pub fn iter(&self) -> BlockIter<'_> {
        BlockIter {
            reader: self,
            offset: 0,
            index: 0,
        }
    }

    /// Binary search for the given target key. Returns an iterator positioned at the
    /// first entry whose key >= target_key.
    /// The comparator compares two key byte slices.
    pub fn seek_and_iter<F>(&self, target_key: &[u8], cmp: &F) -> (bool, BlockIter<'_>)
    where
        F: Fn(&[u8], &[u8]) -> Ordering,
    {
        let mut left: i32 = 0;
        let mut right: i32 = self.record_count as i32 - 1;
        let mut found = false;
        let mut best_index: Option<usize> = None;
        let mut best_offset: Option<usize> = None;

        while left <= right {
            let mid = left + (right - left) / 2;
            let byte_offset = self.seek_to_position(mid as usize);
            let (key, _next_offset) = self.read_key_at(byte_offset);

            match cmp(key, target_key) {
                Ordering::Equal => {
                    found = true;
                    best_index = Some(mid as usize);
                    best_offset = Some(byte_offset);
                    break;
                }
                Ordering::Greater => {
                    best_index = Some(mid as usize);
                    best_offset = Some(byte_offset);
                    right = mid - 1;
                }
                Ordering::Less => {
                    left = mid + 1;
                }
            }
        }

        match (best_index, best_offset) {
            (Some(idx), Some(off)) => (
                found,
                BlockIter {
                    reader: self,
                    offset: off,
                    index: idx,
                },
            ),
            _ => (
                false,
                BlockIter {
                    reader: self,
                    offset: self.data.len(),
                    index: self.record_count,
                },
            ),
        }
    }
}

/// Iterator over block entries.
pub struct BlockIter<'a> {
    reader: &'a BlockReader,
    pub(crate) offset: usize,
    index: usize,
}

impl<'a> BlockIter<'a> {
    pub fn has_next(&self) -> bool {
        self.index < self.reader.record_count && self.offset < self.reader.data.len()
    }

    /// Returns (key, value) as borrowed slices (zero-copy).
    pub fn next(&mut self) -> Option<(&'a [u8], &'a [u8])> {
        if !self.has_next() {
            return None;
        }
        let (key, value, next_offset) = self.reader.read_entry_at(self.offset);
        self.offset = next_offset;
        self.index += 1;
        Some((key, value))
    }
}

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

    #[test]
    fn test_block_roundtrip_aligned() {
        let mut writer = BlockWriter::new(1024);
        // All entries same size -> aligned
        writer.add(b"aa", b"11");
        writer.add(b"bb", b"22");
        writer.add(b"cc", b"33");

        let block = writer.finish();
        let reader = BlockReader::create(&block).unwrap();
        assert_eq!(reader.record_count(), 3);

        let entries: Vec<_> = {
            let mut iter = reader.iter();
            let mut v = Vec::new();
            while let Some(e) = iter.next() {
                v.push(e);
            }
            v
        };
        assert_eq!(entries[0], (&b"aa"[..], &b"11"[..]));
        assert_eq!(entries[1], (&b"bb"[..], &b"22"[..]));
        assert_eq!(entries[2], (&b"cc"[..], &b"33"[..]));
    }

    #[test]
    fn test_block_roundtrip_unaligned() {
        let mut writer = BlockWriter::new(1024);
        writer.add(b"a", b"1");
        writer.add(b"bb", b"22");
        writer.add(b"ccc", b"333");

        let block = writer.finish();
        let reader = BlockReader::create(&block).unwrap();
        assert_eq!(reader.record_count(), 3);

        let entries: Vec<_> = {
            let mut iter = reader.iter();
            let mut v = Vec::new();
            while let Some(e) = iter.next() {
                v.push(e);
            }
            v
        };
        assert_eq!(entries[0], (&b"a"[..], &b"1"[..]));
        assert_eq!(entries[1], (&b"bb"[..], &b"22"[..]));
        assert_eq!(entries[2], (&b"ccc"[..], &b"333"[..]));
    }

    #[test]
    fn test_block_seek() {
        let mut writer = BlockWriter::new(1024);
        writer.add(b"apple", b"1");
        writer.add(b"banana", b"2");
        writer.add(b"cherry", b"3");
        writer.add(b"date", b"4");

        let block = writer.finish();
        let reader = BlockReader::create(&block).unwrap();

        let cmp = |a: &[u8], b: &[u8]| a.cmp(b);

        // Exact match
        let (found, mut iter) = reader.seek_and_iter(b"banana", &cmp);
        assert!(found);
        let (k, v) = iter.next().unwrap();
        assert_eq!(k, b"banana");
        assert_eq!(v, b"2");

        // Seek to position >= "bz" -> should land on "cherry"
        let (found, mut iter) = reader.seek_and_iter(b"bz", &cmp);
        assert!(!found);
        let (k, _) = iter.next().unwrap();
        assert_eq!(k, b"cherry");

        // Seek past all entries
        let (found, iter) = reader.seek_and_iter(b"zzz", &cmp);
        assert!(!found);
        assert!(!iter.has_next());
    }

    #[test]
    fn test_block_handle_roundtrip() {
        let handle = BlockHandle::new(12345, 6789);
        let encoded = handle.encode();
        let decoded = BlockHandle::decode(&encoded).unwrap();
        assert_eq!(decoded.offset, 12345);
        assert_eq!(decoded.size, 6789);
    }

    #[test]
    fn test_block_trailer_roundtrip() {
        let trailer = BlockTrailer {
            compression_type: BlockCompressionType::None,
            crc32c: 0xDEADBEEF,
        };
        let mut buf = Vec::new();
        trailer.write_to(&mut buf).unwrap();
        assert_eq!(buf.len(), BLOCK_TRAILER_LENGTH);
        let decoded = BlockTrailer::read_from(&buf).unwrap();
        assert_eq!(decoded.compression_type, BlockCompressionType::None);
        assert_eq!(decoded.crc32c, 0xDEADBEEF);
    }

    #[test]
    fn test_crc32_compatible() {
        let data = b"hello world";
        let crc = compute_crc32(data, BlockCompressionType::None);
        // Just verify it's deterministic
        let crc2 = compute_crc32(data, BlockCompressionType::None);
        assert_eq!(crc, crc2);
    }
}