Skip to content

Commit

Permalink
ReadCachedOnly bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
abdolence committed Oct 7, 2023
1 parent 18aaa08 commit cc76917
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
45 changes: 39 additions & 6 deletions src/db/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}
26 changes: 22 additions & 4 deletions src/db/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit cc76917

Please sign in to comment.