hawdb 0.38.0

An embedded Rust graph database for local data-plane workloads
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
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
// Copyright 2026 Nowledge
//
// 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
//
//     http://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::analytics::ProjectedGraph;
use crate::cypher;
use crate::error::{HawDBError, Result};
use crate::executor::{self, Row, RowRef};
use crate::optimizer::{
    CascadesOptimizer, OptimizerConfig, OptimizerContext, OptimizerSearchDirective, OptimizerTrace,
    PhysicalPlan, ResourceHints,
};
use crate::qos::{
    BackgroundWorkHint, BackgroundWorkPlan, LocalQosPolicy, LocalQosScheduler, LocalQosState,
    WorkClass, WorkRequest,
};
#[cfg(test)]
use crate::schema::GraphStatistics;
use crate::schema::{Catalog, SchemaObjectState};
#[cfg(test)]
use crate::schema::{
    CompositeIndexDescriptor, ConstraintDescriptor, IndexDescriptor, PropertyDescriptor,
    TableDescriptor,
};
use crate::search::{
    projection_row_from_node_with_graph_metadata, search_metadata_predicate_pushdown,
    AdaptiveVectorSearchOptions, CompressedVectorSearchMode, MetadataRepairOptions,
    MetadataRepairSummary, SearchCandidateSetReport, SearchDerivedArtifactReport,
    SearchEmptyReasonCode, SearchFusionWeights, SearchIndex, SearchMatchedSpan,
    SearchPredicatePushdownReport, SearchProjectionDelta, SearchProjectionDeltaReport,
    SearchProjectionFreshness, SearchProjectionKind, SearchQueryOptions, SearchRebuildOptions,
    SearchRebuildSummary, SearchResultSet, SearchRetrieverCandidateSetReport,
};
use crate::store::{
    restore_storage_backup, AdjacencyConsistencyReport, AdjacencyConsolidationPlan,
    AdjacencyConsolidationReport, AdjacencyDirection, AdjacencyLayout, AppendSegmentReadOutput,
    AppendTableSchema, AppendTransaction, BasicStatisticsConsistencyReport,
    DegreeStatisticsConsistencyReport, DistinctValueStatisticsConsistencyReport, DurabilityPolicy,
    GraphMutationLockFootprint, GraphMutationSavepoint, GraphMutationTransaction,
    GraphSnapshotNodeImport, GraphSnapshotRelationshipImport, GraphStore, HawDBSnapshotRowsImport,
    KernelWriteBatch, MutationSummary, NodeId, NodeRecord, OptimizerStatisticsRefreshWork,
    PreparedCheckpoint, PropertyIndexConsistencyReport, PropertyIndexProjectionRebuildAction,
    PublishedReadView, RecoveryMode, RelId, RelRecord, SchemaMaintenanceAction,
    SegmentCacheSnapshot, StorageBackupReport, StoragePressureSnapshot,
    StorageReclamationWatermark, StorageRecoveryReport, StorageRestoreReport, StorageScrubReport,
    StoreStableIdMapping, WalReplayConfig,
};
use crate::telemetry::{
    operations_telemetry_readiness, qos_telemetry_sink, runtime_telemetry_sink, KernelTelemetry,
    KernelTelemetryOperation, OperationsTelemetryReadiness, TelemetrySink,
};
use crate::value::Value;
use canonical_snapshot::export_canonical_graph_snapshot_for;
use explain::{empty_read_execution_profile, explain_analyze_output_row, explain_output_row};
use hawdb_optimizer::{
    search_field_is_enum_like, SearchPredicate, SearchPredicateOp, SearchPredicateSet,
};
use plan_cache::{
    optimized_query_plan_for, statement_uses_plan_cache, OptimizedQueryPlan,
    OptimizerEnvironmentKey, OptimizerPlanningCache, PlanCache, PlanCacheContext, PlanCacheMode,
    PlanTraceMode, DEFAULT_PLAN_CACHE_MAX_ENTRIES,
};
use std::collections::{BTreeMap, BTreeSet, VecDeque};
use std::num::NonZeroUsize;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, MutexGuard};

use system_variables::{
    apply_set_system_variable, query_statement_variables_for_statement,
    query_work_request_for_statement, reject_system_variable_parameters,
};

mod access_control;
mod config;
use config::*;
pub use config::{
    DatabaseConfig, SessionConfig, DEFAULT_MAX_READ_INTERMEDIATE_ROWS,
    DEFAULT_MAX_READ_RESULT_PAYLOAD_BYTES, DEFAULT_MAX_READ_RESULT_ROWS,
};
mod database_lifecycle;
mod database_maintenance;
mod database_projection;
pub use database_projection::SearchProjectionEpochAdvanceVerification;
mod database_query;
mod database_transactions;
mod retrieval;
pub(crate) use retrieval::search_hit_payload_bytes;
mod transactions;
pub(crate) use observability::sql_statement_kind;
use retrieval::*;
use transactions::*;
pub(crate) use transactions::{
    commit_database_transaction_state, execute_concurrent_graph_transaction_request,
    execute_recorded_database_transaction_sql_request,
};
mod graph_adapter;
mod logical_dump_access;
mod read;

