laurus 0.4.0

Unified search library for lexical, vector, and semantic retrieval
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
//! Storage abstraction layer for Laurus.
//!
//! This module exposes a pluggable storage facade shared by the lexical, vector,
//! and hybrid engines. File and memory backends can be swapped without touching
//! higher-level code, making it easy to move from prototyping to production.
//!
//! # Architecture
//!
//! - **Storage trait**: Unified interface for all storage backends
//! - **StorageConfig enum**: Type-safe configuration for supported backends
//! - **StorageFactory**: Helper for constructing concrete storage instances
//!
//! # Storage Types
//!
//! ## FileStorage
//! - Disk-based persistent storage
//! - Supports memory-mapped files (mmap) for high-performance reads
//! - Configurable buffering, syncing, and locking
//!
//! ## MemoryStorage
//! - In-memory storage for testing and temporary data
//! - Fast but non-persistent
//!
//! # Example
//!
//! ```
//! use laurus::storage::{StorageFactory, StorageConfig};
//! use laurus::storage::file::FileStorageConfig;
//! use laurus::storage::memory::MemoryStorageConfig;
//!
//! # fn main() -> laurus::Result<()> {
//! // Create file storage with mmap enabled
//! let mut file_config = FileStorageConfig::new("/tmp/test_index");
//! file_config.use_mmap = true;
//! let storage = StorageFactory::create(StorageConfig::File(file_config))?;
//!
//! // Create memory storage
//! let storage = StorageFactory::create(StorageConfig::Memory(MemoryStorageConfig::default()))?;
//! # Ok(())
//! # }
//! ```

use std::io::{Read, Seek, Write};
use std::sync::Arc;

use crate::error::{LaurusError, Result};

pub mod column;
pub mod file;
pub mod memory;
pub mod prefixed;
pub mod structured;

/// File metadata information.
#[derive(Debug, Clone)]
pub struct FileMetadata {
    /// File size in bytes.
    pub size: u64,

    /// Last modified time (seconds since epoch).
    pub modified: u64,

    /// Creation time (seconds since epoch).
    pub created: u64,

    /// Whether the file is read-only.
    pub readonly: bool,
}

/// Preferred data loading mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LoadingMode {
    /// Eagerly load data into memory (eager deserialization).
    /// Best for raw performance when memory is sufficient.
    Eager,
    /// Lazily load data on command (e.g., via mmap).
    /// Best for large datasets to reduce memory usage.
    Lazy,
}

/// A trait for storage backends that can store and retrieve data.
///
/// This provides a pluggable interface for different storage implementations
/// like file system, memory, or remote storage.
pub trait Storage: Send + Sync + std::fmt::Debug {
    /// Get the preferred loading mode for this storage.
    fn loading_mode(&self) -> LoadingMode {
        LoadingMode::Eager
    }

    /// Open a file for reading.
    ///
    /// Opens an existing file and returns a `StorageInput` for reading its contents.
    /// The file must exist, or this will return an error.
    ///
    /// # Arguments
    ///
    /// * `name` - The name of the file to open (relative to the storage root)
    ///
    /// # Returns
    ///
    /// * `Ok(Box<dyn StorageInput>)` - A reader for accessing the file contents
    /// * `Err(LaurusError)` - If the file doesn't exist or cannot be opened
    ///
    /// # Example
    ///
    /// ```
    /// use laurus::storage::memory::{MemoryStorage, MemoryStorageConfig};
    /// use laurus::storage::Storage;
    /// use std::io::{Read, Write};
    ///
    /// # fn main() -> laurus::Result<()> {
    /// let storage = MemoryStorage::new(MemoryStorageConfig::default());
    ///
    /// // First create a file
    /// let mut output = storage.create_output("index.bin")?;
    /// output.write_all(b"test data")?;
    /// output.close()?;
    ///
    /// // Now open it for reading
    /// let mut input = storage.open_input("index.bin")?;
    /// let mut buffer = Vec::new();
    /// input.read_to_end(&mut buffer)?;
    /// assert_eq!(buffer, b"test data");
    /// # Ok(())
    /// # }
    /// ```
    fn open_input(&self, name: &str) -> Result<Box<dyn StorageInput>>;

