feat(redis): search hash fields and values
This commit is contained in:
1 parent
9cc7244dab
commit
069dcc479d
11 files changed
+77
-31
No files matched your search
@@ -2205,7 +2205,7 @@ pub async fn load_more_collection<C>(
|
||||
key_type: &str,
|
||||
cursor: u64,
|
||||
count: usize,
|
||||
match_pattern: Option<&str>,
|
||||
filter_query: Option<&str>,
|
||||
) -> Result<RedisValue, String>
|
||||
where
|
||||
C: ConnectionLike + Send + Sync + Unpin,
|
||||
@@ -2232,8 +2232,8 @@ where
|
||||
(serde_json::Value::Array(items), cursor)
|
||||
}
|
||||
"hash" => {
|
||||
let (next, items) = if let Some(pattern) = match_pattern {
|
||||
hscan_matching_page_raw(con, key, cursor, count, pattern).await?
|
||||
let (next, items) = if let Some(query) = filter_query.filter(|query| !query.is_empty()) {
|
||||
hscan_filtered_page_raw(con, key, cursor, count, query).await?
|
||||
} else {
|
||||
hscan_page_raw(con, key, cursor, count, None).await?
|
||||
};
|
||||
@@ -2274,12 +2274,12 @@ where
|
||||
parse_scan_pairs(raw, "hash")
|
||||
}
|
||||
|
||||
async fn hscan_matching_page_raw<C>(
|
||||
async fn hscan_filtered_page_raw<C>(
|
||||
con: &mut C,
|
||||
key: &[u8],
|
||||
cursor: u64,
|
||||
count: usize,
|
||||
pattern: &str,
|
||||
query: &str,
|
||||
) -> Result<(u64, Vec<serde_json::Value>), String>
|
||||
where
|
||||
C: ConnectionLike + Send + Sync + Unpin,
|
||||
@@ -2289,11 +2289,11 @@ where
|
||||
let target = count.max(1);
|
||||
|
||||
for _ in 0..HASH_FILTER_SCAN_MAX_ITERATIONS {
|
||||
let (next, page) = hscan_page_raw(con, key, cur, target, Some(pattern)).await?;
|
||||
items.extend(page);
|
||||
let (next, page) = hscan_page_raw(con, key, cur, target, None).await?;
|
||||
items.extend(page.into_iter().filter(|item| hash_entry_matches_query(item, query)));
|
||||
cur = next;
|
||||
// Redis applies MATCH during incremental scans and may return empty pages.
|
||||
// Stop only after a bounded number of chunks so sparse matches progress without an unbounded full-hash scan.
|
||||
// HSCAN MATCH only checks field names, so value search has to filter returned pairs client-side.
|
||||
// Keep a hard scan bound so sparse value matches cannot turn one UI search into a full hash walk.
|
||||
if cur == 0 || items.len() >= target {
|
||||
break;
|
||||
}
|
||||
@@ -2302,6 +2302,16 @@ where
|
||||
Ok((cur, items))
|
||||
}
|
||||
|
||||
fn hash_entry_matches_query(item: &serde_json::Value, query: &str) -> bool {
|
||||
let query = query.to_lowercase();
|
||||
if query.is_empty() {
|
||||
return true;
|
||||
}
|
||||
let field = item.get("field").and_then(serde_json::Value::as_str).unwrap_or_default();
|
||||
let value = item.get("value").and_then(serde_json::Value::as_str).unwrap_or_default();
|
||||
field.to_lowercase().contains(&query) || value.to_lowercase().contains(&query)
|
||||
}
|
||||
|
||||
async fn sscan_page_raw<C>(
|
||||
con: &mut C,
|
||||
key: &[u8],
|
||||
@@ -2599,18 +2609,31 @@ mod tests {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn filtered_hash_load_more_keeps_scan_cursor_instead_of_full_iteration() {
|
||||
async fn filtered_hash_load_more_matches_fields_and_keeps_scan_cursor() {
|
||||
let mut con = FakeRedisConnection::new(vec![
|
||||
hscan_response("512", vec![("user:1", "Ada")]),
|
||||
hscan_response("0", vec![("user:2", "Bob")]),
|
||||
]);
|
||||
|
||||
let result = super::load_more_collection(&mut con, b"hash-key", "hash", 0, 1, Some("*user*")).await.unwrap();
|
||||
let result = super::load_more_collection(&mut con, b"hash-key", "hash", 0, 1, Some("user")).await.unwrap();
|
||||
|
||||
assert_eq!(result.scan_cursor, Some(512));
|
||||
assert_eq!(result.value, serde_json::json!([{ "field": "user:1", "value": "Ada" }]));
|
||||
assert_eq!(con.command_count("HSCAN"), 1);
|
||||
assert!(con.commands[0].contains("\r\nMATCH\r\n"));
|
||||
assert!(!con.commands[0].contains("\r\nMATCH\r\n"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn filtered_hash_load_more_matches_values() {
|
||||
let mut con =
|
||||
FakeRedisConnection::new(vec![hscan_response("0", vec![("status", "Ada Lovelace"), ("name", "Bob")])]);
|
||||
|
||||
let result = super::load_more_collection(&mut con, b"hash-key", "hash", 0, 20, Some("lovelace")).await.unwrap();
|
||||
|
||||
assert_eq!(result.scan_cursor, None);
|
||||
assert_eq!(result.value, serde_json::json!([{ "field": "status", "value": "Ada Lovelace" }]));
|
||||
assert_eq!(con.command_count("HSCAN"), 1);
|
||||
assert!(!con.commands[0].contains("\r\nMATCH\r\n"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -2620,8 +2643,7 @@ mod tests {
|
||||
.collect();
|
||||
let mut con = FakeRedisConnection::new(responses);
|
||||
|
||||
let result =
|
||||
super::load_more_collection(&mut con, b"hash-key", "hash", 0, 20, Some("*missing*")).await.unwrap();
|
||||
let result = super::load_more_collection(&mut con, b"hash-key", "hash", 0, 20, Some("missing")).await.unwrap();
|
||||
|
||||
assert_eq!(result.scan_cursor, Some(super::HASH_FILTER_SCAN_MAX_ITERATIONS as u64));
|
||||
assert_eq!(result.value, serde_json::json!([]));
|
||||
|
||||
Reference in new issue
Block a user