-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(bulk-model-sync): use production implementation of INodeAssociati…
…on in tests
- Loading branch information
Showing
11 changed files
with
141 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...ync-lib/src/commonMain/kotlin/org/modelix/model/sync/bulk/NodeAssociationToModelServer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.modelix.model.sync.bulk | ||
|
||
import org.modelix.model.ModelIndex | ||
import org.modelix.model.api.IBranch | ||
import org.modelix.model.api.IMutableModel | ||
import org.modelix.model.api.IReadableNode | ||
import org.modelix.model.api.IWritableNode | ||
import org.modelix.model.api.NodeReference | ||
import org.modelix.model.api.PNodeAdapter | ||
import org.modelix.model.api.getOriginalOrCurrentReference | ||
import org.modelix.model.api.getOriginalReference | ||
import org.modelix.model.area.PArea | ||
import org.modelix.model.data.NodeData | ||
|
||
class NodeAssociationToModelServer(val branch: IBranch) : INodeAssociation { | ||
|
||
private val modelIndex | ||
get() = ModelIndex.get(branch.transaction, NodeData.Companion.ID_PROPERTY_KEY) | ||
|
||
override fun resolveTarget(sourceNode: IReadableNode): IWritableNode? { | ||
val ref = sourceNode.getOriginalOrCurrentReference() | ||
return modelIndex.find(ref).map { PNodeAdapter(it, branch) }.firstOrNull()?.asWritableNode() | ||
?: PArea(branch).resolveNode(NodeReference(ref))?.asWritableNode() | ||
} | ||
|
||
override fun associate(sourceNode: IReadableNode, targetNode: IWritableNode) { | ||
val expected = sourceNode.getOriginalOrCurrentReference() | ||
if (expected != targetNode.getOriginalOrCurrentReference()) { | ||
targetNode.setPropertyValue(NodeData.ID_PROPERTY_REF, expected) | ||
} | ||
} | ||
} | ||
|
||
class NodeAssociationFromModelServer(val branch: IBranch, val targetModel: IMutableModel) : INodeAssociation { | ||
private val pendingAssociations = HashMap<Long, String>() | ||
|
||
private fun nodeId(sourceNode: IReadableNode): Long { | ||
val pnode = sourceNode.asLegacyNode() as PNodeAdapter | ||
require(pnode.branch == branch) { | ||
"Node is from a different branch. [node = $sourceNode, expected: $branch, actual: ${pnode.branch}]" | ||
} | ||
return pnode.nodeId | ||
} | ||
|
||
override fun resolveTarget(sourceNode: IReadableNode): IWritableNode? { | ||
return (pendingAssociations[nodeId(sourceNode)] ?: sourceNode.getOriginalReference()) | ||
?.let { targetModel.resolveNode(NodeReference(it)) } | ||
} | ||
|
||
override fun associate(sourceNode: IReadableNode, targetNode: IWritableNode) { | ||
val id = nodeId(sourceNode) | ||
val expected = sourceNode.getOriginalOrCurrentReference() | ||
if (expected != targetNode.getOriginalOrCurrentReference()) { | ||
pendingAssociations[id] = targetNode.getNodeReference().serialize() | ||
} | ||
} | ||
|
||
fun writeAssociations() { | ||
for (entry in pendingAssociations) { | ||
branch.writeTransaction.setProperty(entry.key, NodeData.ID_PROPERTY_KEY, entry.value) | ||
} | ||
pendingAssociations.clear() | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...el-sync-lib/src/commonMain/kotlin/org/modelix/model/sync/bulk/TransientNodeAssociation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.modelix.model.sync.bulk | ||
|
||
import org.modelix.model.api.IMutableModel | ||
import org.modelix.model.api.IReadableNode | ||
import org.modelix.model.api.IWritableNode | ||
import org.modelix.model.api.NodeReference | ||
import org.modelix.model.api.getDescendants | ||
import org.modelix.model.api.getOriginalOrCurrentReference | ||
import org.modelix.model.api.getOriginalReference | ||
import org.modelix.model.data.NodeData | ||
import org.modelix.model.data.NodeDataAsNode | ||
|
||
/** | ||
* Maintains the association in-memory. | ||
* This is the default implementation if the target model doesn't allow any optimizations. | ||
*/ | ||
class TransientNodeAssociation(val writeOriginalIds: Boolean, val targetModel: IMutableModel) : INodeAssociation { | ||
private val associations: MutableMap<String, IWritableNode> by lazy { | ||
HashMap<String, IWritableNode>().also { map -> | ||
if (writeOriginalIds) { | ||
targetModel.getRootNode().getDescendants(true).forEach { node -> | ||
node.getOriginalReference()?.let { ref -> | ||
map[ref] = node | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun resolveTarget(sourceNode: IReadableNode): IWritableNode? { | ||
val ref = sourceNode.getOriginalOrCurrentReference() | ||
return associations[ref] | ||
?: targetModel.resolveNode(NodeReference(ref)) | ||
} | ||
|
||
override fun associate( | ||
sourceNode: IReadableNode, | ||
targetNode: IWritableNode, | ||
) { | ||
associations[sourceNode.getOriginalOrCurrentReference()] = targetNode | ||
if (writeOriginalIds) { | ||
if ((sourceNode as NodeDataAsNode).data.id == null) return | ||
val expected = sourceNode.getOriginalOrCurrentReference() | ||
if (targetNode.getOriginalOrCurrentReference() != expected) { | ||
targetNode.setPropertyValue(NodeData.Companion.ID_PROPERTY_REF, expected) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 0 additions & 53 deletions
53
bulk-model-sync-mps/src/main/kotlin/org/modelix/mps/model/sync/bulk/NodeAssociations.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters