package v071 import ( "encoding/json" "errors" "fmt" "sort" "knowledge-governance/internal/libraryrelease" "knowledge-governance/internal/librarysnapshot" ) // enableTargetMapping 为目标快照构建一次映射器。源身份集合来自归档本身, // 因此引用字段的"未登记即悬挂"判断与真实数据一致,不靠猜测。 func (t *ProductionLibraryTarget) enableTargetMapping(archive *librarysnapshot.Archive, manifest librarysnapshot.Manifest) error { if t.scope.TenantID == 0 || t.scope.KnowledgeBaseID == "" { return errors.New("目标范围身份不完整,拒绝生成目标 ID 映射") } mapper, err := targetIDMapperFromArchive(archive, manifest, t.scope.TenantID, t.scope.KnowledgeBaseID) if err != nil { return fmt.Errorf("构建目标 ID 映射失败: %w", err) } t.targetIDs = mapper t.mappingActive = true return nil } func (t *ProductionLibraryTarget) disableTargetMapping() { t.targetIDs = nil t.mappingActive = false } // targetMappingApplies 只在"把目标快照写进目标"时启用映射; // 备份/恢复快照里的 ID 是目标自己的既有身份,二次映射会把恢复写坏。 func (t *ProductionLibraryTarget) targetMappingApplies(snapshotID string) bool { // 只有持久计划显式声明 scoped 契约时才映射。空 mode 是历史计划: // 它登记的就是原身份,重映射会把恢复/重试写到别的 ID 上。 if t.targetIDMode != libraryrelease.TargetIDModeScoped || t.preserveChunkSequence || snapshotID == "" { return false } if t.activePlan == nil { return false } return snapshotID == t.activePlan.TargetSnapshotID } // archivedRowIdentitiesFor 返回写入后的目标身份集合(映射开启时)或归档原身份。 func (t *ProductionLibraryTarget) archivedRowIdentitiesFor(archive *librarysnapshot.Archive, part librarysnapshot.Part) ([]string, error) { if !t.mappingActive { return archivedRowIdentities(archive, part) } reader, err := archive.OpenPart(part) if err != nil { return nil, err } defer reader.Close() decoder := json.NewDecoder(reader) token, err := decoder.Token() if err != nil || token != json.Delim('[') { return nil, errors.New("归档部件不是行数组") } transform := t.rowTransform(part.Name) ids := make([]string, 0, 64) for decoder.More() { var row map[string]json.RawMessage if err := decoder.Decode(&row); err != nil { return nil, err } if err := transform(row); err != nil { return nil, err } identity, err := parseRowIdentity(part.Name, row) if err != nil { return nil, err } ids = append(ids, identity.key()) } return ids, nil } // archivedTableState 是 archivedTableState 的目标映射版本:身份摘要按写入后的 // ID 计算,否则"写对了"也会被判成与快照不一致。 func (t *ProductionLibraryTarget) archivedTableState(archive *librarysnapshot.Archive, part librarysnapshot.Part) (int64, string, error) { if !t.mappingActive { return archivedTableState(archive, part) } ids, err := t.archivedRowIdentitiesFor(archive, part) if err != nil { if errors.Is(err, errMissingIdentity) { return 0, "", err } return 0, "", err } sort.Strings(ids) return int64(len(ids)), digestIDs(ids), nil } // remapIndexMap 把目标快照索引文档的物理 ID 与引用换成目标身份。 func (t *ProductionLibraryTarget) remapIndexMap(documents map[string]json.RawMessage) (map[string]json.RawMessage, error) { if !t.mappingActive { return documents, nil } out := make(map[string]json.RawMessage, len(documents)) for id, source := range documents { mappedID, mappedSource, err := t.targetIDs.remapIndexDocument(id, source) if err != nil { return nil, err } if _, exists := out[mappedID]; exists { return nil, errors.New("index target id collision") } out[mappedID] = mappedSource } return out, nil }