Skip to content

Commit c8f88c9

Browse files
authored
final review tidy up
1 parent 3e12377 commit c8f88c9

File tree

9 files changed

+31
-37
lines changed

9 files changed

+31
-37
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/cosmos/azure_data_cosmos/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ categories = ["api-bindings"]
1717
async-trait.workspace = true
1818
azure_core.workspace = true
1919
futures.workspace = true
20-
pin-project.workspace = true
2120
serde_json.workspace = true
2221
serde.workspace = true
2322
tracing.workspace = true

sdk/cosmos/azure_data_cosmos/src/cache.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl ContainerMetadata {
9595
/// The cache can be cloned cheaply, and all clones share the same underlying cache data.
9696
#[derive(Clone)]
9797
pub struct ContainerMetadataCache {
98-
/// Caches stable container metadata, mapping from container link and RID to metadata.
98+
/// Caches stable container metadata, mapping from container link to metadata.
9999
container_properties_cache: Cache<ResourceLink, Arc<ContainerMetadata>>,
100100
}
101101

@@ -133,15 +133,11 @@ impl ContainerMetadataCache {
133133
) -> Result<Arc<ContainerMetadata>, CacheError> {
134134
// TODO: Background refresh. We can do background refresh by storing an expiry time in the cache entry.
135135
// Then, if the entry is stale, we can return the stale entry and spawn a background task to refresh it.
136-
// There's a little trickiness here in that
136+
// There's a little trickiness here in that we can't directly spawn a task because that depends on a specific Async Runtime (tokio, smol, etc).
137+
// The core SDK has an AsyncRuntime abstraction that we can use to spawn the task.
137138
Ok(self
138139
.container_properties_cache
139140
.try_get_with_by_ref(key, async { init.await.map(Arc::new) })
140141
.await?)
141142
}
142-
143-
/// Clears the cached container metadata for the specified key, so that the next request will fetch fresh data.
144-
pub async fn clear_container_metadata(&self, key: &ResourceLink) {
145-
self.container_properties_cache.invalidate(key).await;
146-
}
147143
}

