Make FileUnsharedEvent a batch event

pull/62/head
Zlatin Balevsky 2021-06-12 10:05:36 +01:00
parent 29e002f8f1
commit 948ab9d3c0
No known key found for this signature in database
GPG Key ID: A72832072D525E41
12 changed files with 91 additions and 74 deletions

View File

@ -52,7 +52,7 @@ class FilesModel {
void onFileUnsharedEvent(FileUnsharedEvent e) { void onFileUnsharedEvent(FileUnsharedEvent e) {
guiThread.invokeLater { guiThread.invokeLater {
sharedFiles.remove(e.unsharedFile) sharedFiles.removeAll(e.unsharedFiles)
} }
} }

View File

@ -82,7 +82,7 @@ class FilesView extends BasicWindow {
contentPanel.setLayoutManager(new GridLayout(4)) contentPanel.setLayoutManager(new GridLayout(4))
Button unshareButton = new Button("Unshare", { Button unshareButton = new Button("Unshare", {
core.eventBus.publish(new FileUnsharedEvent(unsharedFile : sf)) core.eventBus.publish(new FileUnsharedEvent(unsharedFiles : new SharedFile[]{sf}))
MessageDialog.showMessageDialog(textGUI, "File Unshared", "Unshared "+sf.getFile().getName(), MessageDialogButton.OK) MessageDialog.showMessageDialog(textGUI, "File Unshared", "Unshared "+sf.getFile().getName(), MessageDialogButton.OK)
} ) } )
Button addCommentButton = new Button("Add Comment", { Button addCommentButton = new Button("Add Comment", {

View File

@ -284,14 +284,16 @@ class CollectionManager {
} }
synchronized void onFileUnsharedEvent(FileUnsharedEvent e) { synchronized void onFileUnsharedEvent(FileUnsharedEvent e) {
InfoHash infoHash = new InfoHash(e.unsharedFile.getRoot()) for (SharedFile sf : e.unsharedFiles) {
Set<FileCollection> affected = fileRootToCollections.get(infoHash) InfoHash infoHash = sf.getRootInfoHash()
if (affected == null || affected.isEmpty()) { Set<FileCollection> affected = fileRootToCollections.get(infoHash)
return if (affected == null || affected.isEmpty()) {
} continue
affected.each { c -> }
diskIO.execute({delete(c)} as Runnable) affected.each { c ->
eventBus.publish(new CollectionUnsharedEvent(collection : c)) diskIO.execute({ delete(c) } as Runnable)
eventBus.publish(new CollectionUnsharedEvent(collection: c))
}
} }
} }

View File

@ -135,7 +135,7 @@ class DirectoryWatcher {
log.fine("deleted entry $f") log.fine("deleted entry $f")
SharedFile sf = fileManager.fileToSharedFile.get(f) SharedFile sf = fileManager.fileToSharedFile.get(f)
if (sf != null) if (sf != null)
eventBus.publish(new FileUnsharedEvent(unsharedFile : sf, deleted : true)) eventBus.publish(new FileUnsharedEvent(unsharedFiles : new SharedFile[]{sf}, deleted : true))
else if (watchedDirectoryManager.isWatched(f)) else if (watchedDirectoryManager.isWatched(f))
eventBus.publish(new DirectoryUnsharedEvent(directory : f, deleted : true)) eventBus.publish(new DirectoryUnsharedEvent(directory : f, deleted : true))
else else

View File

@ -132,7 +132,11 @@ class FileManager {
} }
void onFileUnsharedEvent(FileUnsharedEvent e) { void onFileUnsharedEvent(FileUnsharedEvent e) {
SharedFile sf = e.unsharedFile for(SharedFile sharedFile : e.unsharedFiles)
unshareFile(sharedFile, e.deleted)
}
private void unshareFile(SharedFile sf, boolean deleted) {
InfoHash infoHash = new InfoHash(sf.getRoot()) InfoHash infoHash = new InfoHash(sf.getRoot())
SharedFile[] existing = rootToFiles.get(infoHash) SharedFile[] existing = rootToFiles.get(infoHash)
if (existing != null) { if (existing != null) {
@ -149,7 +153,7 @@ class FileManager {
fileToSharedFile.remove(sf.file) fileToSharedFile.remove(sf.file)
positiveTree.remove(sf.file) positiveTree.remove(sf.file)
if (!e.deleted && negativeTree.fileToNode.containsKey(sf.file.getParentFile())) { if (!deleted && negativeTree.fileToNode.containsKey(sf.file.getParentFile())) {
negativeTree.add(sf.file,null) negativeTree.add(sf.file,null)
saveNegativeTree() saveNegativeTree()
} }
@ -258,22 +262,23 @@ class FileManager {
negativeTree.remove(e.directory) negativeTree.remove(e.directory)
saveNegativeTree() saveNegativeTree()
if (!e.deleted) { if (!e.deleted) {
List<SharedFile> unsharedFiles = new ArrayList<>()
e.directory.listFiles().each { e.directory.listFiles().each {
if (it.isDirectory()) if (it.isDirectory())
eventBus.publish(new DirectoryUnsharedEvent(directory : it)) eventBus.publish(new DirectoryUnsharedEvent(directory : it))
else { else {
SharedFile sf = fileToSharedFile.get(it) SharedFile sf = fileToSharedFile.get(it)
if (sf != null) if (sf != null)
eventBus.publish(new FileUnsharedEvent(unsharedFile : sf)) unsharedFiles.add(sf)
} }
} }
eventBus.publish(new FileUnsharedEvent(unsharedFiles : unsharedFiles.toArray(new SharedFile[0])))
} else { } else {
def cb = new DirDeletionCallback() def cb = new DirDeletionCallback()
positiveTree.traverse(e.directory, cb) positiveTree.traverse(e.directory, cb)
positiveTree.remove(e.directory) positiveTree.remove(e.directory)
cb.unsharedFiles.each { eventBus.publish(new FileUnsharedEvent()unsharedFiles: cb.unsharedFiles.toArray(new SharedFile[0]),
eventBus.publish(new FileUnsharedEvent(unsharedFile : it, deleted: true)) deleted: true)
}
cb.subDirs.each { cb.subDirs.each {
eventBus.publish(new DirectoryUnsharedEvent(directory : it, deleted : true)) eventBus.publish(new DirectoryUnsharedEvent(directory : it, deleted : true))
} }

View File

@ -4,6 +4,6 @@ import com.muwire.core.Event
import com.muwire.core.SharedFile import com.muwire.core.SharedFile
class FileUnsharedEvent extends Event { class FileUnsharedEvent extends Event {
SharedFile unsharedFile SharedFile[] unsharedFiles
boolean deleted boolean deleted
} }

View File

@ -78,7 +78,7 @@ class HasherService {
} }
void onFileUnsharedEvent(FileUnsharedEvent evt) { void onFileUnsharedEvent(FileUnsharedEvent evt) {
hashed.remove(evt.unsharedFile.file) hashed.removeAll(evt.unsharedFiles)
} }
void onDirectoryUnsharedEvent(DirectoryUnsharedEvent evt) { void onDirectoryUnsharedEvent(DirectoryUnsharedEvent evt) {

View File

@ -80,12 +80,17 @@ class PersisterFolderService extends BasePersisterService {
* @param unsharedEvent * @param unsharedEvent
*/ */
void onFileUnsharedEvent(FileUnsharedEvent unsharedEvent) { void onFileUnsharedEvent(FileUnsharedEvent unsharedEvent) {
def jsonPath = getJsonPath(unsharedEvent.unsharedFile) for(SharedFile sharedFile : unsharedEvent.unsharedFiles)
unshareFile(sharedFile)
}
private void unshareFile(SharedFile sharedFile) {
def jsonPath = getJsonPath(sharedFile)
def jsonFile = jsonPath.toFile() def jsonFile = jsonPath.toFile()
if(jsonFile.isFile()){ if(jsonFile.isFile()){
jsonFile.delete() jsonFile.delete()
} }
def hashListPath = getHashListPath(unsharedEvent.unsharedFile) def hashListPath = getHashListPath(sharedFile)
def hashListFile = hashListPath.toFile() def hashListFile = hashListPath.toFile()
if (hashListFile.isFile()) if (hashListFile.isFile())
hashListFile.delete() hashListFile.delete()

View File

@ -195,9 +195,7 @@ class WatchedDirectoryManager {
Set<File> deletedFiles = new HashSet<>(cb.files) Set<File> deletedFiles = new HashSet<>(cb.files)
deletedFiles.removeAll(filesOnFS) deletedFiles.removeAll(filesOnFS)
deletedFiles.each { eventBus.publish(new FileUnsharedEvent(unsharedFiles: deletedFiles.toArray(new SharedFile[0]), deleted: true))
eventBus.publish(new FileUnsharedEvent(unsharedFile : fileManager.getFileToSharedFile().get(it), deleted : true))
}
Set<File> deletedDirs = new HashSet<>(cb.dirs) Set<File> deletedDirs = new HashSet<>(cb.dirs)
deletedDirs.removeAll(dirsOnFS) deletedDirs.removeAll(dirsOnFS)
deletedDirs.each { deletedDirs.each {

View File

@ -154,7 +154,7 @@ class FileManagerTest {
manager.onFileLoadedEvent new FileLoadedEvent(loadedFile : sf1) manager.onFileLoadedEvent new FileLoadedEvent(loadedFile : sf1)
manager.onFileLoadedEvent new FileLoadedEvent(loadedFile : sf2) manager.onFileLoadedEvent new FileLoadedEvent(loadedFile : sf2)
manager.onFileUnsharedEvent new FileUnsharedEvent(deleted : true, unsharedFile: sf2) manager.onFileUnsharedEvent new FileUnsharedEvent(deleted : true, unsharedFiles: new SharedFile[]{sf2})
manager.onSearchEvent new SearchEvent(searchHash : ih.getRoot()) manager.onSearchEvent new SearchEvent(searchHash : ih.getRoot())
Thread.sleep(20) Thread.sleep(20)
@ -175,7 +175,7 @@ class FileManagerTest {
SharedFile sf2 = new SharedFile(f2, ih2.getRoot(), 0) SharedFile sf2 = new SharedFile(f2, ih2.getRoot(), 0)
manager.onFileLoadedEvent new FileLoadedEvent(loadedFile: sf2) manager.onFileLoadedEvent new FileLoadedEvent(loadedFile: sf2)
manager.onFileUnsharedEvent new FileUnsharedEvent(deleted : true, unsharedFile: sf2) manager.onFileUnsharedEvent new FileUnsharedEvent(deleted : true, unsharedFiles: new SharedFile[]{sf2})
// 1 match left // 1 match left
manager.onSearchEvent new SearchEvent(searchTerms: ["c"]) manager.onSearchEvent new SearchEvent(searchTerms: ["c"])
@ -202,7 +202,7 @@ class FileManagerTest {
sf1.setComment(comment) sf1.setComment(comment)
manager.onFileLoadedEvent(new FileLoadedEvent(loadedFile : sf1)) manager.onFileLoadedEvent(new FileLoadedEvent(loadedFile : sf1))
manager.onFileUnsharedEvent(new FileUnsharedEvent(unsharedFile : sf1, deleted : true)) manager.onFileUnsharedEvent(new FileUnsharedEvent(unsharedFiles : new SharedFile[]{sf1}, deleted : true))
File f2 = new File("MuWire-0.6.0.AppImage") File f2 = new File("MuWire-0.6.0.AppImage")
InfoHash ih2 = InfoHash.fromHashList(new byte[64]) InfoHash ih2 = InfoHash.fromHashList(new byte[64])

View File

@ -455,6 +455,7 @@ class MainFrameController {
def sfs = view.selectedSharedFiles() def sfs = view.selectedSharedFiles()
if (sfs == null) if (sfs == null)
return return
List<SharedFile> toUnshare = new ArrayList<>()
sfs.each { SharedFile sf -> sfs.each { SharedFile sf ->
if (view.settings.collectionWarning) { if (view.settings.collectionWarning) {
@ -475,9 +476,9 @@ class MainFrameController {
return return
} }
} }
toUnshare.add(sf)
core.eventBus.publish(new FileUnsharedEvent(unsharedFile : sf))
} }
core.eventBus.publish(new FileUnsharedEvent(unsharedFiles : toUnshare.toArray(new SharedFile[0])))
} }
@ControllerAction @ControllerAction

View File

@ -538,60 +538,66 @@ class MainFrameModel {
void onFileUnsharedEvent(FileUnsharedEvent e) { void onFileUnsharedEvent(FileUnsharedEvent e) {
runInsideUIAsync { runInsideUIAsync {
allSharedFiles.remove(e.unsharedFile) synchronized (allSharedFiles) {
allSharedFiles.removeAll(e.unsharedFiles)
}
loadedFiles = allSharedFiles.size() loadedFiles = allSharedFiles.size()
boolean wasVisible = shared.remove(e.unsharedFile) for (SharedFile sharedFile : e.unsharedFiles) {
boolean wasVisible = shared.remove(sharedFile)
DefaultMutableTreeNode dmtn = fileToNode.remove(e.unsharedFile)
if (dmtn != null) { DefaultMutableTreeNode dmtn = fileToNode.remove(sharedFile)
if (dmtn != null) {
if (wasVisible) {
Object[] path = dmtn.getUserObjectPath() if (wasVisible) {
DefaultMutableTreeNode otherNode = treeRoot Object[] path = dmtn.getUserObjectPath()
for (int i = 1; i < path.length; i ++) { DefaultMutableTreeNode otherNode = treeRoot
Object o = path[i] for (int i = 1; i < path.length; i++) {
DefaultMutableTreeNode next = null Object o = path[i]
for (int j = 0; j < otherNode.childCount; j ++) { DefaultMutableTreeNode next = null
if (otherNode.getChildAt(j).getUserObject() == o) { for (int j = 0; j < otherNode.childCount; j++) {
next = otherNode.getChildAt(j) if (otherNode.getChildAt(j).getUserObject() == o) {
break next = otherNode.getChildAt(j)
break
}
} }
if (next == null)
throw new IllegalStateException()
otherNode = next
}
while (true) {
def parent = otherNode.getParent()
otherNode.removeFromParent()
if (parent.getChildCount() == 0) {
otherNode = parent
} else
break
} }
if (next == null)
throw new IllegalStateException()
otherNode = next
} }
while(true) {
def parent = otherNode.getParent() List<File> unshared = new ArrayList<>()
otherNode.removeFromParent() while (true) {
if (parent.getChildCount() == 0) { def parent = dmtn.getParent()
otherNode = parent parent.remove(dmtn)
} else if (parent == allFilesTreeRoot)
break break
} if (parent.getChildCount() == 0) {
} File file = parent.getUserObject().file
if (core.watchedDirectoryManager.isWatched(file))
List<File> unshared = new ArrayList<>() unshared.add(file)
while (true) { dmtn = parent
def parent = dmtn.getParent() continue
parent.remove(dmtn) }
if (parent == allFilesTreeRoot)
break break
if (parent.getChildCount() == 0) {
File file = parent.getUserObject().file
if (core.watchedDirectoryManager.isWatched(file))
unshared.add(file)
dmtn = parent
continue
} }
break if (!unshared.isEmpty()) {
File unsharedRoot = unshared.get(unshared.size() - 1)
core.eventBus.publish(new DirectoryUnsharedEvent(directory: unsharedRoot))
}
} }
if (!unshared.isEmpty()) { }
File unsharedRoot = unshared.get( unshared.size() -1 )
core.eventBus.publish(new DirectoryUnsharedEvent(directory : unsharedRoot)) view.refreshSharedFiles()
}
}
} }
} }