sbol-db-postgres 0.1.8

Postgres persistence layer for sbol-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
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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
//! The SynBioHub query accelerator for Postgres: per-graph derived indexes that
//! answer the fixed SynBioHub query templates with point lookups and range scans
//! instead of graph-pattern evaluation.
//!
//! The indexes are derived from a graph's triples (via the backend-neutral
//! [`build_accel_index`]) and maintained synchronously on the write path:
//! [`AccelRepository::refresh_graph`] rebuilds a graph's indexes inside the
//! write's own transaction, scanning the triples through that transaction's
//! connection so the indexes it writes reflect the triples the same transaction
//! wrote. Indexes and triples therefore commit together, and reads (which never
//! rebuild) always see indexes consistent with the committed triples.

use std::collections::{BTreeMap, HashMap, HashSet};

use sbol_db_core::DomainError;
use sbol_db_storage::{
    build_catalog_projection, generate_metadata_rows, generate_rows, integer,
    merge_resource_occurrences, AccelSolutions, AcceleratedQuery, CatalogSequenceRecord,
    CursorPage, FacetKind, Field, MetaRecord, ResourceOccurrence, ResourceQuery, ResourceRecord,
    Scope, SequenceQuery, TermValue,
};
use sqlx::{QueryBuilder, Row};

use crate::repo::db_err;
use crate::repo::triple::TripleRepository;
use crate::PgPool;

const FK_TYPES: i16 = 1;
const FK_ROLES: i16 = 2;
const FK_CREATORS: i16 = 3;

const XSD_STRING: &str = "http://www.w3.org/2001/XMLSchema#string";
/// Bound the Postgres bind message while rebuilding production-scale graphs.
/// A graph can derive millions of type/member rows; sending every array in one
/// `UNNEST` risks PostgreSQL's one-gigabyte protocol/message ceiling.
const INSERT_CHUNK: usize = 5_000;

/// Derives, maintains, and queries the accelerator indexes for the Postgres
/// backend.
#[derive(Clone)]
pub struct AccelRepository {
    pool: PgPool,
    triples: TripleRepository,
}

impl AccelRepository {
    pub fn new(pool: PgPool, triples: TripleRepository) -> Self {
        Self { pool, triples }
    }

    /// Answer a recognized query from the graph's accelerator indexes, which the
    /// write path keeps in sync with the committed triples (see
    /// [`Self::refresh_graph`]).
    pub async fn run(&self, query: &AcceleratedQuery) -> Result<AccelSolutions, DomainError> {
        match query {
            AcceleratedQuery::ObjectList {
                graph,
                scope,
                projection,
                offset,
                limit,
                subject_prefix,
            } => {
                self.object_list(
                    graph,
                    scope,
                    projection,
                    *offset,
                    *limit,
                    subject_prefix.as_deref(),
                )
                .await
            }
            AcceleratedQuery::Count {
                graph,
                scope,
                var,
                subject_prefix,
            } => {
                self.count(graph, scope, var, subject_prefix.as_deref())
                    .await
            }
            AcceleratedQuery::Facet { graph, kind, var } => self.facet(graph, *kind, var).await,
            AcceleratedQuery::FacetCounts {
                graph,
                kind,
                value_var,
                count_var,
            } => self.facet_counts(graph, *kind, value_var, count_var).await,
            AcceleratedQuery::ObjectMetadata {
                graph,
                subject,
                projection,
                required,
            } => {
                self.object_metadata(graph, subject, projection, required)
                    .await
            }
        }
    }

    /// Every occurrence of one resource, ordered by graph IRI. The graph-local
    /// accelerator metadata is the universal RDF-derived resource catalog.
    pub async fn resource_occurrences(
        &self,
        iri: &str,
    ) -> Result<Vec<ResourceOccurrence>, DomainError> {
        let rows = sqlx::query(
            "SELECT graph_iri, meta FROM accel_object WHERE iri = $1 ORDER BY graph_iri",
        )
        .bind(iri)
        .fetch_all(&self.pool)
        .await
        .map_err(db_err)?;
        rows.into_iter()
            .map(|row| {
                let graph_iri: String = row.try_get("graph_iri").map_err(db_err)?;
                let meta: String = row.try_get("meta").map_err(db_err)?;
                Ok(ResourceOccurrence {
                    graph_iri,
                    resource_iri: iri.to_owned(),
                    meta: serde_json::from_str(&meta).map_err(db_err)?,
                })
            })
            .collect()
    }

    pub async fn resource(&self, iri: &str) -> Result<Option<ResourceRecord>, DomainError> {
        let occurrences = self.resource_occurrences(iri).await?;
        Ok(merge_resource_occurrences(iri, &occurrences))
    }

