@@ -190,7 +190,7 @@ impl Store for AzureCosmosStore {
190
190
}
191
191
192
192
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 ( ) )
194
194
}
195
195
196
196
async fn get_keys ( & self ) -> Result < Vec < String > , Error > {
@@ -424,6 +424,28 @@ impl AzureCosmosStore {
424
424
. map ( |( p, _) | p. clone ( ) ) )
425
425
}
426
426
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
+
427
449
async fn get_keys ( & self ) -> Result < Vec < String > , Error > {
428
450
let query = self
429
451
. client
@@ -446,8 +468,14 @@ impl AzureCosmosStore {
446
468
query
447
469
}
448
470
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
+
449
477
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 ( ) ;
451
479
self . append_store_id ( & mut query, false ) ;
452
480
query
453
481
}
0 commit comments