Skip to content

Commit

Permalink
IJMP-1469: make methods for access to node maps synchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzianis Lisiankou authored and Dzianis Lisiankou committed May 16, 2024
1 parent 3d8d27c commit 2fedc7f
Showing 1 changed file with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.SmartList
import eu.ibagroup.formainframe.explorer.Explorer
import java.util.*
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.LinkedList
import java.util.WeakHashMap
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock

private val PROVIDERS = SmartList(FileExplorerTreeStructureProvider())

Expand All @@ -31,46 +33,51 @@ class CommonExplorerTreeStructure<Expl : Explorer<*, *>>(
private val rootNodeProvider: (explorer: Expl, project: Project, treeStructure: ExplorerTreeStructureBase) -> ExplorerTreeNode<*, *>
) : ExplorerTreeStructureBase(explorer, project) {

private val valueToNodeMap = Collections.synchronizedMap(
WeakHashMap<Any, ConcurrentLinkedQueue<ExplorerTreeNode<*, *>>>()
)
private val fileToNodeMap = Collections.synchronizedMap(
WeakHashMap<VirtualFile, ConcurrentLinkedQueue<ExplorerTreeNode<*, *>>>()
)
private val lock = ReentrantLock()

private val valueToNodeMap = WeakHashMap<Any, MutableCollection<ExplorerTreeNode<*, *>>>()
private val fileToNodeMap = WeakHashMap<VirtualFile, MutableCollection<ExplorerTreeNode<*, *>>>()

/**
* Put the node in the value to node map queue and the file to node map queue if the virtual file is present for the node
* Put the node in the value to node map list and the file to node map list if the virtual file is present for the node
* @param node the node to be registered
*/
override fun registerNode(node: ExplorerTreeNode<*, *>) {
valueToNodeMap.getOrPut(node.value) { ConcurrentLinkedQueue() }.add(node)
val file = node.virtualFile ?: return
fileToNodeMap.getOrPut(file) { ConcurrentLinkedQueue() }.add(node)
lock.withLock {
valueToNodeMap.getOrPut(node.value) { LinkedList() }.add(node)
node.virtualFile?.let { fileToNodeMap.getOrPut(it) { LinkedList() }.add(node) }
}
}

/**
* Get the nodes queue from value to node map by the provided value. Empty set will be returned if the queue is not found
* @param value the value to get the queue by
* Get the nodes list from value to node map by the provided value. Empty list will be returned if the list is not found
* @param value the value to get the list by
*/
@Suppress("UNCHECKED_CAST")
override fun <V : Any> findByValue(value: V): Collection<ExplorerTreeNode<*, V>> {
return valueToNodeMap[value] as Collection<ExplorerTreeNode<*, V>>? ?: emptySet()
return lock.withLock {
valueToNodeMap[value] as Collection<ExplorerTreeNode<*, V>>? ?: emptyList()
}
}

/**
* Search for the values in all the value to node map queues by predicate
* Search for the values in all the value to node map lists by predicate
* @param predicate the predicate to filter by
*/
override fun findByPredicate(predicate: (ExplorerTreeNode<*, *>) -> Boolean): Collection<ExplorerTreeNode<*, *>> {
return valueToNodeMap.values.flatten().filter(predicate)
return lock.withLock {
valueToNodeMap.values.flatten().filter(predicate)
}
}

/**
* Get the nodes queue from file to node map by the provided value. Empty set will be returned if the queue is not found
* @param file the virtual file to get the queue by
* Get the nodes list from file to node map by the provided value. Empty list will be returned if the list is not found
* @param file the virtual file to get the list by
*/
override fun findByVirtualFile(file: VirtualFile): Collection<ExplorerTreeNode<*, *>> {
return fileToNodeMap[file] ?: emptySet()
return lock.withLock {
fileToNodeMap[file] ?: emptyList()
}
}

private val root by lazy { rootNodeProvider(explorer, project, this) }
Expand Down

0 comments on commit 2fedc7f

Please sign in to comment.