| 1 | 'use strict'; |
| 2 | |
| 3 | const fs = require('fs'); |
| 4 | const path = require('path'); |
| 5 | const { getDb } = require('./db'); |
| 6 | const { loadConfig, getEffectivePaths } = require('./config'); |
| 7 | |
| 8 | const ACTIVE_CLIENT_GRACE_MS = toPositiveInt(process.env.SEARCH_INDEX_ACTIVE_CLIENT_GRACE_MS, 5 * 60 * 1000); |
| 9 | const IDLE_SYNC_INTERVAL_MS = toPositiveInt(process.env.SEARCH_INDEX_IDLE_SYNC_INTERVAL_MS, 30 * 60 * 1000); |
| 10 | const ACTIVE_SYNC_INTERVAL_MS = toPositiveInt(process.env.SEARCH_INDEX_ACTIVE_SYNC_INTERVAL_MS, 10 * 60 * 1000); |
| 11 | const MAX_CANDIDATES_PER_SEARCH = toPositiveInt(process.env.SEARCH_INDEX_MAX_CANDIDATES, 8000); |
| 12 | const MAX_SUBSEQUENCE_SCAN = toPositiveInt(process.env.SEARCH_INDEX_MAX_SUBSEQUENCE_SCAN, 12000); |
| 13 | const MAX_VALIDATE_HITS = toPositiveInt(process.env.SEARCH_INDEX_MAX_VALIDATE_HITS, 250); |
| 14 | const MAX_RESCAN_DIRS_PER_SEARCH = toPositiveInt(process.env.SEARCH_INDEX_MAX_RESCAN_DIRS, 12); |
| 15 | const INDEX_BATCH_SIZE = 400; |
| 16 | const ROOT_CHUNK_SIZE = 50; |
| 17 | |
| 18 | let loopTimer = null; |
| 19 | let loopStarted = false; |
| 20 | let backgroundSyncRunning = false; |
| 21 | |
| 22 | function toPositiveInt (value, fallback) |
| 23 | { |
| 24 | const parsed = Number.parseInt(String(value || ''), 10); |
| 25 | return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback; |
| 26 | } |
| 27 | |
| 28 | function yieldToEventLoop () |
| 29 | { |
| 30 | return new Promise((resolve) => setImmediate(resolve)); |
| 31 | } |
| 32 | |
| 33 | function nowMs () |
| 34 | { |
| 35 | return Date.now(); |
| 36 | } |
| 37 | |
| 38 | function fuzzyMatchScore (needle, haystack) |
| 39 | { |
| 40 | const q = String(needle || '').trim().toLowerCase(); |
| 41 | const t = String(haystack || '').toLowerCase(); |
| 42 | if (!q || !t) return null; |
| 43 | |
| 44 | const idx = t.indexOf(q); |
| 45 | if (idx >= 0) |
| 46 | { |
| 47 | const compactBonus = Math.max(0, 200 - idx); |
| 48 | const lengthBonus = Math.max(0, 120 - (t.length - q.length)); |
| 49 | return 1200 + compactBonus + lengthBonus; |
| 50 | } |
| 51 | |
| 52 | let qPos = 0; |
| 53 | let firstHit = -1; |
| 54 | let lastHit = -1; |
| 55 | let gaps = 0; |
| 56 | |
| 57 | for (let i = 0; i < t.length && qPos < q.length; i++) |
| 58 | { |
| 59 | if (t[i] !== q[qPos]) continue; |
| 60 | if (firstHit === -1) firstHit = i; |
| 61 | if (lastHit >= 0) gaps += Math.max(0, i - lastHit - 1); |
| 62 | lastHit = i; |
| 63 | qPos += 1; |
| 64 | } |
| 65 | |
| 66 | if (qPos !== q.length) return null; |
| 67 | |
| 68 | const span = lastHit - firstHit + 1; |
| 69 | const compactness = Math.max(0, 300 - span); |
| 70 | const gapPenalty = Math.min(180, gaps * 3); |
| 71 | const startBonus = Math.max(0, 120 - firstHit); |
| 72 | return 700 + compactness + startBonus - gapPenalty; |
| 73 | } |
| 74 | |
| 75 | function effectiveIndexRoots () |
| 76 | { |
| 77 | const config = loadConfig(); |
| 78 | const roots = getEffectivePaths(config.paths || []); |
| 79 | const userPath = typeof config.userPath === 'string' ? config.userPath.trim() : ''; |
| 80 | if (userPath) roots.push(userPath); |
| 81 | |
| 82 | const seen = new Set(); |
| 83 | const deduped = []; |
| 84 | for (const root of roots) |
| 85 | { |
| 86 | const key = path.resolve(root); |
| 87 | if (seen.has(key)) continue; |
| 88 | seen.add(key); |
| 89 | deduped.push(key); |
| 90 | } |
| 91 | return deduped; |
| 92 | } |
| 93 | |
| 94 | function hasActiveClients () |
| 95 | { |
| 96 | try |
| 97 | { |
| 98 | const { getActiveStreamCount } = require('./video-stream'); |
| 99 | if (Number(getActiveStreamCount()) > 0) return true; |
| 100 | } catch |
| 101 | { |
| 102 | // Stream module unavailable — treat as no active streams. |
| 103 | } |
| 104 | |
| 105 | const db = getDb(); |
| 106 | const cutoff = nowMs() - ACTIVE_CLIENT_GRACE_MS; |
| 107 | const row = db.prepare(` |
| 108 | SELECT COUNT(*) AS n |
| 109 | FROM auth_sessions |
| 110 | WHERE revoked_at IS NULL AND last_seen_at >= ? |
| 111 | `).get([cutoff]); |
| 112 | |
| 113 | return Number(row && row.n || 0) > 0; |
| 114 | } |
| 115 | |
| 116 | function upsertIndexRow (db, row) |
| 117 | { |
| 118 | db.prepare(` |
| 119 | INSERT INTO file_search_index ( |
| 120 | file_path, root_path, name, name_lower, relative_path, relative_lower, size, mtime_ms, indexed_at |
| 121 | ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) |
| 122 | ON CONFLICT(file_path) DO UPDATE SET |
| 123 | root_path = excluded.root_path, |
| 124 | name = excluded.name, |
| 125 | name_lower = excluded.name_lower, |
| 126 | relative_path = excluded.relative_path, |
| 127 | relative_lower = excluded.relative_lower, |
| 128 | size = excluded.size, |
| 129 | mtime_ms = excluded.mtime_ms, |
| 130 | indexed_at = excluded.indexed_at |
| 131 | `).run([ |
| 132 | row.file_path, |
| 133 | row.root_path, |
| 134 | row.name, |
| 135 | row.name_lower, |
| 136 | row.relative_path, |
| 137 | row.relative_lower, |
| 138 | row.size, |
| 139 | row.mtime_ms, |
| 140 | row.indexed_at, |
| 141 | ]); |
| 142 | } |
| 143 | |
| 144 | function deleteIndexPath (filePath) |
| 145 | { |
| 146 | getDb().prepare('DELETE FROM file_search_index WHERE file_path = ?').run([path.resolve(String(filePath || ''))]); |
| 147 | } |
| 148 | |
| 149 | function deleteIndexUnder (targetPath) |
| 150 | { |
| 151 | const resolved = path.resolve(String(targetPath || '')); |
| 152 | const db = getDb(); |
| 153 | db.prepare('DELETE FROM file_search_index WHERE file_path = ?').run([resolved]); |
| 154 | db.prepare('DELETE FROM file_search_index WHERE file_path LIKE ?') |
| 155 | .run([resolved + path.sep + '%']); |
| 156 | } |
| 157 | |
| 158 | function rewriteIndexPaths (fromPath, toPath) |
| 159 | { |
| 160 | const from = path.resolve(String(fromPath || '')); |
| 161 | const to = path.resolve(String(toPath || '')); |
| 162 | const db = getDb(); |
| 163 | |
| 164 | const rows = db.prepare(` |
| 165 | SELECT file_path, root_path, name, relative_path, size, mtime_ms |
| 166 | FROM file_search_index |
| 167 | WHERE file_path = ? OR file_path LIKE ? |
| 168 | `).all([from, from + path.sep + '%']); |
| 169 | |
| 170 | if (!rows.length) return; |
| 171 | |
| 172 | const del = db.prepare('DELETE FROM file_search_index WHERE file_path = ?'); |
| 173 | for (const row of rows) |
| 174 | { |
| 175 | del.run([row.file_path]); |
| 176 | const nextPath = row.file_path === from |
| 177 | ? to |
| 178 | : to + row.file_path.slice(from.length); |
| 179 | const nextName = path.basename(nextPath); |
| 180 | const rootPath = path.resolve(row.root_path); |
| 181 | const relativePath = path.relative(rootPath, nextPath); |
| 182 | upsertIndexRow(db, { |
| 183 | file_path: nextPath, |
| 184 | root_path: rootPath, |
| 185 | name: nextName, |
| 186 | name_lower: nextName.toLowerCase(), |
| 187 | relative_path: relativePath, |
| 188 | relative_lower: String(relativePath || '').toLowerCase(), |
| 189 | size: row.size, |
| 190 | mtime_ms: row.mtime_ms, |
| 191 | indexed_at: nowMs(), |
| 192 | }); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | async function walkFilesUnderRoot (storageRoot, onFile, startDir) |
| 197 | { |
| 198 | const resolvedRoot = path.resolve(storageRoot); |
| 199 | const stack = [path.resolve(startDir || storageRoot)]; |
| 200 | const visitedDirs = new Set(); |
| 201 | let dirsVisited = 0; |
| 202 | |
| 203 | while (stack.length) |
| 204 | { |
| 205 | const dir = stack.pop(); |
| 206 | if (!dir) continue; |
| 207 | const dirKey = path.resolve(dir); |
| 208 | if (visitedDirs.has(dirKey)) continue; |
| 209 | visitedDirs.add(dirKey); |
| 210 | dirsVisited += 1; |
| 211 | if (dirsVisited % 64 === 0) await yieldToEventLoop(); |
| 212 | |
| 213 | let entries; |
| 214 | try |
| 215 | { |
| 216 | entries = await fs.promises.readdir(dir, { withFileTypes: true }); |
| 217 | } catch |
| 218 | { |
| 219 | continue; |
| 220 | } |
| 221 | |
| 222 | for (const entry of entries) |
| 223 | { |
| 224 | const fullPath = path.join(dir, entry.name); |
| 225 | if (entry.isDirectory()) |
| 226 | { |
| 227 | stack.push(fullPath); |
| 228 | continue; |
| 229 | } |
| 230 | if (!entry.isFile()) continue; |
| 231 | |
| 232 | let stat; |
| 233 | try |
| 234 | { |
| 235 | stat = await fs.promises.stat(fullPath); |
| 236 | } catch |
| 237 | { |
| 238 | continue; |
| 239 | } |
| 240 | |
| 241 | const relativePath = path.relative(resolvedRoot, fullPath); |
| 242 | await onFile({ |
| 243 | file_path: fullPath, |
| 244 | root_path: resolvedRoot, |
| 245 | name: entry.name, |
| 246 | name_lower: entry.name.toLowerCase(), |
| 247 | relative_path: relativePath, |
| 248 | relative_lower: String(relativePath || '').toLowerCase(), |
| 249 | size: stat.size, |
| 250 | mtime_ms: stat.mtimeMs, |
| 251 | }); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | async function syncSearchIndexForRoot (rootPath) |
| 257 | { |
| 258 | const resolvedRoot = path.resolve(rootPath); |
| 259 | try |
| 260 | { |
| 261 | const stat = await fs.promises.stat(resolvedRoot); |
| 262 | if (!stat.isDirectory()) return { rootPath: resolvedRoot, indexed: 0, removed: 0 }; |
| 263 | } catch |
| 264 | { |
| 265 | return { rootPath: resolvedRoot, indexed: 0, removed: 0, offline: true }; |
| 266 | } |
| 267 | |
| 268 | const db = getDb(); |
| 269 | const seen = new Set(); |
| 270 | const indexedAt = nowMs(); |
| 271 | let indexed = 0; |
| 272 | |
| 273 | await walkFilesUnderRoot(resolvedRoot, async (fileRow) => |
| 274 | { |
| 275 | const filePath = path.resolve(fileRow.file_path); |
| 276 | seen.add(filePath); |
| 277 | upsertIndexRow(db, { |
| 278 | ...fileRow, |
| 279 | file_path: filePath, |
| 280 | root_path: resolvedRoot, |
| 281 | indexed_at: indexedAt, |
| 282 | }); |
| 283 | indexed += 1; |
| 284 | }); |
| 285 | |
| 286 | const existing = db.prepare('SELECT file_path FROM file_search_index WHERE root_path = ?').all([resolvedRoot]); |
| 287 | let removed = 0; |
| 288 | for (const row of existing) |
| 289 | { |
| 290 | if (seen.has(path.resolve(row.file_path))) continue; |
| 291 | db.prepare('DELETE FROM file_search_index WHERE file_path = ?').run([row.file_path]); |
| 292 | removed += 1; |
| 293 | } |
| 294 | |
| 295 | return { rootPath: resolvedRoot, indexed, removed }; |
| 296 | } |
| 297 | |
| 298 | async function syncSearchIndexOnce (roots) |
| 299 | { |
| 300 | const targetRoots = Array.isArray(roots) && roots.length ? roots.map((r) => path.resolve(r)) : effectiveIndexRoots(); |
| 301 | const summary = { |
| 302 | rootsScanned: 0, |
| 303 | filesIndexed: 0, |
| 304 | filesRemoved: 0, |
| 305 | offlineRoots: 0, |
| 306 | }; |
| 307 | |
| 308 | for (const root of targetRoots) |
| 309 | { |
| 310 | const result = await syncSearchIndexForRoot(root); |
| 311 | summary.rootsScanned += 1; |
| 312 | summary.filesIndexed += Number(result.indexed || 0); |
| 313 | summary.filesRemoved += Number(result.removed || 0); |
| 314 | if (result.offline) summary.offlineRoots += 1; |
| 315 | } |
| 316 | |
| 317 | return summary; |
| 318 | } |
| 319 | |
| 320 | function countIndexedFilesForRoots (roots) |
| 321 | { |
| 322 | const resolved = [...new Set((roots || []).map((r) => path.resolve(r)))]; |
| 323 | if (!resolved.length) return 0; |
| 324 | |
| 325 | let total = 0; |
| 326 | const db = getDb(); |
| 327 | for (let i = 0; i < resolved.length; i += ROOT_CHUNK_SIZE) |
| 328 | { |
| 329 | const chunk = resolved.slice(i, i + ROOT_CHUNK_SIZE); |
| 330 | const placeholders = chunk.map(() => '?').join(', '); |
| 331 | const row = db.prepare(`SELECT COUNT(*) AS n FROM file_search_index WHERE root_path IN (${placeholders})`).get(chunk); |
| 332 | total += Number(row && row.n || 0); |
| 333 | } |
| 334 | return total; |
| 335 | } |
| 336 | |
| 337 | function querySubstringCandidates (resolvedRoots, queryLower, rowLimit) |
| 338 | { |
| 339 | if (!resolvedRoots.length) return []; |
| 340 | |
| 341 | const db = getDb(); |
| 342 | const out = []; |
| 343 | const needle = queryLower; |
| 344 | |
| 345 | for (let i = 0; i < resolvedRoots.length; i += ROOT_CHUNK_SIZE) |
| 346 | { |
| 347 | const chunk = resolvedRoots.slice(i, i + ROOT_CHUNK_SIZE); |
| 348 | const placeholders = chunk.map(() => '?').join(', '); |
| 349 | const rows = db.prepare(` |
| 350 | SELECT file_path, root_path, name, relative_path, size, mtime_ms |
| 351 | FROM file_search_index |
| 352 | WHERE root_path IN (${placeholders}) |
| 353 | AND (instr(name_lower, ?) > 0 OR instr(relative_lower, ?) > 0) |
| 354 | LIMIT ? |
| 355 | `).all([...chunk, needle, needle, rowLimit]); |
| 356 | |
| 357 | for (const row of rows) out.push(row); |
| 358 | if (out.length >= rowLimit) break; |
| 359 | } |
| 360 | |
| 361 | return out.slice(0, rowLimit); |
| 362 | } |
| 363 | |
| 364 | function queryBatchCandidates (resolvedRoots, offset, batchSize) |
| 365 | { |
| 366 | if (!resolvedRoots.length) return []; |
| 367 | |
| 368 | const db = getDb(); |
| 369 | const out = []; |
| 370 | |
| 371 | for (let i = 0; i < resolvedRoots.length; i += ROOT_CHUNK_SIZE) |
| 372 | { |
| 373 | const chunk = resolvedRoots.slice(i, i + ROOT_CHUNK_SIZE); |
| 374 | const placeholders = chunk.map(() => '?').join(', '); |
| 375 | const rows = db.prepare(` |
| 376 | SELECT file_path, root_path, name, relative_path, size, mtime_ms |
| 377 | FROM file_search_index |
| 378 | WHERE root_path IN (${placeholders}) |
| 379 | ORDER BY file_path |
| 380 | LIMIT ? OFFSET ? |
| 381 | `).all([...chunk, batchSize, offset]); |
| 382 | |
| 383 | for (const row of rows) out.push(row); |
| 384 | if (out.length >= batchSize) return out.slice(0, batchSize); |
| 385 | } |
| 386 | |
| 387 | return out; |
| 388 | } |
| 389 | |
| 390 | function rowToSearchResult (row, query, score) |
| 391 | { |
| 392 | const filePath = path.resolve(row.file_path); |
| 393 | const rootPath = path.resolve(row.root_path); |
| 394 | const relativePath = row.relative_path != null |
| 395 | ? String(row.relative_path) |
| 396 | : path.relative(rootPath, filePath); |
| 397 | |
| 398 | let mtime = null; |
| 399 | if (Number.isFinite(Number(row.mtime_ms)) && Number(row.mtime_ms) > 0) |
| 400 | { |
| 401 | mtime = new Date(Number(row.mtime_ms)).toISOString(); |
| 402 | } |
| 403 | |
| 404 | return { |
| 405 | name: row.name || path.basename(filePath), |
| 406 | path: filePath, |
| 407 | dir: path.dirname(filePath), |
| 408 | rootPath, |
| 409 | relativePath, |
| 410 | size: Number.isFinite(Number(row.size)) ? Number(row.size) : null, |
| 411 | mtime, |
| 412 | score, |
| 413 | }; |
| 414 | } |
| 415 | |
| 416 | function collectCandidatesFromIndex (resolvedRoots, query, maxCandidates) |
| 417 | { |
| 418 | const queryLower = String(query || '').trim().toLowerCase(); |
| 419 | const byPath = new Map(); |
| 420 | |
| 421 | const substringRows = querySubstringCandidates(resolvedRoots, queryLower, maxCandidates); |
| 422 | for (const row of substringRows) |
| 423 | { |
| 424 | const score = fuzzyMatchScore(query, row.name) ?? fuzzyMatchScore(query, row.relative_path); |
| 425 | if (score == null) continue; |
| 426 | byPath.set(path.resolve(row.file_path), { row, score }); |
| 427 | } |
| 428 | |
| 429 | if (byPath.size < maxCandidates) |
| 430 | { |
| 431 | let offset = 0; |
| 432 | let scanned = 0; |
| 433 | while (scanned < MAX_SUBSEQUENCE_SCAN && byPath.size < maxCandidates) |
| 434 | { |
| 435 | const batch = queryBatchCandidates(resolvedRoots, offset, INDEX_BATCH_SIZE); |
| 436 | if (!batch.length) break; |
| 437 | offset += batch.length; |
| 438 | scanned += batch.length; |
| 439 | |
| 440 | for (const row of batch) |
| 441 | { |
| 442 | const filePath = path.resolve(row.file_path); |
| 443 | if (byPath.has(filePath)) continue; |
| 444 | const score = fuzzyMatchScore(query, row.name) ?? fuzzyMatchScore(query, row.relative_path); |
| 445 | if (score == null) continue; |
| 446 | byPath.set(filePath, { row, score }); |
| 447 | if (byPath.size >= maxCandidates) break; |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | return [...byPath.values()]; |
| 453 | } |
| 454 | |
| 455 | async function validateCandidates (candidates, dirtyDirs, db) |
| 456 | { |
| 457 | const validated = []; |
| 458 | const toCheck = candidates.slice(0, MAX_VALIDATE_HITS); |
| 459 | |
| 460 | for (const entry of toCheck) |
| 461 | { |
| 462 | const filePath = path.resolve(entry.row.file_path); |
| 463 | let stat; |
| 464 | try |
| 465 | { |
| 466 | stat = await fs.promises.stat(filePath); |
| 467 | } catch |
| 468 | { |
| 469 | db.prepare('DELETE FROM file_search_index WHERE file_path = ?').run([filePath]); |
| 470 | dirtyDirs.add(path.dirname(filePath)); |
| 471 | continue; |
| 472 | } |
| 473 | |
| 474 | if (!stat.isFile()) |
| 475 | { |
| 476 | db.prepare('DELETE FROM file_search_index WHERE file_path = ?').run([filePath]); |
| 477 | dirtyDirs.add(path.dirname(filePath)); |
| 478 | continue; |
| 479 | } |
| 480 | |
| 481 | const size = Number(stat.size); |
| 482 | const mtimeMs = Number(stat.mtimeMs); |
| 483 | const staleMeta = Number(entry.row.size) !== size || Number(entry.row.mtime_ms) !== mtimeMs; |
| 484 | if (staleMeta) |
| 485 | { |
| 486 | const rootPath = path.resolve(entry.row.root_path); |
| 487 | const name = path.basename(filePath); |
| 488 | const relativePath = path.relative(rootPath, filePath); |
| 489 | upsertIndexRow(db, { |
| 490 | file_path: filePath, |
| 491 | root_path: rootPath, |
| 492 | name, |
| 493 | name_lower: name.toLowerCase(), |
| 494 | relative_path: relativePath, |
| 495 | relative_lower: String(relativePath || '').toLowerCase(), |
| 496 | size, |
| 497 | mtime_ms: mtimeMs, |
| 498 | indexed_at: nowMs(), |
| 499 | }); |
| 500 | entry.row.size = size; |
| 501 | entry.row.mtime_ms = mtimeMs; |
| 502 | dirtyDirs.add(path.dirname(filePath)); |
| 503 | } |
| 504 | |
| 505 | validated.push(entry); |
| 506 | } |
| 507 | |
| 508 | for (const entry of candidates.slice(MAX_VALIDATE_HITS)) |
| 509 | { |
| 510 | validated.push(entry); |
| 511 | } |
| 512 | |
| 513 | return validated; |
| 514 | } |
| 515 | |
| 516 | async function reindexDirectories (dirs, rootByDir) |
| 517 | { |
| 518 | const queue = [...dirs].slice(0, MAX_RESCAN_DIRS_PER_SEARCH); |
| 519 | const discovered = []; |
| 520 | |
| 521 | for (const dir of queue) |
| 522 | { |
| 523 | const resolvedDir = path.resolve(dir); |
| 524 | const rootPath = rootByDir.get(resolvedDir); |
| 525 | if (!rootPath) continue; |
| 526 | |
| 527 | try |
| 528 | { |
| 529 | const stat = await fs.promises.stat(resolvedDir); |
| 530 | if (!stat.isDirectory()) continue; |
| 531 | } catch |
| 532 | { |
| 533 | continue; |
| 534 | } |
| 535 | |
| 536 | const db = getDb(); |
| 537 | const seen = new Set(); |
| 538 | const indexedAt = nowMs(); |
| 539 | |
| 540 | const resolvedRootPath = path.resolve(rootPath); |
| 541 | await walkFilesUnderRoot(resolvedRootPath, async (fileRow) => |
| 542 | { |
| 543 | const filePath = path.resolve(fileRow.file_path); |
| 544 | seen.add(filePath); |
| 545 | upsertIndexRow(db, { |
| 546 | ...fileRow, |
| 547 | file_path: filePath, |
| 548 | root_path: resolvedRootPath, |
| 549 | indexed_at: indexedAt, |
| 550 | }); |
| 551 | discovered.push(fileRow); |
| 552 | }, resolvedDir); |
| 553 | |
| 554 | const prefix = resolvedDir + path.sep; |
| 555 | const existing = db.prepare(` |
| 556 | SELECT file_path FROM file_search_index |
| 557 | WHERE root_path = ? AND (file_path = ? OR file_path LIKE ?) |
| 558 | `).all([path.resolve(rootPath), resolvedDir, prefix + '%']); |
| 559 | |
| 560 | for (const row of existing) |
| 561 | { |
| 562 | const filePath = path.resolve(row.file_path); |
| 563 | if (filePath === resolvedDir) continue; |
| 564 | if (seen.has(filePath)) continue; |
| 565 | db.prepare('DELETE FROM file_search_index WHERE file_path = ?').run([filePath]); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | return discovered; |
| 570 | } |
| 571 | |
| 572 | function rootForPath (resolvedRoots, filePath) |
| 573 | { |
| 574 | const resolved = path.resolve(filePath); |
| 575 | for (const root of resolvedRoots) |
| 576 | { |
| 577 | const rel = path.relative(root, resolved); |
| 578 | if (rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel))) return root; |
| 579 | } |
| 580 | return null; |
| 581 | } |
| 582 | |
| 583 | function buildRootByDirMap (resolvedRoots) |
| 584 | { |
| 585 | const map = new Map(); |
| 586 | for (const root of resolvedRoots) |
| 587 | { |
| 588 | map.set(path.resolve(root), path.resolve(root)); |
| 589 | } |
| 590 | return map; |
| 591 | } |
| 592 | |
| 593 | async function searchFilesFilesystemWalk (query, roots, maxResults) |
| 594 | { |
| 595 | const limit = Math.max(1, Math.min(500, Number(maxResults) || 200)); |
| 596 | const results = []; |
| 597 | const visitedDirs = new Set(); |
| 598 | let dirsVisited = 0; |
| 599 | |
| 600 | for (const root of roots) |
| 601 | { |
| 602 | const rootPath = path.resolve(root); |
| 603 | const stack = [rootPath]; |
| 604 | |
| 605 | while (stack.length) |
| 606 | { |
| 607 | const dir = stack.pop(); |
| 608 | if (!dir) continue; |
| 609 | const dirKey = path.resolve(dir); |
| 610 | if (visitedDirs.has(dirKey)) continue; |
| 611 | visitedDirs.add(dirKey); |
| 612 | dirsVisited += 1; |
| 613 | if (dirsVisited % 64 === 0) await yieldToEventLoop(); |
| 614 | |
| 615 | let entries; |
| 616 | try |
| 617 | { |
| 618 | entries = await fs.promises.readdir(dir, { withFileTypes: true }); |
| 619 | } catch |
| 620 | { |
| 621 | continue; |
| 622 | } |
| 623 | |
| 624 | for (const entry of entries) |
| 625 | { |
| 626 | const fullPath = path.join(dir, entry.name); |
| 627 | if (entry.isDirectory()) |
| 628 | { |
| 629 | stack.push(fullPath); |
| 630 | continue; |
| 631 | } |
| 632 | if (!entry.isFile()) continue; |
| 633 | |
| 634 | const relativePath = path.relative(rootPath, fullPath); |
| 635 | const score = fuzzyMatchScore(query, entry.name) |
| 636 | ?? fuzzyMatchScore(query, relativePath); |
| 637 | if (score == null) continue; |
| 638 | |
| 639 | let stat = null; |
| 640 | try { stat = await fs.promises.stat(fullPath); } catch { /* ignore */ } |
| 641 | |
| 642 | results.push(rowToSearchResult({ |
| 643 | file_path: fullPath, |
| 644 | root_path: rootPath, |
| 645 | name: entry.name, |
| 646 | relative_path: relativePath, |
| 647 | size: stat ? stat.size : null, |
| 648 | mtime_ms: stat ? stat.mtimeMs : null, |
| 649 | }, query, score)); |
| 650 | |
| 651 | if (results.length > limit * 4) |
| 652 | { |
| 653 | results.sort((a, b) => b.score - a.score || a.name.localeCompare(b.name)); |
| 654 | results.length = limit * 2; |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | results.sort((a, b) => b.score - a.score || a.name.localeCompare(b.name)); |
| 661 | return results.slice(0, limit); |
| 662 | } |
| 663 | |
| 664 | async function searchFiles (query, roots, maxResults) |
| 665 | { |
| 666 | const limit = Math.max(1, Math.min(500, Number(maxResults) || 200)); |
| 667 | const resolvedRoots = [...new Set((roots || []).map((r) => path.resolve(r)))]; |
| 668 | if (!resolvedRoots.length) return []; |
| 669 | |
| 670 | if (countIndexedFilesForRoots(resolvedRoots) < 1) |
| 671 | { |
| 672 | scheduleBackgroundSync(resolvedRoots); |
| 673 | return searchFilesFilesystemWalk(query, roots, limit); |
| 674 | } |
| 675 | |
| 676 | const candidates = collectCandidatesFromIndex(resolvedRoots, query, MAX_CANDIDATES_PER_SEARCH); |
| 677 | const dirtyDirs = new Set(); |
| 678 | const db = getDb(); |
| 679 | const validated = await validateCandidates(candidates, dirtyDirs, db); |
| 680 | |
| 681 | const rootByDir = buildRootByDirMap(resolvedRoots); |
| 682 | for (const dir of dirtyDirs) |
| 683 | { |
| 684 | const resolvedDir = path.resolve(dir); |
| 685 | if (!rootByDir.has(resolvedDir)) |
| 686 | { |
| 687 | const root = rootForPath(resolvedRoots, resolvedDir); |
| 688 | if (root) rootByDir.set(resolvedDir, root); |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | if (dirtyDirs.size) |
| 693 | { |
| 694 | const discovered = await reindexDirectories(dirtyDirs, rootByDir); |
| 695 | for (const fileRow of discovered) |
| 696 | { |
| 697 | const score = fuzzyMatchScore(query, fileRow.name) ?? fuzzyMatchScore(query, fileRow.relative_path); |
| 698 | if (score == null) continue; |
| 699 | validated.push({ |
| 700 | row: { |
| 701 | file_path: fileRow.file_path, |
| 702 | root_path: fileRow.root_path, |
| 703 | name: fileRow.name, |
| 704 | relative_path: fileRow.relative_path, |
| 705 | size: fileRow.size, |
| 706 | mtime_ms: fileRow.mtime_ms, |
| 707 | }, |
| 708 | score, |
| 709 | }); |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | const merged = new Map(); |
| 714 | for (const entry of validated) |
| 715 | { |
| 716 | const filePath = path.resolve(entry.row.file_path); |
| 717 | const prev = merged.get(filePath); |
| 718 | if (!prev || entry.score > prev.score) merged.set(filePath, entry); |
| 719 | } |
| 720 | |
| 721 | const results = [...merged.values()] |
| 722 | .map((entry) => rowToSearchResult(entry.row, query, entry.score)) |
| 723 | .sort((a, b) => b.score - a.score || a.name.localeCompare(b.name)); |
| 724 | |
| 725 | return results.slice(0, limit); |
| 726 | } |
| 727 | |
| 728 | function scheduleBackgroundSync (roots) |
| 729 | { |
| 730 | if (backgroundSyncRunning) return; |
| 731 | backgroundSyncRunning = true; |
| 732 | void syncSearchIndexOnce(roots) |
| 733 | .catch(() => { /* non-fatal */ }) |
| 734 | .finally(() => |
| 735 | { |
| 736 | backgroundSyncRunning = false; |
| 737 | }); |
| 738 | } |
| 739 | |
| 740 | function scheduleNextLoop (delayMs) |
| 741 | { |
| 742 | loopTimer = setTimeout(async () => |
| 743 | { |
| 744 | loopTimer = null; |
| 745 | try |
| 746 | { |
| 747 | if (!hasActiveClients()) |
| 748 | { |
| 749 | await syncSearchIndexOnce(); |
| 750 | } |
| 751 | } catch (err) |
| 752 | { |
| 753 | console.error('[search-index] sync failed:', err && err.message ? err.message : err); |
| 754 | } |
| 755 | finally |
| 756 | { |
| 757 | if (loopStarted) |
| 758 | { |
| 759 | const delay = hasActiveClients() ? ACTIVE_SYNC_INTERVAL_MS : IDLE_SYNC_INTERVAL_MS; |
| 760 | scheduleNextLoop(delay); |
| 761 | } |
| 762 | } |
| 763 | }, delayMs); |
| 764 | |
| 765 | if (loopTimer && typeof loopTimer.unref === 'function') loopTimer.unref(); |
| 766 | } |
| 767 | |
| 768 | function startSearchIndexLoop () |
| 769 | { |
| 770 | if (loopStarted) return; |
| 771 | loopStarted = true; |
| 772 | scheduleBackgroundSync(effectiveIndexRoots()); |
| 773 | scheduleNextLoop(hasActiveClients() ? ACTIVE_SYNC_INTERVAL_MS : IDLE_SYNC_INTERVAL_MS); |
| 774 | } |
| 775 | |
| 776 | function stopSearchIndexLoop () |
| 777 | { |
| 778 | loopStarted = false; |
| 779 | if (loopTimer) |
| 780 | { |
| 781 | clearTimeout(loopTimer); |
| 782 | loopTimer = null; |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | function getSearchIndexSnapshot () |
| 787 | { |
| 788 | const db = getDb(); |
| 789 | const row = db.prepare('SELECT COUNT(*) AS n FROM file_search_index').get(); |
| 790 | return { |
| 791 | indexedFileCount: Number(row && row.n || 0), |
| 792 | loopStarted, |
| 793 | hasActiveClients: hasActiveClients(), |
| 794 | backgroundSyncRunning, |
| 795 | }; |
| 796 | } |
| 797 | |
| 798 | module.exports = { |
| 799 | fuzzyMatchScore, |
| 800 | searchFiles, |
| 801 | searchFilesFilesystemWalk, |
| 802 | syncSearchIndexOnce, |
| 803 | countIndexedFilesForRoots, |
| 804 | deleteIndexPath, |
| 805 | deleteIndexUnder, |
| 806 | rewriteIndexPaths, |
| 807 | reindexDirectoryTree: async (dirPath) => |
| 808 | { |
| 809 | const resolvedDir = path.resolve(String(dirPath || '')); |
| 810 | const root = rootForPath(effectiveIndexRoots(), resolvedDir); |
| 811 | if (!root) return; |
| 812 | const rootByDir = new Map([[resolvedDir, root]]); |
| 813 | await reindexDirectories(new Set([resolvedDir]), rootByDir); |
| 814 | }, |
| 815 | startSearchIndexLoop, |
| 816 | stopSearchIndexLoop, |
| 817 | getSearchIndexSnapshot, |
| 818 | }; |