mecab-ko-dict 0.7.2

한국어 형태소 사전 관리 - 바이너리 포맷, FST 검색, 연접 비용
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
//! # 파일 변경 감지 모듈
//!
//! `notify` 크레이트를 사용하여 사전 파일 변경을 감지하고
//! 자동으로 핫 리로드를 트리거합니다.
//!
//! ## 아키텍처
//!
//! ```text
//! ┌──────────────────────────────────────┐
//! │ FileWatcher                          │
//! │  - notify::RecommendedWatcher        │
//! │  - crossbeam::Receiver               │
//! └──────────────────────────────────────┘
//!//!//! ┌──────────────────────────────────────┐
//! │ File System Events                   │
//! │  - Create, Modify, Delete            │
//! └──────────────────────────────────────┘
//!//!//! ┌──────────────────────────────────────┐
//! │ HotReloadDictionary::reload()        │
//! └──────────────────────────────────────┘
//! ```
//!
//! ## 사용 예제
//!
//! ```rust,no_run
//! use mecab_ko_dict::file_watcher::{FileWatcher, WatchConfig};
//! use mecab_ko_dict::hot_reload::HotReloadDictionary;
//! use std::sync::Arc;
//!
//! let dict = Arc::new(HotReloadDictionary::new("/path/to/dict").unwrap());
//! let config = WatchConfig::default();
//!
//! let mut watcher = FileWatcher::new(dict.clone(), config).unwrap();
//! watcher.start().unwrap();
//!
//! // 파일 변경 감지 및 자동 리로드
//! // ...
//!
//! watcher.stop().unwrap();
//! ```

use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::thread;
use std::time::Duration;

use crossbeam_channel::{bounded, Receiver, Sender};
use notify::{
    event::{Event, EventKind},
    RecommendedWatcher, RecursiveMode, Watcher,
};

use crate::error::{DictError, Result};
use crate::hot_reload::HotReloadDictionary;

/// 파일 감시 설정
#[derive(Debug, Clone)]
pub struct WatchConfig {
    /// 디바운스 시간 (밀리초)
    pub debounce_ms: u64,
    /// 재귀 감시 여부
    pub recursive: bool,
    /// 감시할 파일 확장자
    pub watch_extensions: Vec<String>,
    /// 무시할 파일 패턴
    pub ignore_patterns: Vec<String>,
}

impl Default for WatchConfig {
    fn default() -> Self {
        Self {
            debounce_ms: 300,
            recursive: false,
            watch_extensions: vec![
                "dic".to_string(),
                "bin".to_string(),
                "def".to_string(),
                "csv".to_string(),
                "zst".to_string(),
            ],
            ignore_patterns: vec![".tmp".to_string(), ".swp".to_string(), "~".to_string()],
        }
    }
}

impl WatchConfig {
    /// 디바운스 시간 설정
    #[must_use]
    pub const fn debounce_ms(mut self, ms: u64) -> Self {
        self.debounce_ms = ms;
        self
    }

    /// 재귀 감시 설정
    #[must_use]
    pub const fn recursive(mut self, recursive: bool) -> Self {
        self.recursive = recursive;
        self
    }

    /// 감시할 파일 확장자 추가
    #[must_use]
    pub fn watch_extension(mut self, ext: impl Into<String>) -> Self {
        self.watch_extensions.push(ext.into());
        self
    }

    /// 무시할 파일 패턴 추가
    #[must_use]
    pub fn ignore_pattern(mut self, pattern: impl Into<String>) -> Self {
        self.ignore_patterns.push(pattern.into());
        self
    }

    /// 파일이 감시 대상인지 확인
    fn should_watch(&self, path: &Path) -> bool {
        let path_str = path.to_string_lossy();

        // 무시 패턴 확인
        for pattern in &self.ignore_patterns {
            if path_str.contains(pattern) {
                return false;
            }
        }

        // 확장자 확인
        if let Some(ext) = path.extension() {
            let ext_str = ext.to_string_lossy();
            return self.watch_extensions.iter().any(|e| e == &*ext_str);
        }

        false
    }
}