mod artifact_jobs;
mod automatic_checkpoint;
pub use automatic_checkpoint::AutomaticCheckpointReport;
mod branch_lifecycle;
mod canonical_snapshot;
mod concurrent;
#[cfg(test)]
pub(crate) use concurrent::set_commit_boundary_hook;
#[cfg(test)]
pub(crate) use concurrent::AutocommitMutationPhase;
pub(crate) use concurrent::{
    ExclusiveDatabaseLease, PublishedConcurrentRead, SharedDatabaseGuard, SharedDatabaseOwner,
};
mod vector_index;
pub use vector_index::{
    VectorIndexBuildError, VectorIndexBuildOptions, VectorIndexField, VectorIndexLanguageBudget,
    VectorIndexLanguageReport, VectorIndexReadError, VectorIndexReadTransaction,
    VectorIndexSqlReport, VectorIndexSqlRequest, VectorIndexUpdateReport,
};
mod transaction_read_dependencies;
pub use transaction_read_dependencies::{
    TransactionIsolation, TransactionReadDependencyLimits, TransactionReadDependencyUsage,
};
mod dependency_read;
pub use dependency_read::{
    PreparedProjectionFeed, PreparedProjectionFullSource, PreparedRead, PreparedReadDependencies,
    PreparedReadLimits, ProjectionBaselineComparisonConfig, ProjectionBaselineComparisonSession,
    ProjectionBaselineComparisonStep, ProjectionBaselineMappingContext,
    ProjectionBaselineMappingFunction, ProjectionBaselineMappingLimits, ProjectionCoverageConfig,
    ProjectionCoverageSession, ProjectionCoverageStep, ProjectionCoveredUpdate,
    ProjectionFeedError, ProjectionFeedItem, ProjectionFeedLimits, ProjectionFeedRead,
    ProjectionFeedResource, ProjectionFeedStep, ProjectionFeedUsage, ProjectionFullRebuildConfig,
    ProjectionFullRebuildSession, ProjectionFullRebuildStep, ProjectionFullSourceItem,
    ProjectionFullSourceLimits, ProjectionMappingContext, ProjectionMappingFunction,
    ProjectionMappingInput, ProjectionMappingLimits, ProjectionMappingStatus,
    ProjectionMappingUsage, ProjectionMatchedBaseline, ProjectionOutputVerificationContext,
    ProjectionOutputVerificationFunction, ProjectionOutputVerificationSession,
    ProjectionPreparedFullRebuild, ProjectionPublicationError, ProjectionSourceDurability,
    ProjectionSourceScope, ProjectionStagedInputs, ProjectionStagingSession,
    ProjectionStagingSessionConfig, ProjectionStagingSessionStep, ReadDependencyValidationFailures,
};
mod explain;
mod graph_analytics;
mod knowledge_graph_filters;
mod knowledge_graph_read;
pub use graph_analytics::{
    GraphAnalyticsAlgorithm, GraphAnalyticsFreshness, GraphAnalyticsPublicationStatus,
    GraphAnalyticsRequest, PreparedGraphAnalytics,
};
#[cfg(test)]
mod explain_format_tests;
mod observability;
mod plan_cache;
/// The unit-test binary's thread-local live-allocation oracle.
#[cfg(test)]
pub(crate) use plan_cache::retention_allocation as test_allocation;
pub(crate) mod publication;
pub use publication::{DatabasePublicationOwner, DatabasePublicationState};
#[cfg(feature = "qualification")]
pub use publication::{PublicationLockDurationSnapshot, PublicationLockMetricsSnapshot};
mod optimizer_qualification;
#[cfg(feature = "qualification")]
mod query_preparation_metrics;
mod query_request;
mod query_runtime;
mod read_execution;
pub use optimizer_qualification::{
    OptimizerQualificationAttemptLimits, OptimizerQualificationCandidateKind,
    OptimizerQualificationCandidateOutput, OptimizerQualificationCandidateRequest,
    OptimizerQualificationCandidateSetOutput, OptimizerQualificationOutput,
    OptimizerQualificationResultError, OPTIMIZER_QUALIFICATION_CANDIDATE_LIMIT,
};
#[cfg(feature = "qualification")]
pub use query_preparation_metrics::{query_preparation_metrics, QueryPreparationMetricsSnapshot};
mod sql_runtime;
#[cfg(feature = "tokio-runtime")]
pub(crate) use sql_runtime::PreparedRuntimeSql;
pub(crate) use sql_runtime::RuntimeSqlPlanningSnapshot;
mod retained_query;
pub(crate) use retained_query::EmbeddedRuntimeBinding;
pub use retained_query::{
    ArrowArray, ArrowArrayStream, ArrowSchema, RetainedArrowCodeOwner, RetainedArrowExport,
    RetainedArrowSchema, RetainedArrowStream, RetainedColumnRole, RetainedColumnSchema,
    RetainedColumnType, RetainedColumnValues, RetainedNodeCursor, RetainedNodeField,
    RetainedNodeLabels, RetainedNodeSnapshotCursor, RetainedNodeTaskCursor, RetainedNodeView,
    RetainedQueryBatch, RetainedQueryCursor, RetainedQueryError, RetainedQueryOptions,
    RetainedQueryProfile, RetainedQueryStatus,
};
mod resource_profile;
mod runtime_cell;
mod schema_guidance;
mod search_projection_catch_up;
mod search_projection_consumer;
mod statement_shape;
pub use search_projection_consumer::*;
mod source_candidates;
mod system_schema;
mod system_variables;
mod transaction_locks;
mod types;

pub(crate) use hawdb_system_sql as system_sql;

pub use branch_lifecycle::{
    BranchAbandonmentAcknowledgement, BranchAbandonmentLimits, BranchAbandonmentPlan,
    BranchAbandonmentReport, BranchCreateRequest, BranchInfo, BranchLifecycleError,
    BranchLifecycleState, BranchReclamationLimits, BranchReclamationReport, BranchSelector,
};
pub use hawdb_core::graph_rag::{
    MissingScoringFeature, ScoringCombination, ScoringProgram, ScoringProgramError,
    ScoringProgramShape,
};
pub(crate) use hawdb_executor::runtime_admission::runtime_planning_request;
#[cfg(feature = "tokio-runtime")]
pub(crate) use hawdb_executor::runtime_admission::RuntimeAdmissionPlan;
pub use hawdb_storage::branch_create_recovery::{
    BranchCreateRecoveryEntry, BranchCreateRecoveryLimits, BranchCreateRecoveryReport,
    BranchCreateRecoveryStatus,
};
use query_request::BoundScoringRequest;
pub use query_request::{
    BudgetedReadSnapshot, HostScoringRequest, OwnedHostScoringRequest, OwnedQueryRequest,
    OwnedSqlQueryRequest, QueryRequest, ReadSnapshotBudget, ReadSnapshotUsage, ScoringRequest,
    SqlQueryRequest,
};
pub(crate) use query_runtime::{runtime_result_memory_budget, PreparedRuntimeQuery};
pub use types::*;

const DEFAULT_SEARCH_PROJECTION_CHANGE_LOG_MAX_ENTRIES: usize = 4096;
const DEFAULT_SEARCH_PROJECTION_CHANGE_LOG_MAX_BYTES: usize = 64 * 1024 * 1024;

