azure_data_cosmos 0.37.1

Rust wrappers around Microsoft Azure REST APIs - Azure Cosmos DB
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

//! Preview Distributed Transaction builders.
//!
//! **Preview / work in progress.** These APIs are gated behind the
//! disabled-by-default `preview_dtx` feature, depend on a service-side feature
//! that is not yet generally available, and may change or be removed without
//! notice. They are **not supported for production use**.

use std::{borrow::Cow, sync::Arc};

use azure_core::fmt::SafeDebug;
use azure_core::Bytes;
use azure_data_cosmos_driver::models as driver_models;
use serde::{de::DeserializeOwned, Serialize};

use crate::clients::{ClientContext, ContainerClient};
use crate::diagnostics::DiagnosticsContext;
use crate::models::{PartitionKey, PatchInstructions, ResponseHeaders};
use crate::options::{Precondition, SessionToken};

/// Options for a single operation inside a distributed transaction.
#[derive(Clone, Default)]
#[non_exhaustive]
pub struct DistributedTransactionOperationOptions {
    /// Per-operation session token.
    pub session_token: Option<SessionToken>,
    /// ETag precondition.
    pub precondition: Option<Precondition>,
}

impl DistributedTransactionOperationOptions {
    /// Sets the per-operation session token.
    pub fn with_session_token(mut self, session_token: impl Into<SessionToken>) -> Self {
        self.session_token = Some(session_token.into());
        self
    }

    /// Sets the ETag precondition.
    pub fn with_precondition(mut self, precondition: Precondition) -> Self {
        self.precondition = Some(precondition);
        self
    }
}

/// Options for a patch operation inside a distributed transaction.
#[derive(Clone, Default)]
#[non_exhaustive]
pub struct DistributedTransactionPatchOperationOptions {
    /// Per-operation session token.
    pub session_token: Option<SessionToken>,
    /// ETag precondition.
    pub precondition: Option<Precondition>,
    /// SQL predicate evaluated before applying the patch.
    pub filter_predicate: Option<Cow<'static, str>>,
}

impl DistributedTransactionPatchOperationOptions {
    /// Sets the per-operation session token.
    pub fn with_session_token(mut self, session_token: impl Into<SessionToken>) -> Self {
        self.session_token = Some(session_token.into());
        self
    }

    /// Sets the ETag precondition.
    pub fn with_precondition(mut self, precondition: Precondition) -> Self {
        self.precondition = Some(precondition);
        self
    }

    /// Sets a SQL predicate evaluated before applying the patch.
    pub fn with_filter_predicate(mut self, predicate: impl Into<Cow<'static, str>>) -> Self {
        self.filter_predicate = Some(predicate.into());
        self
    }
}

/// Preview distributed write transaction builder.
#[derive(Clone, SafeDebug)]
#[safe(true)]
pub struct DistributedWriteTransaction {
    operations: Vec<driver_models::DistributedTransactionOperation>,
}

impl DistributedWriteTransaction {
    /// Creates an empty distributed write transaction.
    pub fn new() -> Self {
        Self {
            operations: Vec::new(),
        }
    }

    /// Consumes this transaction and returns its operations.
    pub(crate) fn into_operations(self) -> Vec<driver_models::DistributedTransactionOperation> {
        self.operations
    }

    /// Adds a create item operation.
    pub fn create_item<T: Serialize>(
        mut self,
        container: &ContainerClient,
        partition_key: impl Into<PartitionKey>,
        item_id: impl Into<std::borrow::Cow<'static, str>>,
        item: T,
        options: Option<DistributedTransactionOperationOptions>,
    ) -> crate::Result<Self> {
        let body = serde_json::to_vec(&item)?;
        self.operations.push(operation_with_options(
            driver_models::DistributedTransactionOperationKind::Create,
            container,
            partition_key,
            item_id,
            Some(Bytes::from(body)),
            options,
        ));
        Ok(self)
    }

    /// Adds a replace item operation.
    pub fn replace_item<T: Serialize>(
        mut self,
        container: &ContainerClient,
        partition_key: impl Into<PartitionKey>,
        item_id: impl Into<std::borrow::Cow<'static, str>>,
        item: T,
        options: Option<DistributedTransactionOperationOptions>,
    ) -> crate::Result<Self> {
        let body = serde_json::to_vec(&item)?;
        self.operations.push(operation_with_options(
            driver_models::DistributedTransactionOperationKind::Replace,
            container,
            partition_key,
            item_id,
            Some(Bytes::from(body)),
            options,
        ));
        Ok(self)
    }

