/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2026 metamuffin <metamuffin.org>
*/

use crate::RowNum;
use anyhow::Result;

pub fn write_counter(txn: &mut dyn jellykv::Transaction, t: &[u8], value: u64) -> Result<()> {
    txn.set(t, &value.to_be_bytes())
}
pub fn read_counter(txn: &dyn jellykv::Transaction, t: &[u8], default: u64) -> Result<u64> {
    Ok(txn
        .get(t)?
        .map(|k| k.as_slice().try_into().map(RowNum::from_be_bytes).ok())
        .flatten()
        .unwrap_or(default))
}
