drasi-lib 0.6.0

Embedded Drasi for in-process data change processing using continuous queries
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
// Copyright 2025 The Drasi Authors.
//
// 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.

//! Fluent builders for DrasiLib and its components.
//!
//! This module provides the builder pattern for constructing DrasiLib instances
//! and their components in a type-safe, ergonomic way.
//!
//! # Overview
//!
//! - [`DrasiLibBuilder`] - Main builder for creating a DrasiLib instance
//! - [`Query`] - Builder for query configurations
//!
//! # Plugin Architecture
//!
//! **Important**: drasi-lib has ZERO awareness of which plugins exist. Sources and
//! reactions are created externally as fully-configured instances implementing
//! `Source` and `Reaction` traits, then passed to DrasiLibBuilder via
//! `with_source()` and `with_reaction()`.
//!
//! # Examples
//!
//! ## Basic Usage with Pre-built Instances
//!
//! ```no_run
//! use drasi_lib::{DrasiLib, Query};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Source and reaction instances are created externally by plugins
//! // Ownership is transferred to DrasiLib when added
//! // let my_source = my_source_plugin::create(...);
//! // let my_reaction = my_reaction_plugin::create(...);
//!
//! let core = DrasiLib::builder()
//!     .with_id("my-server")
//!     // .with_source(my_source)      // Ownership transferred
//!     // .with_reaction(my_reaction)  // Ownership transferred
//!     .with_query(
//!         Query::cypher("my-query")
//!             .query("MATCH (n:Person) RETURN n")
//!             .from_source("events")
//!             .build()
//!     )
//!     .build()
//!     .await?;
//!
//! core.start().await?;
//! # Ok(())
//! # }
//! ```

use std::sync::Arc;

use crate::channels::DispatchMode;
use crate::config::{
    DrasiLibConfig, QueryConfig, QueryJoinConfig, QueryLanguage, SourceSubscriptionConfig,
};
use crate::error::{DrasiError, Result};
use crate::identity::IdentityProvider;
use crate::indexes::IndexBackendPlugin;
use crate::indexes::StorageBackendConfig;
use crate::lib_core::DrasiLib;
use crate::reactions::Reaction as ReactionTrait;
use crate::sources::Source as SourceTrait;
use crate::state_store::StateStoreProvider;
use drasi_core::models::SourceMiddlewareConfig;

// ============================================================================
// DrasiLibBuilder
// ============================================================================

/// Fluent builder for creating DrasiLib instances.
///
/// Use `DrasiLib::builder()` to get started.
///
/// # Plugin Architecture
///
/// **Important**: drasi-lib has ZERO awareness of which plugins exist. Sources and
/// reactions are created externally as fully-configured instances implementing
/// `Source` and `Reaction` traits, then passed via `with_source()` and `with_reaction()`.
///
/// # Example
///
/// ```no_run
/// use drasi_lib::{DrasiLib, Query};
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// // Source and reaction instances are created externally by plugins
/// // Ownership is transferred to DrasiLib when added
/// // let my_source = my_source_plugin::create(...);
/// // let my_reaction = my_reaction_plugin::create(...);
///
/// let core = DrasiLib::builder()
///     .with_id("my-server")
///     // .with_source(my_source)      // Ownership transferred
///     // .with_reaction(my_reaction)  // Ownership transferred
///     .with_query(
///         Query::cypher("my-query")
///             .query("MATCH (n) RETURN n")
///             .from_source("my-source")
///             .build()
///     )
///     .build()
///     .await?;
/// # Ok(())
/// # }
/// ```
pub struct DrasiLibBuilder {
    server_id: Option<String>,
    priority_queue_capacity: Option<usize>,
    dispatch_buffer_capacity: Option<usize>,
    storage_backends: Vec<StorageBackendConfig>,
    query_configs: Vec<QueryConfig>,
    source_instances: Vec<(
        Box<dyn SourceTrait>,
        std::collections::HashMap<String, String>,
    )>,
    reaction_instances: Vec<(
        Box<dyn ReactionTrait>,
        std::collections::HashMap<String, String>,
    )>,
    index_provider: Option<Arc<dyn IndexBackendPlugin>>,
    state_store_provider: Option<Arc<dyn StateStoreProvider>>,
    identity_provider: Option<Arc<dyn IdentityProvider>>,
}

impl Default for DrasiLibBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl DrasiLibBuilder {
    /// Create a new builder with default values.
    pub fn new() -> Self {
        Self {
            server_id: None,
            priority_queue_capacity: None,
            dispatch_buffer_capacity: None,
            storage_backends: Vec::new(),
            query_configs: Vec::new(),
            source_instances: Vec::new(),
            reaction_instances: Vec::new(),
            index_provider: None,
            state_store_provider: None,
            identity_provider: None,
        }
    }

