@@ -27,6 +27,7 @@ class InMemoryPackageIndex {
27
27
late final TokenIndex <String > _descrIndex;
28
28
late final TokenIndex <String > _readmeIndex;
29
29
late final TokenIndex <IndexedApiDocPage > _apiSymbolIndex;
30
+ late final _scorePool = ScorePool (_packageNameIndex._packageNames);
30
31
31
32
/// Adjusted score takes the overall score and transforms
32
33
/// it linearly into the [0.4-1.0] range.
@@ -116,8 +117,16 @@ class InMemoryPackageIndex {
116
117
}
117
118
118
119
PackageSearchResult search (ServiceSearchQuery query) {
119
- final packageScores = IndexedScore (_packageNameIndex._packageNames, 1.0 );
120
+ return _scorePool.withScore (
121
+ value: 1.0 ,
122
+ fn: (score) {
123
+ return _search (query, score);
124
+ },
125
+ );
126
+ }
120
127
128
+ PackageSearchResult _search (
129
+ ServiceSearchQuery query, IndexedScore <String > packageScores) {
121
130
// filter on package prefix
122
131
if (query.parsedQuery.packagePrefix != null ) {
123
132
final String prefix = query.parsedQuery.packagePrefix! .toLowerCase ();
@@ -308,11 +317,17 @@ class InMemoryPackageIndex {
308
317
nameMatches.add (word);
309
318
}
310
319
311
- final wordScore =
312
- _packageNameIndex.searchWord (word, filterOnNonZeros: packageScores);
313
- _descrIndex.searchAndAccumulate (word, score: wordScore);
314
- _readmeIndex.searchAndAccumulate (word, weight: 0.75 , score: wordScore);
315
- packageScores.multiplyAllFrom (wordScore);
320
+ _scorePool.withScore (
321
+ value: 0.0 ,
322
+ fn: (wordScore) {
323
+ _packageNameIndex.searchWord (word,
324
+ score: wordScore, filterOnNonZeros: packageScores);
325
+ _descrIndex.searchAndAccumulate (word, score: wordScore);
326
+ _readmeIndex.searchAndAccumulate (word,
327
+ weight: 0.75 , score: wordScore);
328
+ packageScores.multiplyAllFrom (wordScore);
329
+ },
330
+ );
316
331
}
317
332
318
333
final topApiPages =
@@ -483,7 +498,8 @@ class PackageNameIndex {
483
498
Map <String , double > search (String text) {
484
499
IndexedScore <String >? score;
485
500
for (final w in splitForQuery (text)) {
486
- final s = searchWord (w, filterOnNonZeros: score);
501
+ final s = IndexedScore (_packageNames);
502
+ searchWord (w, score: s, filterOnNonZeros: score);
487
503
if (score == null ) {
488
504
score = s;
489
505
} else {
@@ -498,11 +514,12 @@ class PackageNameIndex {
498
514
///
499
515
/// When [filterOnNonZeros] is present, only the indexes with an already
500
516
/// non-zero value are evaluated.
501
- IndexedScore < String > searchWord (
517
+ void searchWord (
502
518
String word, {
519
+ required IndexedScore <String > score,
503
520
IndexedScore <String >? filterOnNonZeros,
504
521
}) {
505
- final score = IndexedScore ( _packageNames);
522
+ assert ( score.keys.length == _packageNames.length );
506
523
final singularWord = word.length <= 3 || ! word.endsWith ('s' )
507
524
? word
508
525
: word.substring (0 , word.length - 1 );
@@ -543,7 +560,6 @@ class PackageNameIndex {
543
560
}
544
561
}
545
562
}
546
- return score;
547
563
}
548
564
}
549
565
0 commit comments