Skip to content

Commit 989f160

Browse files
committed
Migrating get to read_item; set partition key for exists; cleanup
Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>
1 parent 698eaf5 commit 989f160

1 file changed

Lines changed: 18 additions & 32 deletions

File tree

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

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,18 @@ struct AzureCosmosStore {
132132
#[async_trait]
133133
impl Store for AzureCosmosStore {
134134
async fn get(&self, key: &str, max_result_bytes: usize) -> Result<Option<Vec<u8>>, Error> {
135-
let value = self
136-
.query_one::<Pair>(self.get_query(key))
137-
.await?
138-
.map(|p| p.value);
135+
let partition_key = partition_key(self.store_id.as_deref(), key);
136+
let value = match self.client.read_item(partition_key, key, None).await {
137+
Ok(response) => Some(response.into_model::<Pair>().map_err(log_error)?.value),
138+
Err(e) if e.status().is_not_found() => None,
139+
Err(e) => return Err(log_error(e)),
140+
};
139141

140-
// Currently there's no way to stream a single query result using the
141-
// `azure_data_cosmos` crate without buffering, so the damage (in terms
142-
// of host memory usage) is already done, but we can still enforce the
143-
// limit:
144-
if std::mem::size_of::<Option<Vec<u8>>>() + value.as_ref().map(|v| v.len()).unwrap_or(0)
142+
if std::mem::size_of::<Option<Vec<u8>>>() + value.as_ref().map(Vec::len).unwrap_or(0)
145143
> max_result_bytes
146144
{
147145
Err(Error::Other(format!(
148-
"query result exceeds limit of {max_result_bytes} bytes"
146+
"read result exceeds limit of {max_result_bytes} bytes"
149147
)))
150148
} else {
151149
Ok(value)
@@ -187,10 +185,16 @@ impl Store for AzureCosmosStore {
187185
}
188186

189187
async fn exists(&self, key: &str) -> Result<bool, Error> {
190-
Ok(self
191-
.query_one::<Key>(self.get_id_query(key))
192-
.await?
193-
.is_some())
188+
let mut stream = self
189+
.client
190+
.query_items::<Key>(
191+
Query::from(self.get_id_query(key)),
192+
FeedScope::partition(partition_key(self.store_id.as_deref(), key)),
193+
None,
194+
)
195+
.await
196+
.map_err(log_error)?;
197+
Ok(stream.try_next().await.map_err(log_error)?.is_some())
194198
}
195199

196200
async fn get_keys(&self, max_result_bytes: usize) -> Result<Vec<String>, Error> {
@@ -437,24 +441,6 @@ impl Cas for CompareAndSwap {
437441
}
438442

439443
impl AzureCosmosStore {
440-
async fn query_one<T>(&self, query: String) -> Result<Option<T>, Error>
441-
where
442-
T: serde::de::DeserializeOwned + Send + 'static,
443-
{
444-
let mut stream = self
445-
.client
446-
.query_items(Query::from(query), FeedScope::full_container(), None)
447-
.await
448-
.map_err(log_error)?;
449-
stream.try_next().await.map_err(log_error)
450-
}
451-
452-
fn get_query(&self, key: &str) -> String {
453-
let mut query = format!("SELECT * FROM c WHERE c.id='{key}'");
454-
append_store_id_condition(&mut query, self.store_id.as_deref(), true);
455-
query
456-
}
457-
458444
fn get_id_query(&self, key: &str) -> String {
459445
let mut query = format!("SELECT c.id, c.store_id FROM c WHERE c.id='{key}'");
460446
append_store_id_condition(&mut query, self.store_id.as_deref(), true);

0 commit comments

Comments
 (0)