Skip to content

Commit

Permalink
feat: add mongodb hint to Model::paged to use in stream_connections e…
Browse files Browse the repository at this point in the history
…ndpoint (#177)

…ndpoint
  • Loading branch information
ramiroaisen authored Sep 8, 2023
2 parents 6e5e151 + 582f2fc commit 48475fd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
18 changes: 10 additions & 8 deletions rs/packages/api/src/routes/stream_connections/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,20 @@ pub mod get {
}
};

let filter = doc! { "$and": [ scope_filter, stations_query_filter, show_filter ] };
let filter = doc! { "$and": [ show_filter, scope_filter, stations_query_filter ] };

let sort = match sort {
None | Some(SortQuery::CreationDesc) => {
doc! { StreamConnection::KEY_CREATED_AT: -1 }
}
Some(SortQuery::CreationAsc) => {
doc! { StreamConnection::KEY_CREATED_AT: 1 }
}
None | Some(SortQuery::CreationDesc) => doc! { StreamConnection::KEY_CREATED_AT: -1 },
Some(SortQuery::CreationAsc) => doc! { StreamConnection::KEY_CREATED_AT: 1 },
};

let hint = match show {
None | Some(ShowQuery::All | ShowQuery::Closed) => None,
Some(ShowQuery::Open) => Some(doc! { StreamConnection::KEY_IS_OPEN: 1 }),
};

let page = StreamConnection::paged(filter, sort, skip, limit).await?;
let page =
StreamConnection::paged_with_optional_hint(filter, sort, skip, limit, hint).await?;

Ok(Output(page))
}
Expand Down
33 changes: 31 additions & 2 deletions rs/packages/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use futures_util::{Future, TryStreamExt};
use log::*;
use mongodb::error::Result as MongoResult;
use mongodb::options::{
FindOptions, ReplaceOptions, SelectionCriteria, SessionOptions, TransactionOptions,
CountOptions, FindOptions, Hint, ReplaceOptions, SelectionCriteria, SessionOptions,
TransactionOptions,
};
use mongodb::results::DeleteResult;
use mongodb::{
Expand Down Expand Up @@ -325,15 +326,43 @@ pub trait Model: Sized + Unpin + Send + Sync + Serialize + DeserializeOwned {
sort: impl Into<Option<Document>> + Send,
skip: u64,
limit: i64,
) -> MongoResult<Paged<Self>> {
Self::paged_with_optional_hint(filter, sort, skip, limit, None).await
}

async fn paged_with_hint(
filter: impl Into<Option<Document>> + Send,
sort: impl Into<Option<Document>> + Send,
skip: u64,
limit: i64,
hint: Document,
) -> MongoResult<Paged<Self>> {
Self::paged_with_optional_hint(filter, sort, skip, limit, Some(hint)).await
}

async fn paged_with_optional_hint(
filter: impl Into<Option<Document>> + Send,
sort: impl Into<Option<Document>> + Send,
skip: u64,
limit: i64,
hint: Option<Document>,
) -> MongoResult<Paged<Self>> {
let sort = sort.into().unwrap_or_else(|| doc! { "$natural": 1 });
let filter = filter.into();
let hint = hint.map(Hint::Keys);
let options = FindOptions::builder()
.sort(sort)
.skip(skip)
.limit(limit)
.hint(hint.clone())
.build();
let total = Self::cl().count_documents(filter.clone(), None).await?;

let count_options = CountOptions::builder().hint(hint).build();

let total = Self::cl()
.count_documents(filter.clone(), count_options)
.await?;

let items = Self::cl()
.find(filter, options)
.await?
Expand Down

0 comments on commit 48475fd

Please sign in to comment.