    /// Create a file for writing.
    ///
    /// Creates a new file or truncates an existing file for writing.
    /// If the file already exists, its contents will be overwritten.
    ///
    /// # Arguments
    ///
    /// * `name` - The name of the file to create (relative to the storage root)
    ///
    /// # Returns
    ///
    /// * `Ok(Box<dyn StorageOutput>)` - A writer for writing data to the file
    /// * `Err(LaurusError)` - If the file cannot be created
    ///
    /// # Example
    ///
    /// ```
    /// use laurus::storage::memory::{MemoryStorage, MemoryStorageConfig};
    /// use laurus::storage::Storage;
    /// use std::io::Write;
    ///
    /// # fn main() -> laurus::Result<()> {
    /// let storage = MemoryStorage::new(MemoryStorageConfig::default());
    ///
    /// let mut output = storage.create_output("index.bin")?;
    /// output.write_all(b"Hello, World!")?;
    /// output.close()?;
    ///
    /// assert!(storage.file_exists("index.bin"));
    /// # Ok(())
    /// # }
    /// ```
    fn create_output(&self, name: &str) -> Result<Box<dyn StorageOutput>>;

    /// Create a file for appending.
    ///
    /// Opens an existing file for appending, or creates a new file if it doesn't exist.
    /// New data will be written at the end of the existing file contents.
    ///
    /// # Arguments
    ///
    /// * `name` - The name of the file to open/create (relative to the storage root)
    ///
    /// # Returns
    ///
    /// * `Ok(Box<dyn StorageOutput>)` - A writer positioned at the end of the file
    /// * `Err(LaurusError)` - If the file cannot be opened or created
    ///
    /// # Example
    ///
    /// ```
    /// use laurus::storage::memory::{MemoryStorage, MemoryStorageConfig};
    /// use laurus::storage::Storage;
    /// use std::io::{Read, Write};
    ///
    /// # fn main() -> laurus::Result<()> {
    /// let storage = MemoryStorage::new(MemoryStorageConfig::default());
    ///
    /// // Create file with initial content
    /// let mut output = storage.create_output("log.txt")?;
    /// output.write_all(b"First entry\n")?;
    /// output.close()?;
    ///
    /// // Append to the file
    /// let mut output = storage.create_output_append("log.txt")?;
    /// output.write_all(b"Second entry\n")?;
    /// output.close()?;
    ///
    /// // Verify both entries are present
    /// let mut input = storage.open_input("log.txt")?;
    /// let mut buffer = String::new();
    /// input.read_to_string(&mut buffer)?;
    /// assert!(buffer.contains("First entry"));
    /// assert!(buffer.contains("Second entry"));
    /// # Ok(())
    /// # }
    /// ```
    fn create_output_append(&self, name: &str) -> Result<Box<dyn StorageOutput>>;

    /// Check if a file exists.
    ///
    /// Tests whether a file with the given name exists in the storage.
    /// This is a fast operation that doesn't open the file.
    ///
    /// # Arguments
    ///
    /// * `name` - The name of the file to check (relative to the storage root)
    ///
    /// # Returns
    ///
    /// * `true` - If the file exists
    /// * `false` - If the file doesn't exist
    ///
    /// # Example
    ///
    /// ```
    /// use laurus::storage::memory::{MemoryStorage, MemoryStorageConfig};
    /// use laurus::storage::Storage;
    /// use std::io::Write;
    ///
    /// # fn main() -> laurus::Result<()> {
    /// let storage = MemoryStorage::new(MemoryStorageConfig::default());
    ///
    /// assert!(!storage.file_exists("index.bin"));
    ///
    /// let mut output = storage.create_output("index.bin")?;
    /// output.write_all(b"data")?;
    /// output.close()?;
    ///
    /// assert!(storage.file_exists("index.bin"));
    /// # Ok(())
    /// # }
    /// ```
    fn file_exists(&self, name: &str) -> bool;