    /// Set the server ID.
    pub fn with_id(mut self, id: impl Into<String>) -> Self {
        self.server_id = Some(id.into());
        self
    }

    /// Set the default priority queue capacity for components.
    pub fn with_priority_queue_capacity(mut self, capacity: usize) -> Self {
        self.priority_queue_capacity = Some(capacity);
        self
    }

    /// Set the default dispatch buffer capacity for components.
    pub fn with_dispatch_buffer_capacity(mut self, capacity: usize) -> Self {
        self.dispatch_buffer_capacity = Some(capacity);
        self
    }

    /// Add a storage backend configuration.
    pub fn add_storage_backend(mut self, config: StorageBackendConfig) -> Self {
        self.storage_backends.push(config);
        self
    }

    /// Set the index backend provider for persistent storage.
    ///
    /// When using RocksDB or Redis/Garnet storage backends, you must provide
    /// an index provider that implements `IndexBackendPlugin`. The provider
    /// is responsible for creating the actual index instances.
    ///
    /// If no index provider is set, only in-memory storage backends can be used.
    /// Attempting to use RocksDB or Redis backends without a provider will result
    /// in an error.
    ///
    /// # Example
    /// ```ignore
    /// use drasi_index_rocksdb::RocksDbIndexProvider;
    /// use std::sync::Arc;
    ///
    /// let provider = RocksDbIndexProvider::new("/data/drasi", true, false);
    /// let core = DrasiLib::builder()
    ///     .with_index_provider(Arc::new(provider))
    ///     .build()
    ///     .await?;
    /// ```
    pub fn with_index_provider(mut self, provider: Arc<dyn IndexBackendPlugin>) -> Self {
        self.index_provider = Some(provider);
        self
    }

    /// Set the state store provider for plugin state persistence.
    ///
    /// State store providers allow plugins (Sources, BootstrapProviders, and Reactions)
    /// to store and retrieve runtime state that can persist across runs of DrasiLib.
    ///
    /// If no state store provider is set, the default in-memory provider will be used.
    /// The in-memory provider does not persist state across restarts.
    ///
    /// # Example
    /// ```ignore
    /// use drasi_state_store_json::JsonStateStoreProvider;
    /// use std::sync::Arc;
    ///
    /// let state_store = JsonStateStoreProvider::new("/data/state");
    /// let core = DrasiLib::builder()
    ///     .with_state_store_provider(Arc::new(state_store))
    ///     .build()
    ///     .await?;
    /// ```
    pub fn with_state_store_provider(mut self, provider: Arc<dyn StateStoreProvider>) -> Self {
        self.state_store_provider = Some(provider);
        self
    }

    /// Set the identity provider for credential injection.
    ///
    /// Identity providers supply authentication credentials (passwords, tokens,
    /// certificates) to sources and reactions that need them for connecting to
    /// external systems.
    ///
    /// If no identity provider is set, sources and reactions will receive `None`
    /// for `context.identity_provider`.
    ///
    /// # Example
    /// ```ignore
    /// use drasi_identity_azure::AzureIdentityProvider;
    /// use std::sync::Arc;
    ///
    /// let provider = AzureIdentityProvider::with_default_credentials("user@tenant.onmicrosoft.com")?;
    /// let core = DrasiLib::builder()
    ///     .with_identity_provider(Arc::new(provider))
    ///     .build()
    ///     .await?;
    /// ```
    pub fn with_identity_provider(mut self, provider: Arc<dyn IdentityProvider>) -> Self {
        self.identity_provider = Some(provider);
        self
    }

    /// Add a source instance, taking ownership.
    ///
    /// Source instances are created externally by plugins with their own typed configurations.
    /// drasi-lib only knows about the `Source` trait - it has no knowledge of which plugins exist.
    ///
    /// # Example
    /// ```ignore
    /// let source = MySource::new("my-source", config)?;
    /// let core = DrasiLib::builder()
    ///     .with_source(source)  // Ownership transferred
    ///     .build()
    ///     .await?;
    /// ```
    pub fn with_source(mut self, source: impl SourceTrait + 'static) -> Self {
        self.source_instances
            .push((Box::new(source), std::collections::HashMap::new()));
        self
    }

    /// Add a source instance with additional component metadata.
    ///
    /// Like [`with_source`](Self::with_source) but merges `extra_metadata`
    /// (e.g. `pluginId`) into the component graph node.
    pub fn with_source_metadata(
        mut self,
        source: impl SourceTrait + 'static,
        extra_metadata: std::collections::HashMap<String, String>,
    ) -> Self {
        self.source_instances
            .push((Box::new(source), extra_metadata));
        self
    }