    /// Globally keyset-page unique resource IRIs, applying filters before the
    /// limit. Only the returned page is expanded to occurrence metadata.
    pub async fn resources(
        &self,
        query: &ResourceQuery,
    ) -> Result<CursorPage<ResourceRecord>, DomainError> {
        let limit = query.limit.clamp(1, 500) as i64;
        let mut qb: QueryBuilder<sqlx::Postgres> =
            QueryBuilder::new("SELECT DISTINCT o.iri FROM accel_object o WHERE TRUE");
        if let Some(after) = &query.after {
            qb.push(" AND o.iri > ").push_bind(after.clone());
        }
        if let Some(graph) = &query.graph_iri {
            qb.push(" AND o.graph_iri = ").push_bind(graph.clone());
        }
        if let Some(class) = &query.class {
            qb.push(
                " AND EXISTS (SELECT 1 FROM accel_type ty \
                 WHERE ty.graph_iri = o.graph_iri AND ty.iri = o.iri AND ty.type_iri = ",
            )
            .push_bind(class.clone())
            .push(")");
        }
        if let Some(role) = &query.role {
            qb.push(
                " AND EXISTS (SELECT 1 FROM accel_role r \
                 WHERE r.graph_iri = o.graph_iri AND r.iri = o.iri AND r.role_iri = ",
            )
            .push_bind(role.clone())
            .push(")");
        }
        if let Some(text) = query.text.as_deref().filter(|text| !text.is_empty()) {
            let needle = format!("%{text}%");
            qb.push(" AND (o.iri ILIKE ")
                .push_bind(needle.clone())
                .push(" OR o.meta ILIKE ")
                .push_bind(needle)
                .push(")");
        }
        qb.push(" ORDER BY o.iri LIMIT ").push_bind(limit + 1);
        let mut iris: Vec<String> = qb
            .build_query_scalar()
            .fetch_all(&self.pool)
            .await
            .map_err(db_err)?;
        let has_more = iris.len() > limit as usize;
        iris.truncate(limit as usize);
        let next_cursor = has_more.then(|| iris.last().cloned()).flatten();
        let items = self.resources_for_iris(&iris).await?;
        Ok(CursorPage { items, next_cursor })
    }

    /// Expand a page of resource identities in one query. Keeping this separate
    /// from the keyset query avoids one database round trip per list item.
    pub async fn resources_for_iris(
        &self,
        iris: &[String],
    ) -> Result<Vec<ResourceRecord>, DomainError> {
        if iris.is_empty() {
            return Ok(Vec::new());
        }
        let mut qb: QueryBuilder<sqlx::Postgres> =
            QueryBuilder::new("SELECT iri, graph_iri, meta FROM accel_object WHERE iri IN (");
        {
            let mut values = qb.separated(", ");
            for iri in iris {
                values.push_bind(iri);
            }
        }
        qb.push(") ORDER BY iri, graph_iri");
        let rows = qb.build().fetch_all(&self.pool).await.map_err(db_err)?;
        let mut by_iri: BTreeMap<String, Vec<ResourceOccurrence>> = BTreeMap::new();
        for row in rows {
            let iri: String = row.try_get("iri").map_err(db_err)?;
            let graph_iri: String = row.try_get("graph_iri").map_err(db_err)?;
            let meta: String = row.try_get("meta").map_err(db_err)?;
            by_iri
                .entry(iri.clone())
                .or_default()
                .push(ResourceOccurrence {
                    graph_iri,
                    resource_iri: iri,
                    meta: serde_json::from_str(&meta).map_err(db_err)?,
                });
        }
        Ok(iris
            .iter()
            .filter_map(|iri| {
                by_iri
                    .get(iri)
                    .and_then(|occurrences| merge_resource_occurrences(iri, occurrences))
            })
            .collect())
    }

    pub async fn sequence(&self, iri: &str) -> Result<Option<CatalogSequenceRecord>, DomainError> {
        let graph_count: i64 = sqlx::query_scalar(
            "SELECT count(DISTINCT graph_iri) FROM accel_type WHERE iri = $1 \
             AND type_iri IN ('http://sbols.org/v2#Sequence', 'http://sbols.org/v3#Sequence')",
        )
        .bind(iri)
        .fetch_one(&self.pool)
        .await
        .map_err(db_err)?;
        if graph_count == 0 {
            return Ok(None);
        }
        let rows = sqlx::query(
            r#"
            SELECT predicate_iri::text AS predicate_iri,
                   object_iri::text AS object_iri, object_literal
            FROM sbol_triples
            WHERE subject_iri = $1
              AND predicate_iri IN (
                'http://sbols.org/v2#elements', 'http://sbols.org/v3#elements',
                'http://sbols.org/v2#encoding', 'http://sbols.org/v3#encoding'
              )
            ORDER BY graph_iri NULLS LAST, id
            "#,
        )
        .bind(iri)
        .fetch_all(&self.pool)
        .await
        .map_err(db_err)?;
        let mut elements = None;
        let mut encoding_iri = None;
        for row in rows {
            let predicate: String = row.try_get("predicate_iri").map_err(db_err)?;
            if predicate.ends_with("#elements") && elements.is_none() {
                elements = row.try_get("object_literal").map_err(db_err)?;
            } else if predicate.ends_with("#encoding") && encoding_iri.is_none() {
                encoding_iri = row.try_get("object_iri").map_err(db_err)?;
            }
        }
        Ok(Some(CatalogSequenceRecord {
            iri: iri.to_owned(),
            graph_count: graph_count as u64,
            alphabet: catalog_alphabet(encoding_iri.as_deref()),
            encoding_iri,
            elements,
        }))
    }

    pub async fn sequences(
        &self,
        query: &SequenceQuery,
    ) -> Result<CursorPage<CatalogSequenceRecord>, DomainError> {
        let limit = query.limit.clamp(1, 500) as i64;
        let mut qb: QueryBuilder<sqlx::Postgres> = QueryBuilder::new(
            "SELECT DISTINCT ty.iri FROM accel_type ty WHERE ty.type_iri IN \
             ('http://sbols.org/v2#Sequence', 'http://sbols.org/v3#Sequence')",
        );
        if let Some(after) = &query.after {
            qb.push(" AND ty.iri > ").push_bind(after.clone());
        }
        if let Some(text) = query.text.as_deref().filter(|value| !value.is_empty()) {
            qb.push(
                " AND EXISTS (SELECT 1 FROM accel_object o WHERE o.iri = ty.iri \
                     AND (o.iri ILIKE ",
            )
            .push_bind(format!("%{text}%"))
            .push(" OR o.meta ILIKE ")
            .push_bind(format!("%{text}%"))
            .push("))");
        }
        qb.push(" ORDER BY ty.iri LIMIT ").push_bind(limit + 1);
        let mut iris: Vec<String> = qb
            .build_query_scalar()
            .fetch_all(&self.pool)
            .await
            .map_err(db_err)?;
        let has_more = iris.len() > limit as usize;
        iris.truncate(limit as usize);
        let next_cursor = has_more.then(|| iris.last().cloned()).flatten();
        let items = self.sequences_for_iris(&iris).await?;
        Ok(CursorPage { items, next_cursor })
    }

    /// Expand sequence counts and values with two set queries, independent of
    /// page size. The first value in graph/id order remains the representative.
    async fn sequences_for_iris(
        &self,
        iris: &[String],
    ) -> Result<Vec<CatalogSequenceRecord>, DomainError> {
        if iris.is_empty() {
            return Ok(Vec::new());
        }
        let mut count_qb: QueryBuilder<sqlx::Postgres> = QueryBuilder::new(
            "SELECT iri, count(DISTINCT graph_iri) AS graph_count FROM accel_type \
             WHERE type_iri IN ('http://sbols.org/v2#Sequence', 'http://sbols.org/v3#Sequence') \
             AND iri IN (",
        );
        {
            let mut values = count_qb.separated(", ");
            for iri in iris {
                values.push_bind(iri);
            }
        }
        count_qb.push(") GROUP BY iri");
        let count_rows = count_qb
            .build()
            .fetch_all(&self.pool)
            .await
            .map_err(db_err)?;
        let mut records: HashMap<String, (u64, Option<String>, Option<String>)> = HashMap::new();
        for row in count_rows {
            let iri: String = row.try_get("iri").map_err(db_err)?;
            let graph_count: i64 = row.try_get("graph_count").map_err(db_err)?;
            records.insert(iri, (graph_count as u64, None, None));
        }

        let mut value_qb: QueryBuilder<sqlx::Postgres> = QueryBuilder::new(
            "SELECT subject_iri::text AS subject_iri, predicate_iri::text AS predicate_iri, \
                    object_iri::text AS object_iri, object_literal \
             FROM sbol_triples WHERE predicate_iri IN (\
               'http://sbols.org/v2#elements', 'http://sbols.org/v3#elements', \
               'http://sbols.org/v2#encoding', 'http://sbols.org/v3#encoding'\
             ) AND subject_iri IN (",
        );
        {
            let mut values = value_qb.separated(", ");
            for iri in iris {
                values.push_bind(iri);
            }
        }
        value_qb.push(") ORDER BY subject_iri, graph_iri NULLS LAST, id");
        let value_rows = value_qb
            .build()
            .fetch_all(&self.pool)
            .await
            .map_err(db_err)?;
        for row in value_rows {
            let iri: String = row.try_get("subject_iri").map_err(db_err)?;
            let predicate: String = row.try_get("predicate_iri").map_err(db_err)?;
            let Some((_, elements, encoding)) = records.get_mut(&iri) else {
                continue;
            };
            if predicate.ends_with("#elements") && elements.is_none() {
                *elements = row.try_get("object_literal").map_err(db_err)?;
            } else if predicate.ends_with("#encoding") && encoding.is_none() {
                *encoding = row.try_get("object_iri").map_err(db_err)?;
            }
        }

        Ok(iris
            .iter()
            .filter_map(|iri| {
                records
                    .remove(iri)
                    .map(
                        |(graph_count, elements, encoding_iri)| CatalogSequenceRecord {
                            iri: iri.clone(),
                            graph_count,
                            alphabet: catalog_alphabet(encoding_iri.as_deref()),
                            encoding_iri,
                            elements,
                        },
                    )
            })
            .collect())
    }