    /// Delete a file.
    ///
    /// Removes a file from the storage. If the file doesn't exist, this may
    /// return an error or succeed silently, depending on the implementation.
    ///
    /// # Arguments
    ///
    /// * `name` - The name of the file to delete (relative to the storage root)
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If the file was successfully deleted
    /// * `Err(LaurusError)` - If the file cannot be deleted (e.g., permission denied)
    ///
    /// # Example
    ///
    /// ```
    /// use laurus::storage::memory::{MemoryStorage, MemoryStorageConfig};
    /// use laurus::storage::Storage;
    /// use std::io::Write;
    ///
    /// # fn main() -> laurus::Result<()> {
    /// let storage = MemoryStorage::new(MemoryStorageConfig::default());
    ///
    /// // Create a file
    /// let mut output = storage.create_output("temp.bin")?;
    /// output.write_all(b"temporary data")?;
    /// output.close()?;
    ///
    /// assert!(storage.file_exists("temp.bin"));
    ///
    /// // Delete the file
    /// storage.delete_file("temp.bin")?;
    ///
    /// assert!(!storage.file_exists("temp.bin"));
    /// # Ok(())
    /// # }
    /// ```
    fn delete_file(&self, name: &str) -> Result<()>;

    /// List all files in the storage.
    ///
    /// Returns a vector of all file names in the storage.
    /// This is useful for discovering what files exist, performing cleanup,
    /// or implementing backup/restore functionality.
    ///
    /// # Returns
    ///
    /// * `Ok(Vec<String>)` - A list of all file names in the storage
    /// * `Err(LaurusError)` - If the storage cannot be read
    ///
    /// # Example
    ///
    /// ```
    /// use laurus::storage::memory::{MemoryStorage, MemoryStorageConfig};
    /// use laurus::storage::Storage;
    /// use std::io::Write;
    ///
    /// # fn main() -> laurus::Result<()> {
    /// let storage = MemoryStorage::new(MemoryStorageConfig::default());
    ///
    /// // Create some files
    /// let mut output = storage.create_output("file1.bin")?;
    /// output.write_all(b"data1")?;
    /// output.close()?;
    ///
    /// let mut output = storage.create_output("file2.bin")?;
    /// output.write_all(b"data2")?;
    /// output.close()?;
    ///
    /// // List all files
    /// let files = storage.list_files()?;
    /// assert_eq!(files.len(), 2);
    /// assert!(files.contains(&"file1.bin".to_string()));
    /// assert!(files.contains(&"file2.bin".to_string()));
    /// # Ok(())
    /// # }
    /// ```
    fn list_files(&self) -> Result<Vec<String>>;

    /// Get the size of a file in bytes.
    ///
    /// Returns the size of the file without opening it for reading.
    /// This is faster than opening the file and seeking to the end.
    ///
    /// # Arguments
    ///
    /// * `name` - The name of the file (relative to the storage root)
    ///
    /// # Returns
    ///
    /// * `Ok(u64)` - The size of the file in bytes
    /// * `Err(LaurusError)` - If the file doesn't exist or cannot be accessed
    ///
    /// # Example
    ///
    /// ```
    /// use laurus::storage::memory::{MemoryStorage, MemoryStorageConfig};
    /// use laurus::storage::Storage;
    /// use std::io::Write;
    ///
    /// # fn main() -> laurus::Result<()> {
    /// let storage = MemoryStorage::new(MemoryStorageConfig::default());
    ///
    /// // Create a file with known size
    /// let mut output = storage.create_output("index.bin")?;
    /// output.write_all(b"12345")?;
    /// output.close()?;
    ///
    /// // Get file size
    /// let size = storage.file_size("index.bin")?;
    /// assert_eq!(size, 5);
    /// # Ok(())
    /// # }
    /// ```
    fn file_size(&self, name: &str) -> Result<u64>;