    /// Add a query configuration.
    pub fn with_query(mut self, config: QueryConfig) -> Self {
        self.query_configs.push(config);
        self
    }

    /// Add a reaction instance, taking ownership.
    ///
    /// Reaction instances are created externally by plugins with their own typed configurations.
    /// drasi-lib only knows about the `Reaction` trait - it has no knowledge of which plugins exist.
    ///
    /// # Example
    /// ```ignore
    /// let reaction = MyReaction::new("my-reaction", vec!["query1".into()]);
    /// let core = DrasiLib::builder()
    ///     .with_reaction(reaction)  // Ownership transferred
    ///     .build()
    ///     .await?;
    /// ```
    pub fn with_reaction(mut self, reaction: impl ReactionTrait + 'static) -> Self {
        self.reaction_instances
            .push((Box::new(reaction), std::collections::HashMap::new()));
        self
    }

    /// Add a reaction instance with additional component metadata.
    ///
    /// Like [`with_reaction`](Self::with_reaction) but merges `extra_metadata`
    /// (e.g. `pluginId`) into the component graph node.
    pub fn with_reaction_metadata(
        mut self,
        reaction: impl ReactionTrait + 'static,
        extra_metadata: std::collections::HashMap<String, String>,
    ) -> Self {
        self.reaction_instances
            .push((Box::new(reaction), extra_metadata));
        self
    }

    /// Build the DrasiLib instance.
    ///
    /// This validates the configuration, creates all components, and initializes the server.
    /// After building, you can call `start()` to begin processing.
    pub async fn build(self) -> Result<DrasiLib> {
        // Build the configuration
        let config = DrasiLibConfig {
            id: self.server_id.unwrap_or_else(|| "drasi-lib".to_string()),
            priority_queue_capacity: self.priority_queue_capacity,
            dispatch_buffer_capacity: self.dispatch_buffer_capacity,
            storage_backends: self.storage_backends,
            queries: self.query_configs.clone(),
        };

        // Validate the configuration
        config
            .validate()
            .map_err(|e| DrasiError::validation(e.to_string()))?;

        // Create runtime config and server with optional index and state store providers
        let runtime_config = Arc::new(crate::config::RuntimeConfig::new(
            config,
            self.index_provider,
            self.state_store_provider,
            self.identity_provider,
        ));
        let mut core = DrasiLib::new(runtime_config);

        // Inject state store before provisioning sources (they need it for initialization)
        let state_store = core.config.state_store_provider.clone();
        core.source_manager
            .inject_state_store(state_store.clone())
            .await;
        core.reaction_manager.inject_state_store(state_store).await;

        // Register the component graph source BEFORE initialize (which loads query config).
        // Queries reference sources, so sources must exist in the graph first.
        {
            use crate::sources::component_graph_source::ComponentGraphSource;
            let graph_source = ComponentGraphSource::new(
                core.component_event_broadcast_tx.clone(),
                core.config.id.clone(),
                core.component_graph.clone(),
            )
            .map_err(|e| {
                DrasiError::operation_failed(
                    "source",
                    "component-graph",
                    "add",
                    format!("Failed to create: {e}"),
                )
            })?;

            let source_id = graph_source.id().to_string();
            let source_type = graph_source.type_name().to_string();
            {
                let mut graph = core.component_graph.write().await;
                let mut metadata = std::collections::HashMap::new();
                metadata.insert("kind".to_string(), source_type);
                metadata.insert(
                    "autoStart".to_string(),
                    graph_source.auto_start().to_string(),
                );
                graph.register_source(&source_id, metadata).map_err(|e| {
                    DrasiError::operation_failed(
                        "source",
                        &source_id,
                        "add",
                        format!("Failed to register: {e}"),
                    )
                })?;
            }
            if let Err(e) = core.source_manager.provision_source(graph_source).await {
                let mut graph = core.component_graph.write().await;
                let _ = graph.deregister(&source_id);
                return Err(DrasiError::operation_failed(
                    "source",
                    &source_id,
                    "add",
                    format!("Failed to provision: {e}"),
                ));
            }
        }

        // Inject pre-built source instances BEFORE initialize.
        // Queries reference sources by ID, so sources must be in the graph first.
        for (source, extra_metadata) in self.source_instances {
            let source_id = source.id().to_string();
            let source_type = source.type_name().to_string();
            let auto_start = source.auto_start();

            {
                let mut graph = core.component_graph.write().await;
                let mut metadata = std::collections::HashMap::new();
                metadata.insert("kind".to_string(), source_type);
                metadata.insert("autoStart".to_string(), auto_start.to_string());
                metadata.extend(extra_metadata);
                graph.register_source(&source_id, metadata).map_err(|e| {
                    DrasiError::operation_failed(
                        "source",
                        &source_id,
                        "add",
                        format!("Failed to register: {e}"),
                    )
                })?;
            }
            if let Err(e) = core.source_manager.provision_source(source).await {
                let mut graph = core.component_graph.write().await;
                let _ = graph.deregister(&source_id);
                return Err(DrasiError::operation_failed(
                    "source",
                    &source_id,
                    "add",
                    format!("Failed to provision: {e}"),
                ));
            }
        }

        // Initialize the server (loads query configurations — sources must already be registered)
        core.initialize().await?;

        // Inject pre-built reaction instances
        for (reaction, extra_metadata) in self.reaction_instances {
            let reaction_id = reaction.id().to_string();
            let reaction_type = reaction.type_name().to_string();
            let query_ids = reaction.query_ids();

            // Register in graph first, then provision
            {
                let mut graph = core.component_graph.write().await;
                let mut metadata = std::collections::HashMap::new();
                metadata.insert("kind".to_string(), reaction_type);
                metadata.extend(extra_metadata);
                graph
                    .register_reaction(&reaction_id, metadata, &query_ids)
                    .map_err(|e| {
                        DrasiError::operation_failed(
                            "reaction",
                            &reaction_id,
                            "add",
                            format!("Failed to register: {e}"),
                        )
                    })?;
            }
            if let Err(e) = core.reaction_manager.provision_reaction(reaction).await {
                let mut graph = core.component_graph.write().await;
                let _ = graph.deregister(&reaction_id);
                return Err(DrasiError::operation_failed(
                    "reaction",
                    &reaction_id,
                    "add",
                    format!("Failed to provision: {e}"),
                ));
            }
        }

        // Register the identity provider in the component graph (if configured).
        // This creates an IdentityProvider node with Authenticates edges to all
        // sources and reactions that receive credentials from it.
        if core.config.identity_provider.is_some() {
            let mut graph = core.component_graph.write().await;
            let component_ids: Vec<String> = graph
                .list_by_kind(&crate::component_graph::ComponentKind::Source)
                .into_iter()
                .chain(graph.list_by_kind(&crate::component_graph::ComponentKind::Reaction))
                .map(|(id, _)| id)
                .collect();

            let mut metadata = std::collections::HashMap::new();
            metadata.insert("kind".to_string(), "identity_provider".to_string());
            graph
                .register_identity_provider("identity-provider", metadata, &component_ids)
                .map_err(|e| {
                    DrasiError::operation_failed(
                        "identity_provider",
                        "identity-provider",
                        "add",
                        format!("Failed to register: {e}"),
                    )
                })?;
        }

        Ok(core)
    }
}

