@@ -30,7 +30,7 @@ class InMemoryPackageIndex {
30
30
31
31
/// Adjusted score takes the overall score and transforms
32
32
/// it linearly into the [0.4-1.0] range.
33
- final _adjustedOverallScores = < String , double > {} ;
33
+ late final List < double > _adjustedOverallScores ;
34
34
late final List <IndexedPackageHit > _overallOrderedHits;
35
35
late final List <IndexedPackageHit > _createdOrderedHits;
36
36
late final List <IndexedPackageHit > _updatedOrderedHits;
@@ -197,13 +197,11 @@ class InMemoryPackageIndex {
197
197
/// Adjusted score takes the overall score and transforms
198
198
/// it linearly into the [0.4-1.0] range, to allow better
199
199
/// multiplication outcomes.
200
- final overallScore = textResults.pkgScore
201
- .mapValues ((key, value) => value * _adjustedOverallScores[key]! );
202
- packageHits = _rankWithValues (overallScore);
200
+ packageScores.multiplyAllFromValues (_adjustedOverallScores);
201
+ packageHits = _rankWithValues (packageScores);
203
202
break ;
204
203
case SearchOrder .text:
205
- final score = textResults? .pkgScore ?? Score .empty;
206
- packageHits = _rankWithValues (score);
204
+ packageHits = _rankWithValues (packageScores);
207
205
break ;
208
206
case SearchOrder .created:
209
207
packageHits = _createdOrderedHits.whereInScores (packageScores);
@@ -251,16 +249,16 @@ class InMemoryPackageIndex {
251
249
252
250
/// Update the overall score both on [PackageDocument] and in the [_adjustedOverallScores] map.
253
251
void _updateOverallScores () {
254
- for ( final doc in _documentsByName.values ) {
252
+ _adjustedOverallScores = _documents. map ((doc ) {
255
253
final downloadScore = doc.popularityScore ?? 0.0 ;
256
254
final likeScore = doc.likeScore ?? 0.0 ;
257
255
final popularity = (downloadScore + likeScore) / 2 ;
258
256
final points = doc.grantedPoints / math.max (1 , doc.maxPoints);
259
257
final overall = popularity * 0.5 + points * 0.5 ;
260
258
doc.overallScore = overall;
261
259
// adding a base score prevents later multiplication with zero
262
- _adjustedOverallScores[doc.package] = 0.4 + 0.6 * overall;
263
- }
260
+ return 0.4 + 0.6 * overall;
261
+ }). toList ();
264
262
}
265
263
266
264
_TextResults ? _searchText (
@@ -272,6 +270,9 @@ class InMemoryPackageIndex {
272
270
if (text != null && text.isNotEmpty) {
273
271
final words = splitForQuery (text);
274
272
if (words.isEmpty) {
273
+ for (var i = 0 ; i < packageScores.length; i++ ) {
274
+ packageScores.setValue (i, 0 );
275
+ }
275
276
return _TextResults .empty ();
276
277
}
277
278
@@ -361,26 +362,28 @@ class InMemoryPackageIndex {
361
362
}
362
363
363
364
return _TextResults (
364
- packageScores.toScore (),
365
365
topApiPages,
366
366
nameMatches: nameMatches? .toList (),
367
367
);
368
368
}
369
369
return null ;
370
370
}
371
371
372
- List <PackageHit > _rankWithValues (Map <String , double > values) {
373
- final list = values.entries
374
- .map ((e) => PackageHit (package: e.key, score: e.value))
375
- .toList ();
372
+ List <PackageHit > _rankWithValues (IndexedScore <String > score) {
373
+ final list = < IndexedPackageHit > [];
374
+ for (var i = 0 ; i < score.length; i++ ) {
375
+ final value = score.getValue (i);
376
+ if (value <= 0.0 ) continue ;
377
+ list.add (IndexedPackageHit (
378
+ i, PackageHit (package: score.keys[i], score: value)));
379
+ }
376
380
list.sort ((a, b) {
377
- final int scoreCompare = - a.score! .compareTo (b.score! );
381
+ final scoreCompare = - a.hit. score! .compareTo (b.hit .score! );
378
382
if (scoreCompare != 0 ) return scoreCompare;
379
383
// if two packages got the same score, order by last updated
380
- return _compareUpdated (
381
- _documentsByName[a.package]! , _documentsByName[b.package]! );
384
+ return _compareUpdated (_documents[a.index], _documents[b.index]);
382
385
});
383
- return list;
386
+ return list. map ((h) => h.hit). toList () ;
384
387
}
385
388
386
389
List <IndexedPackageHit > _rankWithComparator (
@@ -438,18 +441,15 @@ class InMemoryPackageIndex {
438
441
}
439
442
440
443
class _TextResults {
441
- final Score pkgScore;
442
444
final Map <String , List <MapEntry <String , double >>> topApiPages;
443
445
final List <String >? nameMatches;
444
446
445
447
factory _TextResults .empty () => _TextResults (
446
- Score .empty,
447
448
{},
448
449
nameMatches: null ,
449
450
);
450
451
451
452
_TextResults (
452
- this .pkgScore,
453
453
this .topApiPages, {
454
454
required this .nameMatches,
455
455
});
0 commit comments