shardex 0.1.0

A high-performance memory-mapped vector search engine with ACID transactions and incremental updates
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
//! ShardexContext - Central context object for ApiThing pattern
//!
//! This module provides the `ShardexContext` struct that serves as the central
//! state holder for all Shardex operations following the ApiThing pattern.
//! The context manages the lifecycle of the index, configuration, and monitoring
//! components without requiring immediate index initialization.
//!
//! # Usage Examples
//!
//! ## Creating a Context
//!
//! ```rust
//! use shardex::api::ShardexContext;
//! use shardex::ShardexConfig;
//!
//! // Create with default configuration
//! let context = ShardexContext::new();
//! assert!(!context.is_initialized());
//!
//! // Create with custom configuration
//! let config = ShardexConfig::new()
//!     .directory_path("./my_index")
//!     .vector_size(768);
//!
//! let context = ShardexContext::with_config(config);
//! assert_eq!(context.get_config().vector_size, 768);
//! ```
//!
//! ## State Management
//!
//! ```rust
//! use shardex::api::ShardexContext;
//! use shardex::ShardexConfig;
//!
//! let mut context = ShardexContext::new()
//!     .set_directory_path("./test_index");
//!
//! // Context can be created without initializing the index
//! assert!(!context.is_initialized());
//!
//! // Configuration can be updated
//! let new_config = ShardexConfig::new().vector_size(512);
//! context.update_config(new_config).expect("Valid config");
//! assert_eq!(context.get_config().vector_size, 512);
//! ```

use crate::config::ShardexConfig;
use crate::error::ShardexError;
use crate::monitoring::{DetailedIndexStats, PerformanceMonitor as MonitoringPerformanceMonitor};
use crate::shardex::ShardexImpl;
use std::collections::HashMap;
use std::path::PathBuf;
use std::time::{Duration, Instant};

/// Performance metrics tracking for batch operations
#[derive(Debug, Clone, Default)]
pub struct PerformanceTracker {
    /// Whether performance tracking is currently active
    pub is_active: bool,
    /// Start time for the current tracking session
    pub start_time: Option<Instant>,
    /// Total operation count across all types
    pub total_operations: u64,
    /// Operation timing breakdown by operation type
    pub operation_timings: HashMap<String, Vec<Duration>>,
    /// Cumulative time spent on different operation categories
    pub cumulative_timings: HashMap<String, Duration>,
}

impl PerformanceTracker {
    /// Create a new performance tracker
    pub fn new() -> Self {
        Self::default()
    }

    /// Start performance tracking
    pub fn start(&mut self) {
        self.is_active = true;
        self.start_time = Some(Instant::now());
    }

    /// Stop performance tracking
    pub fn stop(&mut self) {
        self.is_active = false;
        self.start_time = None;
    }

    /// Record an operation with its duration
    pub fn record_operation(&mut self, operation_type: &str, duration: Duration) {
        if !self.is_active {
            return;
        }

        self.total_operations += 1;

        // Track individual operation timings
        self.operation_timings
            .entry(operation_type.to_string())
            .or_default()
            .push(duration);

        // Track cumulative timings
        *self
            .cumulative_timings
            .entry(operation_type.to_string())
            .or_insert(Duration::ZERO) += duration;
    }

    /// Get average latency for a specific operation type
    pub fn average_latency(&self, operation_type: &str) -> Option<Duration> {
        self.operation_timings.get(operation_type).map(|timings| {
            if timings.is_empty() {
                Duration::ZERO
            } else {
                let total: Duration = timings.iter().sum();
                total / timings.len() as u32
            }
        })
    }

    /// Get overall average latency across all operations
    pub fn overall_average_latency(&self) -> Duration {
        if self.total_operations == 0 {
            return Duration::ZERO;
        }

        let total_time: Duration = self.cumulative_timings.values().sum();
        total_time / self.total_operations as u32
    }

    /// Calculate throughput in operations per second
    pub fn throughput(&self) -> f64 {
        if let Some(start_time) = self.start_time {
            let elapsed = start_time.elapsed().as_secs_f64();
            if elapsed > 0.0 {
                self.total_operations as f64 / elapsed
            } else {
                0.0
            }
        } else {
            0.0
        }
    }

