From dd68d923375f511c1d8bc2a2d07946c7f02eadd0 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sat, 21 Jul 2018 09:27:29 +0100 Subject: [PATCH] remove functionality --- .../com/muwire/core/search/SearchIndex.groovy | 21 +++++++++++++++++-- .../muwire/core/search/SearchIndexTest.groovy | 17 +++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/search/SearchIndex.groovy b/core/src/main/groovy/com/muwire/core/search/SearchIndex.groovy index dfb97440..98f1f566 100644 --- a/core/src/main/groovy/com/muwire/core/search/SearchIndex.groovy +++ b/core/src/main/groovy/com/muwire/core/search/SearchIndex.groovy @@ -6,8 +6,7 @@ class SearchIndex { final Map> keywords = new HashMap<>() void add(String string) { - String name = string.replaceAll("\\."," ") - String [] split = name.split(" ") + String [] split = split(string) split.each { Set existing = keywords.get(it) if (existing == null) { @@ -18,6 +17,24 @@ class SearchIndex { } } + void remove(String string) { + String [] split = split(string) + split.each { + Set existing = keywords.get it + if (existing != null) { + existing.remove(string) + if (existing.isEmpty()) { + keywords.remove(it) + } + } + } + } + + private static String[] split(String source) { + source = source.replaceAll("[\\.,_-]", " ") + source.split(" ") + } + String[] search(List terms) { Set rv = null; diff --git a/core/src/test/groovy/com/muwire/core/search/SearchIndexTest.groovy b/core/src/test/groovy/com/muwire/core/search/SearchIndexTest.groovy index 6e24ce97..45ad6ffe 100644 --- a/core/src/test/groovy/com/muwire/core/search/SearchIndexTest.groovy +++ b/core/src/test/groovy/com/muwire/core/search/SearchIndexTest.groovy @@ -55,4 +55,21 @@ class SearchIndexTest { assert found.size() == 0 } + + @Test + void testRemove() { + initIndex(["a b.c"]) + index.remove("a b.c") + def found = index.search(["a"]) + assert found.size() == 0 + } + + @Test + void testRemoveOverlap() { + initIndex(["a b.c", "b c.d"]) + index.remove("a b.c") + def found = index.search(["b"]) + assert found.size() == 1 + assert found.contains("b c.d") + } }