pub use artifact_jobs::{
    DerivedArtifactJob, DerivedArtifactJobReport, DerivedArtifactJobStatus,
    ExternalContentArtifactJobCompletion, ExternalContentArtifactJobSummary,
    ExternalContentArtifactRuntimeManifest,
};
pub use canonical_snapshot::{
    hawdb_lightning_initial_import_advance_checkpoint,
    hawdb_lightning_initial_import_advance_durable_state_streaming,
    hawdb_lightning_initial_import_advance_durable_state_with_search_projection_batch,
    hawdb_lightning_initial_import_checkpoint_readiness,
    hawdb_lightning_initial_import_cutover_catch_up_report,
    hawdb_lightning_initial_import_decode_durable_state,
    hawdb_lightning_initial_import_document_identity_coverage,
    hawdb_lightning_initial_import_durable_state_report,
    hawdb_lightning_initial_import_encode_durable_state, hawdb_lightning_initial_import_plan,
    hawdb_lightning_initial_import_plan_with_document_identities,
    hawdb_lightning_initial_import_readiness, hawdb_lightning_initial_import_recovery_readiness,
    hawdb_lightning_initial_import_resume_action,
    hawdb_lightning_initial_import_search_projection_batch_report,
    hawdb_lightning_initial_import_search_projection_batch_report_with_document_identities,
    hawdb_lightning_initial_import_session_bundle_readiness,
    hawdb_lightning_initial_import_session_report,
    hawdb_lightning_initial_import_source_bundle_readiness,
    hawdb_lightning_initial_import_source_fingerprint,
    hawdb_lightning_initial_import_startup_readiness, parse_hawdb_lightning_graph_stream_export,
    validate_hawdb_lightning_graph_stream, validate_hawdb_lightning_relational_stream,
    CanonicalGraphSnapshotExport, CanonicalGraphSnapshotValidation,
    CanonicalSnapshotEndpointViolation, CanonicalSnapshotIdentityAudit, CanonicalSnapshotNode,
    CanonicalSnapshotRelationship, CanonicalStableIdMapping, HawDBLightningBootstrapExport,
    HawDBLightningBootstrapManifest, HawDBLightningGraphStream,
    HawDBLightningGraphStreamValidation, HawDBLightningInitialImportApplyReport,
    HawDBLightningInitialImportCheckpoint, HawDBLightningInitialImportCheckpointProgress,
    HawDBLightningInitialImportCheckpointProgressReport,
    HawDBLightningInitialImportCheckpointReadiness,
    HawDBLightningInitialImportCutoverCatchUpReport, HawDBLightningInitialImportDocumentIdentity,
    HawDBLightningInitialImportDocumentIdentityCoverage,
    HawDBLightningInitialImportDocumentIdentityKindReport,
    HawDBLightningInitialImportDurableBatchAdvanceReport, HawDBLightningInitialImportDurableState,
    HawDBLightningInitialImportDurableStateCodecReport,
    HawDBLightningInitialImportDurableStateReport, HawDBLightningInitialImportIdempotencyKey,
    HawDBLightningInitialImportPlan, HawDBLightningInitialImportReadiness,
    HawDBLightningInitialImportReadinessInputs, HawDBLightningInitialImportRecoveryReadinessReport,
    HawDBLightningInitialImportResumeAction, HawDBLightningInitialImportResumeActionKind,
    HawDBLightningInitialImportSearchProjectionBatchReport,
    HawDBLightningInitialImportSessionBundleReadiness, HawDBLightningInitialImportSessionReport,
    HawDBLightningInitialImportSourceBundleReadiness, HawDBLightningInitialImportSourceFingerprint,
    HawDBLightningInitialImportStartupReadinessReport,
    HawDBLightningInitialImportStreamingBatchAdvanceReport, HawDBLightningRelationalStream,
    HawDBLightningRelationalStreamValidation, HAWDB_LIGHTNING_BOOTSTRAP_PROTOCOL_VERSION,
    HAWDB_LIGHTNING_GRAPH_STREAM_FORMAT_VERSION,
    HAWDB_LIGHTNING_INITIAL_IMPORT_DURABLE_STATE_PROTOCOL,
    HAWDB_LIGHTNING_RELATIONAL_STREAM_FORMAT_VERSION,
};
pub use concurrent::{
    ConcurrentDatabase, ConcurrentDatabaseMetrics, ConcurrentDatabaseTransaction,
    ConcurrentPhaseMetrics, ConcurrentTransactionMode, ConcurrentTransactionOptions,
    WalGroupCommitActivation, WalGroupCommitAdaptiveColdStartEvidence,
    WalGroupCommitAdaptivePolicyEvidence, WalGroupCommitAdaptiveSteadyStateEvidence,
    WalGroupCommitConfig, WalGroupCommitDelayPolicy, WalGroupCommitEvidence,
    WalGroupCommitSnapshot, WalGroupCommitTailLatencyEvidence, WalGroupCommitWaitDecision,
    DEFAULT_PESSIMISTIC_LOCK_TIMEOUT, DEFAULT_WAL_GROUP_COMMIT_MAX_BYTES,
    DEFAULT_WAL_GROUP_COMMIT_MAX_DELAY, DEFAULT_WAL_GROUP_COMMIT_MAX_ENTRIES,
};
pub use hawdb_core::QueryAccessControlContext;
pub use hawdb_evidence::AccessControlPolicyReadiness;
pub use hawdb_executor::{BoundedReadQueryOutput, QueryStreamOptions, QueryStreamReport};
pub use hawdb_explain::{ExplainAnalyzeOutput, ExplainOutput, NowledgeGraphExplainOutput};
pub use hawdb_system_sql::{
    SlowQueryLogExportOptions, SlowQueryLogRecordSummary, SLOW_QUERY_LOG_EVENT_PROTOCOL,
};
pub use plan_cache::{PlanCacheBypassReason, PlanCacheLookup, PlanCacheStats};
pub use resource_profile::{
    StorageResourceProfileLimits, StorageResourceProfileReport, STORAGE_RESOURCE_PROFILE_PROTOCOL,
};
pub use search_projection_catch_up::{
    ScheduledSearchProjectionCatchUpReport, SearchProjectionCatchUpReport,
    SearchProjectionCatchUpStopReason,
};
pub use source_candidates::{
    KnowledgeSourceCandidateRow, KnowledgeSourceCandidateScanOrigin,
    KnowledgeSourceCandidateScanOutput, KnowledgeSourceCandidateScanRequest,
};
pub use system_schema::{SystemSchemaMigration, SystemSchemaRegistry, SystemSchemaUpgradeReport};
pub use system_variables::QuerySystemVariables;

fn hawdb_lightning_initial_import_source_fingerprint_key(
    manifest: &HawDBLightningBootstrapManifest,
) -> String {
    let fingerprint = hawdb_lightning_initial_import_source_fingerprint(manifest);
    format!(
        "v{}:{}:{}:{}:{}:{}:{}:{}:{}:{}:{}:{}:{}",
        fingerprint.protocol_version,
        fingerprint.database_commit_epoch,
        fingerprint.graph_commit_epoch,
        fingerprint.logical_checksum,
        fingerprint.graph_stream_checksum,
        fingerprint.graph_stream_byte_len,
        fingerprint.relational_stream_checksum,
        fingerprint.relational_stream_byte_len,
        fingerprint.schema_checksum,
        fingerprint.node_count,
        fingerprint.relationship_count,
        fingerprint.relational_table_count,
        fingerprint.relational_row_count,
    )
}

