sui_indexer_alt_schema/
cp_bloom_blocks.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use diesel::define_sql_function;
5use diesel::prelude::*;
6use diesel::sql_types::Binary;
7use sui_field_count::FieldCount;
8
9use crate::blooms::blocked::BlockedBloomFilter;
10use crate::schema::cp_bloom_blocks;
11
12define_sql_function! {
13    /// Performs bitwise OR on two bytea values. Used for merging bloom filters.
14    fn function_bytea_or(a: Binary, b: Binary) -> Binary;
15}
16
17/// Number of checkpoints per checkpoint block.
18pub const CP_BLOCK_SIZE: u64 = 1000;
19
20/// Size of each bloom block in bytes.
21pub const BLOOM_BLOCK_BYTES: usize = 2048;
22
23/// Number of bits per bloom block.
24pub const BLOOM_BLOCK_BITS: usize = BLOOM_BLOCK_BYTES * 8;
25
26/// Number of blocks in the bloom filter (stored as separate database rows).
27pub const NUM_BLOOM_BLOCKS: usize = 128;
28
29/// Total bits in the bloom filter (256KB).
30pub const TOTAL_BLOOM_BITS: usize = NUM_BLOOM_BLOCKS * BLOOM_BLOCK_BITS;
31
32/// Number of hash functions (k) used per key.
33pub const NUM_HASHES: u32 = 5;
34
35/// Blocked bloom filter with checkpoint block dimensions.
36pub type CpBlockedBloomFilter = BlockedBloomFilter<BLOOM_BLOCK_BYTES, NUM_BLOOM_BLOCKS, NUM_HASHES>;
37
38/// Stored bloom block in the database (one row per bloom block per checkpoint block).
39#[derive(Insertable, Selectable, Queryable, Debug, Clone, FieldCount, QueryableByName)]
40#[diesel(table_name = cp_bloom_blocks)]
41pub struct StoredCpBloomBlock {
42    /// Checkpoint block ID (cp_num / CP_BLOCK_SIZE).
43    pub cp_block_index: i64,
44    /// Index of this bloom block within the 128-block filter (0-127).
45    pub bloom_block_index: i16,
46    /// Bloom filter bytes for this block.
47    pub bloom_filter: Vec<u8>,
48}
49
50/// The block a checkpoint belongs to. Checkpoints in a block share the same bloom filter and the block
51/// id is used as the seed for the blocked bloom filter hash functions.
52pub fn cp_block_index(cp_num: u64) -> i64 {
53    (cp_num / CP_BLOCK_SIZE) as i64
54}