    /// Rebuild a graph's accelerator indexes from its triples, inside the
    /// caller's write transaction (atomic with the triple write). The triple
    /// scan runs through `conn`, so it sees the triples the same transaction
    /// just wrote; the rebuilt indexes are deleted and reinserted on `conn` and
    /// commit together with the triples. Callers invoke this after every write
    /// that changes a graph's triples.
    pub async fn refresh_graph(
        &self,
        conn: &mut sqlx::PgConnection,
        graph: &str,
    ) -> Result<(), DomainError> {
        let triples = self.triples.scan_graph_in_conn(conn, graph).await?;
        let index = build_catalog_projection(&triples);

        let sort_of: HashMap<&str, &str> = index
            .objects
            .iter()
            .map(|o| (o.iri.as_str(), o.meta.sort_key()))
            .collect();
        let root_set: HashSet<(&str, &str)> = index
            .root_members
            .iter()
            .map(|(c, m)| (c.as_str(), m.as_str()))
            .collect();

        let mut obj_iri: Vec<String> = Vec::new();
        let mut obj_sort: Vec<String> = Vec::new();
        let mut obj_top: Vec<bool> = Vec::new();
        let mut obj_meta: Vec<String> = Vec::new();
        let mut ty_type: Vec<String> = Vec::new();
        let mut ty_iri: Vec<String> = Vec::new();
        let mut ty_sort: Vec<String> = Vec::new();
        let mut role_role: Vec<String> = Vec::new();
        let mut role_iri: Vec<String> = Vec::new();
        let mut role_sort: Vec<String> = Vec::new();
        let mut facet_counts: HashMap<(i16, String), i64> = HashMap::new();
        for obj in &index.objects {
            let iri = obj.iri.as_str();
            let m = &obj.meta;
            let sort = m.sort_key();
            obj_iri.push(iri.to_owned());
            obj_sort.push(sort.to_owned());
            obj_top.push(m.top_level);
            obj_meta.push(serde_json::to_string(m).map_err(db_err)?);
            for t in &m.types {
                ty_type.push(t.clone());
                ty_iri.push(iri.to_owned());
                ty_sort.push(sort.to_owned());
            }
            for role in &m.roles {
                role_role.push(role.clone());
                role_iri.push(iri.to_owned());
                role_sort.push(sort.to_owned());
            }
            if m.top_level {
                let mut object_facets: HashSet<(i16, &str)> = HashSet::new();
                for t in &m.types {
                    object_facets.insert((FK_TYPES, t.as_str()));
                }
                for r in &m.roles {
                    object_facets.insert((FK_ROLES, r.as_str()));
                }
                for c in &m.creators {
                    object_facets.insert((FK_CREATORS, c.as_str()));
                }
                for (kind, value) in object_facets {
                    *facet_counts.entry((kind, value.to_owned())).or_default() += 1;
                }
            }
        }
        let mut facets: Vec<(i16, String, i64)> = facet_counts
            .into_iter()
            .map(|((kind, value), count)| (kind, value, count))
            .collect();
        facets.sort();
        let facet_kind: Vec<i16> = facets.iter().map(|(kind, _, _)| *kind).collect();
        let facet_value: Vec<String> = facets.iter().map(|(_, value, _)| value.clone()).collect();
        let facet_count: Vec<i64> = facets.iter().map(|(_, _, count)| *count).collect();

        let mut mem_coll: Vec<String> = Vec::new();
        let mut mem_iri: Vec<String> = Vec::new();
        let mut mem_sort: Vec<String> = Vec::new();
        let mut mem_root: Vec<bool> = Vec::new();
        for (collection, member) in &index.members {
            mem_coll.push(collection.clone());
            mem_iri.push(member.clone());
            mem_sort.push(
                sort_of
                    .get(member.as_str())
                    .copied()
                    .unwrap_or("")
                    .to_owned(),
            );
            mem_root.push(root_set.contains(&(collection.as_str(), member.as_str())));
        }

        for table in [
            "accel_object",
            "accel_type",
            "accel_role",
            "accel_member",
            "accel_facet",
        ] {
            sqlx::query(&format!("DELETE FROM {table} WHERE graph_iri = $1"))
                .bind(graph)
                .execute(&mut *conn)
                .await
                .map_err(db_err)?;
        }
        for start in (0..obj_iri.len()).step_by(INSERT_CHUNK) {
            let end = (start + INSERT_CHUNK).min(obj_iri.len());
            sqlx::query(
                "INSERT INTO accel_object (graph_iri, iri, sort_key, top_level, meta)
                 SELECT $1, iri, sort_key, top_level, meta
                 FROM UNNEST($2::text[], $3::text[], $4::bool[], $5::text[])
                    AS u(iri, sort_key, top_level, meta)",
            )
            .bind(graph)
            .bind(&obj_iri[start..end])
            .bind(&obj_sort[start..end])
            .bind(&obj_top[start..end])
            .bind(&obj_meta[start..end])
            .execute(&mut *conn)
            .await
            .map_err(db_err)?;
        }
        for start in (0..ty_iri.len()).step_by(INSERT_CHUNK) {
            let end = (start + INSERT_CHUNK).min(ty_iri.len());
            sqlx::query(
                "INSERT INTO accel_type (graph_iri, type_iri, iri, sort_key)
                 SELECT $1, type_iri, iri, sort_key
                 FROM UNNEST($2::text[], $3::text[], $4::text[])
                    AS u(type_iri, iri, sort_key)
                 ON CONFLICT DO NOTHING",
            )
            .bind(graph)
            .bind(&ty_type[start..end])
            .bind(&ty_iri[start..end])
            .bind(&ty_sort[start..end])
            .execute(&mut *conn)
            .await
            .map_err(db_err)?;
        }
        for start in (0..role_iri.len()).step_by(INSERT_CHUNK) {
            let end = (start + INSERT_CHUNK).min(role_iri.len());
            sqlx::query(
                "INSERT INTO accel_role (graph_iri, role_iri, iri, sort_key)
                 SELECT $1, role_iri, iri, sort_key
                 FROM UNNEST($2::text[], $3::text[], $4::text[])
                    AS u(role_iri, iri, sort_key)
                 ON CONFLICT DO NOTHING",
            )
            .bind(graph)
            .bind(&role_role[start..end])
            .bind(&role_iri[start..end])
            .bind(&role_sort[start..end])
            .execute(&mut *conn)
            .await
            .map_err(db_err)?;
        }
        for start in (0..mem_iri.len()).step_by(INSERT_CHUNK) {
            let end = (start + INSERT_CHUNK).min(mem_iri.len());
            sqlx::query(
                "INSERT INTO accel_member (graph_iri, collection_iri, member_iri, sort_key, is_root)
                 SELECT $1, collection_iri, member_iri, sort_key, is_root
                 FROM UNNEST($2::text[], $3::text[], $4::text[], $5::bool[])
                    AS u(collection_iri, member_iri, sort_key, is_root)
                 ON CONFLICT DO NOTHING",
            )
            .bind(graph)
            .bind(&mem_coll[start..end])
            .bind(&mem_iri[start..end])
            .bind(&mem_sort[start..end])
            .bind(&mem_root[start..end])
            .execute(&mut *conn)
            .await
            .map_err(db_err)?;
        }
        for start in (0..facet_value.len()).step_by(INSERT_CHUNK) {
            let end = (start + INSERT_CHUNK).min(facet_value.len());
            sqlx::query(
                "INSERT INTO accel_facet (graph_iri, kind, value, subject_count)
                 SELECT $1, kind, value, subject_count
                 FROM UNNEST($2::smallint[], $3::text[], $4::bigint[])
                    AS u(kind, value, subject_count)
                 ON CONFLICT DO NOTHING",
            )
            .bind(graph)
            .bind(&facet_kind[start..end])
            .bind(&facet_value[start..end])
            .bind(&facet_count[start..end])
            .execute(&mut *conn)
            .await
            .map_err(db_err)?;
        }
        Ok(())
    }

