Skip to content

Commit

Permalink
Sorting guarantee (#15)
Browse files Browse the repository at this point in the history
* feat(Backend): ✨ provide sorting for list_by*

* update test
  • Loading branch information
Eason0729 authored Jan 6, 2024
1 parent b43fd16 commit 1dc2834
Show file tree
Hide file tree
Showing 18 changed files with 207 additions and 180 deletions.
149 changes: 93 additions & 56 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions backend/entity/src/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct Model {
#[sea_orm(column_type = "Time", on_update = "current_timestamp")]
pub update_at: chrono::NaiveDateTime,
pub match_rule: i32,
pub order: f32,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand Down
3 changes: 2 additions & 1 deletion backend/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ entity-codegen:

release-docker:
just prepare
cp ../Cargo.toml ws-Cargo.toml
cp ../proto/*.proto .
docker build . --build-arg ARCH=$(uname -m) -t mdoj-backend
sudo docker build . --build-arg ARCH=$(uname -m) -t mdoj-backend

run:
cargo run
Expand Down
3 changes: 3 additions & 0 deletions backend/migration/src/m20231207_000001_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ enum Problem {
CreateAt,
UpdateAt,
MatchRule,
Order,
}

#[derive(Iden)]
Expand Down Expand Up @@ -351,6 +352,7 @@ impl MigrationTrait for Migration {
.extra(UPDATE_AT.to_string()),
)
.col(ColumnDef::new(Problem::MatchRule).integer().not_null())
.col(ColumnDef::new(Problem::Order).float().not_null())
.to_owned(),
)
.await?;
Expand Down Expand Up @@ -609,6 +611,7 @@ impl MigrationTrait for Migration {
index!(manager, Problem, AcRate);
index!(manager, Problem, AcceptCount);
index!(manager, Problem, Difficulty);
index!(manager, Problem, Order);
index!(manager, Submit, Committed);
index!(manager, Submit, Time);
index!(manager, Submit, Memory);
Expand Down
2 changes: 1 addition & 1 deletion backend/src/endpoint/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl ChatSet for Arc<Server> {
let mut pager: Pager<Entity> = match req.request.ok_or(Error::NotInPayload("request"))? {
list_by_request::Request::ParentId(ppk) => {
tracing::debug!(id = ppk);
Pager::parent_search(ppk)
Pager::parent_search(ppk, false)
}
list_by_request::Request::Pager(old) => {
reverse = old.reverse;
Expand Down
2 changes: 1 addition & 1 deletion backend/src/endpoint/education.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl EducationSet for Arc<Server> {
let mut pager: Pager<Entity> = match req.request.ok_or(Error::NotInPayload("request"))? {
list_by_request::Request::ParentId(ppk) => {
tracing::debug!(id = ppk);
Pager::parent_search(ppk)
Pager::parent_search(ppk, false)
}
list_by_request::Request::Pager(old) => {
reverse = old.reverse;
Expand Down
7 changes: 4 additions & 3 deletions backend/src/endpoint/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl ProblemSet for Arc<Server> {
model.user_id = ActiveValue::Set(user_id);

fill_active_model!(
model, req.info, title, difficulty, time, memory, tags, content, match_rule
model, req.info, title, difficulty, time, memory, tags, content, match_rule, order
);

let model = model.save(db).await.map_err(Into::<Error>::into)?;
Expand Down Expand Up @@ -244,7 +244,8 @@ impl ProblemSet for Arc<Server> {
content,
match_rule,
ac_rate,
submit_count
submit_count,
order
);

let model = model.update(db).await.map_err(Into::<Error>::into)?;
Expand Down Expand Up @@ -404,7 +405,7 @@ impl ProblemSet for Arc<Server> {
let mut pager: Pager<Entity> = match req.request.ok_or(Error::NotInPayload("request"))? {
list_by_request::Request::ParentId(ppk) => {
tracing::debug!(id = ppk);
Pager::parent_search(ppk)
Pager::parent_sorted_search(ppk, SortBy::Order, false)
}
list_by_request::Request::Pager(old) => {
reverse = old.reverse;
Expand Down
2 changes: 1 addition & 1 deletion backend/src/endpoint/submit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl SubmitSet for Arc<Server> {
let mut pager: Pager<Entity> = match req.request.ok_or(Error::NotInPayload("request"))? {
list_by_request::Request::ParentId(ppk) => {
tracing::debug!(id = ppk);
Pager::parent_search(ppk)
Pager::parent_search(ppk, false)
}
list_by_request::Request::Pager(old) => {
reverse = old.reverse;
Expand Down
2 changes: 1 addition & 1 deletion backend/src/endpoint/testcase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl TestcaseSet for Arc<Server> {
let mut pager: Pager<Entity> = match req.request.ok_or(Error::NotInPayload("request"))? {
list_by_request::Request::ParentId(ppk) => {
tracing::debug!(id = ppk);
Pager::parent_search(ppk)
Pager::parent_search(ppk, false)
}
list_by_request::Request::Pager(old) => {
reverse = old.reverse;
Expand Down
2 changes: 2 additions & 0 deletions backend/src/endpoint/util/pager/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl PagerTrait for problem::Entity {
SortBy::AcRate => problem::Column::AcRate,
SortBy::SubmitCount => problem::Column::SubmitCount,
SortBy::Difficulty => problem::Column::Difficulty,
SortBy::Order => problem::Column::Order,
_ => problem::Column::Id,
}
}
Expand All @@ -49,6 +50,7 @@ impl PagerTrait for problem::Entity {
SortBy::AcRate => model.ac_rate.to_string(),
SortBy::SubmitCount => model.submit_count.to_string(),
SortBy::Difficulty => model.difficulty.to_string(),
SortBy::Order => model.order.to_string(),
_ => model.id.to_string(),
}
}
Expand Down
12 changes: 6 additions & 6 deletions backend/src/endpoint/util/pager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ pub trait HasParentPager<P, E>
where
E: EntityTrait + PagerTrait<ParentMarker = HasParent<P>>,
{
fn parent_search(ppk: i32) -> Self;
fn parent_sorted_search(ppk: i32, sort: SortBy) -> Self;
fn parent_search(ppk: i32, rev: bool) -> Self;
fn parent_sorted_search(ppk: i32, sort: SortBy, rev: bool) -> Self;
fn from_raw(s: String, server: &Server) -> Result<Pager<E>, Error>;
async fn fetch(
&mut self,
Expand Down Expand Up @@ -131,23 +131,23 @@ where
P: Related<E>,
{
#[instrument]
fn parent_search(ppk: i32) -> Self {
fn parent_search(ppk: i32, rev: bool) -> Self {
Self {
type_number: E::TYPE_NUMBER,
sort: SearchDep::Parent(ppk),
_entity: PhantomData,
last_pk: None,
last_rev: true,
last_rev: rev,
}
}
#[instrument]
fn parent_sorted_search(ppk: i32, sort: SortBy) -> Self {
fn parent_sorted_search(ppk: i32, sort: SortBy, rev: bool) -> Self {
Self {
type_number: E::TYPE_NUMBER,
sort: SearchDep::ParentSort(ppk, sort, LastValue::default()),
_entity: PhantomData,
last_pk: None,
last_rev: false,
last_rev: rev,
}
}
#[instrument(skip_all, name = "pagination_deserialize", level = "trace")]
Expand Down
10 changes: 5 additions & 5 deletions judger/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ release-docker:
cd plugins/rlua-54 && sh ./build.sh
cp ../proto/judger.proto .
cp ../Cargo.toml ws-Cargo.toml
docker build . --build-arg ARCH=$(uname -m) -t mdoj-judger
sudo docker build . --build-arg ARCH=$(uname -m) -t mdoj-judger

build-plugin:
cd plugins && sh build-all.sh
Expand All @@ -27,10 +27,10 @@ feodra-deps:
clean:
sudo rm -rf .temp
cargo clean
docker images rm nsjail-3.1-$(uname -m)-linux-musl
docker images rm protobuf-3.21.1-$(uname -m)-linux-musl
docker images rm libnl-3.2.25-$(uname -m)-linux-musl
docker images rm musl-cross-make-$(uname -m)-linux-musl
sudo docker images rm nsjail-3.1-$(uname -m)-linux-musl
sudo docker images rm protobuf-3.21.1-$(uname -m)-linux-musl
sudo docker images rm libnl-3.2.25-$(uname -m)-linux-musl
sudo docker images rm musl-cross-make-$(uname -m)-linux-musl

test:
sudo rm -rf .temp/*
Expand Down
10 changes: 9 additions & 1 deletion proto/backend.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ enum SortBy {
Committed = 11;
Time = 12;
Memory = 13;
Order = 14;
}

// paginator is used to paginate list
Expand Down Expand Up @@ -171,7 +172,7 @@ service SubmitSet {
rpc Create(CreateSubmitRequest) returns (SubmitId);
rpc Remove(SubmitId) returns (google.protobuf.Empty);

// list submit by problem, sorted by pirmary key(desc)
// list submit by problem, sorted by order(increasing)
rpc ListByProblem(ListByRequest) returns (ListSubmitResponse);

// are not guarantee to yield status
Expand Down Expand Up @@ -241,6 +242,7 @@ service AnnouncementSet {
rpc Publish(AnnouncementId) returns (google.protobuf.Empty);
rpc Unpublish(AnnouncementId) returns (google.protobuf.Empty);

// paginate by id(increasing)
rpc ListByContest(ListByRequest) returns (ListAnnouncementResponse);
rpc FullInfoByContest(AnnouncementLink) returns (AnnouncementFullInfo);
}
Expand Down Expand Up @@ -307,6 +309,7 @@ service EducationSet {
rpc Link(EducationLink) returns (google.protobuf.Empty);
rpc Unlink(EducationLink) returns (google.protobuf.Empty);

// paginate by id(increasing)
rpc ListByProblem(ListByRequest) returns (ListEducationResponse);
rpc FullInfoByProblem(EducationLink) returns (EducationFullInfo);
}
Expand Down Expand Up @@ -350,6 +353,7 @@ message CreateProblemRequest {
required string tags = 6;
required string content = 7;
required MatchRule match_rule = 9;
required float order = 10;
};
required Info info = 1;
required string request_id = 2;
Expand All @@ -366,6 +370,7 @@ message UpdateProblemRequest {
optional MatchRule match_rule = 10;
optional uint32 submit_count = 3;
optional float ac_rate = 8;
optional float order = 11;
};
required Info info = 1;
required ProblemId id = 2;
Expand All @@ -388,6 +393,7 @@ service ProblemSet {
rpc Unpublish(ProblemId) returns (google.protobuf.Empty);

rpc FullInfoByContest(ProblemLink) returns (ProblemFullInfo);
// paginate by order(increasing)
rpc ListByContest(ListByRequest) returns (ListProblemResponse);
}

Expand Down Expand Up @@ -448,6 +454,7 @@ service TestcaseSet {
rpc Unlink(TestcaseLink) returns (google.protobuf.Empty);

rpc FullInfoByProblem(TestcaseLink) returns (TestcaseFullInfo);
// paginate by id(increasing)
rpc ListByProblem(ListByRequest) returns (ListTestcaseResponse);
}

Expand Down Expand Up @@ -692,5 +699,6 @@ service ChatSet {
rpc Create(CreateChatRequest) returns (ChatId);
rpc Remove(ChatId) returns (google.protobuf.Empty);

// paginate by id in increasing order
rpc ListByProblem(ListByRequest) returns (ListChatResponse);
}
2 changes: 2 additions & 0 deletions testsuit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ tokio-rustls = "0.24.1"
rustls-pki-types = "1.0.1"
rustls = "0.21.9"
http-body = "0.4.6"
termion = "3.0.0"
thiserror = "1.0.56"

[dependencies.tokio-stream]
version = "0.1.14"
Expand Down
58 changes: 15 additions & 43 deletions testsuit/src/case.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,22 @@
use tonic::async_trait;

#[async_trait]
pub trait Case<S: Send + Sync> {
const NAME: &'static str;

async fn run(&self, state: &mut S) -> Result<(), String>;
#[derive(Debug,thiserror::Error)]
pub enum Error{
#[error("unreachable")]
Unreachable
}

#[async_trait]
trait Runable<S: Send + Sync> {
async fn run(&self, state: &mut S) -> Result<(), String>;
fn name(&self) -> &'static str;
}
// pub struct StackedTestcase<O> where Self:Sized{
// next:Box<dyn Testcase<O>>
// }

#[async_trait]
impl<S: Send + Sync, Rhs: Case<S> + Send + Sync + 'static> Runable<S> for Rhs {
async fn run(&self, state: &mut S) -> Result<(), String> {
<Self as Case<S>>::run(self, state).await
}
// impl<I, O> Testcase<I> for StackedTestcase<O> {

fn name(&self) -> &'static str {
<Self as Case<S>>::NAME
}
}
// }
// #[async_trait]
// pub trait Testcase<I,O>{
// fn run_inner(&self,state:I)->Result<I,Error>{

#[derive(Default)]
pub struct CaseRunner<S: Send + Sync + Default> {
case: Vec<Box<dyn Runable<S> + Send + Sync + 'static>>,
state: S,
}

impl<S: Send + Sync + Default> CaseRunner<S> {
pub fn add_case<Rhs: Case<S> + Send + Sync + 'static>(&mut self, case: Rhs) {
self.case.push(Box::new(case));
}
pub async fn run(mut self, title: &'static str) -> S {
log::info!("Start testsuit {}", title);
for (i, case) in self.case.into_iter().enumerate() {
log::info!("Running case {} {}", i, case.name());
if let Err(err) = case.run(&mut self.state).await {
log::error!("test fail: {}", err);
break;
}
}
log::info!("End");

self.state
}
}
// }
// fn stack(self,state:I,next:Box<dyn Testcase<O>>)->Result<O,Error>where O:From<I>;
// }
8 changes: 4 additions & 4 deletions testsuit/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ pub mod user;

#[tokio::main(flavor = "current_thread")]
async fn main() {
env_logger::builder()
.filter_module("testsuit", log::LevelFilter::Trace)
.init();
// env_logger::builder()
// .filter_module("testsuit", log::LevelFilter::Trace)
// .init();

user::run().await;
// user::run().await;
}
Loading

0 comments on commit 1dc2834

Please sign in to comment.