Skip to content

Commit

Permalink
use fetch response
Browse files Browse the repository at this point in the history
  • Loading branch information
virratanasangpunth committed Feb 12, 2025
1 parent bf00f96 commit 4a182ff
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 56 deletions.
15 changes: 8 additions & 7 deletions src/leaderboard/leaderboard_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ use crate::leaderboard::messages::data::delete::{DeleteRequest, DeleteResponse};
use crate::leaderboard::messages::data::fetch::FetchResponse;
use crate::leaderboard::messages::data::fetch_by_rank::{FetchByRankRequest, RankRange};
use crate::leaderboard::messages::data::fetch_by_score::{FetchByScoreRequest, ScoreRange};
use crate::leaderboard::messages::data::get_competition_rank::{
GetCompetitionRankRequest, GetCompetitionRankResponse,
};
use crate::leaderboard::messages::data::get_rank::{GetRankRequest, GetRankResponse};
use crate::leaderboard::messages::data::get_competition_rank::GetCompetitionRankRequest;
use crate::leaderboard::messages::data::get_rank::GetRankRequest;
use crate::leaderboard::messages::data::length::{LengthRequest, LengthResponse};
use crate::leaderboard::messages::data::remove_elements::{
RemoveElementsRequest, RemoveElementsResponse,
Expand All @@ -23,6 +21,8 @@ use tonic::transport::Channel;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;

use super::Order;

static NEXT_DATA_CLIENT_INDEX: AtomicUsize = AtomicUsize::new(0);

/// Represents a remote leaderboard resource.
Expand Down Expand Up @@ -83,7 +83,7 @@ impl Leaderboard {
pub async fn get_rank(
&self,
ids: impl IntoIterator<Item = u32>,
) -> MomentoResult<GetRankResponse> {
) -> MomentoResult<FetchResponse> {
let request = GetRankRequest::new(ids);
request.send(self).await
}
Expand All @@ -109,8 +109,9 @@ impl Leaderboard {
pub async fn get_competition_rank(
&self,
ids: impl IntoIterator<Item = u32>,
) -> MomentoResult<GetCompetitionRankResponse> {
let request = GetCompetitionRankRequest::new(ids);
rank: Option<Order>,
) -> MomentoResult<FetchResponse> {
let request = GetCompetitionRankRequest::new(ids, rank);
request.send(self).await
}

Expand Down
34 changes: 8 additions & 26 deletions src/leaderboard/messages/data/get_competition_rank.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{Order, RankedElement};
use super::fetch::FetchResponse;
use super::Order;
use crate::leaderboard::LeaderboardRequest;
use crate::utils::prep_leaderboard_request_with_timeout;
use crate::{Leaderboard, MomentoResult};
Expand All @@ -9,21 +10,16 @@ pub struct GetCompetitionRankRequest {
}

impl GetCompetitionRankRequest {
pub fn new(ids: impl IntoIterator<Item = u32>) -> Self {
pub fn new(ids: impl IntoIterator<Item = u32>, order: Option<Order>) -> Self {
Self {
ids: ids.into_iter().collect(),
order: Order::Descending,
order: order.unwrap_or(Order::Descending),
}
}

pub fn order(mut self, order: Order) -> Self {
self.order = order;
self
}
}

impl LeaderboardRequest for GetCompetitionRankRequest {
type Response = GetCompetitionRankResponse;
type Response = FetchResponse;

async fn send(self, leaderboard: &Leaderboard) -> MomentoResult<Self::Response> {
let cache_name = leaderboard.cache_name();
Expand All @@ -43,22 +39,8 @@ impl LeaderboardRequest for GetCompetitionRankRequest {
.await?
.into_inner();

Ok(Self::Response {
elements: response.elements.iter().map(|v| v.into()).collect(),
})
Ok(FetchResponse::new(
response.elements.iter().map(|v| v.into()).collect(),
))
}
}

pub struct GetCompetitionRankResponse {
elements: Vec<RankedElement>,
}

impl GetCompetitionRankResponse {
pub fn elements(&self) -> &[RankedElement] {
&self.elements
}

pub fn into_elements(self) -> Vec<RankedElement> {
self.elements
}
}
28 changes: 6 additions & 22 deletions src/leaderboard/messages/data/get_rank.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{Order, RankedElement};
use super::fetch::FetchResponse;
use super::Order;
use crate::leaderboard::LeaderboardRequest;
use crate::utils::prep_leaderboard_request_with_timeout;
use crate::{Leaderboard, MomentoResult};
Expand Down Expand Up @@ -31,7 +32,7 @@ impl GetRankRequest {
}

impl LeaderboardRequest for GetRankRequest {
type Response = GetRankResponse;
type Response = FetchResponse;

async fn send(self, leaderboard: &Leaderboard) -> MomentoResult<Self::Response> {
let cache_name = leaderboard.cache_name();
Expand All @@ -51,25 +52,8 @@ impl LeaderboardRequest for GetRankRequest {
.await?
.into_inner();

Ok(Self::Response {
elements: response.elements.iter().map(|v| v.into()).collect(),
})
}
}

/// The response type for a successful `GetRankRequest`
pub struct GetRankResponse {
elements: Vec<RankedElement>,
}

impl GetRankResponse {
/// Returns the ranked elements in the response.
pub fn elements(&self) -> &[RankedElement] {
&self.elements
}

/// Consumes the response and returns the ranked elements.
pub fn into_elements(self) -> Vec<RankedElement> {
self.elements
Ok(FetchResponse::new(
response.elements.iter().map(|v| v.into()).collect(),
))
}
}
2 changes: 1 addition & 1 deletion src/leaderboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use messages::LeaderboardRequest;
pub use messages::data::delete::{DeleteRequest, DeleteResponse};
pub use messages::data::fetch_by_rank::{FetchByRankRequest, RankRange};
pub use messages::data::fetch_by_score::{FetchByScoreRequest, ScoreRange};
pub use messages::data::get_rank::{GetRankRequest, GetRankResponse};
pub use messages::data::get_rank::GetRankRequest;
pub use messages::data::length::{LengthRequest, LengthResponse};
pub use messages::data::remove_elements::{RemoveElementsRequest, RemoveElementsResponse};
pub use messages::data::upsert::{Element, UpsertRequest, UpsertResponse};
Expand Down
47 changes: 47 additions & 0 deletions tests/leaderboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,50 @@ mod fetch_by_score {
Ok(())
}
}

mod get_competition_rank {
use momento::leaderboard::GetRankRequest;

use super::*;

#[tokio::test]
async fn get_competition_rank_of_elements() -> MomentoResult<()> {
let leaderboard = unique_leaderboard();
let test_leaderboard = TestLeaderboard::new();
leaderboard.upsert(test_leaderboard.elements()).await?;

let response = leaderboard.get_rank(test_leaderboard.ids()).await?;

assert_eq!(
test_leaderboard.ranked_elements(),
response.elements(),
"Expected the leaderboard to contain the elements that were upserted"
);
Ok(())
}

#[tokio::test]
async fn get_rank_of_elements_asc() -> MomentoResult<()> {
let leaderboard = unique_leaderboard();
let test_leaderboard = TestLeaderboard::new();
leaderboard.upsert(test_leaderboard.elements()).await?;

let response = leaderboard
.send_request(GetRankRequest::new(test_leaderboard.ids()).order(Order::Ascending))
.await?;

let mut ranked_elements = test_leaderboard.ranked_elements();
let num_elements = ranked_elements.len();
// adjust ranks
for (i, e) in ranked_elements.iter_mut().enumerate() {
e.rank = (num_elements - i - 1) as u32;
}

assert_eq!(
ranked_elements,
response.elements(),
"Expected the leaderboard to contain the elements that were upserted"
);
Ok(())
}
}

0 comments on commit 4a182ff

Please sign in to comment.