@@ -173,7 +173,6 @@ class InMemoryPackageIndex {
173173
174174 final nameMatches = textResults? .nameMatches;
175175 List <String >? topicMatches;
176- List <PackageHit > packageHits;
177176
178177 if (parsedQueryText != null ) {
179178 final parts = parsedQueryText
@@ -187,55 +186,59 @@ class InMemoryPackageIndex {
187186 }
188187 }
189188
189+ List <IndexedPackageHit > indexedHits;
190190 switch (query.effectiveOrder ?? SearchOrder .top) {
191191 case SearchOrder .top:
192192 if (textResults == null ) {
193- packageHits = _overallOrderedHits.whereInScores (packageScores);
193+ indexedHits = _overallOrderedHits.whereInScores (packageScores);
194194 break ;
195195 }
196196
197197 /// Adjusted score takes the overall score and transforms
198198 /// it linearly into the [0.4-1.0] range, to allow better
199199 /// multiplication outcomes.
200200 packageScores.multiplyAllFromValues (_adjustedOverallScores);
201- packageHits = _rankWithValues (packageScores);
201+ indexedHits = _rankWithValues (packageScores);
202202 break ;
203203 case SearchOrder .text:
204- packageHits = _rankWithValues (packageScores);
204+ indexedHits = _rankWithValues (packageScores);
205205 break ;
206206 case SearchOrder .created:
207- packageHits = _createdOrderedHits.whereInScores (packageScores);
207+ indexedHits = _createdOrderedHits.whereInScores (packageScores);
208208 break ;
209209 case SearchOrder .updated:
210- packageHits = _updatedOrderedHits.whereInScores (packageScores);
210+ indexedHits = _updatedOrderedHits.whereInScores (packageScores);
211211 break ;
212212 case SearchOrder .popularity:
213- packageHits = _popularityOrderedHits.whereInScores (packageScores);
213+ indexedHits = _popularityOrderedHits.whereInScores (packageScores);
214214 break ;
215215 case SearchOrder .downloads:
216- packageHits = _downloadsOrderedHits.whereInScores (packageScores);
216+ indexedHits = _downloadsOrderedHits.whereInScores (packageScores);
217217 break ;
218218 case SearchOrder .like:
219- packageHits = _likesOrderedHits.whereInScores (packageScores);
219+ indexedHits = _likesOrderedHits.whereInScores (packageScores);
220220 break ;
221221 case SearchOrder .points:
222- packageHits = _pointsOrderedHits.whereInScores (packageScores);
222+ indexedHits = _pointsOrderedHits.whereInScores (packageScores);
223223 break ;
224224 }
225225
226226 // bound by offset and limit (or randomize items)
227- final totalCount = packageHits.length;
228- packageHits =
229- boundedList (packageHits, offset: query.offset, limit: query.limit);
230-
231- if (textResults != null && textResults.topApiPages.isNotEmpty) {
232- packageHits = packageHits.map ((ps) {
233- final apiPages = textResults.topApiPages[ps.package]
227+ final totalCount = indexedHits.length;
228+ indexedHits =
229+ boundedList (indexedHits, offset: query.offset, limit: query.limit);
230+
231+ late List <PackageHit > packageHits;
232+ if (textResults != null && (textResults.topApiPages? .isNotEmpty ?? false )) {
233+ packageHits = indexedHits.map ((ps) {
234+ final apiPages = textResults.topApiPages? [ps.index]
234235 // TODO(https://github.com/dart-lang/pub-dev/issues/7106): extract title for the page
235236 ? .map ((MapEntry <String , double > e) => ApiPageRef (path: e.key))
236237 .toList ();
237- return ps.change (apiPages: apiPages);
238+ return ps.hit. change (apiPages: apiPages);
238239 }).toList ();
240+ } else {
241+ packageHits = indexedHits.map ((h) => h.hit).toList ();
239242 }
240243
241244 return PackageSearchResult (
@@ -311,7 +314,8 @@ class InMemoryPackageIndex {
311314 packageScores.multiplyAllFrom (wordScore);
312315 }
313316
314- final topApiPages = < String , List <MapEntry <String , double >>> {};
317+ final topApiPages =
318+ List <List <MapEntry <String , double >>?>.filled (_documents.length, null );
315319 const maxApiPageCount = 2 ;
316320 if (! checkAborted ()) {
317321 final symbolPages = _apiSymbolIndex.searchWords (words, weight: 0.70 );
@@ -324,7 +328,7 @@ class InMemoryPackageIndex {
324328 if (! packages.contains (doc.package)) continue ;
325329
326330 // skip if the previously found pages are better than the current one
327- final pages = topApiPages. putIfAbsent ( doc.package, () => []) ;
331+ final pages = topApiPages[ doc.index] ?? = < MapEntry < String , double > > [] ;
328332 if (pages.length >= maxApiPageCount && pages.last.value > value) {
329333 continue ;
330334 }
@@ -369,7 +373,7 @@ class InMemoryPackageIndex {
369373 return null ;
370374 }
371375
372- List <PackageHit > _rankWithValues (IndexedScore <String > score) {
376+ List <IndexedPackageHit > _rankWithValues (IndexedScore <String > score) {
373377 final list = < IndexedPackageHit > [];
374378 for (var i = 0 ; i < score.length; i++ ) {
375379 final value = score.getValue (i);
@@ -383,7 +387,7 @@ class InMemoryPackageIndex {
383387 // if two packages got the same score, order by last updated
384388 return _compareUpdated (_documents[a.index], _documents[b.index]);
385389 });
386- return list.map ((h) => h.hit). toList ();
390+ return list.toList ();
387391 }
388392
389393 List <IndexedPackageHit > _rankWithComparator (
@@ -441,11 +445,11 @@ class InMemoryPackageIndex {
441445}
442446
443447class _TextResults {
444- final Map < String , List <MapEntry <String , double >>> topApiPages;
448+ final List < List <MapEntry <String , double >>?> ? topApiPages;
445449 final List <String >? nameMatches;
446450
447451 factory _TextResults .empty () => _TextResults (
448- {} ,
452+ null ,
449453 nameMatches: null ,
450454 );
451455
@@ -541,8 +545,8 @@ class _PkgNameData {
541545}
542546
543547extension on List <IndexedPackageHit > {
544- List <PackageHit > whereInScores (IndexedScore scores) {
545- return where ((h) => scores.isPositive (h.index)).map ((h) => h.hit). toList ();
548+ List <IndexedPackageHit > whereInScores (IndexedScore scores) {
549+ return where ((h) => scores.isPositive (h.index)).toList ();
546550 }
547551}
548552
0 commit comments