Skip to content

Commit 5396f91

Browse files
authored
Make PackageNameIndex immutable (#8228)
1 parent c661d3f commit 5396f91

File tree

2 files changed

+23
-27
lines changed

2 files changed

+23
-27
lines changed

app/lib/search/mem_index.dart

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ final _textSearchTimeout = Duration(milliseconds: 500);
2222
class InMemoryPackageIndex {
2323
final List<PackageDocument> _documents;
2424
final _documentsByName = <String, PackageDocument>{};
25-
final _packageNameIndex = PackageNameIndex();
25+
late final PackageNameIndex _packageNameIndex;
2626
late final TokenIndex _descrIndex;
2727
late final TokenIndex _readmeIndex;
2828
late final TokenIndex _apiSymbolIndex;
@@ -54,7 +54,6 @@ class InMemoryPackageIndex {
5454
final apiDocPageValues = <String>[];
5555
for (final doc in _documents) {
5656
_documentsByName[doc.package] = doc;
57-
_packageNameIndex.add(doc.package);
5857

5958
final apiDocPages = doc.apiDocPages;
6059
if (apiDocPages != null) {
@@ -74,6 +73,7 @@ class InMemoryPackageIndex {
7473
}
7574

7675
final packageKeys = _documents.map((d) => d.package).toList();
76+
_packageNameIndex = PackageNameIndex(packageKeys);
7777
_descrIndex = TokenIndex(
7878
packageKeys,
7979
_documents.map((d) => d.description).toList(),
@@ -501,35 +501,32 @@ class _TextResults {
501501
/// A simple (non-inverted) index designed for package name lookup.
502502
@visibleForTesting
503503
class PackageNameIndex {
504-
final _data = <String, _PkgNameData>{};
504+
final List<String> _packageNames;
505+
late final Map<String, _PkgNameData> _data;
506+
507+
PackageNameIndex(this._packageNames) {
508+
_data = Map.fromEntries(_packageNames.map((package) {
509+
final collapsed = _collapseName(package);
510+
return MapEntry(
511+
package,
512+
_PkgNameData(collapsed, trigrams(collapsed).toSet()),
513+
);
514+
}));
515+
}
505516

506517
/// Maps package name to a reduced form of the name:
507518
/// the same character parts, but without `-`.
508519
String _collapseName(String package) =>
509520
package.replaceAll('_', '').toLowerCase();
510521

511-
void addAll(Iterable<String> packages) {
512-
for (final package in packages) {
513-
add(package);
514-
}
515-
}
516-
517-
/// Add a new [package] to the index.
518-
void add(String package) {
519-
_data.putIfAbsent(package, () {
520-
final collapsed = _collapseName(package);
521-
return _PkgNameData(collapsed, trigrams(collapsed).toSet());
522-
});
523-
}
524-
525522
/// Search [text] and return the matching packages with scores.
526523
Score search(String text) {
527524
return Score.multiply(splitForQuery(text).map(searchWord).toList());
528525
}
529526

530527
/// Search using the parsed [word] and return the match packages with scores.
531528
Score searchWord(String word, {Set<String>? packages}) {
532-
final pkgNamesToCheck = packages ?? _data.keys;
529+
final pkgNamesToCheck = packages ?? _packageNames;
533530
final values = <String, double>{};
534531
final singularWord = word.length <= 3 || !word.endsWith('s')
535532
? word

app/test/search/package_name_index_test.dart

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ import 'package:test/test.dart';
77

88
void main() {
99
group('PackageNameIndex', () {
10-
final index = PackageNameIndex()
11-
..addAll([
12-
'fluent',
13-
'fluent_ui',
14-
'get',
15-
'get_it',
16-
'modular',
17-
'modular_flutter',
18-
]);
10+
final index = PackageNameIndex([
11+
'fluent',
12+
'fluent_ui',
13+
'get',
14+
'get_it',
15+
'modular',
16+
'modular_flutter',
17+
]);
1918

2019
test('fluent vs fluent_ui', () {
2120
expect(

0 commit comments

Comments
 (0)