From 1897a68b4941002e73ec494f1f1c3b8c64d73062 Mon Sep 17 00:00:00 2001 From: katelyn martin Date: Thu, 9 May 2024 12:18:02 -0400 Subject: [PATCH] =?UTF-8?q?cnidarium:=20=F0=9F=AA=BC=20`watch=5Fchanges`?= =?UTF-8?q?=20regex=20parameters=20aren't=20options?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit see discussion from #4361, here: https://github.com/penumbra-zone/penumbra/pull/4361#discussion_r1595614864 --- crates/cnidarium/src/rpc.rs | 90 ++++++++++++++----------------------- 1 file changed, 33 insertions(+), 57 deletions(-) diff --git a/crates/cnidarium/src/rpc.rs b/crates/cnidarium/src/rpc.rs index 992fb35a38..a8accb144f 100644 --- a/crates/cnidarium/src/rpc.rs +++ b/crates/cnidarium/src/rpc.rs @@ -122,29 +122,17 @@ impl QueryService for Server { const MAX_REGEX_LEN: usize = 1024; - let key_regex = match request.key_regex.as_str() { - "" => None, - _ => Some( - regex::RegexBuilder::new(&request.key_regex) - .size_limit(MAX_REGEX_LEN) - .build() - .map_err(|e| Status::invalid_argument(format!("invalid key_regex: {}", e)))?, - ), - }; + let key_regex = regex::RegexBuilder::new(&request.key_regex) + .size_limit(MAX_REGEX_LEN) + .build() + .map_err(|e| Status::invalid_argument(format!("invalid key_regex: {}", e)))?; // Use the `bytes` regex to allow matching byte strings. - let nv_key_regex = match request.nv_key_regex.as_str() { - "" => None, - _ => Some( - regex::bytes::RegexBuilder::new(&request.nv_key_regex) - .size_limit(MAX_REGEX_LEN) - .unicode(false) - .build() - .map_err(|e| { - Status::invalid_argument(format!("invalid nv_key_regex: {}", e)) - })?, - ), - }; + let nv_key_regex = regex::bytes::RegexBuilder::new(&request.nv_key_regex) + .size_limit(MAX_REGEX_LEN) + .unicode(false) + .build() + .map_err(|e| Status::invalid_argument(format!("invalid nv_key_regex: {}", e)))?; let (tx, rx) = tokio::sync::mpsc::channel::>(10); @@ -161,8 +149,8 @@ impl QueryService for Server { async fn watch_changes( storage: Storage, - key_regex: Option, - nv_key_regex: Option, + key_regex: regex::Regex, + nv_key_regex: regex::bytes::Regex, tx: tokio::sync::mpsc::Sender>, ) -> anyhow::Result<()> { let mut changes_rx = storage.subscribe_changes(); @@ -173,43 +161,31 @@ async fn watch_changes( } let (version, changes) = changes_rx.borrow_and_update().clone(); - if key_regex.is_some() || nv_key_regex.is_none() { - for (key, value) in changes.unwritten_changes().iter() { - if key_regex - .as_ref() - .unwrap_or(&Regex::new(r"").expect("empty regex ok")) - .is_match(key) - { - tx.send(Ok(WatchResponse { - version, - entry: Some(wr::Entry::Kv(wr::KeyValue { - key: key.clone(), - value: value.as_ref().cloned().unwrap_or_default(), - deleted: value.is_none(), - })), - })) - .await?; - } + for (key, value) in changes.unwritten_changes().iter() { + if key_regex.is_match(key) { + tx.send(Ok(WatchResponse { + version, + entry: Some(wr::Entry::Kv(wr::KeyValue { + key: key.clone(), + value: value.as_ref().cloned().unwrap_or_default(), + deleted: value.is_none(), + })), + })) + .await?; } } - if nv_key_regex.is_some() || key_regex.is_none() { - for (key, value) in changes.nonverifiable_changes().iter() { - if nv_key_regex - .as_ref() - .unwrap_or(®ex::bytes::Regex::new(r"").expect("empty regex ok")) - .is_match(key) - { - tx.send(Ok(WatchResponse { - version, - entry: Some(wr::Entry::NvKv(wr::NvKeyValue { - key: key.clone(), - value: value.as_ref().cloned().unwrap_or_default(), - deleted: value.is_none(), - })), - })) - .await?; - } + for (key, value) in changes.nonverifiable_changes().iter() { + if nv_key_regex.is_match(key) { + tx.send(Ok(WatchResponse { + version, + entry: Some(wr::Entry::NvKv(wr::NvKeyValue { + key: key.clone(), + value: value.as_ref().cloned().unwrap_or_default(), + deleted: value.is_none(), + })), + })) + .await?; } } }