From f4c55836258ff9c20fe4d529881281a245ca1306 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Mon, 8 Jan 2024 14:50:09 +0100 Subject: [PATCH] nostr: use `AllocMap` and `AllocSet` in `Filter` --- crates/nostr-database/src/index.rs | 9 +++--- crates/nostr/src/message/subscription.rs | 35 +++++++++++++----------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/crates/nostr-database/src/index.rs b/crates/nostr-database/src/index.rs index 35debe40d..988de8b16 100644 --- a/crates/nostr-database/src/index.rs +++ b/crates/nostr-database/src/index.rs @@ -116,7 +116,7 @@ struct FilterIndex { kinds: HashSet, since: Option, until: Option, - generic_tags: HashMap>, + generic_tags: HashMap>, } impl FilterIndex { @@ -157,6 +157,7 @@ impl FilterIndex { if self.generic_tags.is_empty() { return true; } + if event.tags.is_empty() { return false; } @@ -188,16 +189,16 @@ impl FilterIndex { impl From for FilterIndex { fn from(value: Filter) -> Self { Self { - ids: value.ids.into_iter().collect(), + ids: value.ids, authors: value .authors .into_iter() .map(PublicKeyPrefix::from) .collect(), - kinds: value.kinds.into_iter().collect(), + kinds: value.kinds, since: value.since, until: value.until, - generic_tags: value.generic_tags.into_iter().collect(), + generic_tags: value.generic_tags, } } } diff --git a/crates/nostr/src/message/subscription.rs b/crates/nostr/src/message/subscription.rs index e9e055566..2518c311f 100644 --- a/crates/nostr/src/message/subscription.rs +++ b/crates/nostr/src/message/subscription.rs @@ -5,10 +5,13 @@ //! Subscription filters -use alloc::collections::{BTreeMap, BTreeSet}; +#[cfg(not(feature = "std"))] +use alloc::collections::{BTreeMap as AllocMap, BTreeSet as AllocSet}; use alloc::string::{String, ToString}; use core::fmt; use core::str::FromStr; +#[cfg(feature = "std")] +use std::collections::{HashMap as AllocMap, HashSet as AllocSet}; use bitcoin::hashes::sha256::Hash as Sha256Hash; use bitcoin::hashes::Hash; @@ -316,17 +319,17 @@ impl IntoGenericTagValue for &str { #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] pub struct Filter { /// List of [`EventId`] - #[serde(skip_serializing_if = "BTreeSet::is_empty")] + #[serde(skip_serializing_if = "AllocSet::is_empty")] #[serde(default)] - pub ids: BTreeSet, + pub ids: AllocSet, /// List of [`XOnlyPublicKey`] - #[serde(skip_serializing_if = "BTreeSet::is_empty")] + #[serde(skip_serializing_if = "AllocSet::is_empty")] #[serde(default)] - pub authors: BTreeSet, + pub authors: AllocSet, /// List of a kind numbers - #[serde(skip_serializing_if = "BTreeSet::is_empty")] + #[serde(skip_serializing_if = "AllocSet::is_empty")] #[serde(default)] - pub kinds: BTreeSet, + pub kinds: AllocSet, /// It's a string describing a query in a human-readable form, i.e. "best nostr apps" /// /// @@ -352,7 +355,7 @@ pub struct Filter { deserialize_with = "deserialize_generic_tags" )] #[serde(default)] - pub generic_tags: BTreeMap>, + pub generic_tags: AllocMap>, } impl Filter { @@ -644,7 +647,7 @@ impl Filter { I: IntoIterator, T: IntoGenericTagValue, { - let values: BTreeSet = values + let values: AllocSet = values .into_iter() .map(|v| v.into_generic_tag_value()) .collect(); @@ -663,7 +666,7 @@ impl Filter { I: IntoIterator, T: IntoGenericTagValue, { - let values: BTreeSet = values + let values: AllocSet = values .into_iter() .map(|v| v.into_generic_tag_value()) .collect(); @@ -684,7 +687,7 @@ impl JsonUtil for Filter { } fn serialize_generic_tags( - generic_tags: &BTreeMap>, + generic_tags: &AllocMap>, serializer: S, ) -> Result where @@ -699,14 +702,14 @@ where fn deserialize_generic_tags<'de, D>( deserializer: D, -) -> Result>, D::Error> +) -> Result>, D::Error> where D: Deserializer<'de>, { struct GenericTagsVisitor; impl<'de> Visitor<'de> for GenericTagsVisitor { - type Value = BTreeMap>; + type Value = AllocMap>; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("map in which the keys are \"#X\" for some character X") @@ -716,13 +719,13 @@ where where M: MapAccess<'de>, { - let mut generic_tags = BTreeMap::new(); + let mut generic_tags = AllocMap::new(); while let Some(key) = map.next_key::()? { let mut chars = key.chars(); if let (Some('#'), Some(ch), None) = (chars.next(), chars.next(), chars.next()) { let tag: Alphabet = Alphabet::from_str(ch.to_string().as_str()) .map_err(serde::de::Error::custom)?; - let mut values: BTreeSet = map.next_value()?; + let mut values: AllocSet = map.next_value()?; match tag { Alphabet::P => values.retain(|v| matches!(v, GenericTagValue::Pubkey(_))), @@ -800,7 +803,7 @@ mod test { } #[test] - #[cfg(feature = "std")] + #[cfg(not(feature = "std"))] fn test_filter_serialization() { let filter = Filter::new() .identifier("identifier")