xore-search 1.2.0

XORE Search Engine - Full-text and semantic search
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
//! 增量索引模块
//!
//! 提供增量索引功能,支持:
//! - 文件监控集成
//! - 增量更新(先删后增)
//! - 批量提交优化
//! - 简化版WAL(内存记录)
//! - 索引统计

use std::collections::VecDeque;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{Duration, SystemTime};

use anyhow::{Context, Result};
use tantivy::Index;
use tokio::sync::Mutex;
use tokio::time;
use tracing::{debug, error, info};

use crate::indexer::{IndexBuilder, IndexConfig, IndexSchema};
use crate::scanner::ScannedFile;
use crate::watcher::{FileEvent, FileWatcher, WatcherConfig};

/// WAL(Write-Ahead Log)- 简化版,仅内存记录
///
/// 记录最近的索引操作,用于调试和统计
#[derive(Debug, Clone)]
pub struct WriteAheadLog {
    /// 操作记录(最多保留1000条)
    operations: VecDeque<WalEntry>,
    /// 最大记录数
    max_entries: usize,
}

#[derive(Debug, Clone)]
pub struct WalEntry {
    /// 操作类型
    pub operation: WalOperation,
    /// 文件路径
    pub path: PathBuf,
    /// 时间戳
    pub timestamp: SystemTime,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WalOperation {
    Create,
    Modify,
    Delete,
}

impl WriteAheadLog {
    pub fn new() -> Self {
        Self { operations: VecDeque::new(), max_entries: 1000 }
    }

    pub fn log_create(&mut self, path: &Path) {
        self.add_entry(WalOperation::Create, path);
    }

    pub fn log_modify(&mut self, path: &Path) {
        self.add_entry(WalOperation::Modify, path);
    }

    pub fn log_delete(&mut self, path: &Path) {
        self.add_entry(WalOperation::Delete, path);
    }

    fn add_entry(&mut self, operation: WalOperation, path: &Path) {
        let entry = WalEntry { operation, path: path.to_path_buf(), timestamp: SystemTime::now() };

        self.operations.push_back(entry);

        // 保持最大记录数限制
        while self.operations.len() > self.max_entries {
            self.operations.pop_front();
        }
    }

    /// 获取最近的操作记录
    pub fn recent_operations(&self, limit: usize) -> Vec<&WalEntry> {
        self.operations.iter().rev().take(limit).collect()
    }

    /// 清空记录
    pub fn clear(&mut self) {
        self.operations.clear();
    }

    /// 获取总操作数
    pub fn len(&self) -> usize {
        self.operations.len()
    }

    /// 判断是否为空
    pub fn is_empty(&self) -> bool {
        self.operations.is_empty()
    }
}

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

/// 增量索引统计
#[derive(Debug, Clone, Default)]
pub struct IncrementalStats {
    /// 创建的文档数
    pub created_count: usize,
    /// 修改的文档数
    pub modified_count: usize,
    /// 删除的文档数
    pub deleted_count: usize,
    /// 待提交的变更数
    pub pending_changes: usize,
    /// 错误数
    pub error_count: usize,
    /// 最后更新时间
    pub last_update: Option<SystemTime>,
}

/// 增量索引器配置
#[derive(Debug, Clone)]
pub struct IncrementalConfig {
    /// 索引配置
    pub index_config: IndexConfig,
    /// 监控配置
    pub watcher_config: WatcherConfig,
    /// 批量提交阈值(累积多少变更后提交)
    pub commit_threshold: usize,
    /// 自动提交间隔(秒)
    pub auto_commit_interval: u64,
}

impl Default for IncrementalConfig {
    fn default() -> Self {
        Self {
            index_config: IndexConfig::default(),
            watcher_config: WatcherConfig::default(),
            commit_threshold: 50,
            auto_commit_interval: 30,
        }
    }
}

/// 增量索引器
///
/// 支持文件监控和增量更新的索引器
pub struct IncrementalIndexer {
    /// Tantivy 索引
    index: Index,
    /// 索引 Schema
    schema: IndexSchema,
    /// 索引构建器(用于实际的索引操作)
    builder: Arc<Mutex<IndexBuilder>>,
    /// 文件监控器
    watcher: Arc<Mutex<FileWatcher>>,
    /// WAL(简化版)
    wal: Arc<Mutex<WriteAheadLog>>,
    /// 统计信息
    stats: Arc<Mutex<IncrementalStats>>,
    /// 配置
    config: IncrementalConfig,
}

impl IncrementalIndexer {
    /// 创建增量索引器
    pub async fn new(config: IncrementalConfig) -> Result<Self> {
        info!("Creating incremental indexer");

        // 创建索引构建器
        let builder = IndexBuilder::with_config(config.index_config.clone())?;
        let index = builder.index().clone();
        let schema = builder.schema().clone();

        // 创建文件监控器
        let watcher = FileWatcher::new(config.watcher_config.clone())?;

        Ok(Self {
            index,
            schema,
            builder: Arc::new(Mutex::new(builder)),
            watcher: Arc::new(Mutex::new(watcher)),
            wal: Arc::new(Mutex::new(WriteAheadLog::new())),
            stats: Arc::new(Mutex::new(IncrementalStats::default())),
            config,
        })
    }

