diff --git a/core/src/main/groovy/com/muwire/core/files/SearchIndex.groovy b/core/src/main/groovy/com/muwire/core/files/SearchIndex.groovy new file mode 100644 index 00000000..0da0f3c8 --- /dev/null +++ b/core/src/main/groovy/com/muwire/core/files/SearchIndex.groovy @@ -0,0 +1,37 @@ +package com.muwire.core.files + + +class SearchIndex { + + final Map> keywords = new HashMap<>() + + void add(File f) { + String name = f.getName() + name = name.replaceAll("\\."," ") + String [] split = name.split(" ") + split.each { + Set existing = keywords.get(it) + if (existing == null) { + existing = new HashSet<>() + keywords.put(it, existing) + } + existing.add(f) + } + } + + File[] search(List terms) { + Set rv = null; + + terms.each { + Set forWord = keywords.get it + if (rv == null) { + rv = forWord + } else { + rv.retainAll(forWord) + } + + } + + rv.asList() + } +} diff --git a/core/src/test/groovy/com/muwire/core/files/SearchIndexTest.groovy b/core/src/test/groovy/com/muwire/core/files/SearchIndexTest.groovy new file mode 100644 index 00000000..07b759db --- /dev/null +++ b/core/src/test/groovy/com/muwire/core/files/SearchIndexTest.groovy @@ -0,0 +1,44 @@ +package com.muwire.core.files + +import org.junit.Test + +class SearchIndexTest { + + SearchIndex index + + private void initIndex(List entries) { + index = new SearchIndex() + entries.each { + File f = new File(it) + index.add(f) + } + } + + @Test + void testSingleTerm() { + initIndex(["a b.c", "d e.f"]) + + def found = index.search(["a"]) + assert found.size() == 1 + assert found.contains(new File("a b.c")) + } + + @Test + void testSingleTermOverlap() { + initIndex(["a b.c", "c d.e"]) + + def found = index.search(["c"]) + assert found.size() == 2 + assert found.contains(new File("a b.c")) + assert found.contains(new File("c d.e")) + } + + @Test + void testDrillDown() { + initIndex(["a b.c", "c d.e"]) + + def found = index.search(["c", "e"]) + assert found.size() == 1 + assert found.contains(new File("c d.e")) + } +}