    async fn object_list(
        &self,
        graph: &str,
        scope: &Scope,
        projection: &[(String, sbol_db_storage::Field)],
        offset: usize,
        limit: Option<usize>,
        subject_prefix: Option<&str>,
    ) -> Result<AccelSolutions, DomainError> {
        let candidates = self
            .scope_candidates(graph, scope, subject_prefix, offset, limit)
            .await?;

        let vars: Vec<String> = projection.iter().map(|(v, _)| v.clone()).collect();
        // Generate rows in displayId order, dedup as we go, and stop once we have
        // enough for the requested page (objects are visited in order, and each
        // yields at least one row, so a row's position is fixed by its object).
        let target = limit.map(|l| offset + l);
        let mut seen = HashSet::new();
        let mut rows: Vec<Vec<Option<TermValue>>> = Vec::new();
        let mut object_rows = Vec::new();
        for (iri, meta) in &candidates {
            object_rows.clear();
            generate_rows(iri, meta, projection, &mut object_rows);
            for row in object_rows.drain(..) {
                if seen.insert(format!("{row:?}")) {
                    rows.push(row);
                }
            }
            if target.is_some_and(|t| rows.len() >= t) {
                break;
            }
        }
        let rows = rows
            .into_iter()
            .skip(offset)
            .take(limit.unwrap_or(usize::MAX))
            .collect();
        Ok(AccelSolutions { vars, rows })
    }

