Skip to content

Commit

Permalink
refactor: use consistent naming of access types
Browse files Browse the repository at this point in the history
We have an access type pattern for structs that contain the logic to
read specific tables in the db. There were called CeramicOne{Table},
with this change they are renamed to {Table}Access. The CeramicOne
prefix was meaningless and the table name alone was not enough to
distinguish the type from other types related to the same entities.
  • Loading branch information
nathanielc committed Oct 10, 2024
1 parent bc03421 commit ed542f2
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 28 deletions.
12 changes: 6 additions & 6 deletions event-svc/src/event/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use recon::{HashCount, ReconItem, Result as ReconResult, Sha256a};
use tracing::info;

use crate::event::{DeliverableRequirement, EventService};
use crate::store::{CeramicOneBlock, EventInsertable};
use crate::store::{BlockAccess, EventInsertable};
use crate::Error;

use super::service::{InsertResult, ValidationError, ValidationRequirement};
Expand Down Expand Up @@ -137,17 +137,17 @@ impl recon::Store for EventService {
#[async_trait::async_trait]
impl iroh_bitswap::Store for EventService {
async fn get_size(&self, cid: &Cid) -> anyhow::Result<usize> {
Ok(CeramicOneBlock::get_size(&self.pool, cid).await?)
Ok(BlockAccess::get_size(&self.pool, cid).await?)
}
async fn get(&self, cid: &Cid) -> anyhow::Result<Block> {
let maybe = CeramicOneBlock::get(&self.pool, cid).await?;
let maybe = BlockAccess::get(&self.pool, cid).await?;
maybe.ok_or_else(|| anyhow!("block {} does not exist", cid))
}
async fn has(&self, cid: &Cid) -> anyhow::Result<bool> {
Ok(CeramicOneBlock::has(&self.pool, cid).await?)
Ok(BlockAccess::has(&self.pool, cid).await?)
}
async fn put(&self, block: &Block) -> anyhow::Result<bool> {
Ok(CeramicOneBlock::put(&self.pool, block).await?)
Ok(BlockAccess::put(&self.pool, block).await?)
}
}

Expand Down Expand Up @@ -278,7 +278,7 @@ impl ceramic_api::EventService for EventService {
}

async fn get_block(&self, cid: &Cid) -> anyhow::Result<Option<Vec<u8>>> {
let block = CeramicOneBlock::get(&self.pool, cid).await?;
let block = BlockAccess::get(&self.pool, cid).await?;
Ok(block.map(|b| b.data.to_vec()))
}
}
Expand Down
4 changes: 2 additions & 2 deletions event-svc/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ mod sql;
pub use metrics::{Metrics, StoreMetricsMiddleware};
pub use sql::{
entities::{BlockHash, EventBlockRaw, EventInsertable},
CeramicOneBlock, CeramicOneEventBlock, CeramicOneVersion, Error, EventAccess,
EventRowDelivered, InsertResult, InsertedEvent, Result, SqlitePool, SqliteRootStore,
BlockAccess, Error, EventAccess, EventBlockAccess, EventRowDelivered, InsertResult,
InsertedEvent, Result, SqlitePool, SqliteRootStore, VersionAccess,
};
6 changes: 3 additions & 3 deletions event-svc/src/store/sql/access/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use crate::store::{
};

/// Access to the block table and related logic
pub struct CeramicOneBlock {}
pub struct BlockAccess {}