    /// Reset all tracking data
    pub fn reset(&mut self) {
        self.total_operations = 0;
        self.operation_timings.clear();
        self.cumulative_timings.clear();
        if self.is_active {
            self.start_time = Some(Instant::now());
        }
    }
}

/// Central context object for Shardex operations following the ApiThing pattern.
///
/// `ShardexContext` holds shared state for all operations and manages the lifecycle
/// of Shardex components. The context can be created without requiring immediate
/// index initialization, allowing for flexible configuration and setup.
///
/// # Key Features
///
/// - **Lazy Initialization**: Index creation/opening can be deferred
/// - **Configuration Management**: Centralized config with validation
/// - **Performance Monitoring**: Integrated stats and monitoring
/// - **State Tracking**: Clear state management and lifecycle control
/// - **Batch Processing Support**: Performance tracking for batch operations
///
/// # Thread Safety
///
/// The context is designed to be used in single-threaded scenarios within
/// an ApiThing operation. For concurrent access, wrap in appropriate
/// synchronization primitives.
pub struct ShardexContext {
    /// Core index functionality (None until initialized)
    index: Option<ShardexImpl>,

    /// Configuration (can be updated)
    config: ShardexConfig,

    /// State tracking (None until index is opened/created)
    stats: Option<DetailedIndexStats>,

    /// Performance monitoring (None until enabled)
    monitor: Option<MonitoringPerformanceMonitor>,

    /// Directory path for the index (can be different from config)
    directory_path: Option<PathBuf>,

    /// Performance tracking for batch operations
    performance_tracker: PerformanceTracker,

    /// Text storage configuration and statistics
    text_storage_enabled: bool,

    /// Maximum document text size if text storage is enabled
    max_document_text_size: Option<usize>,
}

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

impl std::fmt::Debug for ShardexContext {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ShardexContext")
            .field("index", &self.index.is_some())
            .field("config", &self.config)
            .field("stats", &self.stats)
            .field("monitor", &self.monitor.is_some())
            .field("directory_path", &self.directory_path)
            .field("performance_tracker", &self.performance_tracker)
            .field("text_storage_enabled", &self.text_storage_enabled)
            .field("max_document_text_size", &self.max_document_text_size)
            .finish()
    }
}

impl ShardexContext {
    /// Create a new empty context with default configuration.
    ///
    /// The returned context will not have an initialized index and will use
    /// default `ShardexConfig` values. Call `is_initialized()` to check if
    /// the index has been created or opened.
    ///
    /// # Example
    ///
    /// ```rust
    /// use shardex::api::ShardexContext;
    ///
    /// let context = ShardexContext::new();
    /// assert!(!context.is_initialized());
    /// assert_eq!(context.get_config().vector_size, 384); // Default
    /// ```
    pub fn new() -> Self {
        Self {
            index: None,
            config: ShardexConfig::default(),
            stats: None,
            monitor: None,
            directory_path: None,
            performance_tracker: PerformanceTracker::new(),
            text_storage_enabled: false,
            max_document_text_size: None,
        }
    }

    /// Create a context with the specified configuration.
    ///
    /// The configuration will be validated when `build()` is called or when
    /// the index is initialized. Invalid configurations will cause operations
    /// to fail at that point.
    ///
    /// # Arguments
    ///
    /// * `config` - The ShardexConfig to use for this context
    ///
    /// # Example
    ///
    /// ```rust
    /// use shardex::api::ShardexContext;
    /// use shardex::ShardexConfig;
    ///
    /// let config = ShardexConfig::new()
    ///     .vector_size(768)
    ///     .shard_size(50000);
    ///
    /// let context = ShardexContext::with_config(config);
    /// assert_eq!(context.get_config().vector_size, 768);
    /// assert_eq!(context.get_config().shard_size, 50000);
    /// ```
    pub fn with_config(config: ShardexConfig) -> Self {
        let text_storage_enabled = config.max_document_text_size > 0;
        let max_document_text_size = if text_storage_enabled {
            Some(config.max_document_text_size)
        } else {
            None
        };

        Self {
            index: None,
            config,
            stats: None,
            monitor: None,
            directory_path: None,
            performance_tracker: PerformanceTracker::new(),
            text_storage_enabled,
            max_document_text_size,
        }
    }