/// Equality-only identity of one admitted database runtime in this process.
///
/// Clones retain only a marker, not database resources. Reopening or switching
/// to another branch creates a different identity, even at equal contents/epochs.
/// This token is not a persistent identity or evidence of a retained fence.
#[derive(Debug, Clone)]
pub struct DatabaseInstanceIdentity(Arc<()>);

impl DatabaseInstanceIdentity {
    fn new() -> Self {
        Self(Arc::new(()))
    }
}

impl PartialEq for DatabaseInstanceIdentity {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.0, &other.0)
    }
}

impl Eq for DatabaseInstanceIdentity {}

#[derive(Debug)]
pub struct Database {
    runtime: runtime_cell::BranchRuntimeCell,
    project_root_path: Option<PathBuf>,
    branch_create_recovery: Option<BranchCreateRecoveryReport>,
    durability: DurabilityPolicy,
    slow_query_log: Arc<SharedState<system_sql::SlowQueryLog>>,
    statement_summary: Arc<SharedState<system_sql::StatementSummary>>,
    config: DatabaseConfig,
    local_qos_scheduler: LocalQosScheduler,
    system_variables: QuerySystemVariables,
    derived_artifact_jobs: hawdb_artifact::DerivedArtifactJobQueue,
    derived_artifact_root_pins:
        BTreeMap<u64, Option<Arc<hawdb_storage::branch_root_pins::RootPin>>>,
    telemetry: Option<Arc<dyn TelemetrySink>>,
    runtime_governor: Option<hawdb_qos::RuntimeGovernor>,
    retained_runtime: Arc<retained_query::RetainedRuntime>,
}

pub(crate) struct DatabaseCheckpointSource {
    catalog: Catalog,
    store: GraphStore,
    suspension: Option<automatic_checkpoint::Suspension>,
}

pub(crate) struct PreparedDatabaseCheckpoint {
    checkpoint: PreparedCheckpoint,
    _suspension: Option<automatic_checkpoint::Suspension>,
}

impl DatabaseCheckpointSource {
    pub(crate) fn prepare(self) -> Result<Option<PreparedDatabaseCheckpoint>> {
        Ok(self
            .store
            .prepare_checkpoint(&self.catalog)?
            .map(|checkpoint| PreparedDatabaseCheckpoint {
                checkpoint,
                _suspension: self.suspension,
            }))
    }
}

#[derive(Debug)]
pub(super) struct SharedState<T>(Mutex<T>);

impl<T> SharedState<T> {
    fn new(value: T) -> Self {
        Self(Mutex::new(value))
    }

    pub(super) fn borrow(&self) -> MutexGuard<'_, T> {
        self.0
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
    }

    pub(super) fn borrow_mut(&self) -> MutexGuard<'_, T> {
        self.borrow()
    }
}

pub(crate) fn system_runtime_snapshot(
    config: &DatabaseConfig,
) -> system_sql::SystemRuntimeSnapshot {
    system_sql::SystemRuntimeSnapshot::new(
        config.read_only,
        config.read_result_row_limit(),
        config.max_read_result_payload_bytes,
        config.storage_residency_mode,
        config.relational_index_mode,
        config.runtime_capabilities,
    )
}

pub use hawdb_executor::QueryOutput;

/// Deterministic outcome for one relational INSERT, UPDATE, or DELETE statement.
///
/// A staged statement is provisional until its enclosing transaction commits.
/// Conflict no-ops increment `conflict_rows`, never `affected_rows`, and never
/// contribute a `RETURNING` row. In the v1 contract, `rows` contains values for
/// supported `INSERT ... RETURNING` statements and remains empty for count-only
/// UPDATE and DELETE outcomes.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RelationalMutationResult {
    pub affected_rows: usize,
    pub conflict_rows: usize,
    pub rows: executor::QueryRows,
    pub provisional: bool,
}

/// Result of one SQL statement executed inside a database transaction.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SqlStatementResult {
    pub output: QueryOutput,
    pub mutation: Option<RelationalMutationResult>,
}

/// Confirmed results produced by the durable transaction commit.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TransactionCommitResult {
    pub output: QueryOutput,
    pub mutations: Vec<RelationalMutationResult>,
    pub append_mutations: Vec<hawdb_storage::append_table::AppendMutationOutcome>,
}

/// Confirmed generated order-key assignments from one durable append commit.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AppendCommitResult {
    pub commit_epoch: u64,
    pub mutations: Vec<hawdb_storage::append_table::AppendMutationOutcome>,
}

/// Declares the relational tables exposed by one owner-scoped projection
/// generation inside a pinned read transaction.
///
/// The binding is generic rather than route-specific. The application owns the
/// PostgreSQL DDL for the named tables and the projection version. HawDB owns
/// active-generation resolution, pin lifetime, row decoding, and query
/// admission.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProjectionRelationalReadBinding {
    projection: String,
    owner_key: Vec<u8>,
    projection_version: u64,
    tables: BTreeSet<String>,
}

impl ProjectionRelationalReadBinding {
    pub fn new(
        projection: impl Into<String>,
        owner_key: impl Into<Vec<u8>>,
        projection_version: u64,
        tables: impl IntoIterator<Item = impl Into<String>>,
    ) -> Result<Self> {
        let projection = projection.into();
        let owner_key = owner_key.into();
        let tables = tables.into_iter().map(Into::into).collect::<BTreeSet<_>>();
        if projection.trim().is_empty() {
            return Err(HawDBError::Semantic(
                "projection relational binding requires a non-empty projection name".to_string(),
            ));
        }
        if owner_key.is_empty() {
            return Err(HawDBError::Semantic(
                "projection relational binding requires a non-empty owner key".to_string(),
            ));
        }
        if projection_version == 0 {
            return Err(HawDBError::Semantic(
                "projection relational binding requires a non-zero projection version".to_string(),
            ));
        }
        if tables.is_empty() || tables.iter().any(|table| table.trim().is_empty()) {
            return Err(HawDBError::Semantic(
                "projection relational binding requires non-empty table names".to_string(),
            ));
        }
        Ok(Self {
            projection,
            owner_key,
            projection_version,
            tables,
        })
    }

    pub fn projection(&self) -> &str {
        &self.projection
    }