/// 파일 변경 이벤트
#[derive(Debug, Clone)]
pub enum FileEvent {
    /// 파일 생성
    Created(PathBuf),
    /// 파일 수정
    Modified(PathBuf),
    /// 파일 삭제
    Deleted(PathBuf),
    /// 파일 이름 변경
    Renamed {
        /// 이전 경로
        from: PathBuf,
        /// 새 경로
        to: PathBuf,
    },
}

/// 파일 감시자
pub struct FileWatcher {
    /// 사전 인스턴스
    dict: Arc<HotReloadDictionary>,
    /// 감시 설정
    config: WatchConfig,
    /// notify 감시자
    watcher: Option<RecommendedWatcher>,
    /// 이벤트 수신자
    event_rx: Option<Receiver<notify::Result<Event>>>,
    /// 종료 신호 송신자
    stop_tx: Option<Sender<()>>,
    /// 워커 스레드 핸들
    worker_handle: Option<thread::JoinHandle<()>>,
}

impl FileWatcher {
    /// 새 파일 감시자 생성
    ///
    /// # Arguments
    ///
    /// * `dict` - 핫 리로드 사전 인스턴스
    /// * `config` - 감시 설정
    ///
    /// # Errors
    ///
    /// Currently always succeeds, but returns Result for future extensibility.
    pub const fn new(dict: Arc<HotReloadDictionary>, config: WatchConfig) -> Result<Self> {
        Ok(Self {
            dict,
            config,
            watcher: None,
            event_rx: None,
            stop_tx: None,
            worker_handle: None,
        })
    }

    /// 기본 설정으로 파일 감시자 생성
    ///
    /// # Errors
    ///
    /// Currently always succeeds, but returns Result for future extensibility.
    pub fn new_default(dict: Arc<HotReloadDictionary>) -> Result<Self> {
        Self::new(dict, WatchConfig::default())
    }

    /// 파일 감시 시작
    ///
    /// # Errors
    ///
    /// Returns an error if the watcher cannot be created or the directory cannot be accessed.
    pub fn start(&mut self) -> Result<()> {
        if self.watcher.is_some() {
            return Err(DictError::Format(
                "File watcher already started".to_string(),
            ));
        }

        let (tx, rx) = bounded(100);
        let (stop_tx, stop_rx) = bounded(1);

        // notify 감시자 생성
        let mut watcher = RecommendedWatcher::new(
            tx,
            notify::Config::default()
                .with_poll_interval(Duration::from_millis(self.config.debounce_ms)),
        )
        .map_err(|e| DictError::Format(format!("Failed to create watcher: {e}")))?;

        // 사전 디렉토리 감시
        let dicdir = self.dict.dicdir();
        let recursive_mode = if self.config.recursive {
            RecursiveMode::Recursive
        } else {
            RecursiveMode::NonRecursive
        };

        watcher
            .watch(dicdir, recursive_mode)
            .map_err(|e| DictError::Format(format!("Failed to watch directory: {e}")))?;

        self.watcher = Some(watcher);
        self.event_rx = Some(rx);
        self.stop_tx = Some(stop_tx);

        // 워커 스레드 시작
        self.start_worker(stop_rx)?;

        Ok(())
    }

    /// 파일 감시 중지
    ///
    /// # Errors
    ///
    /// Currently always succeeds, but returns Result for API consistency.
    pub fn stop(&mut self) -> Result<()> {
        if let Some(stop_tx) = self.stop_tx.take() {
            let _ = stop_tx.send(());
        }

        if let Some(handle) = self.worker_handle.take() {
            let _ = handle.join();
        }

        self.watcher = None;
        self.event_rx = None;

        Ok(())
    }

    /// 감시 중인지 확인
    #[must_use]
    pub const fn is_watching(&self) -> bool {
        self.watcher.is_some()
    }

    /// 워커 스레드 시작
    fn start_worker(&mut self, stop_rx: Receiver<()>) -> Result<()> {
        let event_rx = self
            .event_rx
            .as_ref()
            .ok_or_else(|| DictError::Format("Event receiver not initialized".to_string()))?;

        let dict = Arc::clone(&self.dict);
        let config = self.config.clone();
        let rx = event_rx.clone();

        let handle = thread::spawn(move || {
            Self::worker_loop(&dict, &config, &rx, &stop_rx);
        });

        self.worker_handle = Some(handle);

        Ok(())
    }

