Skip to main content

alopex_core/storage/format/
ingest.rs

1//! 外部で構築されたセクションをそのまま取り込むためのモデル。
2
3use crate::storage::compression::CompressionAlgorithm;
4use crate::storage::format::{FormatError, SectionType};
5
6/// セクションがカバーするキー範囲([start, end))。
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct KeyRange {
9    /// 開始キー(包含)。
10    pub start: Vec<u8>,
11    /// 終了キー(排他)。
12    pub end: Vec<u8>,
13}
14
15impl KeyRange {
16    /// start < end の妥当性を確認する。
17    pub fn validate(&self) -> Result<(), FormatError> {
18        if self.start >= self.end {
19            return Err(FormatError::InvalidKeyRange {
20                start: self.start.clone(),
21                end: self.end.clone(),
22            });
23        }
24        Ok(())
25    }
26
27    /// 範囲が重複しているか判定する。
28    pub fn overlaps(&self, other: &Self) -> bool {
29        !(self.end <= other.start || other.end <= self.start)
30    }
31}
32
33/// 外部ビルド済みセクションのインジェスト情報。
34#[derive(Debug, Clone)]
35pub struct ExternalSectionIngest {
36    /// 圧縮済みセクションデータ(再圧縮しない)。
37    pub section_data: Vec<u8>,
38    /// 圧縮アルゴリズム(セクション作成時の実際の方式)。
39    pub compression: CompressionAlgorithm,
40    /// 解凍後サイズ(バイト)。
41    pub uncompressed_length: u64,
42    /// オプション: 取り込み時に解凍検証を行うか。
43    pub validate_uncompressed: bool,
44    /// セクションタイプ。
45    pub section_type: SectionType,
46    /// 競合検出用のキー範囲。
47    pub key_range: KeyRange,
48}
49
50impl ExternalSectionIngest {
51    /// ヘルパーコンストラクタ。
52    pub fn new(
53        section_data: Vec<u8>,
54        compression: CompressionAlgorithm,
55        uncompressed_length: u64,
56        section_type: SectionType,
57        key_range: KeyRange,
58    ) -> Self {
59        Self {
60            section_data,
61            compression,
62            uncompressed_length,
63            validate_uncompressed: false,
64            section_type,
65            key_range,
66        }
67    }
68
69    /// 検証フラグ付きのコンストラクタ。
70    pub fn new_with_validation(
71        section_data: Vec<u8>,
72        compression: CompressionAlgorithm,
73        uncompressed_length: u64,
74        section_type: SectionType,
75        key_range: KeyRange,
76    ) -> Self {
77        Self {
78            section_data,
79            compression,
80            uncompressed_length,
81            validate_uncompressed: true,
82            section_type,
83            key_range,
84        }
85    }
86}