@@ -10,7 +10,6 @@ import 'package:collection/collection.dart';
10
10
import 'package:logging/logging.dart' ;
11
11
import 'package:meta/meta.dart' ;
12
12
import 'package:pub_dev/search/heap.dart' ;
13
- import 'package:pub_dev/service/topics/models.dart' ;
14
13
import 'package:pub_dev/third_party/bit_array/bit_array.dart' ;
15
14
16
15
import 'models.dart' ;
@@ -23,7 +22,6 @@ final _textSearchTimeout = Duration(milliseconds: 500);
23
22
24
23
class InMemoryPackageIndex {
25
24
final List <PackageDocument > _documents;
26
- final _documentsByName = < String , PackageDocument > {};
27
25
final _nameToIndex = < String , int > {};
28
26
late final PackageNameIndex _packageNameIndex;
29
27
late final TokenIndex <String > _descrIndex;
@@ -47,23 +45,15 @@ class InMemoryPackageIndex {
47
45
late final List <IndexedPackageHit > _pointsOrderedHits;
48
46
late final List <IndexedPackageHit > _trendingOrderedHits;
49
47
50
- // Contains all of the topics the index had seen so far.
51
- // TODO: consider moving this into a separate index
52
- // TODO: get the list of topics from the bucket
53
- final _topics = < String > {
54
- ...canonicalTopics.aliasToCanonicalMap.values,
55
- };
56
-
57
48
late final DateTime _lastUpdated;
58
49
59
50
InMemoryPackageIndex ({
60
51
required Iterable <PackageDocument > documents,
61
52
}) : _documents = [...documents] {
62
53
final apiDocPageKeys = < IndexedApiDocPage > [];
63
- final apiDocPageValues = < String > [];
54
+ final apiDocPageValues = < List < String > > [];
64
55
for (var i = 0 ; i < _documents.length; i++ ) {
65
56
final doc = _documents[i];
66
- _documentsByName[doc.package] = doc;
67
57
_nameToIndex[doc.package] = i;
68
58
69
59
// transform tags into numberical IDs
@@ -78,16 +68,10 @@ class InMemoryPackageIndex {
78
68
for (final page in apiDocPages) {
79
69
if (page.symbols != null && page.symbols! .isNotEmpty) {
80
70
apiDocPageKeys.add (IndexedApiDocPage (i, page));
81
- apiDocPageValues.add (page.symbols! . join ( ' ' ) );
71
+ apiDocPageValues.add (page.symbols! );
82
72
}
83
73
}
84
74
}
85
-
86
- // Note: we are not removing topics from this set, only adding them, no
87
- // need for tracking the current topic count.
88
- _topics.addAll (doc.tags
89
- .where ((t) => t.startsWith ('topic:' ))
90
- .map ((t) => t.split ('topic:' ).last));
91
75
}
92
76
93
77
final packageKeys = _documents.map ((d) => d.package).toList ();
@@ -101,7 +85,7 @@ class InMemoryPackageIndex {
101
85
packageKeys,
102
86
_documents.map ((d) => d.readme).toList (),
103
87
);
104
- _apiSymbolIndex = TokenIndex (apiDocPageKeys, apiDocPageValues);
88
+ _apiSymbolIndex = TokenIndex . fromValues (apiDocPageKeys, apiDocPageValues);
105
89
106
90
// update download scores only if they were not set (should happen on old runtime's snapshot and local tests)
107
91
if (_documents.any ((e) => e.downloadScore == null )) {
@@ -131,7 +115,7 @@ class InMemoryPackageIndex {
131
115
IndexInfo indexInfo () {
132
116
return IndexInfo (
133
117
isReady: true ,
134
- packageCount: _documentsByName .length,
118
+ packageCount: _documents .length,
135
119
lastUpdated: _lastUpdated,
136
120
);
137
121
}
@@ -363,7 +347,7 @@ class InMemoryPackageIndex {
363
347
}
364
348
365
349
// exact package name
366
- if (_documentsByName .containsKey (parsedQueryText)) {
350
+ if (_nameToIndex .containsKey (parsedQueryText)) {
367
351
return parsedQueryText;
368
352
}
369
353
@@ -377,12 +361,9 @@ class InMemoryPackageIndex {
377
361
}
378
362
// Note: to keep it simple, we select the most downloaded one from competing matches.
379
363
return matches.reduce ((a, b) {
380
- if (_documentsByName[a]! .downloadCount >
381
- _documentsByName[b]! .downloadCount) {
382
- return a;
383
- } else {
384
- return b;
385
- }
364
+ final aDoc = _documents[_nameToIndex[a]! ];
365
+ final bDoc = _documents[_nameToIndex[b]! ];
366
+ return aDoc.downloadCount > bDoc.downloadCount ? a : b;
386
367
});
387
368
}
388
369
@@ -487,7 +468,7 @@ class InMemoryPackageIndex {
487
468
packageScores.setValueMaxOf (doc.index, value);
488
469
489
470
// add the page and re-sort the current results
490
- pages.add (MapEntry (doc.page. relativePath, value));
471
+ pages.add (MapEntry (doc.relativePath, value));
491
472
if (pages.length > 1 ) {
492
473
pages.sort ((a, b) => - a.value.compareTo (b.value));
493
474
}
@@ -750,7 +731,8 @@ class IndexedPackageHit {
750
731
751
732
class IndexedApiDocPage {
752
733
final int index;
753
- final ApiDocPage page ;
734
+ final String relativePath ;
754
735
755
- IndexedApiDocPage (this .index, this .page);
736
+ IndexedApiDocPage (this .index, ApiDocPage page)
737
+ : relativePath = page.relativePath;
756
738
}
0 commit comments