    /// 开始监控指定路径
    pub async fn watch(&self, path: &Path) -> Result<()> {
        info!("Starting watch on: {}", path.display());

        let mut watcher = self.watcher.lock().await;
        watcher.watch_path(path)?;

        Ok(())
    }

    /// 停止监控指定路径
    pub async fn unwatch(&self, path: &Path) -> Result<()> {
        info!("Stopping watch on: {}", path.display());

        let mut watcher = self.watcher.lock().await;
        watcher.unwatch_path(path)?;

        Ok(())
    }

    /// 运行增量索引(异步事件循环)
    ///
    /// 持续监听文件变更并更新索引
    pub async fn run(&self) -> Result<()> {
        info!("Starting incremental indexer event loop");

        let mut commit_interval =
            time::interval(Duration::from_secs(self.config.auto_commit_interval));

        loop {
            tokio::select! {
                // 定期自动提交
                _ = commit_interval.tick() => {
                    if let Err(e) = self.commit_if_needed().await {
                        error!("Auto commit failed: {}", e);
                    }
                }

                // 处理文件事件
                _ = time::sleep(Duration::from_millis(100)) => {
                    if let Err(e) = self.process_events().await {
                        error!("Process events failed: {}", e);
                    }
                }
            }
        }
    }

    /// 处理一批文件事件
    async fn process_events(&self) -> Result<()> {
        let mut watcher = self.watcher.lock().await;
        let events = watcher.recv_events()?;

        if events.is_empty() {
            return Ok(());
        }

        debug!("Processing {} file events", events.len());

        for event in events {
            if let Err(e) = self.handle_event(event).await {
                error!("Handle event failed: {}", e);
                let mut stats = self.stats.lock().await;
                stats.error_count += 1;
            }
        }

        // 检查是否需要提交
        self.commit_if_needed().await?;

        Ok(())
    }

    /// 处理单个文件事件
    async fn handle_event(&self, event: FileEvent) -> Result<()> {
        match event {
            FileEvent::Created(path) => {
                self.apply_create(path).await?;
            }
            FileEvent::Modified(path) => {
                self.apply_modify(path).await?;
            }
            FileEvent::Deleted(path) => {
                self.apply_delete(path).await?;
            }
            FileEvent::Renamed { from, to } => {
                self.apply_delete(from).await?;
                self.apply_create(to).await?;
            }
        }

        Ok(())
    }

    /// 处理文件创建
    async fn apply_create(&self, path: PathBuf) -> Result<()> {
        debug!("Applying create: {}", path.display());

        // 检查文件是否存在
        if !path.exists() {
            debug!("File no longer exists, skipping: {}", path.display());
            return Ok(());
        }

        // 扫描文件信息
        let file = self.scan_file(&path)?;

        // 添加到索引
        let mut builder = self.builder.lock().await;
        builder.add_document(&file)?;

        // 记录 WAL
        let mut wal = self.wal.lock().await;
        wal.log_create(&path);

        // 更新统计
        let mut stats = self.stats.lock().await;
        stats.created_count += 1;
        stats.pending_changes += 1;
        stats.last_update = Some(SystemTime::now());

        info!("Indexed new file: {}", path.display());
        Ok(())
    }