impl CeramicOneBlock {
impl BlockAccess {
/// Insert a block in a transaction (i.e. when creating an event)
pub(crate) async fn insert(
conn: &mut SqliteTransaction<'_>,
Expand Down Expand Up @@ -69,7 +69,7 @@ impl CeramicOneBlock {
/// Add a block to the database
pub async fn put(pool: &SqlitePool, block: &iroh_bitswap::Block) -> Result<bool> {
let mut tx = pool.begin_tx().await?;
let new = CeramicOneBlock::insert(&mut tx, block.cid().hash(), block.data()).await?;
let new = BlockAccess::insert(&mut tx, block.cid().hash(), block.data()).await?;
tx.commit().await?;
Ok(new)
}
Expand Down
11 changes: 6 additions & 5 deletions event-svc/src/store/sql/access/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::store::{
},
query::{EventQuery, ReconQuery, SqlBackend},
},
CeramicOneBlock, CeramicOneEventBlock, Error, Result,
BlockAccess, Error, EventBlockAccess, Result,
};

#[derive(Debug)]
Expand Down Expand Up @@ -69,8 +69,7 @@ pub struct EventRowDelivered {
}

/// Access to the ceramic event table and related logic
/// Unlike other access models the event access must maintain state about its current delivered
/// counter.
/// Unlike other access models the event access must maintain state.
#[derive(Debug)]
pub struct EventAccess {
// The delivered count is tied to a specific sql db so we keep a reference to the pool here so
Expand All @@ -81,6 +80,8 @@ pub struct EventAccess {

impl EventAccess {
/// Gain access to the events table.
/// EventAccess is stateful and the state is tied to the give connection pool, as such a pool
/// must be provided so as to not accidentially mix connections to different dbs.
pub async fn try_new(pool: SqlitePool) -> Result<Self> {
let s = Self {
pool,
Expand Down Expand Up @@ -215,8 +216,8 @@ impl EventAccess {
inserted.push(InsertedEvent::new(new_key, item));
if new_key {
for block in item.get_raw_blocks().await?.iter() {
CeramicOneBlock::insert(&mut tx, block.multihash.inner(), &block.bytes).await?;
CeramicOneEventBlock::insert(&mut tx, block).await?;
BlockAccess::insert(&mut tx, block.multihash.inner(), &block.bytes).await?;
EventBlockAccess::insert(&mut tx, block).await?;
}
}
// the item already existed so we didn't mark it as deliverable on insert
Expand Down
4 changes: 2 additions & 2 deletions event-svc/src/store/sql/access/event_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use crate::store::{
};

/// Access to the event_block table and related logic
pub struct CeramicOneEventBlock {}
pub struct EventBlockAccess {}

impl CeramicOneEventBlock {
impl EventBlockAccess {
/// Insert an event block in a transaction i.e. when storing a new ceramic event
pub(crate) async fn insert(
conn: &mut SqliteTransaction<'_>,
Expand Down
8 changes: 4 additions & 4 deletions event-svc/src/store/sql/access/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mod block;
pub mod event;
mod event;
mod event_block;
mod version;

pub use block::CeramicOneBlock;
pub use block::BlockAccess;
pub use event::{EventAccess, EventRowDelivered, InsertResult, InsertedEvent};
pub use event_block::CeramicOneEventBlock;
pub use version::CeramicOneVersion;
pub use event_block::EventBlockAccess;
pub use version::VersionAccess;
8 changes: 4 additions & 4 deletions event-svc/src/store/sql/access/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ impl std::str::FromStr for SemVer {

#[derive(Debug, Clone)]
/// Access to ceramic version information
pub struct CeramicOneVersion {}
pub struct VersionAccess {}

impl CeramicOneVersion {
impl VersionAccess {
/// Fetch the previous version from the database. May be None if no previous version exists.
pub async fn fetch_previous(pool: &SqlitePool) -> Result<Option<VersionRow>> {
let current = SemVer::from_str(env!("CARGO_PKG_VERSION"))?;
Expand All @@ -84,12 +84,12 @@ mod test {
#[tokio::test]
async fn insert_version() {
let mem = SqlitePool::connect_in_memory().await.unwrap();
CeramicOneVersion::insert_current(&mem).await.unwrap();
VersionAccess::insert_current(&mem).await.unwrap();
}

#[tokio::test]
async fn prev_version() {
let mem = SqlitePool::connect_in_memory().await.unwrap();
CeramicOneVersion::fetch_previous(&mem).await.unwrap();
VersionAccess::fetch_previous(&mem).await.unwrap();
}
}
4 changes: 2 additions & 2 deletions event-svc/src/store/sql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ mod root;
mod test;

pub use access::{
CeramicOneBlock, CeramicOneEventBlock, CeramicOneVersion, EventAccess, EventRowDelivered,
InsertResult, InsertedEvent,
BlockAccess, EventAccess, EventBlockAccess, EventRowDelivered, InsertResult, InsertedEvent,
VersionAccess,
};
pub use ceramic_sql::{sqlite::SqlitePool, Error, Result};
pub use root::SqliteRootStore;

0 comments on commit ed542f2

Please sign in to comment.