search-related events, skeleton of file manager

pull/4/head
Zlatin Balevsky 2018-07-21 01:58:35 +01:00
parent 3f3588dbe4
commit f377807101
3 changed files with 80 additions and 0 deletions

View File

@ -0,0 +1,59 @@
package com.muwire.core.files
import com.muwire.core.EventBus
import com.muwire.core.SharedFile
class FileManager {
final EventBus eventBus
final Map<byte[], Set<SharedFile>> rootToFiles = new HashMap<>()
final Map<File, SharedFile> fileToSharedFile = new HashMap<>()
FileManager(EventBus eventBus) {
this.eventBus = eventBus
}
void onFileHashedEvent(FileHashedEvent e) {
addToIndex(e.sharedFile)
}
void onFileLoadedEvent(FileLoadedEvent e) {
addToIndex(e.loadedFile)
}
void onFileDownloadedEvent(FileDownloadedEvent e) {
addToIndex(e.downloadedFile)
}
private void addToIndex(SharedFile sf) {
byte [] root = sf.getInfoHash().getRoot()
Set<SharedFile> existing = rootToFiles.get(root)
if (existing == null) {
existing = new HashSet<>()
rootToFiles.put(root, existing);
}
existing.add(sf)
fileToSharedFile.put(sf.file, sf)
}
Map<File, SharedFile> getSharedFiles() {
// TODO: figure out locking
fileToSharedFile
}
void onSearchEvent(SearchEvent e) {
// hash takes precedence
ResultsEvent re = null
if (e.searchHash != null) {
Set<SharedFile> found = rootToFiles.get e.searchHash
if (found != null && !found.isEmpty())
re = new ResultsEvent(results: found.asList(), uuid: e.uuid)
} else {
// TODO: keyword search
}
if (re != null)
eventBus.publish(re)
}
}

View File

@ -0,0 +1,11 @@
package com.muwire.core.files
import com.muwire.core.Event
import com.muwire.core.SharedFile
class ResultsEvent extends Event {
SharedFile[] results
UUID uuid
}

View File

@ -0,0 +1,10 @@
package com.muwire.core.files
import com.muwire.core.Event
class SearchEvent extends Event {
String[] searchTerms
byte [] searchHash
UUID uuid
}