Skip to content

Commit

Permalink
JPS-10203: get rid of recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
penemue committed May 2, 2020
1 parent 680a232 commit e2b902b
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions query/src/main/kotlin/jetbrains/exodus/query/Or.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import jetbrains.exodus.entitystore.PersistentStoreTransaction
import jetbrains.exodus.entitystore.iterate.EntityIdSetIterable
import jetbrains.exodus.entitystore.iterate.EntityIterableBase
import jetbrains.exodus.query.metadata.ModelMetaData
import java.util.*

@Suppress("EqualsOrHashCode")
class Or(left: NodeBase, right: NodeBase) : CommutativeOperator(left, right) {
Expand Down Expand Up @@ -77,23 +78,29 @@ class Or(left: NodeBase, right: NodeBase) : CommutativeOperator(left, right) {

private fun isUnionOfLinks(txn: PersistentStoreTransaction, linkNames: MutableMap<String, EntityIdSetIterable>): Boolean {
if (analyzed) return false
analyzed = true
val left = left
if (left is Or) {
if (!left.isUnionOfLinks(txn, linkNames)) return false
} else if (left is LinkEqual) {
linkNames.computeIfAbsent(left.name) { EntityIdSetIterable(txn) }.addTarget(left.toId)
} else {
return false
}
val right = right
if (right is Or) {
if (!right.isUnionOfLinks(txn, linkNames)) return false
} else if (right is LinkEqual) {
linkNames.computeIfAbsent(right.name) { EntityIdSetIterable(txn) }.addTarget(right.toId)
} else {
return false
val stack = ArrayDeque<Or>()
stack.push(this)
while (stack.isNotEmpty()) {
val or = stack.pop()
if (or.analyzed) continue
or.analyzed = true
or.left.let { left ->
if (left is Or) {
stack.push(left)
} else {
if (left !is LinkEqual) return false
linkNames.computeIfAbsent(left.name) { EntityIdSetIterable(txn) }.addTarget(left.toId)
}
}
or.right.let { right ->
if (right is Or) {
stack.push(right)
} else {
if (right !is LinkEqual) return false
linkNames.computeIfAbsent(right.name) { EntityIdSetIterable(txn) }.addTarget(right.toId)
}
}
}
return linkNames.isNotEmpty()
return true
}
}

0 comments on commit e2b902b

Please sign in to comment.