Skip to content

Commit

Permalink
Vec to Arc
Browse files Browse the repository at this point in the history
  • Loading branch information
GPeaky committed Oct 26, 2024
1 parent d955002 commit f9bd6e4
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 29 deletions.
12 changes: 6 additions & 6 deletions crates/db/src/cache/championship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use super::{EntityCache, CACHE_CAPACITY};

pub struct ChampionshipCache {
inner: Cache<i32, Arc<Championship>>,
races: Cache<i32, Vec<Arc<Race>>>,
races: Cache<i32, Arc<[Race]>>,
name_to_id: Cache<Box<str>, i32>,
user_championships: Cache<i32, Vec<Arc<Championship>>>,
user_championships: Cache<i32, Arc<[Championship]>>,
}

impl ChampionshipCache {
Expand All @@ -23,19 +23,19 @@ impl ChampionshipCache {
}
}

pub fn get_races(&self, id: &i32) -> Option<Vec<Arc<Race>>> {
pub fn get_races(&self, id: &i32) -> Option<Arc<[Race]>> {
self.races.get(id)
}

pub fn set_races(&self, id: i32, races: Vec<Arc<Race>>) {
pub fn set_races(&self, id: i32, races: Arc<[Race]>) {
self.races.insert(id, races)
}

pub fn delete_races(&self, id: &i32) {
self.races.remove(id);
}

pub fn get_user_championships(&self, user_id: &i32) -> Option<Vec<Arc<Championship>>> {
pub fn get_user_championships(&self, user_id: &i32) -> Option<Arc<[Championship]>> {
self.user_championships.get(user_id)
}

Expand All @@ -45,7 +45,7 @@ impl ChampionshipCache {
.and_then(|id| self.get(&id))
}

pub fn set_user_championships(&self, user_id: i32, championships: Vec<Arc<Championship>>) {
pub fn set_user_championships(&self, user_id: i32, championships: Arc<[Championship]>) {
self.user_championships.insert(user_id, championships);
}

Expand Down
11 changes: 4 additions & 7 deletions crates/entities/src/championship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,12 @@ impl Championship {

/// Creates a Vec<Arc<Championship>> from a RowStream
#[inline]
pub async fn from_row_stream(it: RowStream) -> AppResult<Vec<Arc<Self>>> {
pub async fn from_row_stream(it: RowStream) -> AppResult<Arc<[Self]>> {
tokio::pin!(it);

let mut championships = Vec::new();

let mut vec = Vec::new();
while let Some(row) = it.try_next().await? {
championships.push(Championship::from_row_arc(&row))
vec.push(Self::from_row(&row));
}

Ok(championships)
Ok(Arc::from(vec.into_boxed_slice()))
}
}
19 changes: 8 additions & 11 deletions crates/intelli-core/src/repositories/championship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,13 @@ impl ChampionshipRepository {
}
}

pub async fn races(&self, id: i32) -> AppResult<Vec<Arc<Race>>> {
if let Some(races) = self.db.cache.championship.get_races(&id) {
return Ok(races);
pub async fn races(&self, id: i32) -> AppResult<Arc<[Race]>> {
if let Some(cached) = self.db.cache.championship.get_races(&id) {
return Ok(cached);
}

let stream = {
let conn = self.db.pg.get().await?;

let championship_races_stmt = conn
.prepare_cached(
r#"
Expand All @@ -79,20 +78,18 @@ impl ChampionshipRepository {
"#,
)
.await?;

conn.query_raw(&championship_races_stmt, &[&id]).await?
};

tokio::pin!(stream);
let mut races = Vec::new();

let mut vec = Vec::new();
while let Some(row) = stream.try_next().await? {
races.push(Race::from_row_arc(&row))
vec.push(Race::from_row(&row));
}

self.db.cache.championship.set_races(id, races.clone());

Ok(races)
let result: Arc<[Race]> = Arc::from(vec.into_boxed_slice());
self.db.cache.championship.set_races(id, result.clone());
Ok(result)
}

/// Finds a championship by its name.
Expand Down
2 changes: 1 addition & 1 deletion crates/intelli-core/src/repositories/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl UserRepository {
///
/// # Returns
/// A vector of Championships associated with the user.
pub async fn championships(&self, id: i32) -> AppResult<Vec<Arc<Championship>>> {
pub async fn championships(&self, id: i32) -> AppResult<Arc<[Championship]>> {
if let Some(championships) = self.db.cache.championship.get_user_championships(&id) {
return Ok(championships);
};
Expand Down
6 changes: 4 additions & 2 deletions crates/structs/src/championship.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::sync::Arc;

use garde::Validate;
use serde::{Deserialize, Serialize};
use serde_trim::{option_string_trim, string_trim};

use entities::{Category, ChampionshipRole, SharedChampionship, SharedRace};
use entities::{Category, ChampionshipRole, Race, SharedChampionship};

// Championship Management
#[derive(Debug, Deserialize, Validate)]
Expand Down Expand Up @@ -58,5 +60,5 @@ pub struct ChampionshipAndUserId {
#[derive(Serialize)]
pub struct ChampionshipData {
pub championship: SharedChampionship,
pub races: Vec<SharedRace>,
pub races: Arc<[Race]>,
}
6 changes: 4 additions & 2 deletions crates/structs/src/user.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::sync::Arc;

use garde::Validate;
use serde::{Deserialize, Serialize};
use serde_trim::option_string_trim;

use entities::{SharedChampionship, SharedUser};
use entities::{Championship, SharedUser};

// User Management
#[derive(Debug, Deserialize, Validate)]
Expand All @@ -18,7 +20,7 @@ pub struct UserUpdateData {
#[derive(Debug, Serialize)]
pub struct UserProfileData {
pub user: SharedUser,
pub championships: Vec<SharedChampionship>,
pub championships: Arc<[Championship]>,
}

// Path Parameters
Expand Down

0 comments on commit f9bd6e4

Please sign in to comment.