3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
5
import 'dart:async' ;
6
- import 'dart:io' ;
7
6
import 'dart:math' as math;
8
7
9
8
import 'package:gcloud/service_scope.dart' as ss;
@@ -24,20 +23,19 @@ void registerPackageIndex(PackageIndex index) =>
24
23
ss.register (#packageIndexService, index);
25
24
26
25
class SimplePackageIndex implements PackageIndex {
27
- final bool enableApiIndex;
28
26
final Map <String , PackageDocument > _packages = < String , PackageDocument > {};
29
27
final Map <String , String > _normalizedPackageText = < String , String > {};
30
28
final TokenIndex _nameIndex = new TokenIndex (minLength: 2 );
31
29
final TokenIndex _descrIndex = new TokenIndex (minLength: 3 );
32
30
final TokenIndex _readmeIndex = new TokenIndex (minLength: 3 );
33
31
final TokenIndex _apiDocIndex = new TokenIndex (minLength: 3 );
34
32
final StringInternPool _internPool = new StringInternPool ();
33
+ final bool _apiSearchEnabled;
35
34
DateTime _lastUpdated;
36
35
bool _isReady = false ;
37
36
38
- SimplePackageIndex ({bool enableApiIndex})
39
- : this .enableApiIndex =
40
- enableApiIndex ?? Platform .environment['SEARCH_API_INDEX' ] == '1' ;
37
+ SimplePackageIndex ({bool apiSearchEnabled: false })
38
+ : _apiSearchEnabled = apiSearchEnabled;
41
39
42
40
@override
43
41
bool get isReady => _isReady;
@@ -80,11 +78,9 @@ class SimplePackageIndex implements PackageIndex {
80
78
_nameIndex.add (doc.package, doc.package);
81
79
_descrIndex.add (doc.package, doc.description);
82
80
_readmeIndex.add (doc.package, doc.readme);
83
- if (enableApiIndex) {
84
- for (ApiDocPage page in doc.apiDocPages ?? const []) {
85
- _apiDocIndex.add (
86
- _apiDocPageId (doc.package, page), page.symbols? .join (' ' ));
87
- }
81
+ for (ApiDocPage page in doc.apiDocPages ?? const []) {
82
+ _apiDocIndex.add (
83
+ _apiDocPageId (doc.package, page), page.symbols? .join (' ' ));
88
84
}
89
85
final String allText = [doc.package, doc.description, doc.readme]
90
86
.where ((s) => s != null )
@@ -179,7 +175,8 @@ class SimplePackageIndex implements PackageIndex {
179
175
}
180
176
181
177
// do text matching
182
- final Score textScore = _searchText (packages, query.parsedQuery.text);
178
+ final Score textScore = _searchText (packages, query.parsedQuery.text,
179
+ _apiSearchEnabled || query.parsedQuery.isApiEnabled);
183
180
184
181
// filter packages that doesn't match text query
185
182
if (textScore != null ) {
@@ -299,15 +296,16 @@ class SimplePackageIndex implements PackageIndex {
299
296
return new Score (values);
300
297
}
301
298
302
- Score _searchText (Set <String > packages, String text) {
299
+ Score _searchText (Set <String > packages, String text, bool isExperimental ) {
303
300
if (text != null && text.isNotEmpty) {
304
301
final List <String > words = splitForIndexing (text).toList ();
305
302
final int wordCount = words.length;
306
303
final List <Score > wordScores = words.map ((String word) {
307
304
final nameTokens = _nameIndex.lookupTokens (word);
308
305
final descrTokens = _descrIndex.lookupTokens (word);
309
306
final readmeTokens = _readmeIndex.lookupTokens (word);
310
- final apiDocTokens = _apiDocIndex.lookupTokens (word);
307
+ final apiDocTokens =
308
+ isExperimental ? _apiDocIndex.lookupTokens (word) : new TokenMatch ();
311
309
final maxTokenLength = [
312
310
nameTokens.maxLength,
313
311
descrTokens.maxLength,
@@ -326,15 +324,20 @@ class SimplePackageIndex implements PackageIndex {
326
324
final readme = new Score (_readmeIndex.scoreDocs (readmeTokens,
327
325
weight: 0.90 , wordCount: wordCount));
328
326
329
- final apiPages = new Score (_apiDocIndex.scoreDocs (apiDocTokens,
330
- weight: 0.80 , wordCount: wordCount));
331
- final apiPackages = < String , double > {};
332
- for (String key in apiPages.getKeys ()) {
333
- final pkg = _apiDocPkg (key);
334
- final value = apiPages[key];
335
- apiPackages[pkg] = math.max (value, apiPackages[pkg] ?? 0.0 );
327
+ Score apiScore;
328
+ if (isExperimental) {
329
+ final apiPages = new Score (_apiDocIndex.scoreDocs (apiDocTokens,
330
+ weight: 0.80 , wordCount: wordCount));
331
+ final apiPackages = < String , double > {};
332
+ for (String key in apiPages.getKeys ()) {
333
+ final pkg = _apiDocPkg (key);
334
+ final value = apiPages[key];
335
+ apiPackages[pkg] = math.max (value, apiPackages[pkg] ?? 0.0 );
336
+ }
337
+ apiScore = new Score (apiPackages);
338
+ } else {
339
+ apiScore = new Score ({});
336
340
}
337
- final apiScore = new Score (apiPackages);
338
341
339
342
return Score .max ([name, descr, readme, apiScore]).removeLowValues (
340
343
fraction: 0.01 , minValue: 0.001 );
0 commit comments