prolly-store-spanner 0.3.0

Cloud Spanner store adapter for prolly-map.
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
#![doc = include_str!("../README.md")]

pub use prolly::{
    RemoteBatchOp, RemoteManifestUpdate, RemoteNamedRoot, RemoteProllyStore, RemoteRootCondition,
    RemoteRootWrite, RemoteStoreBackend, RemoteTransactionConflict, RemoteTransactionUpdate,
};

/// Spanner adapter entry point.
pub mod spanner {
    use google_cloud_googleapis::spanner::v1::Mutation;
    use google_cloud_spanner::client::{Client, ClientConfig, Error};
    use google_cloud_spanner::key::Key;
    use google_cloud_spanner::mutation::{delete, insert_or_update};
    use google_cloud_spanner::statement::Statement;
    use google_cloud_spanner::transaction_rw::ReadWriteTransaction;

    use crate::{
        RemoteBatchOp, RemoteManifestUpdate, RemoteNamedRoot, RemoteRootCondition, RemoteRootWrite,
        RemoteStoreBackend, RemoteTransactionConflict, RemoteTransactionUpdate,
    };

    /// Store adapter for Spanner-backed prolly nodes and roots.
    pub type SpannerStore = crate::RemoteProllyStore<SpannerBackend>;

    /// Google Cloud Spanner-backed backend.
    #[derive(Clone)]
    pub struct SpannerBackend {
        client: Client,
        read_parallelism: usize,
    }

