From cc769179dd6c6f97e390f708232e46e142ab0bbb Mon Sep 17 00:00:00 2001 From: Abdulla Abdurakhmanov Date: Sat, 7 Oct 2023 14:44:53 +0200 Subject: [PATCH] ReadCachedOnly bugfixes --- src/db/list.rs | 45 +++++++++++++++++++++++++++++++++++++++------ src/db/query.rs | 26 ++++++++++++++++++++++---- 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/src/db/list.rs b/src/db/list.rs index 57d1847..0348c19 100644 --- a/src/db/list.rs +++ b/src/db/list.rs @@ -494,12 +494,9 @@ impl FirestoreDb { Level::DEBUG, "Firestore List Cached", "/firestore/collection_name" = params.collection_id, + "/firestore/cache_result" = field::Empty, ); - span.in_scope(|| { - debug!("Reading all {} documents from cache", params.collection_id); - }); - let collection_path = if let Some(parent) = params.parent.as_ref() { format!("{}/{}", parent, params.collection_id.as_str()) } else { @@ -511,8 +508,44 @@ impl FirestoreDb { }; let cached_result = cache.list_all_docs(&collection_path).await?; - return Ok(cached_result); + + match cached_result { + FirestoreCachedValue::UseCached(stream) => { + span.record("/firestore/cache_result", "hit"); + span.in_scope(|| { + debug!("Reading all {} documents from cache", params.collection_id); + }); + + Ok(FirestoreCachedValue::UseCached(stream)) + } + FirestoreCachedValue::SkipCache => { + span.record("/firestore/cache_result", "miss"); + if matches!( + self.session_params.cache_mode, + FirestoreDbSessionCacheMode::ReadCachedOnly(_) + ) { + span.in_scope(|| { + debug!( + "Cache doesn't have suitable documents for {}, but cache mode is ReadCachedOnly so returning empty stream", + params.collection_id + ); + }); + Ok(FirestoreCachedValue::UseCached(Box::pin( + futures::stream::empty(), + ))) + } else { + span.in_scope(|| { + debug!( + "Cache doesn't have suitable documents for {} skipping cache and reading from Firestore", + params.collection_id + ); + }); + Ok(FirestoreCachedValue::SkipCache) + } + } + } + } else { + Ok(FirestoreCachedValue::SkipCache) } - Ok(FirestoreCachedValue::SkipCache) } } diff --git a/src/db/query.rs b/src/db/query.rs index 1abcd22..1541a66 100644 --- a/src/db/query.rs +++ b/src/db/query.rs @@ -187,10 +187,28 @@ impl FirestoreDb { } FirestoreCachedValue::SkipCache => { span.record("/firestore/cache_result", "miss"); - span.in_scope(|| { - debug!("Querying {} documents from cache skipped", collection_id); - }); - Ok(FirestoreCachedValue::SkipCache) + if matches!( + self.session_params.cache_mode, + FirestoreDbSessionCacheMode::ReadCachedOnly(_) + ) { + span.in_scope(|| { + debug!( + "Cache doesn't have suitable documents for {}, but cache mode is ReadCachedOnly so returning empty stream", + collection_id.as_str() + ); + }); + Ok(FirestoreCachedValue::UseCached(Box::pin( + futures::stream::empty(), + ))) + } else { + span.in_scope(|| { + debug!( + "Querying {} documents from cache skipped", + collection_id + ); + }); + Ok(FirestoreCachedValue::SkipCache) + } } } } else {