Skip to content

Commit c9c16ea

Browse files
committed
cleaner code
Signed-off-by: Aminu Oluwaseun Joshua <[email protected]>
1 parent 40c4390 commit c9c16ea

File tree

1 file changed

+12
-23
lines changed

1 file changed

+12
-23
lines changed

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

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

192192
async fn exists(&self, key: &str) -> Result<bool, Error> {
193-
Ok(self.get_entity_by_id::<Key>(key).await?.is_some())
193+
let mut stream = self
194+
.client
195+
.query_documents(Query::new(self.get_id_query(key)))
196+
.query_cross_partition(true)
197+
.max_item_count(1)
198+
.into_stream::<Key>();
199+
200+
match stream.next().await {
201+
Some(Ok(res)) => Ok(!res.results.is_empty()),
202+
Some(Err(e)) => Err(log_error(e)),
203+
None => Ok(false),
204+
}
194205
}
195206

196207
async fn get_keys(&self) -> Result<Vec<String>, Error> {
@@ -424,28 +435,6 @@ impl AzureCosmosStore {
424435
.map(|(p, _)| p.clone()))
425436
}
426437

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-
449438
async fn get_keys(&self) -> Result<Vec<String>, Error> {
450439
let query = self
451440
.client

0 commit comments

Comments
 (0)