Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support query by id ascending to support synchronization of pending jobs #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 65 additions & 7 deletions contracts/warp-controller/src/query/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,25 @@ pub fn query_jobs(deps: Deps, env: Env, data: QueryJobsMsg) -> StdResult<JobsRes
ids: Some(ids),
job_status,
..
} => query_jobs_by_ids(deps, env, ids, job_status),
} => query_jobs_filter_by_ids(deps, env, ids, job_status),
QueryJobsMsg {
active: _,
name,
owner,
job_status,
start_after,
limit: _,
use_id_order: Some(true),
..
} => query_jobs_by_id(
deps,
env,
name,
owner,
job_status,
start_after.map(|i| (i._0.u128(), i._1.u64())),
page_size as usize,
),
QueryJobsMsg {
active: _,
name,
Expand All @@ -57,7 +75,7 @@ pub fn query_jobs(deps: Deps, env: Env, data: QueryJobsMsg) -> StdResult<JobsRes
}
}

pub fn query_jobs_by_ids(
pub fn query_jobs_filter_by_ids(
deps: Deps,
env: Env,
ids: Vec<Uint64>,
Expand Down Expand Up @@ -120,15 +138,55 @@ pub fn query_jobs_by_reward(
job_status.clone(),
)
})
.map(|item| {
let (_, job) = item?;
Ok(job)
})
.take(limit)
.collect::<StdResult<Vec<_>>>()?;

Ok(JobsResponse {
total_count: infos.len(),
jobs: infos,
})
}

pub fn query_jobs_by_id(
deps: Deps,
env: Env,
name: Option<String>,
owner: Option<Addr>,
job_status: Option<JobStatus>,
start_after: Option<(u128, u64)>,
limit: usize,
) -> StdResult<JobsResponse> {
let start = start_after.map(|(_reward, id)| Bound::exclusive(id));
let map = if job_status.is_some() && job_status.clone().unwrap() != JobStatus::Pending {
FINISHED_JOBS()
} else {
PENDING_JOBS()
};
let infos = map
.range(deps.storage, start, None, Order::Ascending)
.filter(|h| {
resolve_filters(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of cloning everything, it could be better to just borrow things. For small structs cloning is better, but not sure about strings etc.

deps,
env.clone(),
h.as_ref().unwrap().clone().1,
name.clone(),
owner.clone(),
job_status.clone(),
)
})
.map(|item| {
let (_, job) = item?;
Ok(job)
})
.take(limit)
.collect::<StdResult<Vec<_>>>()?;

let mut jobs = vec![];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also optimized this to do it directly in the map to not copy the whole list again and reiterate it.

for info in infos.clone() {
jobs.push(info.1);
}
Ok(JobsResponse {
jobs,
total_count: infos.len(),
jobs: infos,
})
}
3 changes: 3 additions & 0 deletions packages/warp-protocol/src/controller/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub struct QueryJobMsg {

#[cw_serde]
pub struct QueryJobsMsg {
// if set, will only return the specified ids.
pub ids: Option<Vec<Uint64>>,
pub active: Option<bool>,
pub owner: Option<Addr>,
Expand All @@ -92,6 +93,8 @@ pub struct QueryJobsMsg {
pub condition_status: Option<bool>,
pub start_after: Option<JobIndex>,
pub limit: Option<u32>,
// if set to true, it will order ascending based on the id. start_after will only use the second part of JobIndex (_1)
pub use_id_order: Option<bool>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is set to true and no ids provided, the order will be by id ascending

}

#[cw_serde]
Expand Down