    /// 处理文件修改
    async fn apply_modify(&self, path: PathBuf) -> Result<()> {
        debug!("Applying modify: {}", path.display());

        // 检查文件是否存在
        if !path.exists() {
            debug!("File no longer exists, treating as delete: {}", path.display());
            return self.apply_delete(path).await;
        }

        // 先删除旧文档
        let mut builder = self.builder.lock().await;
        builder.delete_document(&path)?;

        // 扫描文件信息
        let file = self.scan_file(&path)?;

        // 重新添加
        builder.add_document(&file)?;

        // 记录 WAL
        let mut wal = self.wal.lock().await;
        wal.log_modify(&path);

        // 更新统计
        let mut stats = self.stats.lock().await;
        stats.modified_count += 1;
        stats.pending_changes += 1;
        stats.last_update = Some(SystemTime::now());

        info!("Updated file in index: {}", path.display());
        Ok(())
    }

    /// 处理文件删除
    async fn apply_delete(&self, path: PathBuf) -> Result<()> {
        debug!("Applying delete: {}", path.display());

        // 从索引删除
        let mut builder = self.builder.lock().await;
        builder.delete_document(&path)?;

        // 记录 WAL
        let mut wal = self.wal.lock().await;
        wal.log_delete(&path);

        // 更新统计
        let mut stats = self.stats.lock().await;
        stats.deleted_count += 1;
        stats.pending_changes += 1;
        stats.last_update = Some(SystemTime::now());

        Ok(())
    }

    /// 扫描文件信息
    fn scan_file(&self, path: &Path) -> Result<ScannedFile> {
        let metadata =
            fs::metadata(path).with_context(|| format!("Failed to read metadata: {:?}", path))?;

        Ok(ScannedFile {
            path: path.to_path_buf(),
            size: metadata.len(),
            modified: metadata.modified().ok(),
            is_dir: metadata.is_dir(),
        })
    }

    /// 如果需要则提交索引
    async fn commit_if_needed(&self) -> Result<()> {
        let stats = self.stats.lock().await;
        let pending = stats.pending_changes;
        drop(stats);

        if pending >= self.config.commit_threshold {
            info!("Committing {} pending changes", pending);
            self.commit().await?;
        }

        Ok(())
    }

    /// 强制提交索引
    pub async fn commit(&self) -> Result<()> {
        info!("Committing index changes");

        // 调用 IndexBuilder 的 commit_changes 方法
        let mut builder = self.builder.lock().await;
        builder.commit_changes()?;

        // 重置待提交计数
        let mut stats = self.stats.lock().await;
        stats.pending_changes = 0;

        Ok(())
    }

    /// 获取统计信息
    pub async fn stats(&self) -> IncrementalStats {
        self.stats.lock().await.clone()
    }

    /// 获取WAL最近的操作记录
    pub async fn recent_operations(&self, limit: usize) -> Vec<WalEntry> {
        let wal = self.wal.lock().await;
        wal.recent_operations(limit).into_iter().cloned().collect()
    }

    /// 获取索引
    pub fn index(&self) -> &Index {
        &self.index
    }

    /// 获取Schema
    pub fn schema(&self) -> &IndexSchema {
        &self.schema
    }
}

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

    #[test]
    fn test_wal_basic() {
        let mut wal = WriteAheadLog::new();

        wal.log_create(Path::new("test1.txt"));
        wal.log_modify(Path::new("test2.txt"));
        wal.log_delete(Path::new("test3.txt"));

        assert_eq!(wal.len(), 3);

        let recent = wal.recent_operations(2);
        assert_eq!(recent.len(), 2);
        assert_eq!(recent[0].operation, WalOperation::Delete);
        assert_eq!(recent[1].operation, WalOperation::Modify);
    }

    #[test]
    fn test_wal_max_entries() {
        let mut wal = WriteAheadLog::new();

        // 添加超过最大数量的条目
        for i in 0..1500 {
            wal.log_create(&PathBuf::from(format!("test{}.txt", i)));
        }

        // 应该只保留1000条
        assert_eq!(wal.len(), 1000);
    }

