mirror of https://github.com/zlatinb/muwire
remove functionality
parent
01617f876d
commit
dd68d92337
|
@ -6,8 +6,7 @@ class SearchIndex {
|
|||
final Map<String, Set<String>> keywords = new HashMap<>()
|
||||
|
||||
void add(String string) {
|
||||
String name = string.replaceAll("\\."," ")
|
||||
String [] split = name.split(" ")
|
||||
String [] split = split(string)
|
||||
split.each {
|
||||
Set<String> existing = keywords.get(it)
|
||||
if (existing == null) {
|
||||
|
@ -18,6 +17,24 @@ class SearchIndex {
|
|||
}
|
||||
}
|
||||
|
||||
void remove(String string) {
|
||||
String [] split = split(string)
|
||||
split.each {
|
||||
Set<String> 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<String> terms) {
|
||||
Set<String> rv = null;
|
||||
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue