Skip to content

Commit

Permalink
Unify order api and websocket problem sorting (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
smrtrfszm authored Nov 3, 2024
1 parent e70d6e5 commit 10bef7d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
26 changes: 5 additions & 21 deletions src/handlers/problem/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
error::{self, DatabaseError, Result},
extractors::Json,
handlers::socket::Event,
utils::{execute_str, topics},
utils::{execute_str, sort_linked, topics},
StateTrait,
};
use axum::{extract::State, http::StatusCode};
Expand Down Expand Up @@ -275,34 +275,18 @@ pub async fn change<S: StateTrait>(
}

pub async fn get<S: StateTrait>(State(state): State<S>) -> Result<Json<Vec<Uuid>>> {
let mut res = problems_order::Entity::find().all(state.db()).await?;
let res = problems_order::Entity::find().all(state.db()).await?;

debug!("res: {res:?}");

if res.is_empty() {
return Ok(Json(Vec::new()));
}

let Some(last_pos) = res.iter().position(|item| item.next.is_none()) else {
panic!("corrupted problem chain");
};

let length = res.len();
res.swap(last_pos, length - 1);

let mut last_id = res[length - 1].id;

for i in (0..(length - 1)).rev() {
for j in 0..i {
if res[j].next == Some(last_id) {
last_id = res[j].id;
res.swap(i, j);
break;
}
}
}
let sorted = sort_linked(res);
let ids = sorted.into_iter().map(|item| item.id).collect();

Ok(Json(res.into_iter().map(|item| item.id).collect()))
Ok(Json(ids))
}

pub(super) async fn delete_problem<T>(txn: &T, id: Uuid) -> Result<()>
Expand Down
43 changes: 36 additions & 7 deletions src/utils/problems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Problems {
.await
.expect("failed to query the problems");

let problems = Arc::new(RwLock::new(sort_initial_problems(res)));
let problems = Arc::new(RwLock::new(sort_linked(res)));

let mut subscription = nats.subscribe(topics::problems()).await.unwrap();

Expand Down Expand Up @@ -211,31 +211,60 @@ impl Stream for ProblemStream {
}
}

fn sort_initial_problems(problems: Vec<Problem>) -> Vec<Problem> {
let length = problems.len();
pub trait Linked {
fn get_id(&self) -> Uuid;
fn get_next(&self) -> Option<Uuid>;
}

impl Linked for Problem {
#[inline]
fn get_id(&self) -> Uuid {
self.id
}

#[inline]
fn get_next(&self) -> Option<Uuid> {
self.next
}
}

impl Linked for problems_order::Model {
#[inline]
fn get_id(&self) -> Uuid {
self.id
}

#[inline]
fn get_next(&self) -> Option<Uuid> {
self.next
}
}

pub fn sort_linked<T: Linked>(list: Vec<T>) -> Vec<T> {
let length = list.len();

if length == 0 {
return Vec::new();
}

let mut map = HashMap::new();

for problem in problems {
map.insert(problem.next, problem);
for item in list {
map.insert(item.get_next(), item);
}

let mut result = Vec::with_capacity(length);

let mut last_id = {
let last = map.remove(&None).unwrap();
let last_id = last.id;
let last_id = last.get_id();
result.push(last);
last_id
};

while !map.is_empty() {
let item = map.remove(&Some(last_id)).unwrap();
last_id = item.id;
last_id = item.get_id();
result.push(item);
}

Expand Down

0 comments on commit 10bef7d

Please sign in to comment.