    impl std::fmt::Debug for SpannerBackend {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            f.debug_struct("SpannerBackend")
                .field("read_parallelism", &self.read_parallelism)
                .finish_non_exhaustive()
        }
    }

    impl SpannerBackend {
        /// Create a backend from an existing Spanner client.
        pub fn new(client: Client) -> Self {
            Self {
                client,
                read_parallelism: DEFAULT_READ_PARALLELISM,
            }
        }

        /// Connect to a Spanner database resource name using a caller-provided config.
        pub async fn connect(database: &str, config: ClientConfig) -> Result<Self, Error> {
            Ok(Self::new(Client::new(database, config).await?))
        }

        /// Borrow the underlying Spanner client.
        pub fn client(&self) -> &Client {
            &self.client
        }

        /// Set the read parallelism advertised to async prolly traversals.
        pub fn with_read_parallelism(mut self, read_parallelism: usize) -> Self {
            self.read_parallelism = read_parallelism.max(1);
            self
        }

        async fn query_one_value(
            &self,
            statement: Statement,
            column: &str,
        ) -> Result<Option<Vec<u8>>, Error> {
            let mut tx = self.client.single().await?;
            let mut rows = tx.query(statement).await.map_err(Error::from)?;
            let row = rows.next().await.map_err(Error::from)?;
            row.map(|row| row.column_by_name(column).map_err(Error::from))
                .transpose()
        }

        async fn query_bytes_column(
            &self,
            statement: Statement,
            column: &str,
        ) -> Result<Vec<Vec<u8>>, Error> {
            let mut tx = self.client.single().await?;
            let mut rows = tx.query(statement).await.map_err(Error::from)?;
            let mut values = Vec::new();
            while let Some(row) = rows.next().await.map_err(Error::from)? {
                values.push(row.column_by_name(column)?);
            }
            Ok(values)
        }
    }

    impl RemoteStoreBackend for SpannerBackend {
        type Error = Error;

        async fn get_node(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
            let mut statement = Statement::new("SELECT Node FROM ProllyNodes WHERE Cid = @cid");
            statement.add_param("cid", &key.to_vec());
            self.query_one_value(statement, "Node").await
        }

        async fn put_node(&self, key: &[u8], value: &[u8]) -> Result<(), Self::Error> {
            self.client
                .apply(vec![node_upsert(key, value)])
                .await
                .map(|_| ())
        }

        async fn delete_node(&self, key: &[u8]) -> Result<(), Self::Error> {
            self.client.apply(vec![node_delete(key)]).await.map(|_| ())
        }

        async fn batch_nodes(&self, ops: &[RemoteBatchOp<'_>]) -> Result<(), Self::Error> {
            let mutations = ops
                .iter()
                .map(|op| match op {
                    RemoteBatchOp::Upsert { key, value } => node_upsert(key, value),
                    RemoteBatchOp::Delete { key } => node_delete(key),
                })
                .collect::<Vec<_>>();
            if mutations.is_empty() {
                return Ok(());
            }
            self.client.apply(mutations).await.map(|_| ())
        }

        async fn batch_get_nodes_ordered(
            &self,
            keys: &[&[u8]],
        ) -> Result<Vec<Option<Vec<u8>>>, Self::Error> {
            let mut values = Vec::with_capacity(keys.len());
            for key in keys {
                values.push(self.get_node(key).await?);
            }
            Ok(values)
        }

        async fn batch_put_nodes(&self, entries: &[(&[u8], &[u8])]) -> Result<(), Self::Error> {
            let mutations = entries
                .iter()
                .map(|(key, value)| node_upsert(key, value))
                .collect::<Vec<_>>();
            if mutations.is_empty() {
                return Ok(());
            }
            self.client.apply(mutations).await.map(|_| ())
        }

        async fn list_node_cids(&self) -> Result<Vec<Vec<u8>>, Self::Error> {
            self.query_bytes_column(
                Statement::new("SELECT Cid FROM ProllyNodes ORDER BY Cid"),
                "Cid",
            )
            .await
        }

        fn read_parallelism(&self) -> usize {
            self.read_parallelism
        }

        fn supports_hints(&self) -> bool {
            true
        }

        async fn get_hint(
            &self,
            namespace: &[u8],
            key: &[u8],
        ) -> Result<Option<Vec<u8>>, Self::Error> {
            let mut statement = Statement::new(
                "SELECT Value FROM ProllyHints WHERE Namespace = @namespace AND HintKey = @key",
            );
            statement.add_param("namespace", &namespace.to_vec());
            statement.add_param("key", &key.to_vec());
            self.query_one_value(statement, "Value").await
        }

        async fn put_hint(
            &self,
            namespace: &[u8],
            key: &[u8],
            value: &[u8],
        ) -> Result<(), Self::Error> {
            self.client
                .apply(vec![hint_upsert(namespace, key, value)])
                .await
                .map(|_| ())
        }

        async fn batch_put_nodes_with_hint(
            &self,
            entries: &[(&[u8], &[u8])],
            namespace: &[u8],
            key: &[u8],
            value: &[u8],
        ) -> Result<(), Self::Error> {
            let mut mutations = entries
                .iter()
                .map(|(key, value)| node_upsert(key, value))
                .collect::<Vec<_>>();
            mutations.push(hint_upsert(namespace, key, value));
            self.client.apply(mutations).await.map(|_| ())
        }

        async fn get_root_manifest(&self, name: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
            let mut statement =
                Statement::new("SELECT Manifest FROM ProllyRoots WHERE Name = @name");
            statement.add_param("name", &name.to_vec());
            self.query_one_value(statement, "Manifest").await
        }

        async fn put_root_manifest(&self, name: &[u8], manifest: &[u8]) -> Result<(), Self::Error> {
            self.client
                .apply(vec![root_upsert(name, manifest)])
                .await
                .map(|_| ())
        }

        async fn delete_root_manifest(&self, name: &[u8]) -> Result<(), Self::Error> {
            self.client.apply(vec![root_delete(name)]).await.map(|_| ())
        }

        async fn compare_and_swap_root_manifest(
            &self,
            name: &[u8],
            expected: Option<&[u8]>,
            new: Option<&[u8]>,
        ) -> Result<RemoteManifestUpdate, Self::Error> {
            let name = name.to_vec();
            let expected = expected.map(<[u8]>::to_vec);
            let new = new.map(<[u8]>::to_vec);
            let (_, update) = self
                .client
                .read_write_transaction(|tx| {
                    let name = name.clone();
                    let expected = expected.clone();
                    let new = new.clone();
                    Box::pin(async move {
                        let current = read_root_in_transaction(tx, &name).await?;
                        if current.as_deref() != expected.as_deref() {
                            return Ok::<RemoteManifestUpdate, Error>(
                                RemoteManifestUpdate::Conflict { current },
                            );
                        }

                        match new {
                            Some(manifest) => tx.buffer_write(vec![root_upsert(&name, &manifest)]),
                            None => tx.buffer_write(vec![root_delete(&name)]),
                        }
                        Ok::<RemoteManifestUpdate, Error>(RemoteManifestUpdate::Applied)
                    })
                })
                .await?;
            Ok(update)
        }

        async fn list_root_manifests(&self) -> Result<Vec<RemoteNamedRoot>, Self::Error> {
            let mut tx = self.client.single().await?;
            let mut rows = tx
                .query(Statement::new(
                    "SELECT Name, Manifest FROM ProllyRoots ORDER BY Name",
                ))
                .await
                .map_err(Error::from)?;
            let mut roots = Vec::new();
            while let Some(row) = rows.next().await.map_err(Error::from)? {
                roots.push(RemoteNamedRoot::new(
                    row.column_by_name("Name")?,
                    row.column_by_name("Manifest")?,
                ));
            }
            Ok(roots)
        }

        fn supports_transactions(&self) -> bool {
            true
        }

        async fn commit_transaction(
            &self,
            node_writes: &[RemoteBatchOp<'_>],
            root_conditions: &[RemoteRootCondition],
            root_writes: &[RemoteRootWrite],
        ) -> Result<RemoteTransactionUpdate, Self::Error> {
            let node_writes = node_writes
                .iter()
                .map(|write| match write {
                    RemoteBatchOp::Upsert { key, value } => (true, key.to_vec(), value.to_vec()),
                    RemoteBatchOp::Delete { key } => (false, key.to_vec(), Vec::new()),
                })
                .collect::<Vec<_>>();
            let root_conditions = root_conditions.to_vec();
            let root_writes = root_writes.to_vec();

            let (_, update) = self
                .client
                .read_write_transaction(|tx| {
                    let node_writes = node_writes.clone();
                    let root_conditions = root_conditions.clone();
                    let root_writes = root_writes.clone();
                    Box::pin(async move {
                        for condition in &root_conditions {
                            let current = read_root_in_transaction(tx, &condition.name).await?;
                            if current != condition.expected {
                                return Ok::<RemoteTransactionUpdate, Error>(
                                    RemoteTransactionUpdate::Conflict(
                                        RemoteTransactionConflict::new(
                                            condition.name.clone(),
                                            condition.expected.clone(),
                                            current,
                                        ),
                                    ),
                                );
                            }
                        }

                        let mut mutations = Vec::new();
                        for (is_upsert, key, value) in &node_writes {
                            if *is_upsert {
                                mutations.push(node_upsert(key, value));
                            } else {
                                mutations.push(node_delete(key));
                            }
                        }
                        for write in &root_writes {
                            match write {
                                RemoteRootWrite::Put { name, manifest } => {
                                    mutations.push(root_upsert(name, manifest));
                                }
                                RemoteRootWrite::Delete { name } => {
                                    mutations.push(root_delete(name));
                                }
                            }
                        }
                        if !mutations.is_empty() {
                            tx.buffer_write(mutations);
                        }
                        Ok::<RemoteTransactionUpdate, Error>(RemoteTransactionUpdate::Applied)
                    })
                })
                .await?;
            Ok(update)
        }
    }

    async fn read_root_in_transaction(
        tx: &mut ReadWriteTransaction,
        name: &[u8],
    ) -> Result<Option<Vec<u8>>, Error> {
        let name = name.to_vec();
        let row = tx
            .read_row(ROOTS_TABLE, &["Manifest"], Key::new(&name))
            .await
            .map_err(Error::from)?;
        row.map(|row| row.column_by_name("Manifest").map_err(Error::from))
            .transpose()
    }

    fn node_upsert(key: &[u8], value: &[u8]) -> Mutation {
        let key = key.to_vec();
        let value = value.to_vec();
        insert_or_update(NODES_TABLE, &["Cid", "Node"], &[&key, &value])
    }

    fn node_delete(key: &[u8]) -> Mutation {
        let key = key.to_vec();
        delete(NODES_TABLE, Key::new(&key))
    }

    fn hint_upsert(namespace: &[u8], key: &[u8], value: &[u8]) -> Mutation {
        let namespace = namespace.to_vec();
        let key = key.to_vec();
        let value = value.to_vec();
        insert_or_update(
            HINTS_TABLE,
            &["Namespace", "HintKey", "Value"],
            &[&namespace, &key, &value],
        )
    }

    fn root_upsert(name: &[u8], manifest: &[u8]) -> Mutation {
        let name = name.to_vec();
        let manifest = manifest.to_vec();
        insert_or_update(ROOTS_TABLE, &["Name", "Manifest"], &[&name, &manifest])
    }

    fn root_delete(name: &[u8]) -> Mutation {
        let name = name.to_vec();
        delete(ROOTS_TABLE, Key::new(&name))
    }

    const DEFAULT_READ_PARALLELISM: usize = 16;
    const NODES_TABLE: &str = "ProllyNodes";
    const HINTS_TABLE: &str = "ProllyHints";
    const ROOTS_TABLE: &str = "ProllyRoots";

    /// Minimal GoogleSQL table layout for Spanner implementations.
    pub const SPANNER_SCHEMA: &str = "\
CREATE TABLE ProllyNodes (
  Cid BYTES(32) NOT NULL,
  Node BYTES(MAX) NOT NULL
) PRIMARY KEY (Cid);
CREATE TABLE ProllyHints (
  Namespace BYTES(MAX) NOT NULL,
  HintKey BYTES(MAX) NOT NULL,
  Value BYTES(MAX) NOT NULL
) PRIMARY KEY (Namespace, HintKey);
CREATE TABLE ProllyRoots (
  Name BYTES(MAX) NOT NULL,
  Manifest BYTES(MAX) NOT NULL
) PRIMARY KEY (Name);";
}

pub use spanner::*;