    pub fn owner_key(&self) -> &[u8] {
        &self.owner_key
    }

    pub const fn projection_version(&self) -> u64 {
        self.projection_version
    }

    pub fn tables(&self) -> &BTreeSet<String> {
        &self.tables
    }
}

#[derive(Debug, Clone, Copy, Default)]
pub(super) struct StatementExecutionContext<'a> {
    query_identity: Option<&'a hawdb_query::QueryIdentity>,
    execution_profile: Option<&'a executor::ReadExecutionProfile>,
    retained_profile: Option<&'a RetainedQueryProfile>,
    access_control: Option<&'a QueryAccessControlContext>,
    parse_nanos: u64,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NowledgeGraphStatement {
    pub cypher: String,
    pub parameters: BTreeMap<String, Value>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NowledgeGraphTransactionOutput {
    pub statement_outputs: Vec<QueryOutput>,
    pub commit_output: QueryOutput,
}

#[derive(Debug)]
pub struct DatabaseTransaction<'a> {
    db: &'a mut Database,
    runtime: DatabaseTransactionRuntime,
    state: DatabaseTransactionState,
    task_context: Option<hawdb_core::RuntimeTaskContext>,
}

#[derive(Debug)]
pub(super) struct DatabaseTransactionRuntime {
    instance_identity: DatabaseInstanceIdentity,
    session_config: SessionConfig,
    optimizer: CascadesOptimizer,
    plan_cache: SharedState<PlanCache>,
    relational_plan_template_cache: Arc<crate::relational_sql::RelationalPlanTemplateCache>,
    optimizer_planning_cache: SharedState<OptimizerPlanningCache>,
    config: DatabaseConfig,
    system_variables: QuerySystemVariables,
    branch_catalog_path: Option<PathBuf>,
    current_branch: Option<branch_lifecycle::CurrentBranch>,
}

#[derive(Debug)]
pub(super) struct DatabaseTransactionState {
    read_dependencies: Option<transaction_read_dependencies::TransactionReadDependencies>,
    graph_transaction: Option<GraphMutationTransaction>,
    relational_transaction: hawdb_storage::relational::RelationalTransaction,
    relational_state: hawdb_storage::relational::RelationalState,
    append_transaction: hawdb_storage::append_table::AppendTransaction,
    append_state: hawdb_storage::append_table::AppendState,
    pending_generated_append_tables: BTreeSet<String>,
    relational_returning: Vec<Option<crate::relational_sql::RelationalReturningProjection>>,
    relational_index:
        std::result::Result<Option<crate::store::RelationalTransactionIndexView>, String>,
    relational_rows:
        std::result::Result<Option<crate::store::RelationalTransactionRowView>, String>,
    // Moves with the private workspace into a queued commit. Checkpoint
    // reclamation must retain its physical generation until the state retires.
    snapshot_pin: Option<ReaderPin>,
    /// The transaction-start instant. Every SQL statement of the transaction
    /// pins the statement clock to it, so `CURRENT_TIMESTAMP`, `now()`,
    /// `LOCALTIMESTAMP` and clock defaults agree across the transaction.
    sql_clock: hawdb_core::temporal::OffsetDateTime,
    /// Graph change epoch of the transaction's base snapshot.
    base_graph_change_commit_epoch: u64,
    /// Set once the caller reads the graph change epoch: commit is refused
    /// unless the committed graph change epoch still equals this value.
    graph_change_guard: Option<u64>,
}

struct SparseRelationalStatementStage {
    state: hawdb_storage::relational::RelationalState,
    mutation_outcomes: Vec<hawdb_storage::relational::RelationalMutationOutcome>,
    index_capture: hawdb_storage::relational::RelationalIndexChangeCapture,
    row_capture: hawdb_storage::relational::RelationalRowChangeCapture,
}

pub(crate) struct GraphTransactionStatementOutcome {
    pub(crate) output: QueryOutput,
    pub(crate) savepoint: Option<GraphMutationSavepoint>,
    pub(crate) lock_footprint: GraphMutationLockFootprint,
}

#[derive(Debug)]
pub struct DatabaseSession<'a> {
    config: SessionConfig,
    db: &'a mut Database,
    graph_transaction: Option<GraphMutationTransaction>,
    transaction_runtime: Option<DatabaseTransactionRuntime>,
    system_variables: QuerySystemVariables,
}

#[derive(Debug)]
enum ReadPlanCache {
    Private(SharedState<PlanCache>),
    Shared(Arc<SharedState<PlanCache>>),
}

impl std::ops::Deref for ReadPlanCache {
    type Target = SharedState<PlanCache>;

    fn deref(&self) -> &Self::Target {
        match self {
            Self::Private(cache) => cache,
            Self::Shared(cache) => cache,
        }
    }
}

#[derive(Debug)]
pub struct DatabaseReadTransaction<S: crate::executor::ExecutionStore = GraphStore> {
    session_config: SessionConfig,
    system_variables: QuerySystemVariables,
    catalog: Catalog,
    store: S,
    published_read_view: PublishedReadView,
    optimizer: CascadesOptimizer,
    plan_cache: ReadPlanCache,
    relational_plan_template_cache: Arc<crate::relational_sql::RelationalPlanTemplateCache>,
    optimizer_planning_cache: Arc<SharedState<OptimizerPlanningCache>>,
    slow_query_snapshot: Vec<system_sql::SlowQueryRecord>,
    statement_summary_snapshot: Vec<system_sql::StatementSummaryRecord>,
    config: DatabaseConfig,
    branch_catalog_path: Option<PathBuf>,
    current_branch: Option<branch_lifecycle::CurrentBranch>,
    projection_relational: Option<ProjectionRelationalReadSnapshot>,
    task_context: Option<hawdb_core::RuntimeTaskContext>,
    _pin: Arc<ReaderPin>,
    retained_runtime: Arc<retained_query::RetainedRuntime>,
    /// The read transaction's start instant, pinned for its SQL statements.
    sql_clock: hawdb_core::temporal::OffsetDateTime,
}

/// An immutable canonical view that can create independent query contexts.
/// The shared pin survives publication replacement until the last query drops.
#[derive(Debug)]
pub(crate) struct DatabaseReadSnapshot(DatabaseReadTransaction);

impl DatabaseReadSnapshot {
    pub(crate) fn config(&self) -> &DatabaseConfig {
        &self.0.config
    }

    pub(crate) fn ensure_usable(&self) -> Result<()> {
        self.0.store.ensure_usable()
    }

