Skip to content

Commit

Permalink
ZED-22 replace hashmap with btreemap
Browse files Browse the repository at this point in the history
  • Loading branch information
zbigniewzolnierowicz committed Jun 26, 2024
1 parent 6b1e2aa commit d8bebcc
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 28 deletions.
14 changes: 7 additions & 7 deletions backend/src/domain/commands/recipes/create.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::BTreeMap;

use rayon::iter::{IndexedParallelIterator, IntoParallelIterator, ParallelIterator};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -49,7 +49,7 @@ pub struct CreateRecipe<'a> {
pub name: &'a str,
pub description: &'a str,
pub steps: Vec<String>,
pub time: HashMap<String, std::time::Duration>,
pub time: BTreeMap<String, std::time::Duration>,
pub ingredients: Vec<IngredientAmountData>,
pub servings: ServingsType,
}
Expand Down Expand Up @@ -138,7 +138,7 @@ mod tests {
&CreateRecipe {
name: "Recipe test",
description: "This is a test for the recipe",
time: HashMap::from([(
time: BTreeMap::from([(
"Prep time".to_string(),
std::time::Duration::from_secs(60),
)]),
Expand All @@ -159,7 +159,7 @@ mod tests {

#[tokio::test]
async fn create_recipe_with_proper_ingredients() {
let ingredients: HashMap<Uuid, Ingredient> = HashMap::from([(
let ingredients: BTreeMap<Uuid, Ingredient> = BTreeMap::from([(
Uuid::nil(),
IngredientModel {
id: Uuid::nil(),
Expand Down Expand Up @@ -188,7 +188,7 @@ mod tests {
&CreateRecipe {
name: "Recipe test",
description: "This is a test for the recipe",
time: HashMap::from([(
time: BTreeMap::from([(
"Prep time".to_string(),
std::time::Duration::from_secs(60),
)]),
Expand Down Expand Up @@ -241,7 +241,7 @@ mod tests {
&CreateRecipe {
name: "Recipe test",
description: "This is a test for the recipe",
time: HashMap::from([(
time: BTreeMap::from([(
"Prep time".to_string(),
std::time::Duration::from_secs(60),
)]),
Expand Down Expand Up @@ -292,7 +292,7 @@ mod tests {
&CreateRecipe {
name: "Recipe test",
description: "This is a test for the recipe",
time: HashMap::from([(
time: BTreeMap::from([(
"Prep time".to_string(),
std::time::Duration::from_secs(60),
)]),
Expand Down
40 changes: 31 additions & 9 deletions backend/src/domain/entities/recipe/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod errors;
use std::collections::HashMap;
use std::collections::BTreeMap;

use common::{IngredientUnitDTO, IngredientWithAmountDTO, RecipeDTO, ServingsTypeDTO};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
Expand All @@ -18,11 +18,31 @@ pub struct Recipe {
pub description: String,
pub steps: RecipeSteps,
pub ingredients: RecipeIngredients,
pub time: HashMap<String, std::time::Duration>,
pub time: BTreeMap<String, std::time::Duration>,
pub servings: ServingsType,
}
#[derive(Debug, Clone)]
pub struct RecipeIngredients(Vec<IngredientWithAmount>);

#[derive(PartialEq, Debug, Clone)]
impl AsRef<[IngredientWithAmount]> for RecipeIngredients {
fn as_ref(&self) -> &[IngredientWithAmount] {
&self.0
}
}

impl PartialEq for RecipeIngredients {
fn eq(&self, other: &Self) -> bool {
let mut a = self.0.clone();
let mut b = other.0.clone();
a.sort_by_key(|f| f.ingredient.id);
b.sort_by_key(|f| f.ingredient.id);

a.eq(&b)
}
}


#[derive(Debug, Clone)]
pub struct RecipeSteps(Vec<String>);

impl AsRef<[String]> for RecipeSteps {
Expand All @@ -31,12 +51,14 @@ impl AsRef<[String]> for RecipeSteps {
}
}

#[derive(PartialEq, Debug, Clone)]
pub struct RecipeIngredients(Vec<IngredientWithAmount>);
impl PartialEq for RecipeSteps {
fn eq(&self, other: &Self) -> bool {
let mut a = self.0.clone();
let mut b = other.0.clone();
a.sort();
b.sort();

impl AsRef<[IngredientWithAmount]> for RecipeIngredients {
fn as_ref(&self) -> &[IngredientWithAmount] {
&self.0
a.eq(&b)
}
}

Expand Down Expand Up @@ -225,7 +247,7 @@ pub struct RecipeChangeset {
pub name: Option<String>,
pub description: Option<String>,
pub steps: Option<RecipeSteps>,
pub time: Option<HashMap<String, std::time::Duration>>,
pub time: Option<BTreeMap<String, std::time::Duration>>,
pub servings: Option<ServingsType>,
}

Expand Down
10 changes: 5 additions & 5 deletions backend/src/domain/repositories/ingredients/in_memory/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
collections::{HashMap, HashSet},
collections::{BTreeMap, HashSet},
sync::Mutex,
};

Expand All @@ -18,7 +18,7 @@ use super::{
IngredientRepository,
};

pub struct InMemoryIngredientRepository(pub Mutex<HashMap<Uuid, Ingredient>>);
pub struct InMemoryIngredientRepository(pub Mutex<BTreeMap<Uuid, Ingredient>>);

#[async_trait]
impl IngredientRepository for InMemoryIngredientRepository {
Expand Down Expand Up @@ -145,7 +145,7 @@ impl IngredientRepository for InMemoryIngredientRepository {

impl InMemoryIngredientRepository {
pub fn new() -> Self {
HashMap::new().into()
BTreeMap::new().into()
}
}

Expand All @@ -155,8 +155,8 @@ impl Default for InMemoryIngredientRepository {
}
}

impl From<HashMap<Uuid, Ingredient>> for InMemoryIngredientRepository {
fn from(value: HashMap<Uuid, Ingredient>) -> Self {
impl From<BTreeMap<Uuid, Ingredient>> for InMemoryIngredientRepository {
fn from(value: BTreeMap<Uuid, Ingredient>) -> Self {
Self(value.into())
}
}
Expand Down
4 changes: 2 additions & 2 deletions backend/src/domain/repositories/recipe/in_memory/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::Duration;
use std::{collections::BTreeMap, time::Duration};

use crate::{
domain::entities::recipe::ServingsType,
Expand Down Expand Up @@ -90,7 +90,7 @@ async fn updating_a_recipe_succeeds() {
steps: vec!["WE UPDATED ANOTHER THING".to_string()]
.try_into()
.unwrap(),
time: HashMap::from([("Prep time".to_string(), Duration::from_secs(60))]),
time: BTreeMap::from([("Prep time".to_string(), Duration::from_secs(60))]),
servings: ServingsType::Exact(4),
..recipe
}
Expand Down
4 changes: 2 additions & 2 deletions backend/src/domain/repositories/recipe/postgres/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, time::Duration};
use std::{collections::BTreeMap, time::Duration};

use crate::{
domain::{
Expand Down Expand Up @@ -117,7 +117,7 @@ async fn updating_a_recipe_succeeds(pool: PgPool) {
steps: vec!["WE UPDATED ANOTHER THING".to_string()]
.try_into()
.unwrap(),
time: HashMap::from([("Prep time".to_string(), Duration::from_secs(60))]),
time: BTreeMap::from([("Prep time".to_string(), Duration::from_secs(60))]),
servings: ServingsType::Exact(4),
..recipe
}
Expand Down
6 changes: 3 additions & 3 deletions backend/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, time::Duration};
use std::{collections::BTreeMap, time::Duration};

use crate::domain::entities::recipe::{
IngredientUnit, IngredientWithAmount, RecipeChangeset, ServingsType,
Expand All @@ -14,7 +14,7 @@ pub fn recipe_fixture() -> Recipe {
id: uuid::Uuid::nil(),
name: "Hoisin Tofu and Broccoli".to_string(),
description: "If necessary, provide a very brief description of the dish in one or two sentences. For most dishes, this will be unnecessary. If there is a title image of this dish, it should be above this paragraph. You may also include prep/cook time and the number of servings as below:".to_string(),
time: HashMap::from([
time: BTreeMap::from([
("Prep time".to_string(), Duration::from_secs(15 * 60)),
("Cook time".to_string(), Duration::from_secs(10 * 60))
]),
Expand Down Expand Up @@ -85,7 +85,7 @@ pub fn recipe_changeset() -> RecipeChangeset {
RecipeChangeset {
name: Some("WE UPDATED THIS THING".to_string()),
description: Some("WE UPDATED THAT THING".to_string()),
time: Some(HashMap::from([(
time: Some(BTreeMap::from([(
"Prep time".to_string(),
Duration::from_secs(60),
)])),
Expand Down

0 comments on commit d8bebcc

Please sign in to comment.