180 lines
5.7 KiB
Go
180 lines
5.7 KiB
Go
// 节点迁移/解散时修复上一层祖先端口;外层 port ID 保持稳定,因此更高层链路无需全部重写。
|
|
package platform
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"database/sql"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// ancestorPortChange 保存受影响外层端口 ID 及其原始真实节点、handle 与方向。
|
|
type ancestorPortChange struct{ PortID, NodeID, Handle, Direction string }
|
|
|
|
// ancestorPortRepair 描述需加入版本事务的祖先画布、对应 frame 和最小端口修改集。
|
|
type ancestorPortRepair struct {
|
|
Ancestor, Frame string
|
|
Changes []ancestorPortChange
|
|
}
|
|
|
|
// A parent keeps stable port IDs. Updating that one boundary also preserves all
|
|
// higher ancestor chains, which continue to reference the same frame/port pair.
|
|
// planAncestorPortRepair 只制定计划:读取父级所属关系与祖先快照,选出直接或经 source frame 暴露的移动节点。
|
|
func planAncestorPortRepair(r *http.Request, tx *sql.Tx, project, workspace, parent, source, account string, nodeIDs []string, before json.RawMessage) (*ancestorPortRepair, error) {
|
|
relation, err := readSubgraphRelation(r, tx, project, workspace, parent)
|
|
if err == sql.ErrNoRows {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if relation.Archived {
|
|
return nil, errGraphOwnership
|
|
}
|
|
_, role, err := lockMaterial(r.Context(), tx, project, workspace, relation.Parent, account)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if role == "viewer" {
|
|
return nil, sql.ErrNoRows
|
|
}
|
|
snapshot, err := loadCanvas(r, tx, project, workspace, relation.Parent)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ancestorData := canvasNodeData(snapshot.Document)[relation.Frame]
|
|
if ancestorData == nil {
|
|
return nil, errGraphOwnership
|
|
}
|
|
selected := map[string]bool{}
|
|
for _, id := range nodeIDs {
|
|
selected[id] = true
|
|
}
|
|
parentNodes := canvasNodeData(before)
|
|
result := &ancestorPortRepair{Ancestor: relation.Parent, Frame: relation.Frame, Changes: []ancestorPortChange{}}
|
|
ports, _ := ancestorData["ports"].([]any)
|
|
for _, value := range ports {
|
|
port := value.(map[string]any)
|
|
node := port["nodeId"].(string)
|
|
handle := port["handle"].(string)
|
|
direction := port["direction"].(string)
|
|
affected := source == parent && selected[node]
|
|
// 来源是孩子时再解开一层 port:<id>,确定祖先端口实际引用的移动成员。
|
|
if !affected && source != parent {
|
|
data := parentNodes[node]
|
|
sub, _ := data["subgraph"].(map[string]any)
|
|
if sub["boardId"] != source || !strings.HasPrefix(handle, "port:") {
|
|
continue
|
|
}
|
|
inner, _ := data["ports"].([]any)
|
|
for _, value := range inner {
|
|
p := value.(map[string]any)
|
|
if "port:"+p["id"].(string) == handle && p["direction"] == direction && selected[p["nodeId"].(string)] {
|
|
node = p["nodeId"].(string)
|
|
handle = p["handle"].(string)
|
|
affected = true
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if affected {
|
|
result.Changes = append(result.Changes, ancestorPortChange{PortID: port["id"].(string), NodeID: node, Handle: handle, Direction: direction})
|
|
}
|
|
}
|
|
if len(result.Changes) == 0 {
|
|
return nil, nil
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// applyAncestorPortRepair 对副本补齐目标 frame 内端口,再在祖先中保留原 PortID 只换指向。
|
|
// target==parent 表示节点已展开到父级,可直接指向真实节点,无需新的中间端口。
|
|
func applyAncestorPortRepair(repair *ancestorPortRepair, parent, target string, documents map[string]json.RawMessage) error {
|
|
if repair == nil {
|
|
return nil
|
|
}
|
|
var parentDoc, ancestorDoc map[string]any
|
|
if err := json.Unmarshal(documents[parent], &parentDoc); err != nil {
|
|
return err
|
|
}
|
|
if err := json.Unmarshal(documents[repair.Ancestor], &ancestorDoc); err != nil {
|
|
return err
|
|
}
|
|
findNode := func(doc map[string]any, id string) map[string]any {
|
|
for _, v := range doc["nodes"].([]any) {
|
|
n := v.(map[string]any)
|
|
if n["id"] == id {
|
|
return n
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
ancestorFrame := findNode(ancestorDoc, repair.Frame)
|
|
if ancestorFrame == nil {
|
|
return errGraphOwnership
|
|
}
|
|
ancestorData := ancestorFrame["data"].(map[string]any)
|
|
ports, _ := ancestorData["ports"].([]any)
|
|
var targetFrame map[string]any
|
|
if target != parent {
|
|
for _, v := range parentDoc["nodes"].([]any) {
|
|
n := v.(map[string]any)
|
|
sub, _ := n["data"].(map[string]any)["subgraph"].(map[string]any)
|
|
if sub["boardId"] == target {
|
|
targetFrame = n
|
|
break
|
|
}
|
|
}
|
|
if targetFrame == nil {
|
|
return errGraphOwnership
|
|
}
|
|
}
|
|
for _, change := range repair.Changes {
|
|
node, handle := change.NodeID, change.Handle
|
|
if targetFrame != nil {
|
|
data := targetFrame["data"].(map[string]any)
|
|
inner, _ := data["ports"].([]any)
|
|
portID := ""
|
|
for _, v := range inner {
|
|
p := v.(map[string]any)
|
|
if p["nodeId"] == node && p["handle"] == handle && p["direction"] == change.Direction {
|
|
portID = p["id"].(string)
|
|
break
|
|
}
|
|
}
|
|
if portID == "" {
|
|
sum := sha256.Sum256([]byte(change.Direction + "/" + node + "/" + handle))
|
|
portID = "p-" + hex.EncodeToString(sum[:16])
|
|
data["ports"] = append(inner, map[string]any{"id": portID, "nodeId": node, "handle": handle, "direction": change.Direction})
|
|
}
|
|
node = targetFrame["id"].(string)
|
|
handle = "port:" + portID
|
|
}
|
|
found := false
|
|
for _, v := range ports {
|
|
p := v.(map[string]any)
|
|
if p["id"] == change.PortID {
|
|
p["nodeId"] = node
|
|
p["handle"] = handle
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
return errGraphOwnership
|
|
}
|
|
}
|
|
// 父和祖先都重新规范化后写回结果映射;数据库版本推进与提交仍由外层命令负责。
|
|
for board, doc := range map[string]map[string]any{parent: parentDoc, repair.Ancestor: ancestorDoc} {
|
|
encoded, _ := json.Marshal(doc)
|
|
normalized, ok := normalizeCanvas(encoded)
|
|
if !ok {
|
|
return errGraphCapacity
|
|
}
|
|
documents[board] = normalized
|
|
}
|
|
return nil
|
|
}
|