Skip to content

Commit d821409

Browse files
committed
LightModelServer allows to ignore concept members
Throwing and catching exceptions is expensive. Roles that are known to be not serializable can be ignored before trying to serialize them.
1 parent c33d047 commit d821409

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

light-model-server/src/main/kotlin/org/modelix/model/server/light/LightModelServer.kt

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ import kotlinx.coroutines.runBlocking
3030
import kotlinx.coroutines.withContext
3131
import kotlinx.coroutines.withTimeout
3232
import org.modelix.model.api.ConceptReference
33+
import org.modelix.model.api.IConceptReference
3334
import org.modelix.model.api.INode
3435
import org.modelix.model.api.INodeReference
3536
import org.modelix.model.api.INodeReferenceSerializer
37+
import org.modelix.model.api.IRole
3638
import org.modelix.model.api.remove
3739
import org.modelix.model.api.serialize
3840
import org.modelix.model.server.api.AddNewChildNodeOpData
@@ -54,13 +56,14 @@ import java.time.Duration
5456
import java.util.*
5557
import kotlin.time.Duration.Companion.seconds
5658

57-
class LightModelServer(val port: Int, val rootNode: INode) {
59+
class LightModelServer(val port: Int, val rootNode: INode, val ignoredRoles: Set<IRole> = emptySet()) {
5860
companion object {
5961
private val LOG = mu.KotlinLogging.logger { }
6062
}
6163

6264
private var server: NettyApplicationEngine? = null
6365
private val sessions: MutableSet<SessionData> = Collections.synchronizedSet(HashSet())
66+
private val ignoredRolesCache: MutableMap<IConceptReference, IgnoredRoles> = HashMap()
6467

6568
fun start() {
6669
LOG.trace { "server starting on port $port ..." }
@@ -280,8 +283,17 @@ class LightModelServer(val port: Int, val rootNode: INode) {
280283
}
281284

282285
private fun INode.toData(): NodeData {
286+
val conceptRef = concept?.getReference()
287+
val ignored = if (conceptRef == null) IgnoredRoles.EMPTY else ignoredRolesCache.getOrPut(conceptRef) {
288+
val ignoredChildRoles = concept?.getAllChildLinks()?.intersect(ignoredRoles)?.map { it.name }?.toSet() ?: emptySet()
289+
val ignoredPropertyRoles = concept?.getAllProperties()?.intersect(ignoredRoles)?.map { it.name }?.toSet() ?: emptySet()
290+
val ignoredReferenceRoles = concept?.getAllReferenceLinks()?.intersect(ignoredRoles)?.map { it.name }?.toSet() ?: emptySet()
291+
IgnoredRoles(ignoredChildRoles, ignoredPropertyRoles, ignoredReferenceRoles).optimizeEmpty()
292+
}
293+
283294
val childrenMap = LinkedHashMap<String?, List<NodeId>>()
284295
for (group in this.allChildren.groupBy { it.roleInParent }) {
296+
if (ignored.children.contains(group.key)) continue
285297
try {
286298
childrenMap[group.key] = group.value.map { it.nodeId() }
287299
} catch (ex: Exception) {
@@ -293,14 +305,22 @@ class LightModelServer(val port: Int, val rootNode: INode) {
293305
concept = this.getConceptReference()?.getUID(),
294306
parent = this.parent?.reference?.serialize(),
295307
role = this.roleInParent,
296-
properties = this.getPropertyRoles().associateWithNotNull { this.getPropertyValue(it) },
297-
references = this.getReferenceRoles().associateWithNotNull { this.getReferenceTargetRef(it)?.serialize() },
308+
properties = this.getPropertyRoles().minus(ignored.properties)
309+
.associateWithNotNull { this.getPropertyValue(it) },
310+
references = this.getReferenceRoles().minus(ignored.references)
311+
.associateWithNotNull { this.getReferenceTargetRef(it)?.serialize() },
298312
children = childrenMap,
299313
)
300314
}
301315
}
302316

303-
317+
private class IgnoredRoles(val children: Set<String>, val properties: Set<String>, val references: Set<String>) {
318+
fun isEmpty() = children.isEmpty() && properties.isEmpty() && references.isEmpty()
319+
fun optimizeEmpty() = if (isEmpty()) EMPTY else this
320+
companion object {
321+
val EMPTY = IgnoredRoles(emptySet(), emptySet(), emptySet())
322+
}
323+
}
304324

305325
private fun INode.nodeId() = reference.serialize()
306326
private fun INodeReference.nodeId() = serialize()

0 commit comments

Comments
 (0)