    /// Get file metadata.
    ///
    /// Returns detailed metadata about a file, including size, modification time,
    /// creation time, and read-only status. This is useful for versioning,
    /// cache invalidation, and file management.
    ///
    /// # Arguments
    ///
    /// * `name` - The name of the file (relative to the storage root)
    ///
    /// # Returns
    ///
    /// * `Ok(FileMetadata)` - Metadata structure with file information
    /// * `Err(LaurusError)` - If the file doesn't exist or cannot be accessed
    ///
    /// # Example
    ///
    /// ```
    /// use laurus::storage::memory::{MemoryStorage, MemoryStorageConfig};
    /// use laurus::storage::Storage;
    /// use std::io::Write;
    ///
    /// # fn main() -> laurus::Result<()> {
    /// let storage = MemoryStorage::new(MemoryStorageConfig::default());
    ///
    /// // Create a file
    /// let mut output = storage.create_output("index.bin")?;
    /// output.write_all(b"test")?;
    /// output.close()?;
    ///
    /// // Get metadata
    /// let meta = storage.metadata("index.bin")?;
    /// assert_eq!(meta.size, 4);
    /// assert!(meta.modified > 0);
    /// # Ok(())
    /// # }
    /// ```
    fn metadata(&self, name: &str) -> Result<FileMetadata>;

    /// Rename a file.
    ///
    /// Atomically renames a file from `old_name` to `new_name`.
    /// This is commonly used for atomic file replacement: write to a temporary file,
    /// then rename it to the final name to ensure readers never see partial data.
    ///
    /// # Arguments
    ///
    /// * `old_name` - The current name of the file
    /// * `new_name` - The new name for the file
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If the file was successfully renamed
    /// * `Err(LaurusError)` - If the source file doesn't exist, destination exists,
    ///   or the operation fails
    ///
    /// # Example
    ///
    /// ```
    /// use laurus::storage::memory::{MemoryStorage, MemoryStorageConfig};
    /// use laurus::storage::Storage;
    /// use std::io::Write;
    ///
    /// # fn main() -> laurus::Result<()> {
    /// let storage = MemoryStorage::new(MemoryStorageConfig::default());
    ///
    /// // Create a temp file
    /// let mut output = storage.create_output("index.tmp")?;
    /// output.write_all(b"new data")?;
    /// output.close()?;
    ///
    /// // Rename to final name (atomic replacement pattern)
    /// storage.rename_file("index.tmp", "index.bin")?;
    ///
    /// assert!(storage.file_exists("index.bin"));
    /// assert!(!storage.file_exists("index.tmp"));
    /// # Ok(())
    /// # }
    /// ```
    fn rename_file(&self, old_name: &str, new_name: &str) -> Result<()>;

    /// Create a temporary file.
    ///
    /// Creates a file with a unique name based on the given prefix.
    /// The actual filename will include additional characters to ensure uniqueness.
    /// Temporary files should be cleaned up by the caller when no longer needed.
    ///
    /// # Arguments
    ///
    /// * `prefix` - A prefix for the temporary file name (e.g., "temp_")
    ///
    /// # Returns
    ///
    /// * `Ok((String, Box<dyn StorageOutput>))` - A tuple of (filename, writer)
    ///   where filename is the unique generated name
    /// * `Err(LaurusError)` - If the temporary file cannot be created
    ///
    /// # Example
    ///
    /// ```
    /// use laurus::storage::memory::{MemoryStorage, MemoryStorageConfig};
    /// use laurus::storage::Storage;
    /// use std::io::Write;
    ///
    /// # fn main() -> laurus::Result<()> {
    /// let storage = MemoryStorage::new(MemoryStorageConfig::default());
    ///
    /// // Create a temp file with prefix
    /// let (temp_name, mut temp_output) = storage.create_temp_output("merge_")?;
    /// temp_output.write_all(b"temporary data")?;
    /// temp_output.close()?;
    ///
    /// // Verify file was created
    /// assert!(storage.file_exists(&temp_name));
    /// assert!(temp_name.starts_with("merge_"));
    ///
    /// // Clean up
    /// storage.delete_file(&temp_name)?;
    /// # Ok(())
    /// # }
    /// ```
    fn create_temp_output(&self, prefix: &str) -> Result<(String, Box<dyn StorageOutput>)>;