    /// Check if the index is initialized (created or opened).
    ///
    /// Returns `true` if the underlying `ShardexImpl` has been created or opened,
    /// `false` otherwise. Operations that require an initialized index will
    /// fail if this returns `false`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use shardex::api::ShardexContext;
    ///
    /// let context = ShardexContext::new();
    /// assert!(!context.is_initialized());
    ///
    /// // After create() or open() operations (not shown)
    /// // assert!(context.is_initialized());
    /// ```
    pub fn is_initialized(&self) -> bool {
        self.index.is_some()
    }

    /// Get a reference to the current configuration.
    ///
    /// Returns the `ShardexConfig` currently held by this context. The
    /// configuration may be updated using `update_config()`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use shardex::api::ShardexContext;
    /// use shardex::ShardexConfig;
    ///
    /// let config = ShardexConfig::new().vector_size(512);
    /// let context = ShardexContext::with_config(config);
    ///
    /// assert_eq!(context.get_config().vector_size, 512);
    /// ```
    pub fn get_config(&self) -> &ShardexConfig {
        &self.config
    }

    /// Get the current index statistics if available.
    ///
    /// Returns `Some(DetailedIndexStats)` if the index is initialized and
    /// statistics are available, `None` otherwise. Statistics are typically
    /// available after the index has been opened or created.
    ///
    /// # Example
    ///
    /// ```rust
    /// use shardex::api::ShardexContext;
    ///
    /// let context = ShardexContext::new();
    /// assert!(context.get_stats().is_none());
    ///
    /// // After index initialization (not shown)
    /// // let stats = context.get_stats().expect("Stats available");
    /// // println!("Total postings: {}", stats.total_postings);
    /// ```
    pub fn get_stats(&self) -> Option<&DetailedIndexStats> {
        self.stats.as_ref()
    }

    /// Get the current directory path if set.
    ///
    /// Returns the directory path that will be used for index operations,
    /// or `None` if no path has been explicitly set. When `None`, the
    /// path from the configuration will be used.
    pub fn get_directory_path(&self) -> Option<&PathBuf> {
        self.directory_path.as_ref()
    }

    /// Get the effective directory path for operations.
    ///
    /// Returns the directory path that will actually be used for index
    /// operations. This is either the explicitly set directory path or
    /// the path from the configuration.
    pub fn effective_directory_path(&self) -> &PathBuf {
        self.directory_path
            .as_ref()
            .unwrap_or(&self.config.directory_path)
    }

    /// Set the directory path for index operations.
    ///
    /// This sets an explicit directory path that will be used instead of
    /// the path in the configuration. Useful when the same configuration
    /// needs to be used with different directories.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the directory where the index will be stored
    ///
    /// # Example
    ///
    /// ```rust
    /// use shardex::api::ShardexContext;
    ///
    /// let context = ShardexContext::new()
    ///     .set_directory_path("./custom_index");
    ///
    /// assert_eq!(
    ///     context.get_directory_path().unwrap().to_str().unwrap(),
    ///     "./custom_index"
    /// );
    /// ```
    pub fn set_directory_path<P: Into<PathBuf>>(mut self, path: P) -> Self {
        self.directory_path = Some(path.into());
        self
    }

    /// Update the context configuration.
    ///
    /// Replaces the current configuration with a new one. The configuration
    /// will be validated before being applied. If the index is already
    /// initialized, some configuration changes may not take effect until
    /// the next create/open operation.
    ///
    /// # Arguments
    ///
    /// * `config` - The new configuration to apply
    ///
    /// # Errors
    ///
    /// Returns `ShardexError::Config` if the configuration is invalid.
    ///
    /// # Example
    ///
    /// ```rust
    /// use shardex::api::ShardexContext;
    /// use shardex::ShardexConfig;
    ///
    /// let mut context = ShardexContext::new();
    /// assert_eq!(context.get_config().vector_size, 384);
    ///
    /// let new_config = ShardexConfig::new().vector_size(768);
    /// context.update_config(new_config).expect("Valid config");
    /// assert_eq!(context.get_config().vector_size, 768);
    /// ```
    pub fn update_config(&mut self, config: ShardexConfig) -> Result<(), ShardexError> {
        // Validate the configuration before applying
        config.validate()?;

        // Update text storage configuration
        self.text_storage_enabled = config.max_document_text_size > 0;
        self.max_document_text_size = if self.text_storage_enabled {
            Some(config.max_document_text_size)
        } else {
            None
        };

        self.config = config;
        Ok(())
    }