    pub(crate) fn begin_read_transaction(
        &self,
        task_context: &hawdb_core::RuntimeTaskContext,
    ) -> Result<DatabaseReadTransaction> {
        self.fork(Some(task_context.clone()))
    }

    fn fork(
        &self,
        task_context: Option<hawdb_core::RuntimeTaskContext>,
    ) -> Result<DatabaseReadTransaction> {
        let mut transaction = self.0.fork_at_same_snapshot(task_context)?;
        // A publication pins data, not the start time of future transactions.
        // Forks within an existing transaction still inherit its clock.
        transaction.sql_clock = hawdb_core::temporal::statement_now();
        Ok(transaction)
    }
}

impl DatabaseReadTransaction {
    /// Opens an independent query context over this transaction's pinned
    /// snapshot. The shared pin keeps the epoch until both contexts drop.
    pub(crate) fn fork_at_same_snapshot(
        &self,
        task_context: Option<hawdb_core::RuntimeTaskContext>,
    ) -> Result<DatabaseReadTransaction> {
        self.store.ensure_usable()?;
        let source = self;
        Ok(DatabaseReadTransaction {
            session_config: source.session_config,
            system_variables: source.system_variables.clone(),
            catalog: source.catalog.clone(),
            store: source.store.snapshot_for_read(),
            published_read_view: source.published_read_view,
            optimizer: source.optimizer.clone(),
            plan_cache: ReadPlanCache::Private(SharedState::new(PlanCache::with_byte_limits(
                source.config.max_plan_cache_entries,
                Some(source.config.max_physical_plan_cache_bytes),
                Some(source.config.max_physical_plan_cache_entry_bytes),
            ))),
            relational_plan_template_cache: Arc::clone(&source.relational_plan_template_cache),
            optimizer_planning_cache: Arc::clone(&source.optimizer_planning_cache),
            slow_query_snapshot: source.slow_query_snapshot.clone(),
            statement_summary_snapshot: source.statement_summary_snapshot.clone(),
            config: source.config.clone(),
            branch_catalog_path: source.branch_catalog_path.clone(),
            current_branch: source.current_branch.clone(),
            projection_relational: None,
            task_context,
            _pin: Arc::clone(&source._pin),
            retained_runtime: Arc::clone(&source.retained_runtime),
            sql_clock: source.sql_clock,
        })
    }
}

#[derive(Debug)]
struct ProjectionRelationalReadSnapshot {
    binding: ProjectionRelationalReadBinding,
    reader: hawdb_storage::projection_generation::ProjectionGenerationReader,
}

struct ReadStreamingExecutionContext<'a> {
    task_context: Option<&'a hawdb_core::RuntimeTaskContext>,
    external: Option<&'a mut dyn executor::ExternalReadOperator>,
    delivery: executor::StreamDelivery,
    scoring: Option<BoundScoringRequest<'a>>,
    access_control: Option<&'a QueryAccessControlContext>,
}

#[derive(Debug)]
pub struct NowledgeGraphAdapter<'a> {
    db: &'a mut Database,
}

#[derive(Debug, Default)]
struct ReaderPins {
    next_reader_id: u64,
    active_views: BTreeMap<u64, PublishedReadView>,
}

#[derive(Debug)]
struct ReaderPin {
    id: u64,
    pins: Arc<Mutex<ReaderPins>>,
}

fn configure_search_projection_changefeed(store: &mut GraphStore, config: &DatabaseConfig) {
    store.set_search_projection_primary_key_capture_limits(
        config.search_projection_relational_change_limits,
    );
    store.set_max_search_projection_change_log_entries(
        config.max_search_projection_change_log_entries,
    );
    store.set_max_search_projection_change_log_bytes(config.max_search_projection_change_log_bytes);
}

fn configure_relational_fast_paths(store: &mut GraphStore, config: &DatabaseConfig) {
    store.set_relational_monotonic_append_fast_path_enabled(
        config.relational_monotonic_append_fast_path,
    );
    store.set_legacy_relational_checkpoint_file_enabled(config.legacy_relational_checkpoint_file);
}

impl Default for Database {
    fn default() -> Self {
        let config = effective_database_config(DatabaseConfig::default());
        let local_qos_scheduler = LocalQosScheduler::new(config.local_qos_policy);
        let mut store = GraphStore::default();
        configure_search_projection_changefeed(&mut store, &config);
        configure_relational_fast_paths(&mut store, &config);
        Self {
            runtime: runtime_cell::BranchRuntimeCell::admitted(
                runtime_cell::AdmittedBranchRuntime {
                    catalog: Catalog::default(),
                    branch_selection: None,
                    projection_consumers: search_projection_consumer::ConsumerRegistry::load(
                        store.search_projection_registry_root(),
                        store.search_projection_database_identity(),
                    ),
                    store,
                    optimizer: optimizer_from_database_config(&config),
                    plan_cache: Arc::new(SharedState::new(PlanCache::with_byte_limits(
                        config.max_plan_cache_entries,
                        Some(config.max_physical_plan_cache_bytes),
                        Some(config.max_physical_plan_cache_entry_bytes),
                    ))),
                    relational_plan_template_cache:
                        relational_plan_template_cache_from_database_config(&config),
                    optimizer_planning_cache: Arc::new(SharedState::new(
                        OptimizerPlanningCache::default(),
                    )),
                    reader_pins: Arc::new(Mutex::new(ReaderPins::default())),
                },
            ),
            project_root_path: None,

            durability: DurabilityPolicy::default(),

            slow_query_log: Arc::new(SharedState::new(system_sql::SlowQueryLog::new(
                config.slow_query_log_capacity,
            ))),
            statement_summary: Arc::new(SharedState::new(system_sql::StatementSummary::new(
                config.statement_summary_capacity,
            ))),
            config,
            local_qos_scheduler,
            system_variables: QuerySystemVariables::default(),

            derived_artifact_jobs: hawdb_artifact::DerivedArtifactJobQueue::default(),
            derived_artifact_root_pins: BTreeMap::new(),
            telemetry: None,
            runtime_governor: None,
            retained_runtime: Arc::new(retained_query::RetainedRuntime::default()),
            branch_create_recovery: None,
        }
    }
}

impl Database {
    fn ensure_writable(&self) -> Result<()> {
        self.runtime.get()?.store.ensure_usable()?;
        if self.config.read_only {
            return Err(HawDBError::Execution(
                "database is opened in read-only mode".to_string(),
            ));
        }
        Ok(())
    }

