From 583ca3f168b4a2e2ab9381281e781e6322043be6 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Thu, 28 Nov 2024 22:57:02 +0800 Subject: [PATCH] filter_map as set --- lib/ain-ocean/src/indexer/loan_token.rs | 26 +++++++++---------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/lib/ain-ocean/src/indexer/loan_token.rs b/lib/ain-ocean/src/indexer/loan_token.rs index d05d2ecd9a..1182d4c36c 100644 --- a/lib/ain-ocean/src/indexer/loan_token.rs +++ b/lib/ain-ocean/src/indexer/loan_token.rs @@ -87,18 +87,14 @@ pub fn index_active_price(services: &Arc, block: &BlockContext) -> Res _ => 120, }; if block.height % block_interval == 0 { - let mut set: HashSet<(Token, Currency)> = HashSet::new(); let pairs = services .price_ticker .by_id .list(None, SortOrder::Descending)? - .flat_map(|item| { - let ((_, _, token, currency), _) = item?; - set.insert((token, currency)); - Ok::, Error>(set.clone()) + .filter_map(|item| { + item.ok().map(|((_, _, token, currency), _)| (token, currency)) }) - .next() - .unwrap_or(set); + .collect::>(); for (token, currency) in pairs { perform_active_price_tick(services, (token, currency), block)?; @@ -143,26 +139,22 @@ pub fn invalidate_active_price(services: &Arc, block: &BlockContext) - _ => 120, }; if block.height % block_interval == 0 { - let mut set: HashSet<(Token, Currency)> = HashSet::new(); let pairs = services .price_ticker .by_id .list(None, SortOrder::Descending)? - .flat_map(|item| { - let ((_, _, token, currency), _) = item?; - set.insert((token, currency)); - Ok::, Error>(set.clone()) + .filter_map(|item| { + item.ok().map(|((_, _, token, currency), _)| (token, currency)) }) - .next() - .unwrap_or(set); + .collect::>(); // convert to vector to reverse the hashset is required - let mut vec = Vec::new(); + let mut rev_pairs = Vec::new(); for pair in pairs { - vec.insert(0, pair); + rev_pairs.insert(0, pair); } - for (token, currency) in vec { + for (token, currency) in rev_pairs { services.oracle_price_active.by_id.delete(&( token, currency,