use {
crate::types::{EventHeapHeader, EventNode},
carbon_core::{account::AccountMetadata, postgres::metadata::AccountRowMetadata},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct EventHeapRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub header: sqlx::types::Json<EventHeapHeader>,
pub nodes: sqlx::types::Json<Vec<EventNode>>,
pub reserved: Vec<u8>,
}
impl EventHeapRow {
pub fn from_parts(
source: crate::accounts::event_heap::EventHeap,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
header: sqlx::types::Json(source.header),
nodes: sqlx::types::Json(source.nodes.to_vec()),
reserved: source.reserved.to_vec(),
}
}
}
impl TryFrom<EventHeapRow> for crate::accounts::event_heap::EventHeap {
type Error = carbon_core::error::Error;
fn try_from(source: EventHeapRow) -> Result<Self, Self::Error> {
Ok(Self {
header: source.header.0,
nodes: source
.nodes
.0
.into_iter()
.collect::<Vec<_>>()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
reserved: source.reserved.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 64 bytes"
.to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::event_heap::EventHeap {
fn table() -> &'static str {
"event_heap_account"
}
fn columns() -> Vec<&'static str> {
vec!["__pubkey", "__slot", "header", "nodes", "reserved"]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for EventHeapRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO event_heap_account (
"header",
"nodes",
"reserved",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5
)"#,
)
.bind(&self.header)
.bind(&self.nodes)
.bind(&self.reserved)
.bind(self.account_metadata.pubkey)
.bind(&self.account_metadata.slot)
.execute(pool)
.await
.map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
Ok(())
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Upsert for EventHeapRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO event_heap_account (
"header",
"nodes",
"reserved",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"header" = EXCLUDED."header",
"nodes" = EXCLUDED."nodes",
"reserved" = EXCLUDED."reserved",
__slot = EXCLUDED.__slot
"#,
)
.bind(&self.header)
.bind(&self.nodes)
.bind(&self.reserved)
.bind(self.account_metadata.pubkey)
.bind(&self.account_metadata.slot)
.execute(pool)
.await
.map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
Ok(())
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Delete for EventHeapRow {
type Key = carbon_core::postgres::primitives::Pubkey;
async fn delete(key: Self::Key, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"DELETE FROM event_heap_account WHERE
__pubkey = $1
"#,
)
.bind(key)
.execute(pool)
.await
.map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
Ok(())
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Lookup for EventHeapRow {
type Key = carbon_core::postgres::primitives::Pubkey;
async fn lookup(
key: Self::Key,
pool: &sqlx::PgPool,
) -> carbon_core::error::CarbonResult<Option<Self>> {
let row = sqlx::query_as(
r#"SELECT * FROM event_heap_account WHERE
__pubkey = $1
"#,
)
.bind(key)
.fetch_optional(pool)
.await
.map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
Ok(row)
}
}
pub struct EventHeapMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for EventHeapMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS event_heap_account (
-- Account data
"header" JSONB NOT NULL,
"nodes" JSONB NOT NULL,
"reserved" BYTEA NOT NULL,
-- Account metadata
__pubkey BYTEA NOT NULL,
__slot NUMERIC(20),
PRIMARY KEY (__pubkey)
)"#,
)
.execute(connection)
.await?;
Ok(())
}
async fn down(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(r#"DROP TABLE IF EXISTS event_heap_account"#)
.execute(connection)
.await?;
Ok(())
}
}