    /// Sync all pending writes to storage.
    ///
    /// Ensures that all buffered writes are flushed and persisted to the underlying
    /// storage medium (disk, network, etc.). This is important for durability:
    /// after sync() returns, data should survive system crashes.
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If all pending writes were successfully synced
    /// * `Err(LaurusError)` - If the sync operation fails
    ///
    /// # Example
    ///
    /// ```
    /// use laurus::storage::memory::{MemoryStorage, MemoryStorageConfig};
    /// use laurus::storage::Storage;
    /// use std::io::Write;
    ///
    /// # fn main() -> laurus::Result<()> {
    /// let storage = MemoryStorage::new(MemoryStorageConfig::default());
    ///
    /// // Write some data
    /// let mut output = storage.create_output("important.dat")?;
    /// output.write_all(b"critical data")?;
    /// output.close()?;
    ///
    /// // Ensure data is persisted
    /// storage.sync()?;
    /// # Ok(())
    /// # }
    /// ```
    fn sync(&self) -> Result<()>;

    /// Close the storage and release resources.
    ///
    /// Closes all open files, flushes pending writes, and releases any resources
    /// held by the storage. After calling close(), the storage should not be used.
    /// This method takes `&mut self` to ensure exclusive access during cleanup.
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If the storage was successfully closed
    /// * `Err(LaurusError)` - If errors occur during cleanup (though resources
    ///   should still be released as much as possible)
    ///
    /// # Example
    ///
    /// ```
    /// use laurus::storage::memory::{MemoryStorage, MemoryStorageConfig};
    /// use laurus::storage::Storage;
    /// use std::io::Write;
    ///
    /// # fn main() -> laurus::Result<()> {
    /// let mut storage = MemoryStorage::new(MemoryStorageConfig::default());
    ///
    /// // Use storage
    /// let mut output = storage.create_output("data.bin")?;
    /// output.write_all(b"some data")?;
    /// output.close()?;
    ///
    /// // Clean shutdown
    /// storage.close()?;
    /// # Ok(())
    /// # }
    /// ```
    fn close(&mut self) -> Result<()>;
}

/// A trait for reading data from storage.
pub trait StorageInput: Read + Seek + Send + Sync + std::fmt::Debug {
    /// Get the size of the input stream.
    fn size(&self) -> Result<u64>;

    /// Clone this input stream.
    fn clone_input(&self) -> Result<Box<dyn StorageInput>>;

    /// Close the input stream.
    fn close(&mut self) -> Result<()>;
}

/// A trait for writing data to storage.
pub trait StorageOutput: Write + Seek + Send + std::fmt::Debug {
    /// Flush and sync the output to storage.
    fn flush_and_sync(&mut self) -> Result<()>;

    /// Get the current position in the output stream.
    fn position(&self) -> Result<u64>;

    /// Close the output stream.
    fn close(&mut self) -> Result<()>;
}

// Implement StorageOutput for Box<dyn StorageOutput> to allow trait objects
impl StorageOutput for Box<dyn StorageOutput> {
    fn flush_and_sync(&mut self) -> Result<()> {
        self.as_mut().flush_and_sync()
    }

    fn position(&self) -> Result<u64> {
        self.as_ref().position()
    }

    fn close(&mut self) -> Result<()> {
        self.as_mut().close()
    }
}

// Implement StorageInput for Box<dyn StorageInput> to allow trait objects
impl StorageInput for Box<dyn StorageInput> {
    fn size(&self) -> Result<u64> {
        self.as_ref().size()
    }

    fn clone_input(&self) -> Result<Box<dyn StorageInput>> {
        self.as_ref().clone_input()
    }

    fn close(&mut self) -> Result<()> {
        self.as_mut().close()
    }
}

/// A lock manager for coordinating access to storage.
pub trait LockManager: Send + Sync + std::fmt::Debug {
    /// Acquire a lock with the given name.
    fn acquire_lock(&self, name: &str) -> Result<Box<dyn StorageLock>>;

    /// Try to acquire a lock with the given name, returning None if not available.
    fn try_acquire_lock(&self, name: &str) -> Result<Option<Box<dyn StorageLock>>>;

    /// Check if a lock with the given name exists.
    fn lock_exists(&self, name: &str) -> bool;

    /// Release all locks (for cleanup).
    fn release_all(&self) -> Result<()>;
}

/// A lock on a resource in storage.
pub trait StorageLock: Send + std::fmt::Debug {
    /// Get the name of the lock.
    fn name(&self) -> &str;

