maproom 0.1.0

Semantic code search powered by embeddings and SQLite
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
//! Update task types for incremental indexing queue.
//!
//! This module defines the core types for the update queue system:
//! - `UpdateTask` - A file update task with metadata
//! - `Trigger` - What triggered the update (User, Save, Auto)
//! - `Priority` - Task priority level (High, Medium, Low)

use chrono::{DateTime, Utc};
use std::cmp::Ordering;
use std::path::PathBuf;

use super::detector::ChangeType;

/// What triggered this update task.
///
/// The trigger type determines the priority of the task:
/// - User: Manual user action (e.g., "reindex this file") → High priority
/// - Save: File save event from editor → Medium priority
/// - Auto: Automatic detection from file watcher → Low priority
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Trigger {
    /// User-triggered action (highest priority)
    User,
    /// File save event (medium priority)
    Save,
    /// Automatic detection (lowest priority)
    Auto,
}

/// Priority level for update tasks.
///
/// Priority determines the order in which tasks are processed:
/// - High: Process immediately (user-triggered)
/// - Medium: Process soon (save events)
/// - Low: Process eventually (automatic detection)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Priority {
    /// High priority - process immediately
    High,
    /// Medium priority - process soon
    Medium,
    /// Low priority - process eventually
    Low,
}

impl Priority {
    /// Calculate priority from a trigger type.
    ///
    /// # Arguments
    /// * `trigger` - The trigger that created the task
    ///
    /// # Returns
    /// The appropriate priority level for the trigger
    pub fn from_trigger(trigger: Trigger) -> Self {
        match trigger {
            Trigger::User => Priority::High,
            Trigger::Save => Priority::Medium,
            Trigger::Auto => Priority::Low,
        }
    }

    /// Get a numeric value for priority comparison.
    ///
    /// Higher values = higher priority.
    /// Used internally by the priority queue.
    fn as_value(&self) -> u8 {
        match self {
            Priority::High => 3,
            Priority::Medium => 2,
            Priority::Low => 1,
        }
    }
}

impl PartialOrd for Priority {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Priority {
    fn cmp(&self, other: &Self) -> Ordering {
        self.as_value().cmp(&other.as_value())
    }
}

/// An update task for the incremental indexing queue.
///
/// Each task represents a file that needs to be reindexed, along with
/// metadata about what changed and why.
#[derive(Debug, Clone)]
pub struct UpdateTask {
    /// Filesystem path to the file
    pub path: PathBuf,
    /// Type of change detected
    pub change_type: ChangeType,
    /// What triggered this update
    pub trigger: Trigger,
    /// Priority level (calculated from trigger)
    pub priority: Priority,
    /// When the task was created
    pub created_at: DateTime<Utc>,
    /// Number of retry attempts (for error handling)
    pub retry_count: u32,
}

impl UpdateTask {
    /// Create a new update task.
    ///
    /// # Arguments
    /// * `path` - Filesystem path to the file
    /// * `change_type` - Type of change detected
    /// * `trigger` - What triggered this update
    ///
    /// # Returns
    /// A new update task with priority calculated from the trigger
    pub fn new(path: PathBuf, change_type: ChangeType, trigger: Trigger) -> Self {
        Self {
            path,
            change_type,
            trigger,
            priority: Priority::from_trigger(trigger),
            created_at: Utc::now(),
            retry_count: 0,
        }
    }

    /// Merge another task into this one.
    ///
    /// When the same file has multiple pending updates, we merge them
    /// to avoid duplicate work.
    ///
    /// # Merge Rules
    /// - Keep the highest priority
    /// - Update change_type appropriately:
    ///   - New + Deleted → cancel out (no net change)
    ///   - Modified + Modified → keep Modified
    ///   - New + Modified → keep New (still new, just different content)
    ///   - Deleted + anything → keep Deleted
    /// - Update timestamp to latest
    /// - Reset retry count to 0
    ///
    /// # Arguments
    /// * `other` - The other task to merge into this one
    pub fn merge(&mut self, other: UpdateTask) {
        // Keep highest priority
        if other.priority > self.priority {
            self.priority = other.priority;
            self.trigger = other.trigger;
        }

        // Update change_type based on merge rules
        self.change_type = Self::merge_change_types(&self.change_type, &other.change_type);

        // Update timestamp to latest
        if other.created_at > self.created_at {
            self.created_at = other.created_at;
        }

        // Reset retry count when merging (fresh attempt)
        self.retry_count = 0;
    }