    /// The objects in `scope`, in displayId order, each with its metadata. A
    /// member with no metadata (an external reference) is returned with a default
    /// (empty) record, so it still yields a subject-only row. When a row limit is
    /// set, the scan is capped to `offset + limit` objects: every object yields at
    /// least one distinct row, so that many objects suffice to fill the page.
    async fn scope_candidates(
        &self,
        graph: &str,
        scope: &Scope,
        subject_prefix: Option<&str>,
        offset: usize,
        limit: Option<usize>,
    ) -> Result<Vec<(String, MetaRecord)>, DomainError> {
        let mut qb: QueryBuilder<sqlx::Postgres> = QueryBuilder::new("");
        match scope {
            Scope::TopLevel => {
                qb.push("SELECT iri, meta FROM accel_object WHERE graph_iri = ");
                qb.push_bind(graph.to_owned());
                qb.push(" AND top_level");
                if let Some(p) = subject_prefix {
                    qb.push(" AND iri LIKE ").push_bind(like_prefix(p));
                }
                qb.push(
                    " ORDER BY left(sort_key, 128) COLLATE \"C\", sort_key COLLATE \"C\", \
                              left(iri, 128) COLLATE \"C\", iri COLLATE \"C\"",
                );
            }
            Scope::ByType(t) => {
                qb.push(
                    "SELECT ty.iri AS iri, o.meta AS meta FROM accel_type ty \
                     LEFT JOIN accel_object o \
                       ON o.graph_iri = ty.graph_iri AND o.iri = ty.iri \
                     WHERE ty.graph_iri = ",
                );
                qb.push_bind(graph.to_owned());
                qb.push(" AND ty.type_iri = ").push_bind(t.clone());
                if let Some(p) = subject_prefix {
                    qb.push(" AND ty.iri LIKE ").push_bind(like_prefix(p));
                }
                qb.push(
                    " ORDER BY left(ty.sort_key, 128) COLLATE \"C\", ty.sort_key COLLATE \"C\", \
                              left(ty.iri, 128) COLLATE \"C\", ty.iri COLLATE \"C\"",
                );
            }
            Scope::TopLevelByType(t) => {
                qb.push(
                    "SELECT ty.iri AS iri, o.meta AS meta FROM accel_type ty \
                     JOIN accel_object o \
                       ON o.graph_iri = ty.graph_iri AND o.iri = ty.iri \
                     WHERE ty.graph_iri = ",
                );
                qb.push_bind(graph.to_owned());
                qb.push(" AND ty.type_iri = ").push_bind(t.clone());
                qb.push(" AND o.top_level");
                if let Some(p) = subject_prefix {
                    qb.push(" AND ty.iri LIKE ").push_bind(like_prefix(p));
                }
                qb.push(
                    " ORDER BY left(ty.sort_key, 128) COLLATE \"C\", ty.sort_key COLLATE \"C\", \
                              left(ty.iri, 128) COLLATE \"C\", ty.iri COLLATE \"C\"",
                );
            }
            Scope::TopLevelByRole(role) => {
                qb.push(
                    "SELECT r.iri AS iri, o.meta AS meta FROM accel_role r \
                     JOIN accel_object o \
                       ON o.graph_iri = r.graph_iri AND o.iri = r.iri \
                     WHERE r.graph_iri = ",
                );
                qb.push_bind(graph.to_owned());
                qb.push(" AND r.role_iri = ").push_bind(role.clone());
                qb.push(" AND o.top_level");
                if let Some(p) = subject_prefix {
                    qb.push(" AND r.iri LIKE ").push_bind(like_prefix(p));
                }
                qb.push(
                    " ORDER BY left(r.sort_key, 128) COLLATE \"C\", r.sort_key COLLATE \"C\", \
                              left(r.iri, 128) COLLATE \"C\", r.iri COLLATE \"C\"",
                );
            }
            Scope::TopLevelByTypeAndRole { object_type, role } => {
                qb.push(
                    "SELECT r.iri AS iri, o.meta AS meta FROM accel_role r \
                     JOIN accel_type ty \
                       ON ty.graph_iri = r.graph_iri AND ty.iri = r.iri \
                     JOIN accel_object o \
                       ON o.graph_iri = r.graph_iri AND o.iri = r.iri \
                     WHERE r.graph_iri = ",
                );
                qb.push_bind(graph.to_owned());
                qb.push(" AND r.role_iri = ").push_bind(role.clone());
                qb.push(" AND ty.type_iri = ")
                    .push_bind(object_type.clone());
                qb.push(" AND o.top_level");
                if let Some(p) = subject_prefix {
                    qb.push(" AND r.iri LIKE ").push_bind(like_prefix(p));
                }
                qb.push(
                    " ORDER BY left(r.sort_key, 128) COLLATE \"C\", r.sort_key COLLATE \"C\", \
                              left(r.iri, 128) COLLATE \"C\", r.iri COLLATE \"C\"",
                );
            }
            Scope::RootByType(t) => {
                qb.push(
                    "SELECT ty.iri AS iri, o.meta AS meta FROM accel_type ty \
                     LEFT JOIN accel_object o \
                       ON o.graph_iri = ty.graph_iri AND o.iri = ty.iri \
                     WHERE ty.graph_iri = ",
                );
                qb.push_bind(graph.to_owned());
                qb.push(" AND ty.type_iri = ").push_bind(t.clone());
                qb.push(
                    " AND NOT EXISTS (SELECT 1 FROM accel_member m \
                       WHERE m.graph_iri = ty.graph_iri AND m.member_iri = ty.iri)",
                );
                if let Some(p) = subject_prefix {
                    qb.push(" AND ty.iri LIKE ").push_bind(like_prefix(p));
                }
                qb.push(
                    " ORDER BY left(ty.sort_key, 128) COLLATE \"C\", ty.sort_key COLLATE \"C\", \
                              left(ty.iri, 128) COLLATE \"C\", ty.iri COLLATE \"C\"",
                );
            }
            Scope::RootTopLevelByType(t) => {
                qb.push(
                    "SELECT ty.iri AS iri, o.meta AS meta FROM accel_type ty \
                     JOIN accel_object o \
                       ON o.graph_iri = ty.graph_iri AND o.iri = ty.iri \
                     WHERE ty.graph_iri = ",
                );
                qb.push_bind(graph.to_owned());
                qb.push(" AND ty.type_iri = ").push_bind(t.clone());
                qb.push(" AND o.top_level");
                qb.push(
                    " AND NOT EXISTS (SELECT 1 FROM accel_member m \
                       WHERE m.graph_iri = ty.graph_iri AND m.member_iri = ty.iri)",
                );
                if let Some(p) = subject_prefix {
                    qb.push(" AND ty.iri LIKE ").push_bind(like_prefix(p));
                }
                qb.push(
                    " ORDER BY left(ty.sort_key, 128) COLLATE \"C\", ty.sort_key COLLATE \"C\", \
                              left(ty.iri, 128) COLLATE \"C\", ty.iri COLLATE \"C\"",
                );
            }
            Scope::Collection {
                collection,
                root_only,
            } => {
                qb.push(
                    "SELECT m.member_iri AS iri, o.meta AS meta FROM accel_member m \
                     LEFT JOIN accel_object o \
                       ON o.graph_iri = m.graph_iri AND o.iri = m.member_iri \
                     WHERE m.graph_iri = ",
                );
                qb.push_bind(graph.to_owned());
                qb.push(" AND m.collection_iri = ")
                    .push_bind(collection.clone());
                if *root_only {
                    qb.push(" AND m.is_root");
                }
                if let Some(p) = subject_prefix {
                    qb.push(" AND m.member_iri LIKE ").push_bind(like_prefix(p));
                }
                qb.push(
                    " ORDER BY left(m.sort_key, 128) COLLATE \"C\", m.sort_key COLLATE \"C\", \
                              left(m.member_iri, 128) COLLATE \"C\", m.member_iri COLLATE \"C\"",
                );
            }
        }
        if let Some(l) = limit {
            qb.push(" LIMIT ").push_bind((offset + l) as i64);
        }

        let rows = qb.build().fetch_all(&self.pool).await.map_err(db_err)?;
        let mut out = Vec::with_capacity(rows.len());
        for row in rows {
            let iri: String = row.try_get("iri").map_err(db_err)?;
            let meta_json: Option<String> = row.try_get("meta").map_err(db_err)?;
            let meta = match meta_json {
                Some(j) => serde_json::from_str(&j).map_err(db_err)?,
                None => MetaRecord::default(),
            };
            out.push((iri, meta));
        }
        Ok(out)
    }