sdk/cosmos/azure_data_cosmos/src/clients/container_client.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use serde::{de::DeserializeOwned, Serialize};
2626
/// You can get a `Container` by calling [`DatabaseClient::container_client()`](crate::clients::DatabaseClient::container_client()).
2727
#[derive(Clone)]
2828
pub struct ContainerClient {
29-
id: String,
3029
link: ResourceLink,
3130
items_link: ResourceLink,
3231
connection: CosmosConnection,
@@ -44,7 +43,6 @@ impl ContainerClient {
4443
let items_link = link.feed(ResourceType::Items);
4544

4645
Self {
47-
id: container_id.to_string(),
4846
link,
4947
items_link,
5048
connection,
@@ -77,7 +75,10 @@ impl ContainerClient {
7775
// TODO: Replace with `response.body().json()` when that becomes borrowing.
7876
let properties = serde_json::from_slice::<ContainerProperties>(response.body())?;
7977
let metadata = ContainerMetadata::from_properties(&properties, self.link.clone())?;
80-
self.connection.cache.set_container_metadata(metadata).await;
78+
self.connection
79+
.cache()
80+
.set_container_metadata(metadata)
81+
.await;
8182

8283
Ok(response.into())
8384
}
@@ -705,7 +706,7 @@ impl ContainerClient {
705706
async fn metadata(&self) -> azure_core::Result<Arc<ContainerMetadata>> {
706707
Ok(self
707708
.connection
708-
.cache
709+
.cache()
709710
.get_container_metadata(&self.link, async {
710711
let properties = self.read_properties(None).await?.into_body()?;
711712
ContainerMetadata::from_properties(&properties, self.link.clone())

sdk/cosmos/azure_data_cosmos/src/clients/cosmos_client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl CosmosClient {
139139

140140
/// Gets the endpoint of the database account this client is connected to.
141141
pub fn endpoint(&self) -> &Url {
142-
&self.connection.endpoint
142+
self.connection.endpoint()
143143
}
144144

145145
/// Executes a query against databases in the account.

sdk/cosmos/azure_data_cosmos/src/connection/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ use crate::{
3131
/// A connection is cheap to clone, and all clones share the same underlying HTTP pipeline and metadata cache.
3232
#[derive(Clone)]
3333
pub struct CosmosConnection {
34-
pub endpoint: Url,
35-
pub cache: ContainerMetadataCache,
34+
endpoint: Url,
35+
cache: ContainerMetadataCache,
3636
pipeline: azure_core::http::Pipeline,
3737
}
3838

@@ -56,6 +56,14 @@ impl CosmosConnection {
5656
}
5757
}
5858

59+
pub fn endpoint(&self) -> &Url {
60+
&self.endpoint
61+
}
62+
63+
pub fn cache(&self) -> &ContainerMetadataCache {
64+
&self.cache
65+
}
66+
5967
/// Creates a [`Url`] out of the provided [`ResourceLink`]
6068
///
6169
/// This is a little backwards, ideally we'd accept [`ResourceLink`] in the [`CosmosPipeline::send`] method,

sdk/cosmos/azure_data_cosmos/src/types.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
//! Internal module to define several newtypes used in the SDK.
22
33
macro_rules! string_newtype {
4-
($name:ident) => {
4+
($(#[$attr:meta])* $name:ident) => {
5+
$(#[$attr])*
56
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
67
#[serde(transparent)]
78
pub struct $name(String);
89

910
impl $name {
11+
#[doc = concat!("Creates a new `", stringify!($name), "` from a `String`.")]
1012
pub fn new(value: String) -> Self {
1113
Self(value)
1214
}
1315

16+
#[doc = concat!("Returns a reference to the inner `str` of the `", stringify!($name), "`.")]
1417
pub fn value(&self) -> &str {
1518
&self.0
1619
}
@@ -36,4 +39,9 @@ macro_rules! string_newtype {
3639
};
3740
}
3841

39-
string_newtype!(ResourceId);
42+
string_newtype!(
43+
/// Represents a Resource ID, which is a unique identifier for a resource within a Cosmos DB account.
44+
///
45+
/// In most cases, you don't need to use this type directly, as the SDK will handle resource IDs for you.
46+
ResourceId
47+
);

sdk/cosmos/azure_data_cosmos/tests/cosmos_containers.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,6 @@ pub async fn container_crud_hierarchical_pk(context: TestContext) -> Result<(),
241241

242242
#[recorded::test]
243243
pub async fn container_read_throughput_twice(context: TestContext) -> Result<(), Box<dyn Error>> {
244-
use azure_core::http::StatusCode;
245-
246244
let recorder = Arc::new(LocalRecorder::new());
247245
let account = TestAccount::from_env(
248246
context,
@@ -256,7 +254,6 @@ pub async fn container_read_throughput_twice(context: TestContext) -> Result<(),
256254
let cosmos_client = account.connect_with_key(None)?;
257255
let db_client = test_data::create_database(&account, &cosmos_client).await?;
258256

259-
// Create the container with manual throughput
260257
let properties = ContainerProperties {
261258
id: "ThroughputTestContainer".into(),
262259
partition_key: "/id".into(),

sdk/cosmos/azure_data_cosmos/tests/framework/local_recorder.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::sync::Arc;
22

33
use azure_core::http::{
44
policies::{Policy, PolicyResult},
5-
BufResponse, Context, Method, RawResponse, Request, StatusCode,
5+
BufResponse, Context, RawResponse, Request,
66
};
77

88
#[derive(Debug, Clone)]
@@ -23,21 +23,7 @@ impl LocalRecorder {
2323
}
2424
}
2525

26-
pub async fn to_transaction_summary(&self) -> Vec<(Method, String, Option<StatusCode>)> {
27-
self.transactions
28-
.read()
29-
.await
30-
.iter()
31-
.map(|t| {
32-
(
33-
t.request.method(),
34-
t.request.url().to_string(),
35-
t.response.as_ref().map(|r| r.status()),
36-
)
37-
})
38-
.collect()
39-
}
40-
26+
/// Returns a copy of all recorded transactions
4127
pub async fn to_transactions(&self) -> Vec<Transaction> {
4228
self.transactions.write().await.clone()
4329
}

0 commit comments

Comments
 (0)