    /// Release the lock.
    fn release(&mut self) -> Result<()>;

    /// Check if the lock is still valid.
    fn is_valid(&self) -> bool;
}

/// Configuration for storage backends.
///
/// This enum provides type-safe configuration for different storage implementations.
/// Each variant contains the configuration specific to that storage type, including
/// the path for file-based storage.
///
/// # Design Pattern
///
/// This follows an enum-based configuration pattern where:
/// - Each storage type has its own dedicated config struct
/// - The path is part of `FileStorageConfig` (not a separate parameter)
/// - Pattern matching ensures exhaustive handling of all storage types
///
/// # Example
///
/// ```
/// use laurus::storage::StorageConfig;
/// use laurus::storage::file::FileStorageConfig;
/// use laurus::storage::memory::MemoryStorageConfig;
///
/// // File storage with custom settings
/// let mut file_config = FileStorageConfig::new("/data/index");
/// file_config.use_mmap = true;
/// file_config.buffer_size = 131072; // 128KB
/// let config = StorageConfig::File(file_config);
///
/// // Memory storage with default settings
/// let config = StorageConfig::Memory(MemoryStorageConfig::default());
/// ```
#[derive(Debug, Clone)]
pub enum StorageConfig {
    /// File-based storage configuration (includes path)
    File(file::FileStorageConfig),

    /// Memory-based storage configuration
    Memory(memory::MemoryStorageConfig),
}

impl Default for StorageConfig {
    fn default() -> Self {
        StorageConfig::Memory(memory::MemoryStorageConfig::default())
    }
}

/// A factory for creating storage instances.
///
/// This factory creates appropriate storage implementations based on the
/// provided configuration. It follows the same pattern as LexicalIndexFactory.
pub struct StorageFactory;

impl StorageFactory {
    /// Create a new storage instance with the given configuration.
    ///
    /// # Arguments
    ///
    /// * `config` - Storage configuration (enum containing type-specific settings including path for file storage)
    ///
    /// # Returns
    ///
    /// A boxed storage implementation based on the configured storage type.
    ///
    /// # Example
    ///
    /// ```
    /// use laurus::storage::{StorageFactory, StorageConfig};
    /// use laurus::storage::file::FileStorageConfig;
    /// use laurus::storage::memory::MemoryStorageConfig;
    ///
    /// # fn main() -> laurus::Result<()> {
    /// // Create memory storage
    /// let config = StorageConfig::Memory(MemoryStorageConfig::default());
    /// let storage = StorageFactory::create(config)?;
    ///
    /// // Create file storage
    /// let file_config = FileStorageConfig::new("/tmp/index");
    /// let config = StorageConfig::File(file_config);
    /// let storage = StorageFactory::create(config)?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn create(config: StorageConfig) -> Result<Arc<dyn Storage>> {
        match config {
            StorageConfig::Memory(mem_config) => {
                let storage = memory::MemoryStorage::new(mem_config);
                Ok(Arc::new(storage))
            }
            StorageConfig::File(file_config) => {
                let path = file_config.path.clone();
                let storage = file::FileStorage::new(&path, file_config)?;
                Ok(Arc::new(storage))
            }
        }
    }

    /// Open an existing storage instance.
    ///
    /// This is similar to `create` but intended for opening existing storage.
    /// The behavior is currently the same, but may differ in the future.
    pub fn open(config: StorageConfig) -> Result<Arc<dyn Storage>> {
        Self::create(config)
    }
}

/// Error types specific to storage operations.
#[derive(Debug, Clone)]
pub enum StorageError {
    /// File not found.
    FileNotFound(String),

    /// File already exists.
    FileExists(String),

    /// Permission denied.
    PermissionDenied(String),

    /// I/O error.
    IoError(String),

    /// Lock acquisition failed.
    LockFailed(String),

    /// Storage is closed.
    StorageClosed,

    /// Invalid operation.
    InvalidOperation(String),
}