    pub fn project_graph(&self, rel_type: Option<&str>) -> Result<ProjectedGraph> {
        Ok({
            let runtime = self.runtime.get()?;
            match rel_type {
                Some(name) => runtime
                    .catalog
                    .rel_type_id(name)
                    .map(|rel_type_id| {
                        ProjectedGraph::from_store(&runtime.store, Some(rel_type_id))
                    })
                    .unwrap_or_else(|| ProjectedGraph::from_store_without_edges(&runtime.store)),
                None => ProjectedGraph::from_store(&runtime.store, None),
            }
        })
    }
}

fn effective_database_config(mut config: DatabaseConfig) -> DatabaseConfig {
    config.runtime_capabilities =
        crate::compiled_capabilities::effective_runtime_capabilities(config.runtime_capabilities);
    config
}

fn require_search_projection_source_epoch(expected: Option<u64>, actual: u64) -> Result<()> {
    if let Some(expected) = expected
        && expected != actual
    {
        if expected > actual {
            return Err(HawDBError::Storage(format!(
                "search projection graph delta complete-through epoch {expected} is ahead of graph commit epoch {actual}"
            )));
        }
        // A prefix behind the pinned source is a typed, non-poisoning refusal
        // (#1924): hosts must cover the whole interval or re-prepare.
        return Err(HawDBError::SearchProjectionPrefixBehindSource {
            complete_through_commit_epoch: expected,
            source_commit_epoch: actual,
        });
    }
    Ok(())
}

/// `externally_owned` kinds derive no rows: the host is their only writer
/// (#1814). Their documents are still removed through `delete_document_ids`.
fn search_projection_graph_delta_for(
    catalog: &Catalog,
    store: &GraphStore,
    request: &SearchProjectionGraphDeltaRequest,
    externally_owned: &BTreeSet<SearchProjectionKind>,
) -> Result<SearchProjectionDelta> {
    let operation_count = request.operation_count();
    if let Some(limit) = request.max_operations
        && operation_count > limit
    {
        return Err(HawDBError::SearchProjectionBatchBudgetExceeded {
            budget: hawdb_core::SearchProjectionBatchBudget::GraphDelta,
            commit_epoch: None,
            required_operations: operation_count,
            limit,
        });
    }
    // A changefeed prefix describes IDs, not a historical read view.
    // Hydrating it from a newer head would publish future data under an old watermark.
    require_search_projection_source_epoch(
        request.complete_through_graph_commit_epoch,
        store.commit_epoch(),
    )?;

    let mut upserts = Vec::new();
    for node_id in &request.upsert_node_ids {
        let Some(node) = store.node_owned(NodeId(*node_id))? else {
            continue;
        };
        if let Some(row) = projection_row_from_node_with_graph_metadata(catalog, store, &node)?
            && !externally_owned.contains(&row.kind)
        {
            upserts.push(row);
        }
    }

    Ok(SearchProjectionDelta {
        upserts,
        deletes: request.delete_document_ids.clone(),
        max_operations: request.max_operations,
        source_graph_commit_epoch: request.complete_through_graph_commit_epoch,
    })
}

fn search_projection_commit_lag(
    search_index: &SearchIndex,
    required_projection_commit_epoch: u64,
) -> u64 {
    search_projection_freshness_commit_lag(
        &search_index.projection_freshness(),
        required_projection_commit_epoch,
    )
}

fn knowledge_entity_from_node(catalog: &Catalog, node: &NodeRecord) -> KnowledgeEntity {
    KnowledgeEntity {
        node_id: node.id.0,
        labels: node_label_names(catalog, node),
        external_id: Some(projected_node_external_id(node)),
        properties: node.properties.clone(),
    }
}

impl ReaderPins {
    fn oldest_epoch(&self) -> Option<u64> {
        self.oldest_epoch_excluding(None)
    }

    fn oldest_epoch_excluding(&self, excluded: Option<u64>) -> Option<u64> {
        self.active_views
            .iter()
            .filter_map(|(&id, view)| (Some(id) != excluded).then_some(view.visible_commit_epoch()))
            .min()
    }

    fn pinned_physical_generations(&self) -> hawdb_storage::read_view::PhysicalGenerationPins {
        hawdb_storage::read_view::PhysicalGenerationPins::from_views(
            self.active_views.values().copied(),
        )
    }
}

fn elapsed_micros(started: hawdb_core::time::Instant) -> u64 {
    started.elapsed().as_micros().min(u64::MAX as u128) as u64
}

impl ReaderPin {
    fn new(id: u64, pins: Arc<Mutex<ReaderPins>>) -> Self {
        Self { id, pins }
    }
}

impl Drop for ReaderPin {
    fn drop(&mut self) {
        self.pins
            .lock()
            .expect("database reader pins lock should not be poisoned")
            .active_views
            .remove(&self.id);
    }
}

fn search_kind_to_label(kind: &str) -> Option<&'static str> {
    match kind {
        "Memory" | "memory" => Some("Memory"),
        "Message" | "message" => Some("Message"),
        "Entity" | "entity" => Some("Entity"),
        "Source" | "source" => Some("Source"),
        "SourceChunk" | "source_chunk" | "sourcechunk" | "chunk" => Some("SourceChunk"),
        "Community" | "community" => Some("Community"),
        _ => None,
    }
}

fn search_label_to_kind(label: &str) -> Option<&'static str> {
    match label {
        "Memory" | "memory" => Some("memory"),
        "Message" | "message" => Some("message"),
        "Entity" | "entity" => Some("entity"),
        "Source" | "source" => Some("source"),
        "SourceChunk" | "source_chunk" | "sourcechunk" | "chunk" => Some("source_chunk"),
        "Community" | "community" => Some("community"),
        _ => None,
    }
}

fn node_label_names(catalog: &Catalog, node: &NodeRecord) -> Vec<String> {
    node.labels
        .iter()
        .filter_map(|label_id| catalog.label_name(*label_id))
        .map(str::to_string)
        .collect()
}

fn node_external_id(node: &NodeRecord) -> Option<String> {
    node.properties
        .get("id")
        .map(value_to_external_id)
        .filter(|external_id| !external_id.is_empty())
}

fn projected_node_external_id(node: &NodeRecord) -> String {
    node_external_id(node).unwrap_or_else(|| node.id.0.to_string())
}