    /// 워커 루프
    fn worker_loop(
        dict: &Arc<HotReloadDictionary>,
        config: &WatchConfig,
        event_rx: &Receiver<notify::Result<Event>>,
        stop_rx: &Receiver<()>,
    ) {
        loop {
            // 종료 신호 확인
            if stop_rx.try_recv().is_ok() {
                break;
            }

            // 이벤트 수신 (타임아웃 설정)
            match event_rx.recv_timeout(Duration::from_millis(100)) {
                Ok(Ok(event)) => {
                    Self::handle_event(dict, config, event);
                }
                Ok(Err(e)) => {
                    eprintln!("File watcher error: {e}");
                }
                Err(crossbeam_channel::RecvTimeoutError::Timeout) => {
                    // 타임아웃은 정상
                }
                Err(crossbeam_channel::RecvTimeoutError::Disconnected) => {
                    // 채널 닫힘
                    break;
                }
            }
        }
    }

    /// 이벤트 처리
    fn handle_event(dict: &Arc<HotReloadDictionary>, config: &WatchConfig, event: Event) {
        match event.kind {
            EventKind::Create(_) | EventKind::Modify(_) => {
                for path in event.paths {
                    if config.should_watch(&path) {
                        Self::reload_dictionary(dict, &path);
                    }
                }
            }
            EventKind::Remove(_) | EventKind::Access(_) | EventKind::Any | EventKind::Other => {
                // 파일 삭제 및 기타 이벤트는 무시 (기존 사전 유지)
            }
        }
    }

    /// 사전 리로드
    fn reload_dictionary(dict: &Arc<HotReloadDictionary>, path: &Path) {
        if let Some(filename) = path.file_name() {
            let filename_str = filename.to_string_lossy();

            // 시스템 사전 파일 변경 시
            if filename_str.contains("sys.dic")
                || filename_str.contains("matrix")
                || filename_str.ends_with(".zst")
            {
                match dict.reload_system_dict() {
                    Ok(version) => {
                        println!("Dictionary reloaded successfully (version {version})");
                    }
                    Err(e) => {
                        eprintln!("Failed to reload dictionary: {e}");
                    }
                }
            }
        }
    }
}

impl Drop for FileWatcher {
    fn drop(&mut self) {
        let _ = self.stop();
    }
}

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

    #[test]
    fn test_watch_config_default() {
        let config = WatchConfig::default();
        assert_eq!(config.debounce_ms, 300);
        assert!(!config.recursive);
        assert!(config.watch_extensions.contains(&"dic".to_string()));
    }

    #[test]
    fn test_watch_config_builder() {
        let config = WatchConfig::default()
            .debounce_ms(500)
            .recursive(true)
            .watch_extension("txt")
            .ignore_pattern(".bak");

        assert_eq!(config.debounce_ms, 500);
        assert!(config.recursive);
        assert!(config.watch_extensions.contains(&"txt".to_string()));
        assert!(config.ignore_patterns.contains(&".bak".to_string()));
    }

    #[test]
    fn test_should_watch() {
        let config = WatchConfig::default();

        assert!(config.should_watch(Path::new("test.dic")));
        assert!(config.should_watch(Path::new("matrix.bin")));
        assert!(config.should_watch(Path::new("user.csv")));
        assert!(!config.should_watch(Path::new("test.txt")));
        assert!(!config.should_watch(Path::new("test.dic~")));
        assert!(!config.should_watch(Path::new(".test.dic.swp")));
    }

    #[test]
    fn test_file_event_types() {
        let created = FileEvent::Created(PathBuf::from("test.dic"));
        let modified = FileEvent::Modified(PathBuf::from("test.dic"));
        let deleted = FileEvent::Deleted(PathBuf::from("test.dic"));
        let renamed = FileEvent::Renamed {
            from: PathBuf::from("old.dic"),
            to: PathBuf::from("new.dic"),
        };

        assert!(
            matches!(created, FileEvent::Created(_)),
            "Expected Created event"
        );

        assert!(
            matches!(modified, FileEvent::Modified(_)),
            "Expected Modified event"
        );

        assert!(
            matches!(deleted, FileEvent::Deleted(_)),
            "Expected Deleted event"
        );

        assert!(
            matches!(renamed, FileEvent::Renamed { .. }),
            "Expected Renamed event"
        );
    }
}