    /// Merge two change types according to semantic rules.
    ///
    /// # Arguments
    /// * `first` - The first change type (existing in queue)
    /// * `second` - The second change type (new task)
    ///
    /// # Returns
    /// The merged change type
    fn merge_change_types(first: &ChangeType, second: &ChangeType) -> ChangeType {
        match (first, second) {
            // New + Deleted → None (cancel out)
            (ChangeType::New(_), ChangeType::Deleted(_)) => ChangeType::None,

            // Deleted + New → Modified (file was deleted then recreated)
            // Use the new hash as both old and new for simplicity
            (ChangeType::Deleted(old), ChangeType::New(new)) => ChangeType::Modified {
                old: *old,
                new: *new,
            },

            // New + Modified → New (still new, just different content)
            (ChangeType::New(_), ChangeType::Modified { new, .. }) => ChangeType::New(*new),

            // Modified + Modified → Modified (chain the changes)
            (ChangeType::Modified { old, .. }, ChangeType::Modified { new, .. }) => {
                ChangeType::Modified {
                    old: *old,
                    new: *new,
                }
            }

            // Modified + Deleted → Deleted
            (ChangeType::Modified { old, .. }, ChangeType::Deleted(_)) => ChangeType::Deleted(*old),

            // Modified + New → Modified (treat New as a significant modification)
            (ChangeType::Modified { old, .. }, ChangeType::New(new)) => ChangeType::Modified {
                old: *old,
                new: *new,
            },

            // Deleted + Modified/Deleted → keep Deleted
            (ChangeType::Deleted(hash), ChangeType::Modified { .. })
            | (ChangeType::Deleted(hash), ChangeType::Deleted(_)) => ChangeType::Deleted(*hash),

            // None + anything → take the new change
            (ChangeType::None, change) => change.clone(),

            // anything + None → keep existing change
            (change, ChangeType::None) => change.clone(),

            // New + New → keep the latest New
            (ChangeType::New(_), ChangeType::New(new)) => ChangeType::New(*new),
        }
    }

    /// Increment the retry count.
    ///
    /// Called when a task fails and needs to be retried.
    pub fn increment_retry(&mut self) {
        self.retry_count += 1;
    }