    /// Fetch one object's metadata projection by primary key. A missing or
    /// metadata-less object yields no rows (the required title cannot bind).
    async fn object_metadata(
        &self,
        graph: &str,
        subject: &str,
        projection: &[(String, Field)],
        required: &[bool],
    ) -> Result<AccelSolutions, DomainError> {
        let vars: Vec<String> = projection.iter().map(|(v, _)| v.clone()).collect();
        let meta_json: Option<String> =
            sqlx::query_scalar("SELECT meta FROM accel_object WHERE graph_iri = $1 AND iri = $2")
                .bind(graph)
                .bind(subject)
                .fetch_optional(&self.pool)
                .await
                .map_err(db_err)?;
        let mut rows = Vec::new();
        if let Some(json) = meta_json {
            let meta: MetaRecord = serde_json::from_str(&json).map_err(db_err)?;
            generate_metadata_rows(subject, &meta, projection, required, &mut rows);
        }
        Ok(AccelSolutions { vars, rows })
    }

    async fn count(
        &self,
        graph: &str,
        scope: &Scope,
        var: &str,
        subject_prefix: Option<&str>,
    ) -> Result<AccelSolutions, DomainError> {
        let mut qb: QueryBuilder<sqlx::Postgres> = QueryBuilder::new("");
        match scope {
            Scope::TopLevel => {
                qb.push("SELECT COUNT(*) FROM accel_object WHERE graph_iri = ");
                qb.push_bind(graph.to_owned());
                qb.push(" AND top_level");
                if let Some(p) = subject_prefix {
                    qb.push(" AND iri LIKE ").push_bind(like_prefix(p));
                }
            }
            Scope::ByType(t) => {
                qb.push("SELECT COUNT(*) FROM accel_type WHERE graph_iri = ");
                qb.push_bind(graph.to_owned());
                qb.push(" AND type_iri = ").push_bind(t.clone());
                if let Some(p) = subject_prefix {
                    qb.push(" AND iri LIKE ").push_bind(like_prefix(p));
                }
            }
            Scope::TopLevelByType(t) => {
                qb.push(
                    "SELECT COUNT(*) FROM accel_type ty \
                     JOIN accel_object o \
                       ON o.graph_iri = ty.graph_iri AND o.iri = ty.iri \
                     WHERE ty.graph_iri = ",
                );
                qb.push_bind(graph.to_owned());
                qb.push(" AND ty.type_iri = ").push_bind(t.clone());
                qb.push(" AND o.top_level");
                if let Some(p) = subject_prefix {
                    qb.push(" AND ty.iri LIKE ").push_bind(like_prefix(p));
                }
            }
            Scope::TopLevelByRole(role) => {
                qb.push(
                    "SELECT COUNT(*) FROM accel_role r \
                     JOIN accel_object o \
                       ON o.graph_iri = r.graph_iri AND o.iri = r.iri \
                     WHERE r.graph_iri = ",
                );
                qb.push_bind(graph.to_owned());
                qb.push(" AND r.role_iri = ").push_bind(role.clone());
                qb.push(" AND o.top_level");
                if let Some(p) = subject_prefix {
                    qb.push(" AND r.iri LIKE ").push_bind(like_prefix(p));
                }
            }
            Scope::TopLevelByTypeAndRole { object_type, role } => {
                qb.push(
                    "SELECT COUNT(*) FROM accel_role r \
                     JOIN accel_type ty \
                       ON ty.graph_iri = r.graph_iri AND ty.iri = r.iri \
                     JOIN accel_object o \
                       ON o.graph_iri = r.graph_iri AND o.iri = r.iri \
                     WHERE r.graph_iri = ",
                );
                qb.push_bind(graph.to_owned());
                qb.push(" AND r.role_iri = ").push_bind(role.clone());
                qb.push(" AND ty.type_iri = ")
                    .push_bind(object_type.clone());
                qb.push(" AND o.top_level");
                if let Some(p) = subject_prefix {
                    qb.push(" AND r.iri LIKE ").push_bind(like_prefix(p));
                }
            }
            Scope::RootByType(t) => {
                qb.push("SELECT COUNT(*) FROM accel_type ty WHERE ty.graph_iri = ");
                qb.push_bind(graph.to_owned());
                qb.push(" AND ty.type_iri = ").push_bind(t.clone());
                qb.push(
                    " AND NOT EXISTS (SELECT 1 FROM accel_member m \
                       WHERE m.graph_iri = ty.graph_iri AND m.member_iri = ty.iri)",
                );
                if let Some(p) = subject_prefix {
                    qb.push(" AND ty.iri LIKE ").push_bind(like_prefix(p));
                }
            }
            Scope::RootTopLevelByType(t) => {
                qb.push(
                    "SELECT COUNT(*) FROM accel_type ty \
                     JOIN accel_object o \
                       ON o.graph_iri = ty.graph_iri AND o.iri = ty.iri \
                     WHERE ty.graph_iri = ",
                );
                qb.push_bind(graph.to_owned());
                qb.push(" AND ty.type_iri = ").push_bind(t.clone());
                qb.push(" AND o.top_level");
                qb.push(
                    " AND NOT EXISTS (SELECT 1 FROM accel_member m \
                       WHERE m.graph_iri = ty.graph_iri AND m.member_iri = ty.iri)",
                );
                if let Some(p) = subject_prefix {
                    qb.push(" AND ty.iri LIKE ").push_bind(like_prefix(p));
                }
            }
            Scope::Collection {
                collection,
                root_only,
            } => {
                qb.push("SELECT COUNT(*) FROM accel_member WHERE graph_iri = ");
                qb.push_bind(graph.to_owned());
                qb.push(" AND collection_iri = ")
                    .push_bind(collection.clone());
                if *root_only {
                    qb.push(" AND is_root");
                }
                if let Some(p) = subject_prefix {
                    qb.push(" AND member_iri LIKE ").push_bind(like_prefix(p));
                }
            }
        }
        let n: i64 = qb
            .build_query_scalar()
            .fetch_one(&self.pool)
            .await
            .map_err(db_err)?;
        Ok(AccelSolutions {
            vars: vec![var.to_owned()],
            rows: vec![vec![Some(integer(n as u64))]],
        })
    }