    /// Clear the directory path override.
    ///
    /// After calling this method, `effective_directory_path()` will return
    /// the path from the configuration instead of an explicitly set path.
    pub fn clear_directory_path(&mut self) {
        self.directory_path = None;
    }

    /// Check if performance monitoring is enabled.
    ///
    /// Returns `true` if a performance monitor is attached to this context.
    pub fn has_monitor(&self) -> bool {
        self.monitor.is_some()
    }

    /// Get a reference to the performance monitor if available.
    pub fn get_monitor(&self) -> Option<&MonitoringPerformanceMonitor> {
        self.monitor.as_ref()
    }

    /// Start performance tracking for batch operations
    ///
    /// Enables performance monitoring and resets any existing tracking data.
    /// This should be called before starting a batch operation sequence.
    ///
    /// # Example
    ///
    /// ```rust
    /// use shardex::api::ShardexContext;
    ///
    /// let mut context = ShardexContext::new();
    /// context.start_performance_tracking();
    /// assert!(context.is_performance_tracking_active());
    /// ```
    pub fn start_performance_tracking(&mut self) {
        self.performance_tracker.start();
    }

    /// Stop performance tracking for batch operations
    ///
    /// Disables performance monitoring. Tracked data remains available
    /// for retrieval until the next start or reset.
    ///
    /// # Example
    ///
    /// ```rust
    /// use shardex::api::ShardexContext;
    ///
    /// let mut context = ShardexContext::new();
    /// context.start_performance_tracking();
    /// context.stop_performance_tracking();
    /// assert!(!context.is_performance_tracking_active());
    /// ```
    pub fn stop_performance_tracking(&mut self) {
        self.performance_tracker.stop();
    }

    /// Record an operation with its duration for performance tracking
    ///
    /// If performance tracking is active, records the operation type and
    /// duration for later analysis. This is typically called automatically
    /// by batch operations.
    ///
    /// # Arguments
    ///
    /// * `operation` - Name of the operation type (e.g., "batch_add", "search", "flush")
    /// * `duration` - Time taken to complete the operation
    ///
    /// # Example
    ///
    /// ```rust
    /// use shardex::api::ShardexContext;
    /// use std::time::Duration;
    ///
    /// let mut context = ShardexContext::new();
    /// context.start_performance_tracking();
    /// context.record_operation("test_operation", Duration::from_millis(100));
    /// ```
    pub fn record_operation(&mut self, operation: &str, duration: Duration) {
        self.performance_tracker
            .record_operation(operation, duration);
    }

    /// Get current performance statistics from the tracking system
    ///
    /// Returns performance metrics collected since tracking was started.
    /// This includes operation counts, average latencies, and throughput data.
    ///
    /// # Returns
    ///
    /// Returns performance data including:
    /// - Total operations performed
    /// - Average latency across all operations
    /// - Current throughput (operations per second)
    /// - Operation-specific breakdowns
    ///
    /// # Example
    ///
    /// ```rust
    /// use shardex::api::ShardexContext;
    /// use std::time::Duration;
    ///
    /// let mut context = ShardexContext::new();
    /// context.start_performance_tracking();
    /// context.record_operation("test", Duration::from_millis(50));
    ///
    /// let total_ops = context.get_total_operations();
    /// let avg_latency = context.get_average_latency();
    /// let throughput = context.get_throughput();
    ///
    /// assert_eq!(total_ops, 1);
    /// assert!(avg_latency.as_millis() > 0);
    /// assert!(throughput >= 0.0);
    /// ```
    pub fn get_total_operations(&self) -> u64 {
        self.performance_tracker.total_operations
    }

    /// Get the average latency across all tracked operations
    pub fn get_average_latency(&self) -> Duration {
        self.performance_tracker.overall_average_latency()
    }

    /// Get the current throughput in operations per second
    pub fn get_throughput(&self) -> f64 {
        self.performance_tracker.throughput()
    }

    /// Get the average latency for a specific operation type
    pub fn get_operation_average_latency(&self, operation_type: &str) -> Option<Duration> {
        self.performance_tracker.average_latency(operation_type)
    }

    /// Check if performance tracking is currently active
    pub fn is_performance_tracking_active(&self) -> bool {
        self.performance_tracker.is_active
    }

