use crate::shard::MappingShard;
use crate::traits::{ShardKey, ShardValue};
use anchor_lang::prelude::*;
use anchor_lang::solana_program::pubkey::Pubkey;
pub const SHARD_SEED_PREFIX: &[u8] = b"mapping_shard";
pub fn derive_shard_pda(program_id: &Pubkey, shard_index: u8) -> (Pubkey, u8) {
Pubkey::find_program_address(&[SHARD_SEED_PREFIX, &[shard_index]], program_id)
}
pub fn estimate_shard_account_size(k_size: usize, v_size: usize, max_items: usize) -> usize {
8 + 4 + (max_items * (k_size + v_size + 4)) + 32
}
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug)]
pub struct AccountShard<K: ShardKey, V: ShardValue> {
pub authority: Pubkey,
pub shard: MappingShard<K, V>,
}
impl<K: ShardKey, V: ShardValue> AccountShard<K, V> {
pub fn new(authority: Pubkey, shard_id: u8, max_items: u16) -> Self {
Self {
authority,
shard: MappingShard::new(shard_id, max_items),
}
}
}