Skip to content

Commit 40c4390

Browse files
committed
selects only id when checking existence of data
Signed-off-by: Aminu Oluwaseun Joshua <[email protected]>
1 parent 9dbfaec commit 40c4390

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

crates/key-value-azure/src/store.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl Store for AzureCosmosStore {
190190
}
191191

192192
async fn exists(&self, key: &str) -> Result<bool, Error> {
193-
Ok(self.get_entity::<Key>(key).await?.is_some())
193+
Ok(self.get_entity_by_id::<Key>(key).await?.is_some())
194194
}
195195

196196
async fn get_keys(&self) -> Result<Vec<String>, Error> {
@@ -424,6 +424,28 @@ impl AzureCosmosStore {
424424
.map(|(p, _)| p.clone()))
425425
}
426426

427+
async fn get_entity_by_id<F>(&self, key: &str) -> Result<Option<F>, Error>
428+
where
429+
F: CosmosEntity + Send + Sync + serde::de::DeserializeOwned + Clone,
430+
{
431+
let query = self
432+
.client
433+
.query_documents(Query::new(self.get_id_query(key)))
434+
.query_cross_partition(true)
435+
.max_item_count(1);
436+
437+
// There can be no duplicated keys, so we create the stream and only take the first result.
438+
let mut stream = query.into_stream::<F>();
439+
let Some(res) = stream.next().await else {
440+
return Ok(None);
441+
};
442+
Ok(res
443+
.map_err(log_error)?
444+
.results
445+
.first()
446+
.map(|(p, _)| p.clone()))
447+
}
448+
427449
async fn get_keys(&self) -> Result<Vec<String>, Error> {
428450
let query = self
429451
.client
@@ -446,8 +468,14 @@ impl AzureCosmosStore {
446468
query
447469
}
448470

471+
fn get_id_query(&self, key: &str) -> String {
472+
let mut query = format!("SELECT c.id FROM c WHERE c.id='{key}'");
473+
self.append_store_id(&mut query, true);
474+
query
475+
}
476+
449477
fn get_keys_query(&self) -> String {
450-
let mut query = "SELECT * FROM c".to_owned();
478+
let mut query = "SELECT c.id, c.store_id FROM c".to_owned();
451479
self.append_store_id(&mut query, false);
452480
query
453481
}

0 commit comments

Comments
 (0)