fn value_to_external_id(value: &Value) -> String {
    match value {
        Value::Null => String::new(),
        Value::Bool(value) => value.to_string(),
        Value::Int(value) => value.to_string(),
        Value::Float(value) => value.to_string(),
        Value::Decimal(value) => value.to_string(),
        Value::String(value) => value.clone(),
        Value::Binary(value) => format!(
            "\\x{}",
            value
                .iter()
                .map(|byte| format!("{byte:02x}"))
                .collect::<String>()
        ),
        Value::Uuid(value) => value.to_string(),
        Value::Vector(_)
        | Value::Date(_)
        | Value::LocalTime(_)
        | Value::LocalDateTime(_)
        | Value::DateTime(_)
        | Value::Duration(_) => value.to_string(),
        Value::List(values) => values
            .iter()
            .map(value_to_external_id)
            .collect::<Vec<_>>()
            .join(","),
        Value::Map(values) => values
            .iter()
            .map(|(key, value)| format!("{key}:{}", value_to_external_id(value)))
            .collect::<Vec<_>>()
            .join(","),
    }
}

fn schema_state_value(state: SchemaObjectState) -> Value {
    let value = match state {
        SchemaObjectState::DeleteOnly => "delete_only",
        SchemaObjectState::WriteOnly => "write_only",
        SchemaObjectState::Backfill => "backfill",
        SchemaObjectState::Validating => "validating",
        SchemaObjectState::Public => "public",
        SchemaObjectState::Gc => "gc",
    };
    Value::String(value.to_string())
}

fn schema_maintenance_actions_output(actions: Vec<SchemaMaintenanceAction>) -> QueryOutput {
    let rows = actions
        .into_iter()
        .map(|action| {
            BTreeMap::from([
                ("object_type".to_string(), Value::String(action.object_type)),
                ("object".to_string(), Value::String(action.object)),
                (
                    "from_state".to_string(),
                    schema_state_value(action.from_state),
                ),
                (
                    "to_state".to_string(),
                    action
                        .to_state
                        .map(schema_state_value)
                        .unwrap_or(Value::Null),
                ),
                ("action".to_string(), Value::String(action.action)),
            ])
        })
        .collect();
    QueryOutput { rows }
}

fn property_index_projection_rebuild_output(
    actions: Vec<PropertyIndexProjectionRebuildAction>,
) -> QueryOutput {
    let rows = actions
        .into_iter()
        .map(|action| {
            BTreeMap::from([
                ("index_kind".to_string(), Value::String(action.index_kind)),
                ("label".to_string(), Value::String(action.label)),
                (
                    "properties".to_string(),
                    Value::List(action.properties.into_iter().map(Value::String).collect()),
                ),
                (
                    "estimated_operations".to_string(),
                    Value::Int(i64::try_from(action.estimated_operations).unwrap_or(i64::MAX)),
                ),
                (
                    "indexed_entries".to_string(),
                    Value::Int(i64::try_from(action.indexed_entries).unwrap_or(i64::MAX)),
                ),
            ])
        })
        .collect();
    QueryOutput { rows }
}

fn optional_u64_value(value: Option<u64>) -> Value {
    value
        .map(|value| Value::Int(value as i64))
        .unwrap_or(Value::Null)
}

fn optional_usize_value(value: Option<usize>) -> Value {
    value
        .map(|value| Value::Int(value as i64))
        .unwrap_or(Value::Null)
}

fn statement_body(statement: &cypher::Statement) -> &cypher::Statement {
    match statement {
        cypher::Statement::CypherQuery(query) => &query.statement,
        _ => statement,
    }
}

pub(crate) fn statement_kind(statement: &cypher::Statement) -> &'static str {
    match statement {
        cypher::Statement::AlterPropertyState(_) => "alter_property_state",
        cypher::Statement::AlterTableState(_) => "alter_table_state",
        cypher::Statement::BeginTransaction => "begin_transaction",
        cypher::Statement::Checkpoint => "checkpoint",
        cypher::Statement::Commit => "commit",
        cypher::Statement::CreateCompositeIndex(_) => "create_composite_index",
        cypher::Statement::CreateFullTextIndex(_) => "create_full_text_index",
        cypher::Statement::CreateIndex(_) => "create_index",
        cypher::Statement::CreateNode(_) => "create_node",
        cypher::Statement::CreateNodeLabel(_) => "create_node_label",
        cypher::Statement::CreateNodePropertyExistsConstraint(_) => {
            "create_node_property_exists_constraint"
        }
        cypher::Statement::CreateNodeTable(_) => "create_node_table",
        cypher::Statement::CreateProperty(_) => "create_property",
        cypher::Statement::CreateRangeIndex(_) => "create_range_index",
        cypher::Statement::CreateRelationship(_) => "create_relationship",
        cypher::Statement::CreateRelationshipPropertyExistsConstraint(_) => {
            "create_relationship_property_exists_constraint"
        }
        cypher::Statement::CreateRelationshipTable(_) => "create_relationship_table",
        cypher::Statement::CreateRelationshipType(_) => "create_relationship_type",
        cypher::Statement::CreateRelationshipUniqueConstraint(_) => {
            "create_relationship_unique_constraint"
        }
        cypher::Statement::CreateUniqueConstraint(_) => "create_unique_constraint",
        cypher::Statement::CypherQuery(query) => statement_kind(&query.statement),
        cypher::Statement::Explain(explain) => {
            if explain.analyze {
                "explain_analyze"
            } else {
                "explain"
            }
        }
        cypher::Statement::GraphAlgorithm(_) => "graph_algorithm",
        cypher::Statement::VectorSearch(_) => "vector_search",
        cypher::Statement::Pipeline(query) => statement_shape::pipeline_statement_kind(query),
        cypher::Statement::MergeNode(_) => "merge_node",
        cypher::Statement::MergeRelationship(_) => "merge_relationship",
        cypher::Statement::UnwindMutation(_) => "unwind_mutation",
        cypher::Statement::ProjectGraph(_) => "project_graph",
        cypher::Statement::Rollback => "rollback",
        cypher::Statement::SetSystemVariable(_) => "set_system_variable",
    }
}

fn single_import_label(node: &CanonicalSnapshotNode) -> Result<String> {
    match node.labels.as_slice() {
        [label] => Ok(label.clone()),
        [] => Err(HawDBError::Storage(
            "HawDB Lightning initial import node has no label".to_string(),
        )),
        _ => Err(HawDBError::Storage(
            "HawDB Lightning initial import multi-label node is unsupported".to_string(),
        )),
    }
}

fn relational_state_counts(state: &hawdb_storage::relational::RelationalState) -> (usize, usize) {
    let table_count = state.table_schemas().count();
    let row_count = state
        .table_schemas()
        .map(|schema| state.row_count(&schema.name))
        .sum();
    (table_count, row_count)
}

#[cfg(test)]
mod tests;

#[cfg(test)]
mod legacy_graph_seed_budget_tests;