    /// Reset performance tracking data
    ///
    /// Clears all accumulated performance data while maintaining the
    /// current active/inactive state of the tracker.
    pub fn reset_performance_tracking(&mut self) {
        self.performance_tracker.reset();
    }

    /// Get a reference to the performance tracker for detailed analysis
    pub fn get_performance_tracker(&self) -> &PerformanceTracker {
        &self.performance_tracker
    }

    /// Check if text storage is enabled for this context
    ///
    /// Returns `true` if the index configuration supports document text storage,
    /// `false` otherwise. Text storage is enabled when max_document_text_size > 0.
    pub fn is_text_storage_enabled(&self) -> bool {
        self.text_storage_enabled
    }

    /// Get the maximum document text size if text storage is enabled
    ///
    /// Returns `Some(size)` if text storage is enabled, `None` otherwise.
    pub fn get_max_document_text_size(&self) -> Option<usize> {
        self.max_document_text_size
    }

    /// Validate that text storage is available for the given text size
    ///
    /// Checks that text storage is enabled and the text size is within limits.
    ///
    /// # Arguments
    ///
    /// * `text_size` - Size of the text to validate in bytes
    ///
    /// # Errors
    ///
    /// Returns `ShardexError::Config` if text storage is not enabled or
    /// the text size exceeds the configured maximum.
    pub fn validate_text_storage(&self, text_size: usize) -> Result<(), ShardexError> {
        if !self.text_storage_enabled {
            return Err(ShardexError::config_error(
                "text_storage",
                "text storage is not enabled",
                "Enable text storage by setting max_document_text_size > 0 in the configuration",
            ));
        }

        if let Some(max_size) = self.max_document_text_size {
            if text_size > max_size {
                return Err(ShardexError::config_error(
                    "text_size",
                    format!("text size {} bytes exceeds maximum {}", text_size, max_size),
                    format!(
                        "Reduce document size or increase max_document_text_size to at least {} bytes",
                        text_size
                    ),
                ));
            }
        }

        Ok(())
    }

    /// Validate that text storage is available for multiple documents
    ///
    /// Checks that text storage is enabled and all document text sizes are within limits.
    ///
    /// # Arguments
    ///
    /// * `text_sizes` - Iterator of text sizes to validate in bytes
    ///
    /// # Errors
    ///
    /// Returns `ShardexError::Config` if text storage is not enabled or
    /// any text size exceeds the configured maximum.
    pub fn validate_batch_text_storage<I>(&self, text_sizes: I) -> Result<(), ShardexError>
    where
        I: IntoIterator<Item = usize>,
    {
        if !self.text_storage_enabled {
            return Err(ShardexError::config_error(
                "text_storage",
                "text storage is not enabled",
                "Enable text storage by setting max_document_text_size > 0 in the configuration",
            ));
        }

        if let Some(max_size) = self.max_document_text_size {
            for (i, text_size) in text_sizes.into_iter().enumerate() {
                if text_size > max_size {
                    return Err(ShardexError::config_error(
                        format!("text_sizes[{}]", i),
                        format!("text size {} bytes exceeds maximum {}", text_size, max_size),
                        format!(
                            "Reduce document sizes or increase max_document_text_size to handle {} bytes",
                            text_size
                        ),
                    ));
                }
            }
        }

        Ok(())
    }

    /// Internal method to set the index implementation.
    ///
    /// This is used by operations that create or open indices to store
    /// the initialized index in the context.
    pub(crate) fn set_index(&mut self, index: ShardexImpl) {
        self.index = Some(index);
    }

    /// Internal method to get a reference to the index implementation.
    ///
    /// Returns `Some(&ShardexImpl)` if the index is initialized, `None` otherwise.
    /// This is used by operations that need to access the underlying index.
    pub(crate) fn get_index(&self) -> Option<&ShardexImpl> {
        self.index.as_ref()
    }

    /// Internal method to get a mutable reference to the index implementation.
    ///
    /// Returns `Some(&mut ShardexImpl)` if the index is initialized, `None` otherwise.
    /// This is used by operations that need to modify the underlying index.
    pub(crate) fn get_index_mut(&mut self) -> Option<&mut ShardexImpl> {
        self.index.as_mut()
    }
}

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

    #[test]
    fn test_new_context() {
        let context = ShardexContext::new();
        assert!(!context.is_initialized());
        assert!(context.get_stats().is_none());
        assert!(context.get_directory_path().is_none());
        assert!(!context.has_monitor());
    }

    #[test]
    fn test_default_context() {
        let context = ShardexContext::default();
        assert_eq!(context.get_config().vector_size, 384); // Default
        assert!(!context.is_initialized());
    }

    #[test]
    fn test_with_config() {
        let config = ShardexConfig::new().vector_size(768).shard_size(50000);
        let context = ShardexContext::with_config(config);

        assert_eq!(context.get_config().vector_size, 768);
        assert_eq!(context.get_config().shard_size, 50000);
        assert!(!context.is_initialized());
    }

    #[test]
    fn test_set_directory_path() {
        let context = ShardexContext::new().set_directory_path("./test_index");

        assert_eq!(context.get_directory_path().unwrap().to_str().unwrap(), "./test_index");
        assert_eq!(context.effective_directory_path().to_str().unwrap(), "./test_index");
    }

    #[test]
    fn test_effective_directory_path_fallback() {
        let config = ShardexConfig::new().directory_path("./config_path");
        let context = ShardexContext::with_config(config);

        // Should use config path when no explicit path is set
        assert_eq!(context.effective_directory_path().to_str().unwrap(), "./config_path");
    }

    #[test]
    fn test_update_config_valid() {
        let mut context = ShardexContext::new();
        assert_eq!(context.get_config().vector_size, 384);

        let new_config = ShardexConfig::new().vector_size(768);
        let result = context.update_config(new_config);

        assert!(result.is_ok());
        assert_eq!(context.get_config().vector_size, 768);
    }

    #[test]
    fn test_update_config_invalid() {
        let mut context = ShardexContext::new();

        let invalid_config = ShardexConfig::new().vector_size(0); // Invalid
        let result = context.update_config(invalid_config);

        assert!(result.is_err());
        // Original config should be unchanged
        assert_eq!(context.get_config().vector_size, 384);
    }

    #[test]
    fn test_clear_directory_path() {
        let mut context = ShardexContext::new().set_directory_path("./test");

        assert!(context.get_directory_path().is_some());

        context.clear_directory_path();
        assert!(context.get_directory_path().is_none());
    }

    #[test]
    fn test_context_creation_equality() {
        let config = ShardexConfig::new().vector_size(512);
        let context1 = ShardexContext::with_config(config.clone()).set_directory_path("./test");

        let context2 = ShardexContext::with_config(config).set_directory_path("./test");

        assert_eq!(context1.get_config().vector_size, context2.get_config().vector_size);
        assert_eq!(context1.get_directory_path(), context2.get_directory_path());
        assert_eq!(context1.is_initialized(), context2.is_initialized());
    }

    #[test]
    fn test_context_debug() {
        let context = ShardexContext::new();
        let debug_str = format!("{:?}", context);
        assert!(debug_str.contains("ShardexContext"));
        assert!(debug_str.contains("performance_tracker"));
    }

    #[test]
    fn test_performance_tracking() {
        let mut context = ShardexContext::new();

        assert!(!context.is_performance_tracking_active());
        assert_eq!(context.get_total_operations(), 0);

        // Start tracking
        context.start_performance_tracking();
        assert!(context.is_performance_tracking_active());

        // Record some operations
        context.record_operation("test_op", Duration::from_millis(100));
        context.record_operation("test_op", Duration::from_millis(200));
        context.record_operation("other_op", Duration::from_millis(50));

        assert_eq!(context.get_total_operations(), 3);
        assert!(context.get_average_latency().as_millis() > 0);
        assert!(context.get_throughput() >= 0.0);

        // Test operation-specific latency
        let test_op_latency = context.get_operation_average_latency("test_op");
        assert!(test_op_latency.is_some());
        assert_eq!(test_op_latency.unwrap(), Duration::from_millis(150)); // Average of 100 and 200

        let other_op_latency = context.get_operation_average_latency("other_op");
        assert!(other_op_latency.is_some());
        assert_eq!(other_op_latency.unwrap(), Duration::from_millis(50));

        // Stop tracking
        context.stop_performance_tracking();
        assert!(!context.is_performance_tracking_active());

        // Data should still be available
        assert_eq!(context.get_total_operations(), 3);
    }

    #[test]
    fn test_performance_tracking_reset() {
        let mut context = ShardexContext::new();

        context.start_performance_tracking();
        context.record_operation("test", Duration::from_millis(100));
        assert_eq!(context.get_total_operations(), 1);

        context.reset_performance_tracking();
        assert_eq!(context.get_total_operations(), 0);
        assert!(context.is_performance_tracking_active()); // Should still be active
    }

    #[test]
    fn test_performance_tracker_standalone() {
        let mut tracker = PerformanceTracker::new();

        assert!(!tracker.is_active);
        assert_eq!(tracker.total_operations, 0);

        tracker.start();
        assert!(tracker.is_active);

        tracker.record_operation("test", Duration::from_millis(100));
        tracker.record_operation("test", Duration::from_millis(200));

        assert_eq!(tracker.total_operations, 2);
        assert_eq!(tracker.average_latency("test"), Some(Duration::from_millis(150)));
        assert_eq!(tracker.overall_average_latency(), Duration::from_millis(150));

        tracker.stop();
        assert!(!tracker.is_active);
    }

    #[test]
    fn test_performance_tracker_throughput() {
        let mut tracker = PerformanceTracker::new();
        tracker.start();

        // Record operations and check that throughput can be calculated
        tracker.record_operation("test", Duration::from_millis(10));

        // Throughput should be calculable (operations per second)
        let throughput = tracker.throughput();
        assert!(throughput >= 0.0);
    }

    #[test]
    fn test_text_storage_configuration() {
        // Test with text storage disabled (default)
        let context = ShardexContext::new();
        assert!(!context.is_text_storage_enabled());
        assert!(context.get_max_document_text_size().is_none());

        // Test with text storage enabled
        let config = ShardexConfig::new().max_document_text_size(1024 * 1024); // 1MB
        let context = ShardexContext::with_config(config);
        assert!(context.is_text_storage_enabled());
        assert_eq!(context.get_max_document_text_size(), Some(1024 * 1024));
    }

    #[test]
    fn test_text_storage_validation() {
        // Test with text storage disabled
        let context = ShardexContext::new();
        let result = context.validate_text_storage(1000);
        assert!(result.is_err());

        // Test with text storage enabled
        let config = ShardexConfig::new().max_document_text_size(2048); // 2KB
        let context = ShardexContext::with_config(config);

        // Valid size should pass
        let result = context.validate_text_storage(1000);
        assert!(result.is_ok());

        // Size exceeding limit should fail
        let result = context.validate_text_storage(3000);
        assert!(result.is_err());

        // Size at limit should pass
        let result = context.validate_text_storage(2048);
        assert!(result.is_ok());
    }

    #[test]
    fn test_batch_text_storage_validation() {
        let config = ShardexConfig::new().max_document_text_size(1000);
        let context = ShardexContext::with_config(config);

        // All valid sizes
        let sizes = vec![500, 800, 1000];
        let result = context.validate_batch_text_storage(sizes);
        assert!(result.is_ok());

        // One size exceeds limit
        let sizes = vec![500, 1200, 800];
        let result = context.validate_batch_text_storage(sizes);
        assert!(result.is_err());

        // Empty batch should pass
        let sizes: Vec<usize> = vec![];
        let result = context.validate_batch_text_storage(sizes);
        assert!(result.is_ok());
    }

    #[test]
    fn test_config_update_with_text_storage() {
        let mut context = ShardexContext::new();
        assert!(!context.is_text_storage_enabled());

        // Enable text storage through config update
        let new_config = ShardexConfig::new().max_document_text_size(5 * 1024 * 1024);
        context.update_config(new_config).unwrap();

        assert!(context.is_text_storage_enabled());
        assert_eq!(context.get_max_document_text_size(), Some(5 * 1024 * 1024));

        // Disable text storage
        let disable_config = ShardexConfig::new().max_document_text_size(0);
        context.update_config(disable_config).unwrap();

        assert!(!context.is_text_storage_enabled());
        assert!(context.get_max_document_text_size().is_none());
    }

    #[test]
    fn test_context_debug_includes_text_storage() {
        let config = ShardexConfig::new().max_document_text_size(1024);
        let context = ShardexContext::with_config(config);
        let debug_str = format!("{:?}", context);

        assert!(debug_str.contains("text_storage_enabled: true"));
        assert!(debug_str.contains("max_document_text_size: Some(1024)"));
    }
}