Lines
100 %
Functions
Branches
//! The per-user account hierarchy lock. Every hierarchy writer takes it first,
//! then the tag keys, then the account rows.
use sqlx::PgConnection;
use uuid::Uuid;
/// Advisory lock class of the account hierarchy ("hier").
const HIERARCHY_LOCK_CLASS: i32 = 0x6869_6572;
/// A stable 32-bit key for `user_id`: the xor of its four big-endian words.
pub(crate) fn hierarchy_key(user_id: Uuid) -> i32 {
user_id
.as_bytes()
.as_chunks::<4>()
.0
.iter()
.fold(0, |key, word| key ^ i32::from_be_bytes(*word))
}
/// Lock the account hierarchy of `user_id` until the transaction ends.
pub(crate) async fn lock_hierarchy(
conn: &mut PgConnection,
user_id: Uuid,
) -> Result<(), sqlx::Error> {
sqlx::query_file!(
"sql/select/accounts/lock_hierarchy.sql",
HIERARCHY_LOCK_CLASS,
hierarchy_key(user_id)
)
.fetch_one(conn)
.await?;
Ok(())