prolly-store-postgres 0.4.0

PostgreSQL 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
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
#![doc = include_str!("../README.md")]

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

/// Postgres adapter entry point.
pub mod postgres {
    use std::collections::{BTreeMap, BTreeSet};
    use std::num::NonZeroUsize;

    use sqlx::{PgConnection, PgPool, Row};

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

    /// Store adapter for PostgreSQL-backed prolly nodes and roots.
    pub type PostgresStore = crate::RemoteProllyStore<PostgresBackend>;

    /// PostgreSQL adapter tuning that does not change stored data.
    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    pub struct PostgresBackendOptions {
        max_batch_items: NonZeroUsize,
    }

    impl PostgresBackendOptions {
        /// Create options with the maximum number of items sent in one SQL batch.
        pub const fn new(max_batch_items: NonZeroUsize) -> Self {
            Self { max_batch_items }
        }

        /// Maximum number of items sent in one SQL batch.
        pub const fn max_batch_items(self) -> usize {
            self.max_batch_items.get()
        }
    }

    impl Default for PostgresBackendOptions {
        fn default() -> Self {
            Self::new(NonZeroUsize::new(1_024).expect("1024 is nonzero"))
        }
    }

    /// SQLx-backed PostgreSQL backend.
    #[derive(Clone, Debug)]
    pub struct PostgresBackend {
        pool: PgPool,
        options: PostgresBackendOptions,
    }

    impl PostgresBackend {
        /// Create a backend from an existing SQLx pool.
        pub fn new(pool: PgPool) -> Self {
            Self::new_with_options(pool, PostgresBackendOptions::default())
        }

        /// Create a backend from an existing SQLx pool and adapter options.
        pub fn new_with_options(pool: PgPool, options: PostgresBackendOptions) -> Self {
            Self { pool, options }
        }

        /// Connect to PostgreSQL using `database_url`.
        pub async fn connect(database_url: &str) -> Result<Self, sqlx::Error> {
            Self::connect_with_options(database_url, PostgresBackendOptions::default()).await
        }

        /// Connect to PostgreSQL using `database_url` and adapter options.
        pub async fn connect_with_options(
            database_url: &str,
            options: PostgresBackendOptions,
        ) -> Result<Self, sqlx::Error> {
            Ok(Self::new_with_options(
                PgPool::connect(database_url).await?,
                options,
            ))
        }

        /// Borrow the underlying pool.
        pub fn pool(&self) -> &PgPool {
            &self.pool
        }

        /// Return this backend's adapter options.
        pub const fn options(&self) -> PostgresBackendOptions {
            self.options
        }

        /// Create the required tables if they do not already exist.
        pub async fn initialize_schema(&self) -> Result<(), sqlx::Error> {
            execute_statements(&self.pool, POSTGRES_SCHEMA).await
        }
    }

    impl RemoteStoreBackend for PostgresBackend {
        type Error = sqlx::Error;

        async fn get_node(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
            sqlx::query("SELECT node FROM prolly_nodes WHERE cid = $1")
                .bind(key)
                .fetch_optional(&self.pool)
                .await?
                .map(|row| row.try_get("node"))
                .transpose()
        }

        async fn put_node(&self, key: &[u8], value: &[u8]) -> Result<(), Self::Error> {
            sqlx::query(
                "\
                INSERT INTO prolly_nodes (cid, node) VALUES ($1, $2) \
                ON CONFLICT(cid) DO UPDATE SET node = excluded.node",
            )
            .bind(key)
            .bind(value)
            .execute(&self.pool)
            .await?;
            Ok(())
        }

        async fn delete_node(&self, key: &[u8]) -> Result<(), Self::Error> {
            sqlx::query("DELETE FROM prolly_nodes WHERE cid = $1")
                .bind(key)
                .execute(&self.pool)
                .await?;
            Ok(())
        }

        async fn batch_nodes(&self, ops: &[RemoteBatchOp<'_>]) -> Result<(), Self::Error> {
            if ops.is_empty() {
                return Ok(());
            }
            let mut final_ops = BTreeMap::<Vec<u8>, Option<Vec<u8>>>::new();
            for op in ops {
                match op {
                    RemoteBatchOp::Upsert { key, value } => {
                        final_ops.insert((*key).to_vec(), Some((*value).to_vec()));
                    }
                    RemoteBatchOp::Delete { key } => {
                        final_ops.insert((*key).to_vec(), None);
                    }
                }
            }
            let deletes = final_ops
                .iter()
                .filter_map(|(key, value)| value.is_none().then_some(key.as_slice()))
                .collect::<Vec<_>>();
            let upserts = final_ops
                .iter()
                .filter_map(|(key, value)| value.as_deref().map(|value| (key.as_slice(), value)))
                .collect::<Vec<_>>();
            let mut tx = self.pool.begin().await?;
            delete_node_chunks(&mut tx, &deletes, self.options.max_batch_items()).await?;
            upsert_node_chunks(&mut tx, &upserts, self.options.max_batch_items()).await?;
            tx.commit().await
        }

        async fn batch_get_nodes_ordered(
            &self,
            keys: &[&[u8]],
        ) -> Result<Vec<Option<Vec<u8>>>, Self::Error> {
            if keys.is_empty() {
                return Ok(Vec::new());
            }
            let mut values = Vec::with_capacity(keys.len());
            for chunk in keys.chunks(self.options.max_batch_items()) {
                let requested = chunk.iter().map(|key| (*key).to_vec()).collect::<Vec<_>>();
                let rows = sqlx::query(
                    "\
                    SELECT requested.ord, nodes.node \
                    FROM unnest($1::bytea[]) WITH ORDINALITY AS requested(cid, ord) \
                    LEFT JOIN prolly_nodes AS nodes ON nodes.cid = requested.cid \
                    ORDER BY requested.ord",
                )
                .bind(requested)
                .fetch_all(&self.pool)
                .await?;
                for row in rows {
                    values.push(row.try_get::<Option<Vec<u8>>, _>("node")?);
                }
            }
            Ok(values)
        }

        async fn batch_put_nodes(&self, entries: &[(&[u8], &[u8])]) -> Result<(), Self::Error> {
            if entries.is_empty() {
                return Ok(());
            }
            let entries = deduplicate_entries(entries);
            let entries = entries
                .iter()
                .map(|(key, value)| (key.as_slice(), value.as_slice()))
                .collect::<Vec<_>>();
            let mut tx = self.pool.begin().await?;
            upsert_node_chunks(&mut tx, &entries, self.options.max_batch_items()).await?;
            tx.commit().await
        }

        async fn list_node_cids(&self) -> Result<Vec<Vec<u8>>, Self::Error> {
            let rows = sqlx::query("SELECT cid FROM prolly_nodes ORDER BY cid")
                .fetch_all(&self.pool)
                .await?;
            rows.into_iter().map(|row| row.try_get("cid")).collect()
        }

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

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

        async fn get_hint(
            &self,
            namespace: &[u8],
            key: &[u8],
        ) -> Result<Option<Vec<u8>>, Self::Error> {
            sqlx::query("SELECT value FROM prolly_hints WHERE namespace = $1 AND key = $2")
                .bind(namespace)
                .bind(key)
                .fetch_optional(&self.pool)
                .await?
                .map(|row| row.try_get("value"))
                .transpose()
        }

        async fn put_hint(
            &self,
            namespace: &[u8],
            key: &[u8],
            value: &[u8],
        ) -> Result<(), Self::Error> {
            sqlx::query(
                "\
                INSERT INTO prolly_hints (namespace, key, value) VALUES ($1, $2, $3) \
                ON CONFLICT(namespace, key) DO UPDATE SET value = excluded.value",
            )
            .bind(namespace)
            .bind(key)
            .bind(value)
            .execute(&self.pool)
            .await?;
            Ok(())
        }

        async fn batch_put_nodes_with_hint(
            &self,
            entries: &[(&[u8], &[u8])],
            namespace: &[u8],
            key: &[u8],
            value: &[u8],
        ) -> Result<(), Self::Error> {
            let entries = deduplicate_entries(entries);
            let entries = entries
                .iter()
                .map(|(key, value)| (key.as_slice(), value.as_slice()))
                .collect::<Vec<_>>();
            let mut tx = self.pool.begin().await?;
            upsert_node_chunks(&mut tx, &entries, self.options.max_batch_items()).await?;
            sqlx::query(
                "\
                INSERT INTO prolly_hints (namespace, key, value) VALUES ($1, $2, $3) \
                ON CONFLICT(namespace, key) DO UPDATE SET value = excluded.value",
            )
            .bind(namespace)
            .bind(key)
            .bind(value)
            .execute(&mut *tx)
            .await?;
            tx.commit().await
        }

        async fn get_root_manifest(&self, name: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
            sqlx::query("SELECT manifest FROM prolly_roots WHERE name = $1")
                .bind(name)
                .fetch_optional(&self.pool)
                .await?
                .map(|row| row.try_get("manifest"))
                .transpose()
        }

        async fn put_root_manifest(&self, name: &[u8], manifest: &[u8]) -> Result<(), Self::Error> {
            let mut tx = self.pool.begin().await?;
            lock_root_names(&mut tx, &[name.to_vec()]).await?;
            upsert_root_chunks(&mut tx, &[(name, manifest)], self.options.max_batch_items())
                .await?;
            tx.commit().await
        }

        async fn delete_root_manifest(&self, name: &[u8]) -> Result<(), Self::Error> {
            let mut tx = self.pool.begin().await?;
            lock_root_names(&mut tx, &[name.to_vec()]).await?;
            delete_root_chunks(&mut tx, &[name], self.options.max_batch_items()).await?;
            tx.commit().await
        }

        async fn compare_and_swap_root_manifest(
            &self,
            name: &[u8],
            expected: Option<&[u8]>,
            new: Option<&[u8]>,
        ) -> Result<RemoteManifestUpdate, Self::Error> {
            let mut tx = self.pool.begin().await?;
            lock_root_names(&mut tx, &[name.to_vec()]).await?;

            let current = sqlx::query("SELECT manifest FROM prolly_roots WHERE name = $1")
                .bind(name)
                .fetch_optional(&mut *tx)
                .await?
                .map(|row| row.try_get("manifest"))
                .transpose()?;
            if current.as_deref() != expected {
                tx.rollback().await?;
                return Ok(RemoteManifestUpdate::Conflict { current });
            }

            match new {
                Some(manifest) => {
                    upsert_root_chunks(
                        &mut tx,
                        &[(name, manifest)],
                        self.options.max_batch_items(),
                    )
                    .await?;
                }
                None => {
                    delete_root_chunks(&mut tx, &[name], self.options.max_batch_items()).await?;
                }
            }

            tx.commit().await?;
            Ok(RemoteManifestUpdate::Applied)
        }

        async fn list_root_manifests(&self) -> Result<Vec<RemoteNamedRoot>, Self::Error> {
            let rows = sqlx::query("SELECT name, manifest FROM prolly_roots ORDER BY name")
                .fetch_all(&self.pool)
                .await?;
            rows.into_iter()
                .map(|row| {
                    Ok(RemoteNamedRoot::new(
                        row.try_get("name")?,
                        row.try_get("manifest")?,
                    ))
                })
                .collect()
        }

        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 mut tx = self.pool.begin().await?;
            let root_names = root_names(root_conditions, root_writes);
            lock_root_names(&mut tx, &root_names).await?;
            let current_roots = read_root_manifests(&mut tx, &root_names).await?;

            for condition in root_conditions {
                let current = current_roots.get(&condition.name).cloned().unwrap_or(None);
                if current != condition.expected {
                    tx.rollback().await?;
                    return Ok(RemoteTransactionUpdate::Conflict(
                        RemoteTransactionConflict::new(
                            condition.name.clone(),
                            condition.expected.clone(),
                            current,
                        ),
                    ));
                }
            }

            let mut final_nodes = BTreeMap::<Vec<u8>, Option<Vec<u8>>>::new();
            for write in node_writes {
                match write {
                    RemoteBatchOp::Upsert { key, value } => {
                        final_nodes.insert((*key).to_vec(), Some((*value).to_vec()));
                    }
                    RemoteBatchOp::Delete { key } => {
                        final_nodes.insert((*key).to_vec(), None);
                    }
                };
            }
            let node_deletes = final_nodes
                .iter()
                .filter_map(|(key, value)| value.is_none().then_some(key.as_slice()))
                .collect::<Vec<_>>();
            let node_upserts = final_nodes
                .iter()
                .filter_map(|(key, value)| value.as_deref().map(|value| (key.as_slice(), value)))
                .collect::<Vec<_>>();
            delete_node_chunks(&mut tx, &node_deletes, self.options.max_batch_items()).await?;
            upsert_node_chunks(&mut tx, &node_upserts, self.options.max_batch_items()).await?;

            let mut final_roots = BTreeMap::<Vec<u8>, Option<Vec<u8>>>::new();
            for write in root_writes {
                match write {
                    RemoteRootWrite::Put { name, manifest } => {
                        final_roots.insert(name.clone(), Some(manifest.clone()));
                    }
                    RemoteRootWrite::Delete { name } => {
                        final_roots.insert(name.clone(), None);
                    }
                }
            }
            let root_deletes = final_roots
                .iter()
                .filter_map(|(name, manifest)| manifest.is_none().then_some(name.as_slice()))
                .collect::<Vec<_>>();
            let root_upserts = final_roots
                .iter()
                .filter_map(|(name, manifest)| {
                    manifest
                        .as_deref()
                        .map(|manifest| (name.as_slice(), manifest))
                })
                .collect::<Vec<_>>();
            delete_root_chunks(&mut tx, &root_deletes, self.options.max_batch_items()).await?;
            upsert_root_chunks(&mut tx, &root_upserts, self.options.max_batch_items()).await?;

            tx.commit().await?;
            Ok(RemoteTransactionUpdate::Applied)
        }
    }

    async fn upsert_node_chunks(
        connection: &mut PgConnection,
        entries: &[(&[u8], &[u8])],
        max_batch_items: usize,
    ) -> Result<(), sqlx::Error> {
        for chunk in entries.chunks(max_batch_items) {
            let keys = chunk
                .iter()
                .map(|(key, _)| (*key).to_vec())
                .collect::<Vec<_>>();
            let values = chunk
                .iter()
                .map(|(_, value)| (*value).to_vec())
                .collect::<Vec<_>>();
            sqlx::query(
                "\
                INSERT INTO prolly_nodes (cid, node) \
                SELECT input.cid, input.node \
                FROM unnest($1::bytea[], $2::bytea[]) AS input(cid, node) \
                ON CONFLICT(cid) DO UPDATE SET node = excluded.node",
            )
            .bind(keys)
            .bind(values)
            .execute(&mut *connection)
            .await?;
        }
        Ok(())
    }

    fn deduplicate_entries(entries: &[(&[u8], &[u8])]) -> BTreeMap<Vec<u8>, Vec<u8>> {
        entries
            .iter()
            .map(|(key, value)| ((*key).to_vec(), (*value).to_vec()))
            .collect()
    }

    async fn delete_node_chunks(
        connection: &mut PgConnection,
        keys: &[&[u8]],
        max_batch_items: usize,
    ) -> Result<(), sqlx::Error> {
        for chunk in keys.chunks(max_batch_items) {
            let keys = chunk.iter().map(|key| (*key).to_vec()).collect::<Vec<_>>();
            sqlx::query("DELETE FROM prolly_nodes WHERE cid = ANY($1::bytea[])")
                .bind(keys)
                .execute(&mut *connection)
                .await?;
        }
        Ok(())
    }

    async fn upsert_root_chunks(
        connection: &mut PgConnection,
        entries: &[(&[u8], &[u8])],
        max_batch_items: usize,
    ) -> Result<(), sqlx::Error> {
        for chunk in entries.chunks(max_batch_items) {
            let names = chunk
                .iter()
                .map(|(name, _)| (*name).to_vec())
                .collect::<Vec<_>>();
            let manifests = chunk
                .iter()
                .map(|(_, manifest)| (*manifest).to_vec())
                .collect::<Vec<_>>();
            sqlx::query(
                "\
                INSERT INTO prolly_roots (name, manifest) \
                SELECT input.name, input.manifest \
                FROM unnest($1::bytea[], $2::bytea[]) AS input(name, manifest) \
                ON CONFLICT(name) DO UPDATE SET manifest = excluded.manifest",
            )
            .bind(names)
            .bind(manifests)
            .execute(&mut *connection)
            .await?;
        }
        Ok(())
    }

    async fn delete_root_chunks(
        connection: &mut PgConnection,
        names: &[&[u8]],
        max_batch_items: usize,
    ) -> Result<(), sqlx::Error> {
        for chunk in names.chunks(max_batch_items) {
            let names = chunk
                .iter()
                .map(|name| (*name).to_vec())
                .collect::<Vec<_>>();
            sqlx::query("DELETE FROM prolly_roots WHERE name = ANY($1::bytea[])")
                .bind(names)
                .execute(&mut *connection)
                .await?;
        }
        Ok(())
    }

