Skip to content

Commit 9a44dec

Browse files
committed
Use proper enum for TextMatchExtent.
1 parent f765364 commit 9a44dec

File tree

3 files changed

+29
-31
lines changed

3 files changed

+29
-31
lines changed

app/lib/search/mem_index.dart

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ class InMemoryPackageIndex {
226226
packageScores,
227227
parsedQueryText,
228228
includeNameMatches: (query.offset ?? 0) == 0,
229-
textMatchExtent: query.textMatchExtent,
229+
textMatchExtent: query.textMatchExtent ?? TextMatchExtent.api,
230230
);
231231

232232
final nameMatches = textResults?.nameMatches;
@@ -288,7 +288,7 @@ class InMemoryPackageIndex {
288288
boundedList(indexedHits, offset: query.offset, limit: query.limit);
289289

290290
late List<PackageHit> packageHits;
291-
if (TextMatchExtent.shouldMatchApi(query.textMatchExtent) &&
291+
if ((query.textMatchExtent ?? TextMatchExtent.api).shouldMatchApi() &&
292292
textResults != null &&
293293
(textResults.topApiPages?.isNotEmpty ?? false)) {
294294
packageHits = indexedHits.map((ps) {
@@ -336,7 +336,7 @@ class InMemoryPackageIndex {
336336
IndexedScore<String> packageScores,
337337
String? text, {
338338
required bool includeNameMatches,
339-
required int? textMatchExtent,
339+
required TextMatchExtent textMatchExtent,
340340
}) {
341341
if (text == null || text.isEmpty) {
342342
return null;
@@ -349,7 +349,7 @@ class InMemoryPackageIndex {
349349
return _TextResults.empty();
350350
}
351351

352-
final matchName = TextMatchExtent.shouldMatchName(textMatchExtent);
352+
final matchName = textMatchExtent.shouldMatchName();
353353
if (!matchName) {
354354
packageScores.fillRange(0, packageScores.length, 0);
355355
return _TextResults.empty(
@@ -379,10 +379,9 @@ class InMemoryPackageIndex {
379379
/// However, API docs search should be filtered on the original list.
380380
final indexedPositiveList = packageScores.toIndexedPositiveList();
381381

382-
final matchDescription =
383-
TextMatchExtent.shouldMatchDescription(textMatchExtent);
384-
final matchReadme = TextMatchExtent.shouldMatchReadme(textMatchExtent);
385-
final matchApi = TextMatchExtent.shouldMatchApi(textMatchExtent);
382+
final matchDescription = textMatchExtent.shouldMatchDescription();
383+
final matchReadme = textMatchExtent.shouldMatchReadme();
384+
final matchApi = textMatchExtent.shouldMatchApi();
386385

387386
for (final word in words) {
388387
if (includeNameMatches && _documentsByName.containsKey(word)) {

app/lib/search/search_service.dart

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'dart:math' show max;
88
import 'package:_pub_shared/search/search_form.dart';
99
import 'package:_pub_shared/search/tags.dart';
1010
import 'package:clock/clock.dart';
11+
import 'package:collection/collection.dart';
1112
import 'package:json_annotation/json_annotation.dart';
1213
import 'package:pub_dev/shared/utils.dart';
1314

@@ -166,7 +167,7 @@ class ServiceSearchQuery {
166167
final int? limit;
167168

168169
/// The scope/depth of text matching.
169-
final int? textMatchExtent;
170+
final TextMatchExtent? textMatchExtent;
170171

171172
ServiceSearchQuery._({
172173
this.query,
@@ -189,7 +190,7 @@ class ServiceSearchQuery {
189190
int? minPoints,
190191
int offset = 0,
191192
int? limit = 10,
192-
int? textMatchExtent,
193+
TextMatchExtent? textMatchExtent,
193194
}) {
194195
final q = query?.trimToNull();
195196
return ServiceSearchQuery._(
@@ -216,8 +217,10 @@ class ServiceSearchQuery {
216217
int.tryParse(uri.queryParameters['minPoints'] ?? '0') ?? 0;
217218
final offset = int.tryParse(uri.queryParameters['offset'] ?? '0') ?? 0;
218219
final limit = int.tryParse(uri.queryParameters['limit'] ?? '0') ?? 0;
219-
final textMatchExtent =
220-
int.tryParse(uri.queryParameters['textMatchExtent'] ?? '');
220+
final textMatchExtentValue =
221+
uri.queryParameters['textMatchExtent']?.trim() ?? '';
222+
final textMatchExtent = TextMatchExtent.values
223+
.firstWhereOrNull((e) => e.name == textMatchExtentValue);
221224

222225
return ServiceSearchQuery.parse(
223226
query: q,
@@ -238,7 +241,7 @@ class ServiceSearchQuery {
238241
SearchOrder? order,
239242
int? offset,
240243
int? limit,
241-
int? textMatchExtent,
244+
TextMatchExtent? textMatchExtent,
242245
}) {
243246
return ServiceSearchQuery._(
244247
query: query ?? this.query,
@@ -262,8 +265,7 @@ class ServiceSearchQuery {
262265
'minPoints': minPoints.toString(),
263266
'limit': limit?.toString(),
264267
'order': order?.name,
265-
if (textMatchExtent != null)
266-
'textMatchExtent': textMatchExtent.toString(),
268+
if (textMatchExtent != null) 'textMatchExtent': textMatchExtent!.name,
267269
};
268270
map.removeWhere((k, v) => v == null);
269271
return map;
@@ -291,7 +293,7 @@ class ServiceSearchQuery {
291293
_isNaturalOrder &&
292294
_hasNoOwnershipScope &&
293295
!_isFlutterFavorite &&
294-
TextMatchExtent.shouldMatchApi(textMatchExtent);
296+
(textMatchExtent ?? TextMatchExtent.api).shouldMatchApi();
295297

296298
bool get considerHighlightedHit => _hasOnlyFreeText && _hasNoOwnershipScope;
297299
bool get includeHighlightedHit => considerHighlightedHit && offset == 0;
@@ -310,38 +312,35 @@ class ServiceSearchQuery {
310312
}
311313

312314
/// The scope (depth) of the text matching.
313-
abstract class TextMatchExtent {
315+
enum TextMatchExtent {
314316
/// No text search is done.
315317
/// Requests with text queries will return a failure message.
316-
static final int none = 10;
318+
none,
317319

318320
/// Text search is on package names.
319-
static final int name = 20;
321+
name,
320322

321323
/// Text search is on package names, descriptions and topic tags.
322-
static final int description = 30;
324+
description,
323325

324326
/// Text search is on names, descriptions, topic tags and readme content.
325-
static final int readme = 40;
327+
readme,
326328

327329
/// Text search is on names, descriptions, topic tags, readme content and API symbols.
328-
static final int api = 50;
329-
330-
/// No value was given, assuming default behavior of including everything.
331-
static final int unspecified = 99;
330+
api,
331+
;
332332

333333
/// Text search is on package names.
334-
static bool shouldMatchName(int? value) => (value ?? unspecified) >= name;
334+
bool shouldMatchName() => index >= name.index;
335335

336336
/// Text search is on package names, descriptions and topic tags.
337-
static bool shouldMatchDescription(int? value) =>
338-
(value ?? unspecified) >= description;
337+
bool shouldMatchDescription() => index >= description.index;
339338

340339
/// Text search is on names, descriptions, topic tags and readme content.
341-
static bool shouldMatchReadme(int? value) => (value ?? unspecified) >= readme;
340+
bool shouldMatchReadme() => index >= readme.index;
342341

343342
/// Text search is on names, descriptions, topic tags, readme content and API symbols.
344-
static bool shouldMatchApi(int? value) => (value ?? unspecified) >= api;
343+
bool shouldMatchApi() => index >= api.index;
345344
}
346345

347346
class QueryValidity {

app/lib/service/entrypoint/search_index.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class LatencyAwareSearchIndex implements SearchIndex {
180180
/// Note: the latency here may be a residue of a large spike that happened
181181
/// more than a few minute ago, therefore we are deciding on latency
182182
/// range over the default 5 seconds timeout window.
183-
int _selectTextMatchExtent() {
183+
TextMatchExtent _selectTextMatchExtent() {
184184
final latency = _latencyTracker.getLatency();
185185
if (latency < const Duration(seconds: 1)) {
186186
_logger.info('[text-match-normal]');

0 commit comments

Comments
 (0)