    /// Adds an upsert item operation.
    pub fn upsert_item<T: Serialize>(
        mut self,
        container: &ContainerClient,
        partition_key: impl Into<PartitionKey>,
        item_id: impl Into<std::borrow::Cow<'static, str>>,
        item: T,
        options: Option<DistributedTransactionOperationOptions>,
    ) -> crate::Result<Self> {
        let body = serde_json::to_vec(&item)?;
        self.operations.push(operation_with_options(
            driver_models::DistributedTransactionOperationKind::Upsert,
            container,
            partition_key,
            item_id,
            Some(Bytes::from(body)),
            options,
        ));
        Ok(self)
    }

    /// Adds a delete item operation.
    pub fn delete_item(
        mut self,
        container: &ContainerClient,
        partition_key: impl Into<PartitionKey>,
        item_id: impl Into<std::borrow::Cow<'static, str>>,
        options: Option<DistributedTransactionOperationOptions>,
    ) -> Self {
        self.operations.push(operation_with_options(
            driver_models::DistributedTransactionOperationKind::Delete,
            container,
            partition_key,
            item_id,
            None,
            options,
        ));
        self
    }

    /// Adds a patch item operation.
    pub fn patch_item(
        mut self,
        container: &ContainerClient,
        partition_key: impl Into<PartitionKey>,
        item_id: impl Into<std::borrow::Cow<'static, str>>,
        patch: PatchInstructions,
        options: Option<DistributedTransactionPatchOperationOptions>,
    ) -> crate::Result<Self> {
        let body = serde_json::to_vec(&patch)?;
        self.operations.push(patch_operation_with_options(
            container,
            partition_key,
            item_id,
            Bytes::from(body),
            options,
        )?);
        Ok(self)
    }
}

impl Default for DistributedWriteTransaction {
    fn default() -> Self {
        Self::new()
    }
}

/// Preview distributed read transaction builder.
#[derive(Clone, SafeDebug)]
#[safe(true)]
pub struct DistributedReadTransaction {
    operations: Vec<driver_models::DistributedTransactionOperation>,
}

impl DistributedReadTransaction {
    /// Creates an empty distributed read transaction.
    pub fn new() -> Self {
        Self {
            operations: Vec::new(),
        }
    }

    /// Consumes this transaction and returns its operations.
    pub(crate) fn into_operations(self) -> Vec<driver_models::DistributedTransactionOperation> {
        self.operations
    }

    /// Adds a read item operation.
    pub fn read_item(
        mut self,
        container: &ContainerClient,
        partition_key: impl Into<PartitionKey>,
        item_id: impl Into<std::borrow::Cow<'static, str>>,
        options: Option<DistributedTransactionOperationOptions>,
    ) -> Self {
        self.operations.push(operation_with_options(
            driver_models::DistributedTransactionOperationKind::Read,
            container,
            partition_key,
            item_id,
            None,
            options,
        ));
        self
    }
}

impl Default for DistributedReadTransaction {
    fn default() -> Self {
        Self::new()
    }
}

fn operation_with_options(
    kind: driver_models::DistributedTransactionOperationKind,
    container: &ContainerClient,
    partition_key: impl Into<PartitionKey>,
    item_id: impl Into<std::borrow::Cow<'static, str>>,
    body: Option<Bytes>,
    options: Option<DistributedTransactionOperationOptions>,
) -> driver_models::DistributedTransactionOperation {
    let mut operation = driver_models::DistributedTransactionOperation::new(
        kind,
        driver_models::DistributedTransactionTarget::new(
            container.container_reference().clone(),
            partition_key,
            item_id,
        ),
    );
    if let Some(body) = body {
        operation = operation.with_resource_body(body);
    }
    if let Some(options) = options {
        if let Some(session_token) = options.session_token {
            operation = operation.with_session_token(session_token);
        }
        if let Some(precondition) = options.precondition {
            operation = operation.with_precondition(precondition);
        }
    }
    operation
}

fn patch_operation_with_options(
    container: &ContainerClient,
    partition_key: impl Into<PartitionKey>,
    item_id: impl Into<std::borrow::Cow<'static, str>>,
    body: Bytes,
    options: Option<DistributedTransactionPatchOperationOptions>,
) -> crate::Result<driver_models::DistributedTransactionOperation> {
    let mut operation = driver_models::DistributedTransactionOperation::new(
        driver_models::DistributedTransactionOperationKind::Patch,
        driver_models::DistributedTransactionTarget::new(
            container.container_reference().clone(),
            partition_key,
            item_id,
        ),
    )
    .with_resource_body(body);

    if let Some(options) = options {
        if let Some(session_token) = options.session_token {
            operation = operation.with_session_token(session_token);
        }
        if let Some(precondition) = options.precondition {
            operation = operation.with_precondition(precondition);
        }
        if let Some(predicate) = options.filter_predicate {
            operation = operation.with_patch_filter_predicate(predicate);
        }
    }

    Ok(operation)
}

pub(crate) async fn commit_distributed_write(
    context: &ClientContext,
    transaction: DistributedWriteTransaction,
) -> crate::Result<DistributedTransactionResponse> {
    let operations = transaction.into_operations();
    validate_transaction_account(context, &operations)?;
    let request = driver_models::DistributedTransactionRequest::new(
        driver_models::DistributedTransactionType::Write,
        operations,
    );
    let response = context
        .driver
        .execute_distributed_transaction(request, Default::default())
        .await?;
    Ok(DistributedTransactionResponse::from_driver(response))
}

pub(crate) async fn execute_distributed_read(
    context: &ClientContext,
    transaction: DistributedReadTransaction,
) -> crate::Result<DistributedTransactionResponse> {
    let operations = transaction.into_operations();
    validate_transaction_account(context, &operations)?;
    let request = driver_models::DistributedTransactionRequest::new(
        driver_models::DistributedTransactionType::Read,
        operations,
    );
    let response = context
        .driver
        .execute_distributed_transaction(request, Default::default())
        .await?;
    Ok(DistributedTransactionResponse::from_driver(response))
}

fn validate_transaction_account(
    context: &ClientContext,
    operations: &[driver_models::DistributedTransactionOperation],
) -> crate::Result<()> {
    if operations
        .iter()
        .all(|operation| operation.target.container.account() == context.driver.account())
    {
        Ok(())
    } else {
        Err(crate::DriverCosmosError::builder()
            .with_status(crate::CosmosStatus::new(
                azure_core::http::StatusCode::BadRequest,
            ))
            .with_message(
                "distributed transaction operations must target containers from the same Cosmos account as the committing client",
            )
            .build()
            .into())
    }
}

/// Response from a distributed transaction.
#[derive(Clone, SafeDebug)]
#[safe(true)]
pub struct DistributedTransactionResponse {
    inner: driver_models::DistributedTransactionResponse,
    headers: ResponseHeaders,
}

impl DistributedTransactionResponse {
    fn from_driver(inner: driver_models::DistributedTransactionResponse) -> Self {
        let headers = inner.headers.clone().into();
        Self { inner, headers }
    }

    /// Returns the overall transaction status.
    ///
    /// Unlike a transactional batch — which surfaces a raw `207 MultiStatus` and
    /// leaves per-operation inspection to the caller — a distributed transaction
    /// **promotes** a `207` to the status of the first failing operation in
    /// request order (excluding `424 FailedDependency`). A fully successful
    /// transaction reports its success status directly (for example `200`/`201`,
    /// or `304 NotModified` for an all-unchanged read snapshot). Use
    /// [`operation_result`](Self::operation_result) to inspect each operation's
    /// individual status.
    pub fn status(&self) -> crate::CosmosStatus {
        let status = crate::CosmosStatus::new(self.inner.status_code);
        match self.inner.sub_status_code {
            Some(sub_status) => status.with_sub_status(sub_status.value()),
            None => status,
        }
    }

    /// Returns true when the overall status is successful.
    pub fn is_success_status_code(&self) -> bool {
        self.inner.is_success_status_code()
    }

    /// Returns true when the overall transaction completed successfully.
    ///
    /// For read transactions this also treats `304 NotModified` as completed,
    /// even though `304` is not an HTTP 2xx status.
    pub fn is_completed_status_code(&self) -> bool {
        self.inner.is_completed_status_code()
    }

    /// Returns the number of operation results.
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    /// Returns true when no operation results are present.
    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    /// Returns the operation result at `index`.
    pub fn operation_result(
        &self,
        index: usize,
    ) -> Option<DistributedTransactionOperationResult<'_>> {
        self.inner
            .operation_results
            .get(index)
            .map(|inner| DistributedTransactionOperationResult { inner })
    }

    /// Returns the response headers.
    pub fn headers(&self) -> &ResponseHeaders {
        &self.headers
    }

    /// Returns the coordinator diagnostic string, when present.
    pub fn diagnostic_string(&self) -> Option<&str> {
        self.inner.diagnostic_string.as_deref()
    }

    /// Returns the idempotency token for the transaction as a string.
    pub fn idempotency_token(&self) -> String {
        self.inner.idempotency_token.to_string()
    }

    /// Returns whether the coordinator marked this response as retryable.
    pub fn is_retriable(&self) -> bool {
        self.inner.is_retriable
    }

    /// Returns a driver-synthesized error message for malformed coordinator responses.
    pub fn error_message(&self) -> Option<&str> {
        self.inner.error_message.as_deref()
    }

    /// Returns diagnostics captured while executing the transaction.
    pub fn diagnostics(&self) -> Option<Arc<DiagnosticsContext>> {
        self.inner.diagnostics.clone()
    }

    /// Returns the activity ID from the response headers, when present.
    pub fn activity_id(&self) -> Option<&str> {
        self.inner.activity_id.as_ref().map(|id| id.as_str())
    }

    /// Returns the total request charge from the response headers, when present.
    pub fn request_charge(&self) -> Option<f64> {
        self.inner.request_charge.map(|charge| charge.value())
    }

    /// Returns the retry-after hint in milliseconds, when present.
    pub fn retry_after_ms(&self) -> Option<u64> {
        self.inner.retry_after_ms
    }
}

/// Result for one operation in a distributed transaction response.
#[derive(Clone, Copy, SafeDebug)]
#[safe(true)]
pub struct DistributedTransactionOperationResult<'a> {
    inner: &'a driver_models::DistributedTransactionOperationResult,
}

impl DistributedTransactionOperationResult<'_> {
    /// Returns the operation index.
    pub fn index(&self) -> usize {
        self.inner.index
    }

    /// Returns the operation status.
    pub fn status_code(&self) -> azure_core::http::StatusCode {
        self.inner.status_code
    }

    /// Returns the operation sub-status, when present.
    pub fn sub_status_code(&self) -> Option<crate::SubStatusCode> {
        self.inner.sub_status_code
    }

    /// Returns true when the operation status is successful.
    pub fn is_success_status_code(&self) -> bool {
        self.inner.is_success_status_code()
    }

    /// Returns true when the operation is a completed DTX success outcome.
    ///
    /// For read transactions this also treats `304 NotModified` as completed,
    /// even though `304` is not an HTTP 2xx status.
    pub fn is_completed_status_code(&self) -> bool {
        self.inner.is_completed_status_code()
    }

    /// Returns the ETag, when present.
    pub fn etag(&self) -> Option<&azure_core::http::Etag> {
        self.inner.etag.as_ref()
    }

    /// Returns the per-operation session token, when present.
    pub fn session_token(&self) -> Option<&SessionToken> {
        self.inner.session_token.as_ref()
    }

    /// Returns the partition key range id, when present.
    pub fn partition_key_range_id(&self) -> Option<&str> {
        self.inner.partition_key_range_id.as_deref()
    }

    /// Returns the request charge for this operation, when present.
    pub fn request_charge(&self) -> Option<f64> {
        self.inner.request_charge.map(|charge| charge.value())
    }

    /// Deserializes the operation resource body, when present.
    pub fn resource<T: DeserializeOwned>(&self) -> crate::Result<Option<T>> {
        match &self.inner.resource_body {
            driver_models::DistributedTransactionResultBody::None => Ok(None),
            driver_models::DistributedTransactionResultBody::Bytes(bytes) => {
                serde_json::from_slice(bytes).map(Some).map_err(Into::into)
            }
            _ => Ok(None),
        }
    }
}