    async fn facet(
        &self,
        graph: &str,
        kind: FacetKind,
        var: &str,
    ) -> Result<AccelSolutions, DomainError> {
        let tag = match kind {
            FacetKind::Types => FK_TYPES,
            FacetKind::Roles => FK_ROLES,
            FacetKind::Creators => FK_CREATORS,
        };
        let values = sqlx::query_scalar::<_, String>(
            "SELECT value FROM accel_facet WHERE graph_iri = $1 AND kind = $2 \
             ORDER BY value COLLATE \"C\"",
        )
        .bind(graph)
        .bind(tag)
        .fetch_all(&self.pool)
        .await
        .map_err(db_err)?;
        let rows = values
            .into_iter()
            .map(|value| {
                let term = match kind {
                    FacetKind::Creators => TermValue::Literal {
                        value,
                        datatype: XSD_STRING.to_owned(),
                        language: None,
                    },
                    _ => TermValue::Iri(value),
                };
                vec![Some(term)]
            })
            .collect();
        Ok(AccelSolutions {
            vars: vec![var.to_owned()],
            rows,
        })
    }

    async fn facet_counts(
        &self,
        graph: &str,
        kind: FacetKind,
        value_var: &str,
        count_var: &str,
    ) -> Result<AccelSolutions, DomainError> {
        let (tag, iri_value) = match kind {
            FacetKind::Types => (FK_TYPES, true),
            FacetKind::Roles => (FK_ROLES, true),
            FacetKind::Creators => (FK_CREATORS, false),
        };
        let rows = sqlx::query(
            "SELECT value, subject_count FROM accel_facet \
             WHERE graph_iri = $1 AND kind = $2 ORDER BY value COLLATE \"C\"",
        )
        .bind(graph)
        .bind(tag)
        .fetch_all(&self.pool)
        .await
        .map_err(db_err)?;
        let rows = rows
            .into_iter()
            .map(|row| {
                let value: String = row.try_get("value").map_err(db_err)?;
                let count: i64 = row.try_get("subject_count").map_err(db_err)?;
                let value = if iri_value {
                    TermValue::Iri(value)
                } else {
                    TermValue::Literal {
                        value,
                        datatype: XSD_STRING.to_owned(),
                        language: None,
                    }
                };
                Ok(vec![Some(value), Some(integer(count as u64))])
            })
            .collect::<Result<Vec<_>, DomainError>>()?;
        Ok(AccelSolutions {
            vars: vec![value_var.to_owned(), count_var.to_owned()],
            rows,
        })
    }
}

/// A `LIKE` pattern matching everything that starts with `prefix`, escaping the
/// `LIKE` metacharacters (`%`, `_`, `\`) so an IRI prefix is matched literally.
fn like_prefix(prefix: &str) -> String {
    let mut out = String::with_capacity(prefix.len() + 1);
    for ch in prefix.chars() {
        if matches!(ch, '%' | '_' | '\\') {
            out.push('\\');
        }
        out.push(ch);
    }
    out.push('%');
    out
}

fn catalog_alphabet(encoding: Option<&str>) -> Option<String> {
    let encoding = encoding?.to_ascii_lowercase();
    Some(
        if encoding.contains("protein") || encoding.contains("amino") {
            "PROTEIN"
        } else if encoding.contains("rna") {
            "RNA"
        } else if encoding.contains("dna")
            || encoding.contains("naseq")
            || encoding.contains("1207")
        {
            "DNA"
        } else {
            "OTHER"
        }
        .to_owned(),
    )
}