    /// Check if this task has exceeded the maximum retry limit.
    ///
    /// # Arguments
    /// * `max_retries` - Maximum number of retries allowed
    ///
    /// # Returns
    /// True if the task has been retried too many times
    pub fn has_exceeded_retries(&self, max_retries: u32) -> bool {
        self.retry_count >= max_retries
    }
}

impl PartialEq for UpdateTask {
    fn eq(&self, other: &Self) -> bool {
        self.path == other.path
    }
}

impl Eq for UpdateTask {}

impl std::hash::Hash for UpdateTask {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.path.hash(state);
    }
}

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

    #[test]
    fn test_priority_from_trigger() {
        assert_eq!(Priority::from_trigger(Trigger::User), Priority::High);
        assert_eq!(Priority::from_trigger(Trigger::Save), Priority::Medium);
        assert_eq!(Priority::from_trigger(Trigger::Auto), Priority::Low);
    }

    #[test]
    fn test_priority_ordering() {
        assert!(Priority::High > Priority::Medium);
        assert!(Priority::Medium > Priority::Low);
        assert!(Priority::High > Priority::Low);
    }

    #[test]
    fn test_task_creation() {
        let path = PathBuf::from("/test/file.rs");
        let hash = FileHasher::hash_bytes(b"test content");
        let change = ChangeType::New(hash);

        let task = UpdateTask::new(path.clone(), change.clone(), Trigger::Save);

        assert_eq!(task.path, path);
        assert_eq!(task.priority, Priority::Medium);
        assert_eq!(task.trigger, Trigger::Save);
        assert_eq!(task.retry_count, 0);
    }

    #[test]
    fn test_task_merge_priority() {
        let path = PathBuf::from("/test/file.rs");
        let hash = FileHasher::hash_bytes(b"test");

        let mut task1 = UpdateTask::new(path.clone(), ChangeType::New(hash), Trigger::Auto);
        let task2 = UpdateTask::new(path.clone(), ChangeType::New(hash), Trigger::User);

        assert_eq!(task1.priority, Priority::Low);

        task1.merge(task2);

        // Should take the higher priority
        assert_eq!(task1.priority, Priority::High);
        assert_eq!(task1.trigger, Trigger::User);
    }

    #[test]
    fn test_merge_change_types_new_deleted() {
        let hash1 = FileHasher::hash_bytes(b"content1");
        let hash2 = FileHasher::hash_bytes(b"content2");

        // New + Deleted → None (cancel out)
        let result =
            UpdateTask::merge_change_types(&ChangeType::New(hash1), &ChangeType::Deleted(hash2));
        assert_eq!(result, ChangeType::None);
    }

    #[test]
    fn test_merge_change_types_modified_modified() {
        let hash1 = FileHasher::hash_bytes(b"v1");
        let hash2 = FileHasher::hash_bytes(b"v2");
        let hash3 = FileHasher::hash_bytes(b"v3");

        // Modified + Modified → chain the changes
        let result = UpdateTask::merge_change_types(
            &ChangeType::Modified {
                old: hash1,
                new: hash2,
            },
            &ChangeType::Modified {
                old: hash2,
                new: hash3,
            },
        );

        match result {
            ChangeType::Modified { old, new } => {
                assert_eq!(old, hash1);
                assert_eq!(new, hash3);
            }
            _ => panic!("Expected Modified change type"),
        }
    }

    #[test]
    fn test_merge_change_types_new_modified() {
        let hash1 = FileHasher::hash_bytes(b"v1");
        let hash2 = FileHasher::hash_bytes(b"v2");

        // New + Modified → New (still new, just different content)
        let result = UpdateTask::merge_change_types(
            &ChangeType::New(hash1),
            &ChangeType::Modified {
                old: hash1,
                new: hash2,
            },
        );

        assert_eq!(result, ChangeType::New(hash2));
    }

    #[test]
    fn test_merge_change_types_deleted_new() {
        let hash1 = FileHasher::hash_bytes(b"v1");
        let hash2 = FileHasher::hash_bytes(b"v2");

        // Deleted + New → Modified (file recreated)
        let result =
            UpdateTask::merge_change_types(&ChangeType::Deleted(hash1), &ChangeType::New(hash2));

        match result {
            ChangeType::Modified { old, new } => {
                assert_eq!(old, hash1);
                assert_eq!(new, hash2);
            }
            _ => panic!("Expected Modified change type"),
        }
    }

    #[test]
    fn test_retry_count() {
        let path = PathBuf::from("/test/file.rs");
        let hash = FileHasher::hash_bytes(b"test");
        let mut task = UpdateTask::new(path, ChangeType::New(hash), Trigger::Auto);

        assert_eq!(task.retry_count, 0);
        assert!(!task.has_exceeded_retries(3));

        task.increment_retry();
        assert_eq!(task.retry_count, 1);
        assert!(!task.has_exceeded_retries(3));

        task.increment_retry();
        task.increment_retry();
        assert_eq!(task.retry_count, 3);
        assert!(task.has_exceeded_retries(3));
    }

    #[test]
    fn test_task_equality() {
        let path1 = PathBuf::from("/test/file1.rs");
        let path2 = PathBuf::from("/test/file2.rs");
        let hash = FileHasher::hash_bytes(b"test");

        let task1 = UpdateTask::new(path1.clone(), ChangeType::New(hash), Trigger::Auto);
        let task2 = UpdateTask::new(path1.clone(), ChangeType::New(hash), Trigger::User);
        let task3 = UpdateTask::new(path2, ChangeType::New(hash), Trigger::Auto);

        // Tasks are equal if they have the same path
        assert_eq!(task1, task2);
        assert_ne!(task1, task3);
    }
}