search and unsharing of files

This commit is contained in:
Zlatin Balevsky
2018-07-21 10:30:24 +01:00
parent dd68d92337
commit 11167a291b
3 changed files with 228 additions and 3 deletions

View File

@@ -4,6 +4,7 @@ import com.muwire.core.EventBus
import com.muwire.core.SharedFile
import com.muwire.core.search.ResultsEvent
import com.muwire.core.search.SearchEvent
import com.muwire.core.search.SearchIndex
class FileManager {
@@ -11,13 +12,16 @@ class FileManager {
final EventBus eventBus
final Map<byte[], Set<SharedFile>> rootToFiles = new HashMap<>()
final Map<File, SharedFile> fileToSharedFile = new HashMap<>()
final Map<String, Set<File>> nameToFiles = new HashMap<>()
final SearchIndex index = new SearchIndex()
FileManager(EventBus eventBus) {
this.eventBus = eventBus
}
void onFileHashedEvent(FileHashedEvent e) {
addToIndex(e.sharedFile)
if (e.sharedFile != null)
addToIndex(e.sharedFile)
}
void onFileLoadedEvent(FileLoadedEvent e) {
@@ -37,6 +41,41 @@ class FileManager {
}
existing.add(sf)
fileToSharedFile.put(sf.file, sf)
String name = sf.getFile().getName()
Set<File> existingFiles = nameToFiles.get(name)
if (existingFiles == null) {
existingFiles = new HashSet<>()
nameToFiles.put(name, existingFiles)
}
existingFiles.add(sf.getFile())
index.add(name)
}
void onFileUnsharedEvent(FileUnsharedEvent e) {
SharedFile sf = e.unsharedFile
byte [] root = sf.getInfoHash().getRoot()
Set<SharedFile> existing = rootToFiles.get(root)
if (existing != null) {
existing.remove(sf)
if (existing.isEmpty()) {
rootToFiles.remove(root)
}
}
fileToSharedFile.remove(sf.file)
String name = sf.getFile().getName()
Set<File> existingFiles = nameToFiles.get(name)
if (existingFiles != null) {
existingFiles.remove(sf.file)
if (existingFiles.isEmpty()) {
nameToFiles.remove(name)
}
}
index.remove(name)
}
Map<File, SharedFile> getSharedFiles() {
@@ -52,7 +91,14 @@ class FileManager {
if (found != null && !found.isEmpty())
re = new ResultsEvent(results: found.asList(), uuid: e.uuid)
} else {
// TODO: keyword search
def names = index.search e.searchTerms
Set<File> files = new HashSet<>()
names.each { files.addAll nameToFiles.getOrDefault(it, []) }
Set<SharedFile> sharedFiles = new HashSet<>()
files.each { sharedFiles.add fileToSharedFile[it] }
if (!sharedFiles.isEmpty())
re = new ResultsEvent(results: sharedFiles.asList(), uuid: e.uuid)
}
if (re != null)

View File

@@ -4,7 +4,7 @@ import com.muwire.core.Event
class SearchEvent extends Event {
String[] searchTerms
List<String> searchTerms
byte [] searchHash
UUID uuid
}

View File

@@ -0,0 +1,179 @@
package com.muwire.core.files
import org.junit.Before
import org.junit.Test
import com.muwire.core.EventBus
import com.muwire.core.InfoHash
import com.muwire.core.SharedFile
import com.muwire.core.search.ResultsEvent
import com.muwire.core.search.SearchEvent
class FileManagerTest {
EventBus eventBus
FileManager manager
ResultsEvent results
def listener = new Object() {
void onResultsEvent(ResultsEvent e) {
results = e
}
}
@Before
void before() {
eventBus = new EventBus()
eventBus.register(ResultsEvent.class, listener)
manager = new FileManager(eventBus)
results = null
}
@Test
void testHash1Result() {
File f = new File("a b.c")
InfoHash ih = InfoHash.fromHashList(new byte[32])
SharedFile sf = new SharedFile(f,ih)
FileHashedEvent fhe = new FileHashedEvent(sharedFile: sf)
manager.onFileHashedEvent(fhe)
UUID uuid = UUID.randomUUID()
SearchEvent se = new SearchEvent(searchHash: ih.getRoot(), uuid: uuid)
manager.onSearchEvent(se)
assert results != null
assert results.results.size() == 1
assert results.results.contains(sf)
assert results.uuid == uuid
}
@Test
void testHash2Results() {
InfoHash ih = InfoHash.fromHashList(new byte[32])
SharedFile sf1 = new SharedFile(new File("a b.c"), ih)
SharedFile sf2 = new SharedFile(new File("d e.f"), ih)
manager.onFileLoadedEvent new FileLoadedEvent(loadedFile : sf1)
manager.onFileLoadedEvent new FileLoadedEvent(loadedFile : sf2)
UUID uuid = UUID.randomUUID()
SearchEvent se = new SearchEvent(searchHash: ih.getRoot(), uuid: uuid)
manager.onSearchEvent(se)
assert results != null
assert results.results.size() == 2
assert results.results.contains(sf1)
assert results.results.contains(sf2)
assert results.uuid == uuid
}
@Test
void testHash0Results() {
File f = new File("a b.c")
InfoHash ih = InfoHash.fromHashList(new byte[32])
SharedFile sf = new SharedFile(f,ih)
FileHashedEvent fhe = new FileHashedEvent(sharedFile: sf)
manager.onFileHashedEvent(fhe)
manager.onSearchEvent new SearchEvent(searchHash: new byte[32], uuid: UUID.randomUUID())
assert results == null
}
@Test
void testKeyword1Result() {
File f = new File("a b.c")
InfoHash ih = InfoHash.fromHashList(new byte[32])
SharedFile sf = new SharedFile(f,ih)
FileHashedEvent fhe = new FileHashedEvent(sharedFile: sf)
manager.onFileHashedEvent(fhe)
UUID uuid = UUID.randomUUID()
manager.onSearchEvent new SearchEvent(searchTerms: ["a"], uuid:uuid)
assert results != null
assert results.results.size() == 1
assert results.results.contains(sf)
assert results.uuid == uuid
}
@Test
void testKeyword2Results() {
File f1 = new File("a b.c")
InfoHash ih1 = InfoHash.fromHashList(new byte[32])
SharedFile sf1 = new SharedFile(f1, ih1)
manager.onFileLoadedEvent new FileLoadedEvent(loadedFile: sf1)
File f2 = new File("c d.e")
InfoHash ih2 = InfoHash.fromHashList(new byte[64])
SharedFile sf2 = new SharedFile(f2, ih2)
manager.onFileLoadedEvent new FileLoadedEvent(loadedFile: sf2)
UUID uuid = UUID.randomUUID()
manager.onSearchEvent new SearchEvent(searchTerms: ["c"], uuid:uuid)
assert results != null
assert results.results.size() == 2
assert results.results.contains(sf1)
assert results.results.contains(sf2)
assert results.uuid == uuid
}
@Test
void testKeyword0Results() {
File f = new File("a b.c")
InfoHash ih = InfoHash.fromHashList(new byte[32])
SharedFile sf = new SharedFile(f,ih)
FileHashedEvent fhe = new FileHashedEvent(sharedFile: sf)
manager.onFileHashedEvent(fhe)
manager.onSearchEvent new SearchEvent(searchTerms: ["d"], uuid: UUID.randomUUID())
assert results == null
}
@Test
void testRemoveFileExistingHash() {
InfoHash ih = InfoHash.fromHashList(new byte[32])
SharedFile sf1 = new SharedFile(new File("a b.c"), ih)
SharedFile sf2 = new SharedFile(new File("d e.f"), ih)
manager.onFileLoadedEvent new FileLoadedEvent(loadedFile : sf1)
manager.onFileLoadedEvent new FileLoadedEvent(loadedFile : sf2)
manager.onFileUnsharedEvent new FileUnsharedEvent(unsharedFile: sf2)
manager.onSearchEvent new SearchEvent(searchHash : ih.getRoot())
assert results != null
assert results.results.size() == 1
assert results.results.contains(sf1)
}
@Test
void testRemoveFile() {
File f1 = new File("a b.c")
InfoHash ih1 = InfoHash.fromHashList(new byte[32])
SharedFile sf1 = new SharedFile(f1, ih1)
manager.onFileLoadedEvent new FileLoadedEvent(loadedFile: sf1)
File f2 = new File("c d.e")
InfoHash ih2 = InfoHash.fromHashList(new byte[64])
SharedFile sf2 = new SharedFile(f2, ih2)
manager.onFileLoadedEvent new FileLoadedEvent(loadedFile: sf2)
manager.onFileUnsharedEvent new FileUnsharedEvent(unsharedFile: sf2)
// 1 match left
manager.onSearchEvent new SearchEvent(searchTerms: ["c"])
assert results != null
assert results.results.size() == 1
assert results.results.contains(sf1)
// no match
results = null
manager.onSearchEvent new SearchEvent(searchTerms: ["d"])
assert results == null
}
}