impl std::fmt::Display for StorageError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            StorageError::FileNotFound(name) => write!(f, "File not found: {name}"),
            StorageError::FileExists(name) => write!(f, "File already exists: {name}"),
            StorageError::PermissionDenied(name) => write!(f, "Permission denied: {name}"),
            StorageError::IoError(msg) => write!(f, "I/O error: {msg}"),
            StorageError::LockFailed(name) => write!(f, "Failed to acquire lock: {name}"),
            StorageError::StorageClosed => write!(f, "Storage is closed"),
            StorageError::InvalidOperation(msg) => write!(f, "Invalid operation: {msg}"),
        }
    }
}

impl std::error::Error for StorageError {}

impl From<StorageError> for LaurusError {
    fn from(err: StorageError) -> Self {
        LaurusError::storage(err.to_string())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::file::FileStorageConfig;
    use crate::storage::memory::MemoryStorageConfig;

    #[test]
    fn test_storage_config_default() {
        let config = StorageConfig::default();

        // Default is Memory
        match config {
            StorageConfig::Memory(mem_config) => {
                assert_eq!(mem_config.initial_capacity, 16);
            }
            _ => panic!("Expected Memory config"),
        }
    }

    #[test]
    fn test_file_storage_config() {
        let config = FileStorageConfig::new("/tmp/test");

        assert_eq!(config.path, std::path::PathBuf::from("/tmp/test"));
        assert!(!config.use_mmap);
        assert_eq!(config.buffer_size, 65536);
        assert!(!config.sync_writes);
        assert!(config.use_locking);
        assert!(config.temp_dir.is_none());
    }

    #[test]
    fn test_storage_error_display() {
        let err = StorageError::FileNotFound("test.txt".to_string());
        assert_eq!(err.to_string(), "File not found: test.txt");

        let err = StorageError::FileExists("test.txt".to_string());
        assert_eq!(err.to_string(), "File already exists: test.txt");

        let err = StorageError::PermissionDenied("test.txt".to_string());
        assert_eq!(err.to_string(), "Permission denied: test.txt");

        let err = StorageError::IoError("connection failed".to_string());
        assert_eq!(err.to_string(), "I/O error: connection failed");

        let err = StorageError::LockFailed("write.lock".to_string());
        assert_eq!(err.to_string(), "Failed to acquire lock: write.lock");

        let err = StorageError::StorageClosed;
        assert_eq!(err.to_string(), "Storage is closed");

        let err = StorageError::InvalidOperation("cannot write to read-only storage".to_string());
        assert_eq!(
            err.to_string(),
            "Invalid operation: cannot write to read-only storage"
        );
    }

    #[test]
    fn test_file_metadata() {
        let metadata = FileMetadata {
            size: 1024,
            modified: 1234567890,
            created: 1234567890,
            readonly: false,
        };

        assert_eq!(metadata.size, 1024);
        assert_eq!(metadata.modified, 1234567890);
        assert_eq!(metadata.created, 1234567890);
        assert!(!metadata.readonly);
    }

    #[test]
    fn test_storage_factory_memory() {
        let config = StorageConfig::Memory(MemoryStorageConfig::default());
        let storage = StorageFactory::create(config).unwrap();

        // Test that we can use the storage
        assert!(!storage.file_exists("test.txt"));
    }

    #[test]
    fn test_storage_factory_file() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let file_config = FileStorageConfig::new(temp_dir.path());
        let config = StorageConfig::File(file_config);
        let storage = StorageFactory::create(config).unwrap();

        // Test that we can use the storage
        assert!(!storage.file_exists("test.txt"));
    }

    #[test]
    fn test_storage_factory_with_mmap() {
        use std::io::Write;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let mut file_config = FileStorageConfig::new(temp_dir.path());
        file_config.use_mmap = true;

        let config = StorageConfig::File(file_config);
        let storage = StorageFactory::create(config).unwrap();

        // Create and write a file
        let mut output = storage.create_output("test.txt").unwrap();
        output.write_all(b"Hello, Factory!").unwrap();
        output.close().unwrap();

        // Read the file back (should use mmap)
        let mut input = storage.open_input("test.txt").unwrap();
        let mut buffer = Vec::new();
        input.read_to_end(&mut buffer).unwrap();

        assert_eq!(buffer, b"Hello, Factory!");
    }
}