Skip to content

Commit 1cbb7b3

Browse files
committed
feat(search): Integrate index cache into search flow
When use_sqlite_cache is disabled, try the binary-index cache first for literal queries. Regex metacharacters (.+*?|[]{}^$\ etc.) skip the cache path and go directly to the regex full-scan, preserving documented regex semantics. Cache hit: in-memory search (~200ms), no file I/O. Cache miss: fall back to original regex search (~3s), then build cache for next invocation. Also separates the query string from the compiled Regex object, fixing a latent bug where search_remotes received a Regex instead of the original string (previously relied on .ToString() coincidence).
1 parent 5a57496 commit 1cbb7b3

1 file changed

Lines changed: 25 additions & 10 deletions

File tree

libexec/scoop-search.ps1

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ function search_by_index($query) {
115115
$manifest = Get-Content -Path $info.path -Raw | ConvertFrom-Json -ErrorAction Stop
116116
$binaries = ''
117117
$binMatches = @()
118+
if (-not $manifest) { continue }
118119
if ($manifest.bin) {
119120
foreach ($binEntry in $manifest.bin) {
120121
$exe = $null; $alias = $null
@@ -291,20 +292,34 @@ if (get_config USE_SQLITE_CACHE) {
291292
})
292293
}
293294
} else {
294-
try {
295-
$query = New-Object Regex $query, 'IgnoreCase'
296-
} catch {
297-
abort "Invalid regular expression: $($_.Exception.InnerException.Message)"
295+
# Try search index cache first for literal queries (fast, substring matching).
296+
# Queries with regex metacharacters skip the cache and go straight to regex.
297+
$cacheWasFresh = $false
298+
if ($query -notmatch '[.+*?|\[\](){}^$\\]') {
299+
$cacheWasFresh = init_search_cache
300+
if ($cacheWasFresh) { search_by_index $query }
298301
}
299302

300-
$jsonTextAvailable = [System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { [System.IO.Path]::GetFileNameWithoutExtension($_.Location) -eq 'System.Text.Json' }
303+
# Fall back to regex full-scan when cache produced no results
304+
if ($list.Count -eq 0) {
305+
try {
306+
$regex = New-Object Regex $query, 'IgnoreCase'
307+
} catch {
308+
abort "Invalid regular expression: $($_.Exception.InnerException.Message)"
309+
}
301310

302-
Get-LocalBucket | ForEach-Object {
303-
if ($jsonTextAvailable) {
304-
search_bucket $_ $query
305-
} else {
306-
search_bucket_legacy $_ $query
311+
$jsonTextAvailable = [System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { [System.IO.Path]::GetFileNameWithoutExtension($_.Location) -eq 'System.Text.Json' }
312+
313+
Get-LocalBucket | ForEach-Object {
314+
if ($jsonTextAvailable) {
315+
search_bucket $_ $regex
316+
} else {
317+
search_bucket_legacy $_ $regex
318+
}
307319
}
320+
321+
# Build cache for next time only if it wasn't already fresh
322+
if (-not $cacheWasFresh) { build_search_cache }
308323
}
309324
}
310325

0 commit comments

Comments
 (0)