rename
Renames the node at path to name, keeping everything else about it and everything beneath it. [] renames the root. The name is not checked here; Topology.from reports an empty one.
A rename is a delete plus an add: TopologyEdit.History.REPLACED, since the old child and everything beneath it drain and the new one starts with no history (see LocalHeddle.apply). Renaming the root is TopologyEdit.History.NEW_TREE. The same name again is TopologyEdit.History.KEPT.
Refused with TopologyEdit.NoSuchNode, or TopologyEdit.DuplicateName naming another sibling that has name.
Samples
val running = draft("pool") {
group("acme", Weight.of(3)) {
lane("interactive")
lane("batch")
}
group("hobby") { lane("batch") }
}
var document = running.toDocument()
// A path is child indices, as on a TopologyProblem: [0, 1] is acme's batch lane.
val reweighted = document.setWeight(listOf(0, 1), "2")
check(reweighted is TopologyEdit.Edited && reweighted.history == TopologyEdit.History.SHARE_RESTARTS)
document = reweighted.document
// history is what one edit does: a rename replaces hobby, and so does renaming it back.
val renamed = document.rename(listOf(1), "hobbyists")
check(renamed is TopologyEdit.Edited && renamed.history == TopologyEdit.History.REPLACED)
val back = renamed.document.rename(listOf(1), "hobby")
check(back is TopologyEdit.Edited && back.history == TopologyEdit.History.REPLACED)
document = back.document
// A template lets the app attach children to hobby while it runs, here two levels deep.
// Adding one costs nobody history; removing it later would drain the attached children.
val attaching = document.setTemplate(listOf(1), TopologyDocument.ChildTemplate("2", template = TopologyDocument.ChildTemplate()))
check(attaching is TopologyEdit.Edited && attaching.history == TopologyEdit.History.KEPT)
document = attaching.document
// An edit that would give two siblings one name is refused, naming the sibling that has it.
val refused = document.addChild(listOf(0), TopologyDocument.Lane("batch"))
check(refused == TopologyEdit.DuplicateName(listOf(0, 1), "batch"))
// The warning before saving compares with the tree the editor loaded: the round trip is gone.
val candidate = Topology.from(document)
check(candidate is TopologyParse.Valid)
val attached = Topology.ChildTemplate(Weight.of(2), template = Topology.ChildTemplate(Weight.ONE))
check(
running.changesTo(candidate.topology) ==
listOf(
TopologyChange.WeightChanged(listOf(0, 1), listOf("pool", "acme", "batch"), Weight.ONE, Weight.of(2)),
TopologyChange.TemplateAdded(listOf(1), listOf("pool", "hobby"), attached),
),
)Content copied to clipboard