google-cloud-spanner 0.34.1-preview

Google Cloud Client Libraries for Rust - Spanner
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::key::KeySet;
use crate::model::batch_write_request::MutationGroup as ProtoMutationGroup;
use crate::model::mutation::Operation;
use crate::to_value::ToValue;
use crate::value::Value;
use rand::seq::IteratorRandom;
use std::slice::Iter;
use std::vec::IntoIter;

/// Represents an individual table modification to be applied to Cloud Spanner.
///
/// # Example
/// ```rust
/// use google_cloud_spanner::mutation::Mutation;
///
/// let mutation = Mutation::new_insert_builder("Users")
///     .set("UserId").to(&1)
///     .set("UserName").to(&"Alice")
///     .build();
/// ```
///
/// Use the methods on `Mutation` to create a builder for the desired operation type.
#[derive(Clone, Debug, PartialEq)]
pub struct Mutation {
    pub(crate) inner: InternalMutation,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum InternalMutation {
    /// Inserts a new row in a table. If the row already exists, the write or transaction fails with
    /// `ALREADY_EXISTS`.
    Insert(Write),
    /// Updates an existing row in a table. If the row does not already exist, the transaction fails
    /// with error `NOT_FOUND`.
    Update(Write),
    /// Like `Insert`, except that if the row already exists, then its column values are
    /// overwritten with the ones provided.
    InsertOrUpdate(Write),
    /// Like `Insert`, except that if the row already exists, it is deleted, and the column
    /// values provided are inserted instead.
    Replace(Write),
    /// Deletes rows from a table. Succeeds whether or not the referenced rows were present.
    Delete(Delete),
}

/// A mutation that inserts, updates, or replaces rows in a table.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct Write {
    pub(crate) table: String,
    pub(crate) columns: Vec<String>,
    pub(crate) values: Vec<Value>,
}

/// A mutation that deletes rows from a table.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct Delete {
    pub(crate) table: String,
    // This will be replaced with the KeySet definition from the
    // spanner-keys branch once it has been merged.
    pub(crate) key_set: KeySet,
}

impl Mutation {
    /// Returns a builder that can be used to construct an `Insert` mutation against `table`.
    ///
    /// # Example
    /// ```rust
    /// use google_cloud_spanner::mutation::Mutation;
    /// let mutation = Mutation::new_insert_builder("Users")
    ///     .set("UserId").to(&1)
    ///     .build();
    /// ```
    pub fn new_insert_builder(table: impl Into<String>) -> WriteBuilder {
        WriteBuilder::new(table, MutationType::Insert)
    }

    /// Returns a builder that can be used to construct an `Update` mutation against `table`.
    ///
    /// # Example
    /// ```rust
    /// use google_cloud_spanner::mutation::Mutation;
    /// let mutation = Mutation::new_update_builder("Users")
    ///     .set("UserId").to(&1)
    ///     .set("UserName").to(&"Bob")
    ///     .build();
    /// ```
    pub fn new_update_builder(table: impl Into<String>) -> WriteBuilder {
        WriteBuilder::new(table, MutationType::Update)
    }

    /// Returns a builder that can be used to construct an `InsertOrUpdate` mutation against `table`.
    ///
    /// # Example
    /// ```rust
    /// use google_cloud_spanner::mutation::Mutation;
    /// let mutation = Mutation::new_insert_or_update_builder("Users")
    ///     .set("UserId").to(&1)
    ///     .set("UserName").to(&"Bob")
    ///     .build();
    /// ```
    pub fn new_insert_or_update_builder(table: impl Into<String>) -> WriteBuilder {
        WriteBuilder::new(table, MutationType::InsertOrUpdate)
    }

    /// Returns a builder that can be used to construct a `Replace` mutation against `table`.
    ///
    /// # Example
    /// ```rust
    /// use google_cloud_spanner::mutation::Mutation;
    /// let mutation = Mutation::new_replace_builder("Users")
    ///     .set("UserId").to(&1)
    ///     .set("UserName").to(&"Bob")
    ///     .build();
    /// ```
    pub fn new_replace_builder(table: impl Into<String>) -> WriteBuilder {
        WriteBuilder::new(table, MutationType::Replace)
    }

    /// Returns a mutation that will delete all rows with primary keys covered by `key_set`.
    ///
    /// # Example
    /// ```text
    /// // Example omitted temporarily until the new KeySet API is merged
    /// ```
    pub fn delete(table: impl Into<String>, key_set: KeySet) -> Mutation {
        Mutation {
            inner: InternalMutation::Delete(Delete {
                table: table.into(),
                key_set,
            }),
        }
    }

    pub(crate) fn build_proto(self) -> crate::model::Mutation {
        match self.inner {
            InternalMutation::Insert(write) => {
                crate::model::Mutation::new().set_insert(write.into_proto())
            }
            InternalMutation::Update(write) => {
                crate::model::Mutation::new().set_update(write.into_proto())
            }
            InternalMutation::InsertOrUpdate(write) => {
                crate::model::Mutation::new().set_insert_or_update(write.into_proto())
            }
            InternalMutation::Replace(write) => {
                crate::model::Mutation::new().set_replace(write.into_proto())
            }
            InternalMutation::Delete(delete) => {
                crate::model::Mutation::new().set_delete(delete.into_proto())
            }
        }
    }

    /// Selects the best mutation to act as a routing `mutation_key`.
    /// Prefers any non-`Insert` variation (like `Update`, `InsertOrUpdate`, `Replace`, `Delete`)
    /// since inserts more often use auto-generated columns (e.g. for primary key generation).
    /// Using a mutation with only non-generated values as the mutation key is preferred, as it reduces
    /// the overhead internally in Spanner.
    /// If only `Insert` mutations are present, it selects the insert mutation with the largest number of rows.
    pub(crate) fn select_mutation_key(
        mutations: &[crate::model::Mutation],
    ) -> Option<crate::model::Mutation> {
        if mutations.is_empty() {
            return None;
        }

        // 1. Filter for any mutations other than Operation::Insert, Send, or Ack, selecting one randomly.
        let selected_non_insert = mutations
            .iter()
            .filter(|m| {
                m.operation.as_ref().is_some_and(|op| {
                    !matches!(
                        op,
                        Operation::Insert(_) | Operation::Send(_) | Operation::Ack(_)
                    )
                })
            })
            .choose(&mut rand::rng())
            .cloned();

        if selected_non_insert.is_some() {
            return selected_non_insert;
        }

        // 2. If only Inserts are present, choose the one with the largest number of values (rows).
        let max_insert = mutations
            .iter()
            .filter_map(|m| match &m.operation {
                Some(Operation::Insert(write)) => Some((m, write.values.len())),
                _ => None,
            })
            .max_by_key(|&(_, rows)| rows)
            .map(|(m, _)| m);

        max_insert.cloned().or_else(|| mutations.first().cloned())
    }
}

impl Write {
    fn into_proto(self) -> crate::model::mutation::Write {
        crate::model::mutation::Write::new()
            .set_table(self.table)
            .set_columns(self.columns)
            .set_values(vec![
                self.values
                    .into_iter()
                    .map(Value::into_serde_value)
                    .collect::<wkt::ListValue>(),
            ])
    }
}

impl Delete {
    fn into_proto(self) -> crate::model::mutation::Delete {
        crate::model::mutation::Delete::new()
            .set_table(self.table)
            .set_key_set(self.key_set.into_proto())
    }
}

/// A builder for constructing `Write` mutations fluently.
pub struct WriteBuilder {
    table: String,
    mutation_type: MutationType,
    columns: Vec<String>,
    values: Vec<Value>,
}

enum MutationType {
    Insert,
    Update,
    InsertOrUpdate,
    Replace,
}

impl WriteBuilder {
    fn new(table: impl Into<String>, mutation_type: MutationType) -> Self {
        Self {
            table: table.into(),
            mutation_type,
            columns: Vec::new(),
            values: Vec::new(),
        }
    }

    /// Returns a binder to set the value of `column_name` that should be applied by the mutation.
    ///
    /// # Example
    /// ```rust
    /// use google_cloud_spanner::mutation::Mutation;
    /// let mutation = Mutation::new_insert_builder("Users")
    ///     .set("UserId").to(&1)
    ///     .build();
    /// ```
    pub fn set(self, column_name: impl Into<String>) -> ValueBinder {
        ValueBinder {
            builder: self,
            column: column_name.into(),
        }
    }

    /// Builds and returns the finalized `Mutation`.
    pub fn build(self) -> Mutation {
        let write = Write {
            table: self.table,
            columns: self.columns,
            values: self.values,
        };
        let inner = match self.mutation_type {
            MutationType::Insert => InternalMutation::Insert(write),
            MutationType::Update => InternalMutation::Update(write),
            MutationType::InsertOrUpdate => InternalMutation::InsertOrUpdate(write),
            MutationType::Replace => InternalMutation::Replace(write),
        };
        Mutation { inner }
    }
}

/// A binder that associates a column name with a value within a `WriteBuilder`.
pub struct ValueBinder {
    builder: WriteBuilder,
    column: String,
}

impl ValueBinder {
    /// Sets the value for the column.
    pub fn to<T: ToValue + ?Sized>(mut self, value: &T) -> WriteBuilder {
        self.builder.columns.push(self.column);
        self.builder.values.push(value.to_value());
        self.builder
    }
}

/// A group of mutations that are applied atomically in a [crate::batch::BatchWriteTransaction].
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct MutationGroup {
    mutations: Vec<Mutation>,
}

impl MutationGroup {
    /// Creates a new mutation group from a list of mutations.
    pub fn new(mutations: Vec<Mutation>) -> Self {
        Self { mutations }
    }

    /// Returns a reference to the collection of mutations in this group.
    pub fn mutations(&self) -> &[Mutation] {
        &self.mutations
    }

    #[allow(dead_code)]
    pub(crate) fn build_proto(self) -> ProtoMutationGroup {
        ProtoMutationGroup::new().set_mutations(self.mutations.into_iter().map(|m| m.build_proto()))
    }
}

impl IntoIterator for MutationGroup {
    type Item = Mutation;
    type IntoIter = IntoIter<Mutation>;

    fn into_iter(self) -> Self::IntoIter {
        self.mutations.into_iter()
    }
}

impl<'a> IntoIterator for &'a MutationGroup {
    type Item = &'a Mutation;
    type IntoIter = Iter<'a, Mutation>;

    fn into_iter(self) -> Self::IntoIter {
        self.mutations.iter()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn auto_traits() {
        static_assertions::assert_impl_all!(Mutation: Send, Sync, Clone, std::fmt::Debug);
        static_assertions::assert_impl_all!(Write: Send, Sync, Clone, std::fmt::Debug);
        static_assertions::assert_impl_all!(Delete: Send, Sync, Clone, std::fmt::Debug);
        static_assertions::assert_impl_all!(WriteBuilder: Send, Sync);
        static_assertions::assert_impl_all!(ValueBinder: Send, Sync);
        static_assertions::assert_impl_all!(MutationGroup: Send, Sync, Clone, std::fmt::Debug);
    }

    #[test]
    fn mutation_group() {
        let mutation1 = Mutation::new_insert_builder("Users")
            .set("UserId")
            .to(&1)
            .build();
        let mutation2 = Mutation::new_insert_builder("Users")
            .set("UserId")
            .to(&2)
            .build();
        let group = MutationGroup::new(vec![mutation1.clone(), mutation2.clone()]);
        assert_eq!(group.mutations.len(), 2);
        assert_eq!(group.mutations[0], mutation1);
        assert_eq!(group.mutations[1], mutation2);
    }

    #[test]
    fn mutation_group_into_iter() {
        let mutation1 = Mutation::new_insert_builder("Users")
            .set("UserId")
            .to(&1)
            .build();
        let mutation2 = Mutation::new_insert_builder("Users")
            .set("UserId")
            .to(&2)
            .build();
        let group = MutationGroup::new(vec![mutation1.clone(), mutation2.clone()]);

        let mutations: Vec<_> = group.into_iter().collect();
        assert_eq!(mutations, vec![mutation1, mutation2]);
    }

    #[test]
    fn mutation_group_iter_ref() {
        let mutation1 = Mutation::new_insert_builder("Users")
            .set("UserId")
            .to(&1)
            .build();
        let mutation2 = Mutation::new_insert_builder("Users")
            .set("UserId")
            .to(&2)
            .build();
        let group = MutationGroup::new(vec![mutation1.clone(), mutation2.clone()]);

        let mutations: Vec<_> = (&group).into_iter().collect();
        assert_eq!(mutations, vec![&mutation1, &mutation2]);
    }

    #[test]
    fn insert_builder() {
        let mutation = Mutation::new_insert_builder("Users")
            .set("UserId")
            .to(&1)
            .set("UserName")
            .to(&"Alice")
            .build();

        match mutation.inner {
            InternalMutation::Insert(write) => {
                assert_eq!(write.table, "Users");
                assert_eq!(write.columns, vec!["UserId", "UserName"]);
                assert_eq!(write.values.len(), 2);
                assert_eq!(write.values[0].as_string(), "1");
                assert_eq!(write.values[1].as_string(), "Alice");
            }
            _ => panic!("Expected Insert mutation"),
        }
    }

    #[test]
    fn update_builder() {
        let mutation = Mutation::new_update_builder("Users")
            .set("UserId")
            .to(&1)
            .build();

        match mutation.inner {
            InternalMutation::Update(write) => {
                assert_eq!(write.table, "Users");
                assert_eq!(write.columns, vec!["UserId"]);
                assert_eq!(write.values.len(), 1);
                assert_eq!(write.values[0].as_string(), "1");
            }
            _ => panic!("Expected Update mutation"),
        }
    }

    #[test]
    fn insert_or_update_builder() {
        let mutation = Mutation::new_insert_or_update_builder("Users")
            .set("UserId")
            .to(&1)
            .build();

        match mutation.inner {
            InternalMutation::InsertOrUpdate(write) => {
                assert_eq!(write.table, "Users");
                assert_eq!(write.columns, vec!["UserId"]);
                assert_eq!(write.values.len(), 1);
                assert_eq!(write.values[0].as_string(), "1");
            }
            _ => panic!("Expected InsertOrUpdate mutation"),
        }
    }

    #[test]
    fn replace_builder() {
        let mutation = Mutation::new_replace_builder("Users")
            .set("UserId")
            .to(&1)
            .build();

        match mutation.inner {
            InternalMutation::Replace(write) => {
                assert_eq!(write.table, "Users");
                assert_eq!(write.columns, vec!["UserId"]);
                assert_eq!(write.values.len(), 1);
                assert_eq!(write.values[0].as_string(), "1");
            }
            _ => panic!("Expected Replace mutation"),
        }
    }

    #[test]
    fn build_proto_insert() {
        let mutation = Mutation::new_insert_builder("Users")
            .set("UserId")
            .to(&1)
            .set("UserName")
            .to(&"Alice")
            .build();
        let proto = mutation.build_proto();
        match proto.operation {
            Some(Operation::Insert(write)) => {
                assert_eq!(write.table, "Users");
                assert_eq!(write.columns, vec!["UserId", "UserName"]);
                assert_eq!(write.values.len(), 1);
                assert_eq!(write.values[0].len(), 2);
                assert_eq!(write.values[0][0], serde_json::json!("1"));
                assert_eq!(write.values[0][1], serde_json::json!("Alice"));
            }
            _ => panic!("Expected Insert operation, got {:?}", proto.operation),
        }
    }

    #[test]
    fn build_proto_update() {
        let mutation = Mutation::new_update_builder("Users")
            .set("UserId")
            .to(&1)
            .build();
        let proto = mutation.build_proto();
        match proto.operation {
            Some(Operation::Update(write)) => {
                assert_eq!(write.table, "Users");
                assert_eq!(write.columns, vec!["UserId"]);
                assert_eq!(write.values.len(), 1);
            }
            _ => panic!("Expected Update operation, got {:?}", proto.operation),
        }
    }

    #[test]
    fn build_proto_insert_or_update() {
        let mutation = Mutation::new_insert_or_update_builder("Users")
            .set("UserId")
            .to(&1)
            .build();
        let proto = mutation.build_proto();
        match proto.operation {
            Some(Operation::InsertOrUpdate(write)) => {
                assert_eq!(write.table, "Users");
                assert_eq!(write.columns, vec!["UserId"]);
                assert_eq!(write.values.len(), 1);
            }
            _ => panic!(
                "Expected InsertOrUpdate operation, got {:?}",
                proto.operation
            ),
        }
    }

    #[test]
    fn build_proto_replace() {
        let mutation = Mutation::new_replace_builder("Users")
            .set("UserId")
            .to(&1)
            .build();
        let proto = mutation.build_proto();
        match proto.operation {
            Some(Operation::Replace(write)) => {
                assert_eq!(write.table, "Users");
                assert_eq!(write.columns, vec!["UserId"]);
                assert_eq!(write.values.len(), 1);
            }
            _ => panic!("Expected Replace operation, got {:?}", proto.operation),
        }
    }

    #[test]
    fn build_proto_delete() {
        let key_set = crate::key::KeySet::builder().build();
        let mutation = Mutation::delete("Users", key_set);
        let proto = mutation.build_proto();
        match proto.operation {
            Some(Operation::Delete(delete)) => {
                assert_eq!(delete.table, "Users");
            }
            _ => panic!("Expected Delete operation, got {:?}", proto.operation),
        }
    }

    #[test]
    fn test_select_mutation_key_empty() {
        let mutations = vec![];
        let key = Mutation::select_mutation_key(&mutations);
        assert!(key.is_none());
    }

    #[test]
    fn test_select_mutation_key_prefers_insert_or_update_over_insert() {
        let m1 = Mutation::new_insert_builder("Users")
            .set("UserId")
            .to(&1)
            .build()
            .build_proto();
        let m2 = Mutation::new_insert_or_update_builder("Users")
            .set("UserId")
            .to(&2)
            .build()
            .build_proto();
        let mutations = vec![m1.clone(), m2.clone()];
        let key = Mutation::select_mutation_key(&mutations);
        assert_eq!(key, Some(m2));
    }

    #[test]
    fn test_select_mutation_key_only_insert_prefers_largest() {
        let m1 = Mutation::new_insert_builder("Users")
            .set("UserId")
            .to(&1)
            .build()
            .build_proto();

        // Create an insert mutation with two rows (larger than m1 which has one row)
        let row1 = vec![serde_json::json!("2")]
            .into_iter()
            .collect::<wkt::ListValue>();
        let row2 = vec![serde_json::json!("3")]
            .into_iter()
            .collect::<wkt::ListValue>();
        let write2 = crate::model::mutation::Write::new()
            .set_table("Users")
            .set_columns(vec!["UserId".to_string()])
            .set_values(vec![row1, row2]);
        let m2 = crate::model::Mutation::new().set_insert(write2);

        let mutations = vec![m1.clone(), m2.clone()];
        let key = Mutation::select_mutation_key(&mutations);
        assert_eq!(key, Some(m2));
    }

    #[test]
    fn test_select_mutation_key_mix() {
        let m1 = Mutation::new_insert_builder("Users")
            .set("UserId")
            .to(&1)
            .build()
            .build_proto();
        let m2 = Mutation::new_update_builder("Users")
            .set("UserId")
            .to(&2)
            .build()
            .build_proto();
        let m3 = Mutation::new_insert_or_update_builder("Users")
            .set("UserId")
            .to(&3)
            .build()
            .build_proto();
        let mutations = vec![m1.clone(), m2.clone(), m3.clone()];
        let key = Mutation::select_mutation_key(&mutations).expect("Expected a key");
        // Either of the non-insert mutations (m2 or m3) can be selected randomly.
        assert!(
            key == m2 || key == m3,
            "Expected either m2 or m3 to be selected, got {:?}",
            key
        );
    }

    #[test]
    fn test_select_mutation_key_only_non_insert() {
        let m1 = Mutation::new_update_builder("Users")
            .set("UserId")
            .to(&1)
            .build()
            .build_proto();
        let m2 = Mutation::new_replace_builder("Users")
            .set("UserId")
            .to(&2)
            .build()
            .build_proto();
        let mutations = vec![m1.clone(), m2.clone()];
        let key = Mutation::select_mutation_key(&mutations).expect("Expected a key");
        // Either non-insert mutation can be selected randomly.
        assert!(
            key == m1 || key == m2,
            "Expected either m1 or m2 to be selected, got {:?}",
            key
        );
    }

    #[test]
    fn test_select_mutation_key_operation_none() {
        let m1 = crate::model::Mutation::default();
        let m2 = crate::model::Mutation::default();
        let mutations = vec![m1.clone(), m2.clone()];
        let key = Mutation::select_mutation_key(&mutations);
        assert_eq!(key, Some(m1));
    }
}