    fn root_names(conditions: &[RemoteRootCondition], writes: &[RemoteRootWrite]) -> Vec<Vec<u8>> {
        let mut names = BTreeSet::new();
        names.extend(conditions.iter().map(|condition| condition.name.clone()));
        names.extend(writes.iter().map(|write| match write {
            RemoteRootWrite::Put { name, .. } | RemoteRootWrite::Delete { name } => name.clone(),
        }));
        names.into_iter().collect()
    }

    async fn lock_root_names(
        connection: &mut PgConnection,
        names: &[Vec<u8>],
    ) -> Result<(), sqlx::Error> {
        for name in names {
            sqlx::query(
                "\
                SELECT pg_advisory_xact_lock( \
                    hashtextextended('prolly-root-v1:' || encode($1::bytea, 'hex'), 0) \
                )",
            )
            .bind(name)
            .execute(&mut *connection)
            .await?;
        }
        Ok(())
    }

    async fn read_root_manifests(
        connection: &mut PgConnection,
        names: &[Vec<u8>],
    ) -> Result<BTreeMap<Vec<u8>, Option<Vec<u8>>>, sqlx::Error> {
        if names.is_empty() {
            return Ok(BTreeMap::new());
        }
        let rows = sqlx::query(
            "\
            SELECT requested.name, roots.manifest \
            FROM unnest($1::bytea[]) AS requested(name) \
            LEFT JOIN prolly_roots AS roots ON roots.name = requested.name",
        )
        .bind(names)
        .fetch_all(&mut *connection)
        .await?;
        rows.into_iter()
            .map(|row| {
                Ok((
                    row.try_get::<Vec<u8>, _>("name")?,
                    row.try_get::<Option<Vec<u8>>, _>("manifest")?,
                ))
            })
            .collect()
    }

    async fn execute_statements(pool: &PgPool, sql: &str) -> Result<(), sqlx::Error> {
        for statement in sql
            .split(';')
            .map(str::trim)
            .filter(|stmt| !stmt.is_empty())
        {
            sqlx::query(statement).execute(pool).await?;
        }
        Ok(())
    }

    /// Minimal table layout for PostgreSQL implementations.
    pub const POSTGRES_SCHEMA: &str = "\
CREATE TABLE IF NOT EXISTS prolly_nodes (
  cid bytea PRIMARY KEY,
  node bytea NOT NULL
);
CREATE TABLE IF NOT EXISTS prolly_hints (
  namespace bytea NOT NULL,
  key bytea NOT NULL,
  value bytea NOT NULL,
  PRIMARY KEY(namespace, key)
);
CREATE TABLE IF NOT EXISTS prolly_roots (
  name bytea PRIMARY KEY,
  manifest bytea NOT NULL
);";
}

pub use postgres::*;