// ============================================================================
// Query Builder
// ============================================================================

/// Fluent builder for query configurations.
///
/// Use `Query::cypher()` or `Query::gql()` to get started.
///
/// # Example
///
/// ```no_run
/// use drasi_lib::Query;
///
/// let query_config = Query::cypher("my-query")
///     .query("MATCH (n:Person) RETURN n.name, n.age")
///     .from_source("my-source")
///     .auto_start(true)
///     .build();
/// ```
pub struct Query {
    id: String,
    query: String,
    query_language: QueryLanguage,
    sources: Vec<SourceSubscriptionConfig>,
    middleware: Vec<SourceMiddlewareConfig>,
    auto_start: bool,
    joins: Option<Vec<QueryJoinConfig>>,
    enable_bootstrap: bool,
    bootstrap_buffer_size: usize,
    priority_queue_capacity: Option<usize>,
    dispatch_buffer_capacity: Option<usize>,
    dispatch_mode: Option<DispatchMode>,
    storage_backend: Option<crate::indexes::StorageBackendRef>,
    recovery_policy: Option<crate::recovery::RecoveryPolicy>,
}

impl Query {
    /// Create a new Cypher query builder.
    pub fn cypher(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            query: String::new(),
            query_language: QueryLanguage::Cypher,
            sources: Vec::new(),
            middleware: Vec::new(),
            auto_start: true,
            joins: None,
            enable_bootstrap: true,
            bootstrap_buffer_size: 10000,
            priority_queue_capacity: None,
            dispatch_buffer_capacity: None,
            dispatch_mode: None,
            storage_backend: None,
            recovery_policy: None,
        }
    }

    /// Create a new GQL (ISO 9074:2024) query builder.
    pub fn gql(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            query: String::new(),
            query_language: QueryLanguage::GQL,
            sources: Vec::new(),
            middleware: Vec::new(),
            auto_start: true,
            joins: None,
            enable_bootstrap: true,
            bootstrap_buffer_size: 10000,
            priority_queue_capacity: None,
            dispatch_buffer_capacity: None,
            dispatch_mode: None,
            storage_backend: None,
            recovery_policy: None,
        }
    }

    /// Set the query string.
    pub fn query(mut self, query: impl Into<String>) -> Self {
        self.query = query.into();
        self
    }

    /// Subscribe to a source.
    pub fn from_source(mut self, source_id: impl Into<String>) -> Self {
        self.sources.push(SourceSubscriptionConfig {
            source_id: source_id.into(),
            nodes: Vec::new(),
            relations: Vec::new(),
            pipeline: Vec::new(),
        });
        self
    }

    /// Subscribe to a source with a middleware pipeline.
    ///
    /// The pipeline is a list of middleware names (strings) that will be applied to
    /// data from this source before it reaches the query.
    pub fn from_source_with_pipeline(
        mut self,
        source_id: impl Into<String>,
        pipeline: Vec<String>,
    ) -> Self {
        self.sources.push(SourceSubscriptionConfig {
            source_id: source_id.into(),
            nodes: Vec::new(),
            relations: Vec::new(),
            pipeline,
        });
        self
    }

    /// Add middleware to the query.
    pub fn with_middleware(mut self, middleware: SourceMiddlewareConfig) -> Self {
        self.middleware.push(middleware);
        self
    }

    /// Set whether the query should auto-start.
    pub fn auto_start(mut self, auto_start: bool) -> Self {
        self.auto_start = auto_start;
        self
    }

    /// Set the join configuration.
    pub fn with_joins(mut self, joins: Vec<QueryJoinConfig>) -> Self {
        self.joins = Some(joins);
        self
    }

    /// Enable or disable bootstrap.
    pub fn enable_bootstrap(mut self, enable: bool) -> Self {
        self.enable_bootstrap = enable;
        self
    }

    /// Set the bootstrap buffer size.
    pub fn with_bootstrap_buffer_size(mut self, size: usize) -> Self {
        self.bootstrap_buffer_size = size;
        self
    }

    /// Set the priority queue capacity.
    pub fn with_priority_queue_capacity(mut self, capacity: usize) -> Self {
        self.priority_queue_capacity = Some(capacity);
        self
    }

    /// Set the dispatch buffer capacity.
    pub fn with_dispatch_buffer_capacity(mut self, capacity: usize) -> Self {
        self.dispatch_buffer_capacity = Some(capacity);
        self
    }

    /// Set the dispatch mode.
    pub fn with_dispatch_mode(mut self, mode: DispatchMode) -> Self {
        self.dispatch_mode = Some(mode);
        self
    }

    /// Set the storage backend reference.
    pub fn with_storage_backend(mut self, backend: crate::indexes::StorageBackendRef) -> Self {
        self.storage_backend = Some(backend);
        self
    }

    /// Set the recovery policy. Applies only to queries with a persistent
    /// storage backend. See [`RecoveryPolicy`](crate::RecoveryPolicy).
    pub fn with_recovery_policy(mut self, policy: crate::recovery::RecoveryPolicy) -> Self {
        self.recovery_policy = Some(policy);
        self
    }

    /// Build the query configuration.
    pub fn build(self) -> QueryConfig {
        QueryConfig {
            id: self.id,
            query: self.query,
            query_language: self.query_language,
            sources: self.sources,
            middleware: self.middleware,
            auto_start: self.auto_start,
            joins: self.joins,
            enable_bootstrap: self.enable_bootstrap,
            bootstrap_buffer_size: self.bootstrap_buffer_size,
            priority_queue_capacity: self.priority_queue_capacity,
            dispatch_buffer_capacity: self.dispatch_buffer_capacity,
            dispatch_mode: self.dispatch_mode,
            storage_backend: self.storage_backend,
            recovery_policy: self.recovery_policy,
        }
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    // ==========================================================================
    // Query Builder Tests
    // ==========================================================================

    #[test]
    fn test_query_builder_cypher() {
        let config = Query::cypher("test-query")
            .query("MATCH (n) RETURN n")
            .from_source("source1")
            .auto_start(false)
            .build();

        assert_eq!(config.id, "test-query");
        assert_eq!(config.query, "MATCH (n) RETURN n");
        assert_eq!(config.query_language, QueryLanguage::Cypher);
        assert!(!config.auto_start);
        assert_eq!(config.sources.len(), 1);
        assert_eq!(config.sources[0].source_id, "source1");
    }

    #[test]
    fn test_query_builder_gql() {
        let config = Query::gql("test-query")
            .query("MATCH (n:Person) RETURN n.name")
            .from_source("source1")
            .build();

        assert_eq!(config.query_language, QueryLanguage::GQL);
    }

    #[test]
    fn test_query_builder_multiple_sources() {
        let config = Query::cypher("test-query")
            .query("MATCH (n) RETURN n")
            .from_source("source1")
            .from_source("source2")
            .build();

        assert_eq!(config.sources.len(), 2);
    }

    #[tokio::test]
    async fn test_drasi_lib_builder_empty() {
        let core = DrasiLibBuilder::new().build().await.unwrap();

        assert!(!core.is_running().await);
    }

    #[tokio::test]
    async fn test_drasi_lib_builder_with_id() {
        let core = DrasiLibBuilder::new()
            .with_id("test-server")
            .build()
            .await
            .unwrap();

        assert_eq!(core.get_config().id, "test-server");
    }

    #[tokio::test]
    async fn test_drasi_lib_builder_with_query_no_source() {
        // Test builder with query configuration that has no source subscriptions
        // In the instance-based approach, sources are added after build()
        let core = DrasiLibBuilder::new()
            .with_id("test-server")
            .with_query(
                Query::cypher("query1")
                    .query("MATCH (n) RETURN n")
                    // No from_source() call - query has no source subscriptions
                    .auto_start(false)
                    .build(),
            )
            .build()
            .await
            .unwrap();

        let queries = core.list_queries().await.unwrap();
        assert_eq!(queries.len(), 1);
    }

    // ==========================================================================
    // DrasiLib Builder Integration Tests (from builder_tests.rs)
    // ==========================================================================

    #[tokio::test]
    async fn test_builder_creates_initialized_server() {
        let core = DrasiLib::builder().with_id("builder-test").build().await;

        assert!(core.is_ok(), "Builder should create initialized server");
        let core = core.unwrap();
        assert!(
            core.state_guard.is_initialized(),
            "Server should be initialized"
        );
    }

    #[tokio::test]
    async fn test_builder_with_query() {
        // In the instance-based approach, sources and reactions are added as instances
        // after the builder creates the core. Here we just test query config addition.
        // Source must be registered before a query can reference it
        let source = crate::sources::tests::TestMockSource::new("source1".to_string()).unwrap();
        let core = DrasiLib::builder()
            .with_id("complex-server")
            .with_source(source)
            .with_query(
                Query::cypher("query1")
                    .query("MATCH (n) RETURN n")
                    .from_source("source1")
                    .build(),
            )
            .build()
            .await;

        assert!(core.is_ok(), "Builder with query should succeed");
        let core = core.unwrap();
        assert!(core.state_guard.is_initialized());
        assert_eq!(core.config.queries.len(), 1);
    }

    // ==========================================================================
    // DrasiLibBuilder Unit Tests
    // ==========================================================================

    #[test]
    fn test_builder_with_id_sets_id() {
        let builder = DrasiLibBuilder::new().with_id("my-server");
        assert_eq!(builder.server_id, Some("my-server".to_string()));
    }

    #[test]
    fn test_builder_with_id_accepts_string() {
        let builder = DrasiLibBuilder::new().with_id(String::from("owned-id"));
        assert_eq!(builder.server_id, Some("owned-id".to_string()));
    }

    #[test]
    fn test_builder_with_priority_queue_capacity() {
        let builder = DrasiLibBuilder::new().with_priority_queue_capacity(50000);
        assert_eq!(builder.priority_queue_capacity, Some(50000));
    }

    #[test]
    fn test_builder_with_dispatch_buffer_capacity() {
        let builder = DrasiLibBuilder::new().with_dispatch_buffer_capacity(2000);
        assert_eq!(builder.dispatch_buffer_capacity, Some(2000));
    }

    #[test]
    fn test_builder_with_query_adds_to_list() {
        let q = Query::cypher("q1").query("MATCH (n) RETURN n").build();
        let builder = DrasiLibBuilder::new().with_query(q);
        assert_eq!(builder.query_configs.len(), 1);
        assert_eq!(builder.query_configs[0].id, "q1");
    }

    #[test]
    fn test_builder_with_multiple_queries() {
        let q1 = Query::cypher("q1").query("MATCH (a) RETURN a").build();
        let q2 = Query::gql("q2").query("MATCH (b) RETURN b").build();
        let builder = DrasiLibBuilder::new().with_query(q1).with_query(q2);
        assert_eq!(builder.query_configs.len(), 2);
        assert_eq!(builder.query_configs[0].id, "q1");
        assert_eq!(builder.query_configs[1].id, "q2");
    }

    #[test]
    fn test_builder_add_storage_backend() {
        use crate::indexes::config::{StorageBackendConfig, StorageBackendSpec};

        let backend = StorageBackendConfig {
            id: "mem1".to_string(),
            spec: StorageBackendSpec::Memory {
                enable_archive: false,
            },
        };
        let builder = DrasiLibBuilder::new().add_storage_backend(backend);
        assert_eq!(builder.storage_backends.len(), 1);
        assert_eq!(builder.storage_backends[0].id, "mem1");
    }

    #[test]
    fn test_builder_add_multiple_storage_backends() {
        use crate::indexes::config::{StorageBackendConfig, StorageBackendSpec};

        let b1 = StorageBackendConfig {
            id: "mem1".to_string(),
            spec: StorageBackendSpec::Memory {
                enable_archive: false,
            },
        };
        let b2 = StorageBackendConfig {
            id: "mem2".to_string(),
            spec: StorageBackendSpec::Memory {
                enable_archive: true,
            },
        };
        let builder = DrasiLibBuilder::new()
            .add_storage_backend(b1)
            .add_storage_backend(b2);
        assert_eq!(builder.storage_backends.len(), 2);
        assert_eq!(builder.storage_backends[0].id, "mem1");
        assert_eq!(builder.storage_backends[1].id, "mem2");
    }

    #[test]
    fn test_builder_default_values() {
        let builder = DrasiLibBuilder::new();
        assert_eq!(builder.server_id, None);
        assert_eq!(builder.priority_queue_capacity, None);
        assert_eq!(builder.dispatch_buffer_capacity, None);
        assert!(builder.storage_backends.is_empty());
        assert!(builder.query_configs.is_empty());
        assert!(builder.source_instances.is_empty());
        assert!(builder.reaction_instances.is_empty());
        assert!(builder.index_provider.is_none());
        assert!(builder.state_store_provider.is_none());
    }

    #[test]
    fn test_builder_fluent_chaining() {
        use crate::indexes::config::{StorageBackendConfig, StorageBackendSpec};

        let backend = StorageBackendConfig {
            id: "mem".to_string(),
            spec: StorageBackendSpec::Memory {
                enable_archive: false,
            },
        };
        let q = Query::cypher("q1").query("MATCH (n) RETURN n").build();

        let builder = DrasiLibBuilder::new()
            .with_id("chained")
            .with_priority_queue_capacity(20000)
            .with_dispatch_buffer_capacity(3000)
            .add_storage_backend(backend)
            .with_query(q);

        assert_eq!(builder.server_id, Some("chained".to_string()));
        assert_eq!(builder.priority_queue_capacity, Some(20000));
        assert_eq!(builder.dispatch_buffer_capacity, Some(3000));
        assert_eq!(builder.storage_backends.len(), 1);
        assert_eq!(builder.query_configs.len(), 1);
    }

    #[tokio::test]
    async fn test_builder_default_id_when_none_set() {
        let core = DrasiLibBuilder::new().build().await.unwrap();
        assert_eq!(core.get_config().id, "drasi-lib");
    }

    #[tokio::test]
    async fn test_builder_with_storage_backend_builds_ok() {
        use crate::indexes::config::{StorageBackendConfig, StorageBackendSpec};

        let backend = StorageBackendConfig {
            id: "test-mem".to_string(),
            spec: StorageBackendSpec::Memory {
                enable_archive: false,
            },
        };
        let core = DrasiLibBuilder::new()
            .add_storage_backend(backend)
            .build()
            .await;
        assert!(core.is_ok(), "Builder with storage backend should succeed");
    }

    // ==========================================================================
    // Query Builder Unit Tests
    // ==========================================================================

    #[test]
    fn test_query_cypher_sets_id_and_language() {
        let q = Query::cypher("cypher-q");
        assert_eq!(q.id, "cypher-q");
        assert_eq!(q.query_language, QueryLanguage::Cypher);
    }

    #[test]
    fn test_query_gql_sets_id_and_language() {
        let q = Query::gql("gql-q");
        assert_eq!(q.id, "gql-q");
        assert_eq!(q.query_language, QueryLanguage::GQL);
    }

    #[test]
    fn test_query_from_source_adds_source() {
        let q = Query::cypher("q").from_source("src1");
        assert_eq!(q.sources.len(), 1);
        assert_eq!(q.sources[0].source_id, "src1");
    }

    #[test]
    fn test_query_from_source_chaining() {
        let q = Query::cypher("q")
            .from_source("src1")
            .from_source("src2")
            .from_source("src3");
        assert_eq!(q.sources.len(), 3);
        assert_eq!(q.sources[0].source_id, "src1");
        assert_eq!(q.sources[1].source_id, "src2");
        assert_eq!(q.sources[2].source_id, "src3");
    }

    #[test]
    fn test_query_auto_start_default_true() {
        let q = Query::cypher("q");
        assert!(q.auto_start);
    }

    #[test]
    fn test_query_auto_start_false() {
        let q = Query::cypher("q").auto_start(false);
        assert!(!q.auto_start);
    }

    #[test]
    fn test_query_enable_bootstrap_default_true() {
        let q = Query::cypher("q");
        assert!(q.enable_bootstrap);
    }

    #[test]
    fn test_query_enable_bootstrap_false() {
        let q = Query::cypher("q").enable_bootstrap(false);
        assert!(!q.enable_bootstrap);
    }

    #[test]
    fn test_query_bootstrap_buffer_size_default() {
        let q = Query::cypher("q");
        assert_eq!(q.bootstrap_buffer_size, 10000);
    }

    #[test]
    fn test_query_with_bootstrap_buffer_size() {
        let q = Query::cypher("q").with_bootstrap_buffer_size(5000);
        assert_eq!(q.bootstrap_buffer_size, 5000);
    }

    #[test]
    fn test_query_with_dispatch_mode_broadcast() {
        let q = Query::cypher("q").with_dispatch_mode(DispatchMode::Broadcast);
        assert_eq!(q.dispatch_mode, Some(DispatchMode::Broadcast));
    }

    #[test]
    fn test_query_with_dispatch_mode_channel() {
        let q = Query::cypher("q").with_dispatch_mode(DispatchMode::Channel);
        assert_eq!(q.dispatch_mode, Some(DispatchMode::Channel));
    }

    #[test]
    fn test_query_dispatch_mode_default_none() {
        let q = Query::cypher("q");
        assert_eq!(q.dispatch_mode, None);
    }

    #[test]
    fn test_query_with_priority_queue_capacity() {
        let q = Query::cypher("q").with_priority_queue_capacity(50000);
        assert_eq!(q.priority_queue_capacity, Some(50000));
    }

    #[test]
    fn test_query_priority_queue_capacity_default_none() {
        let q = Query::cypher("q");
        assert_eq!(q.priority_queue_capacity, None);
    }

    #[test]
    fn test_query_with_dispatch_buffer_capacity() {
        let q = Query::cypher("q").with_dispatch_buffer_capacity(5000);
        assert_eq!(q.dispatch_buffer_capacity, Some(5000));
    }

    #[test]
    fn test_query_dispatch_buffer_capacity_default_none() {
        let q = Query::cypher("q");
        assert_eq!(q.dispatch_buffer_capacity, None);
    }

    #[test]
    fn test_query_build_propagates_all_fields() {
        let config = Query::cypher("full-query")
            .query("MATCH (n:Person) RETURN n.name")
            .from_source("source-a")
            .from_source("source-b")
            .auto_start(false)
            .enable_bootstrap(false)
            .with_bootstrap_buffer_size(5000)
            .with_priority_queue_capacity(50000)
            .with_dispatch_buffer_capacity(2500)
            .with_dispatch_mode(DispatchMode::Broadcast)
            .build();

        assert_eq!(config.id, "full-query");
        assert_eq!(config.query, "MATCH (n:Person) RETURN n.name");
        assert_eq!(config.query_language, QueryLanguage::Cypher);
        assert_eq!(config.sources.len(), 2);
        assert_eq!(config.sources[0].source_id, "source-a");
        assert_eq!(config.sources[1].source_id, "source-b");
        assert!(!config.auto_start);
        assert!(!config.enable_bootstrap);
        assert_eq!(config.bootstrap_buffer_size, 5000);
        assert_eq!(config.priority_queue_capacity, Some(50000));
        assert_eq!(config.dispatch_buffer_capacity, Some(2500));
        assert_eq!(config.dispatch_mode, Some(DispatchMode::Broadcast));
        assert!(config.joins.is_none());
        assert!(config.middleware.is_empty());
        assert!(config.storage_backend.is_none());
    }

    #[test]
    fn test_query_build_gql_propagates_language() {
        let config = Query::gql("gql-full")
            .query("MATCH (n) RETURN n")
            .from_source("src")
            .build();

        assert_eq!(config.id, "gql-full");
        assert_eq!(config.query_language, QueryLanguage::GQL);
        assert_eq!(config.query, "MATCH (n) RETURN n");
        assert_eq!(config.sources.len(), 1);
        // Verify defaults are preserved through build
        assert!(config.auto_start);
        assert!(config.enable_bootstrap);
        assert_eq!(config.bootstrap_buffer_size, 10000);
    }

    #[test]
    fn test_query_build_defaults() {
        let config = Query::cypher("defaults-only").build();

        assert_eq!(config.id, "defaults-only");
        assert_eq!(config.query, "");
        assert_eq!(config.query_language, QueryLanguage::Cypher);
        assert!(config.sources.is_empty());
        assert!(config.middleware.is_empty());
        assert!(config.auto_start);
        assert!(config.joins.is_none());
        assert!(config.enable_bootstrap);
        assert_eq!(config.bootstrap_buffer_size, 10000);
        assert_eq!(config.priority_queue_capacity, None);
        assert_eq!(config.dispatch_buffer_capacity, None);
        assert_eq!(config.dispatch_mode, None);
        assert!(config.storage_backend.is_none());
    }
}