    #[test]
    fn test_wal_clear() {
        let mut wal = WriteAheadLog::new();

        wal.log_create(Path::new("test.txt"));
        assert_eq!(wal.len(), 1);

        wal.clear();
        assert_eq!(wal.len(), 0);
        assert!(wal.is_empty());
    }

    #[tokio::test]
    async fn test_incremental_config_default() {
        let config = IncrementalConfig::default();
        assert_eq!(config.commit_threshold, 50);
        assert_eq!(config.auto_commit_interval, 30);
    }

    #[tokio::test]
    async fn test_incremental_indexer_creation() {
        let temp_dir = TempDir::new().unwrap();
        let config = IncrementalConfig {
            index_config: IndexConfig {
                index_path: temp_dir.path().join("index"),
                ..Default::default()
            },
            ..Default::default()
        };

        let indexer = IncrementalIndexer::new(config).await;
        assert!(indexer.is_ok());
    }

    #[tokio::test]
    async fn test_incremental_stats() {
        let temp_dir = TempDir::new().unwrap();
        let config = IncrementalConfig {
            index_config: IndexConfig {
                index_path: temp_dir.path().join("index"),
                ..Default::default()
            },
            ..Default::default()
        };

        let indexer = IncrementalIndexer::new(config).await.unwrap();
        let stats = indexer.stats().await;

        assert_eq!(stats.created_count, 0);
        assert_eq!(stats.modified_count, 0);
        assert_eq!(stats.deleted_count, 0);
    }

    #[tokio::test]
    async fn test_incremental_watch() {
        let temp_dir = TempDir::new().unwrap();
        let config = IncrementalConfig {
            index_config: IndexConfig {
                index_path: temp_dir.path().join("index"),
                ..Default::default()
            },
            ..Default::default()
        };

        let indexer = IncrementalIndexer::new(config).await.unwrap();
        let result = indexer.watch(temp_dir.path()).await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_scan_file() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.txt");
        fs::write(&test_file, "hello").unwrap();

        let config = IncrementalConfig {
            index_config: IndexConfig {
                index_path: temp_dir.path().join("index"),
                ..Default::default()
            },
            ..Default::default()
        };

        let indexer = IncrementalIndexer::new(config).await.unwrap();
        let scanned = indexer.scan_file(&test_file).unwrap();

        assert_eq!(scanned.path, test_file);
        assert_eq!(scanned.size, 5);
        assert!(!scanned.is_dir);
    }

    #[tokio::test]
    async fn test_apply_create() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.txt");
        fs::write(&test_file, "hello").unwrap();

        let config = IncrementalConfig {
            index_config: IndexConfig {
                index_path: temp_dir.path().join("index"),
                ..Default::default()
            },
            ..Default::default()
        };

        let indexer = IncrementalIndexer::new(config).await.unwrap();
        let result = indexer.apply_create(test_file).await;

        assert!(result.is_ok());

        let stats = indexer.stats().await;
        assert_eq!(stats.created_count, 1);
        assert_eq!(stats.pending_changes, 1);
    }

    #[tokio::test]
    async fn test_apply_delete() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.txt");

        let config = IncrementalConfig {
            index_config: IndexConfig {
                index_path: temp_dir.path().join("index"),
                ..Default::default()
            },
            ..Default::default()
        };

        let indexer = IncrementalIndexer::new(config).await.unwrap();
        let result = indexer.apply_delete(test_file).await;

        assert!(result.is_ok());

        let stats = indexer.stats().await;
        assert_eq!(stats.deleted_count, 1);
    }

    #[tokio::test]
    async fn test_recent_operations() {
        let temp_dir = TempDir::new().unwrap();
        let config = IncrementalConfig {
            index_config: IndexConfig {
                index_path: temp_dir.path().join("index"),
                ..Default::default()
            },
            ..Default::default()
        };

        let indexer = IncrementalIndexer::new(config).await.unwrap();

        // 记录一些操作
        {
            let mut wal = indexer.wal.lock().await;
            wal.log_create(Path::new("file1.txt"));
            wal.log_modify(Path::new("file2.txt"));
            wal.log_delete(Path::new("file3.txt"));
        }

        let ops = indexer.recent_operations(5).await;
        assert_eq!(ops.len(), 3);
    }
}