Skip to content

Commit 0835bad

Browse files
committed
Do not clear TokenIndex when updating with the same value.
1 parent 5c7c24a commit 0835bad

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

app/lib/search/index_simple.dart

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,13 @@ class SimplePackageIndex implements PackageIndex {
7272
@override
7373
Future addPackage(PackageDocument document) async {
7474
final PackageDocument doc = document.intern(_internPool.intern);
75-
await removePackage(doc.package);
76-
if (document.isDiscontinued == true) return; // isDiscontinued may be null
75+
76+
// isDiscontinued may be null
77+
if (document.isDiscontinued == true) {
78+
await removePackage(doc.package);
79+
return;
80+
}
81+
7782
_packages[doc.package] = doc;
7883
_nameIndex.add(doc.package, doc.package);
7984
_descrIndex.add(doc.package, doc.description);
@@ -531,6 +536,7 @@ class TokenMatch {
531536
}
532537

533538
class TokenIndex {
539+
final Map<String, String> _textHashes = <String, String>{};
534540
final Map<String, Set<String>> _inverseIds = <String, Set<String>>{};
535541
final Map<String, double> _docSizes = <String, double>{};
536542
final int _minLength;
@@ -544,17 +550,28 @@ class TokenIndex {
544550

545551
void add(String id, String text) {
546552
final Set<String> tokens = _tokenize(text, _minLength);
547-
if (tokens == null || tokens.isEmpty) return;
553+
if (tokens == null || tokens.isEmpty) {
554+
if (_textHashes.containsKey(id)) {
555+
remove(id);
556+
}
557+
return;
558+
}
559+
final String textHash = '${text.hashCode}/${tokens.length}';
560+
if (_textHashes.containsKey(id) && _textHashes[id] != textHash) {
561+
remove(id);
562+
}
548563
for (String token in tokens) {
549564
final Set<String> set = _inverseIds.putIfAbsent(token, () => new Set());
550565
set.add(id);
551566
}
552567
// Document size is a highly scaled-down proxy of the length.
553568
final docSize = 1 + math.log(1 + tokens.length) / 100;
554569
_docSizes[id] = docSize;
570+
_textHashes[id] = textHash;
555571
}
556572

557573
void remove(String id) {
574+
_textHashes.remove(id);
558575
_docSizes.remove(id);
559576
final List<String> removeKeys = [];
560577
_inverseIds.forEach((String key, Set<String> set) {

0 commit comments

Comments
 (0)