package v071 // DS-02 本地号映射 oracle。 // // 旧检查只数"每表来源/目标各几个、是否重复",因此三种语义错误能一路绿: // 目标互换(1→11,2→10)、来源集合被替换(999→10)、目标根本不存在(1→999)。 // 这里的检查器只接收四样东西: // * 测试声明的来源行(数字 id 来自夹具声明,不由被测映射器产生); // * 独立推导的期望目标行(来源 id 经过独立算出的分配顺序得到目标数字 id); // * 真实目标行(按目标数字 id 从数据库读回); // * 持久账本(WriteSet.LocalIDs)。 // // 它不调用任何产品映射函数,也不读写存储:把 (table,column,source,target) 当 // 精确集合比较,并用 source 关联声明行、用 target 关联真实目标行。因此 // "账本里的配对本身正确、真实行也正确" 和 "账本把配对写错" 是两件独立的事: // 语义副本变异只改账本内存副本,不会碰 digest 校验;反过来,改归档字节让摘要 // 拒绝也不等于映射语义检查红,两者必须分开报告。 import ( "encoding/json" "fmt" "sort" "strconv" "knowledge-governance/internal/libraryrelease" ) // ds02LocalColumn 是一张含本地号的表的独立期望与真实读回。 type ds02LocalColumn struct { Table string Column string // Sources 是夹具声明的来源数字 id(升序,即分配顺序)。 Sources []int64 // Expected 是 source→target 的独立期望(来源序列起点 + 声明顺序)。 Expected map[int64]int64 // ExpectedRows 是 source→独立推导的完整目标行(逐列,含真实目标数字 id)。 ExpectedRows map[int64]map[string]any // Actual 是真实目标行:target 数字 id→整行。 Actual map[int64]map[string]any } func ds02RowKey(row map[string]any) string { raw, err := json.Marshal(row) if err != nil { return fmt.Sprintf("", err) } return string(raw) } // ds02CheckLocalIDMapping 返回差异;空表示账本与独立期望一致,且每个配对都能 // 落到真实目标行上。 func ds02CheckLocalIDMapping(columns []ds02LocalColumn, ledger []libraryrelease.LocalIDMapping) []string { var diffs []string declared := map[string]*ds02LocalColumn{} for index := range columns { column := &columns[index] if _, ok := declared[column.Table]; ok { diffs = append(diffs, "duplicate declared local column for "+column.Table) continue } declared[column.Table] = column } // 解析账本:逐条严格校验,任何解析失败都是差异,不静默跳过。 type pair struct { source int64 target int64 } observed := map[string]map[int64]int64{} seenSource := map[string]map[int64]bool{} seenTarget := map[string]map[int64]bool{} seenColumn := map[string]map[string]bool{} for _, mapping := range ledger { column, ok := declared[mapping.Table] if !ok { diffs = append(diffs, "unexpected ledger table "+mapping.Table) continue } if mapping.Column != column.Column { diffs = append(diffs, fmt.Sprintf("%s ledger column %q, want %q", mapping.Table, mapping.Column, column.Column)) continue } source, err := strconv.ParseInt(mapping.Source, 10, 64) if err != nil || source <= 0 { diffs = append(diffs, fmt.Sprintf("%s ledger source %q is not a positive integer", mapping.Table, mapping.Source)) continue } target, err := strconv.ParseInt(mapping.Target, 10, 64) if err != nil || target <= 0 { diffs = append(diffs, fmt.Sprintf("%s ledger target %q is not a positive integer", mapping.Table, mapping.Target)) continue } if observed[mapping.Table] == nil { observed[mapping.Table] = map[int64]int64{} seenSource[mapping.Table] = map[int64]bool{} seenTarget[mapping.Table] = map[int64]bool{} seenColumn[mapping.Table] = map[string]bool{} } if seenSource[mapping.Table][source] { diffs = append(diffs, fmt.Sprintf("%s duplicate ledger source %d", mapping.Table, source)) } if seenTarget[mapping.Table][target] { diffs = append(diffs, fmt.Sprintf("%s duplicate ledger target %d", mapping.Table, target)) } key := strconv.FormatInt(source, 10) + "->" + strconv.FormatInt(target, 10) if seenColumn[mapping.Table][key] { diffs = append(diffs, fmt.Sprintf("%s duplicate ledger pair %s", mapping.Table, key)) } seenSource[mapping.Table][source] = true seenTarget[mapping.Table][target] = true seenColumn[mapping.Table][key] = true observed[mapping.Table][source] = target } for _, column := range columns { table := column.Table got := observed[table] // 1) 来源集合必须精确等于声明集合。 var missing []int64 expectedTargets := map[int64]bool{} for _, source := range column.Sources { want, ok := column.Expected[source] if !ok { diffs = append(diffs, fmt.Sprintf("%s source %d has no independent expectation", table, source)) continue } expectedTargets[want] = true actual, ok := got[source] if !ok { missing = append(missing, source) continue } if actual != want { diffs = append(diffs, fmt.Sprintf("%s wrong target pair: source %d mapped to %d, want %d", table, source, actual, want)) } // 3) 来源 id 关联声明行、target id 关联真实目标行:必须逐列相等。 wantRow, ok := column.ExpectedRows[source] if !ok { diffs = append(diffs, fmt.Sprintf("%s source %d has no expected target row", table, source)) continue } actualRow, ok := column.Actual[want] if !ok { diffs = append(diffs, fmt.Sprintf("%s target %d has no real target row", table, want)) continue } if ds02RowKey(wantRow) != ds02RowKey(actualRow) { diffs = append(diffs, fmt.Sprintf("%s source %d -> target %d row mismatch: want %s got %s", table, source, want, ds02RowKey(wantRow), ds02RowKey(actualRow))) } } var extraSources []int64 for source := range got { if _, ok := column.Expected[source]; !ok { extraSources = append(extraSources, source) } } if len(missing) > 0 { sort.Slice(missing, func(i, j int) bool { return missing[i] < missing[j] }) diffs = append(diffs, fmt.Sprintf("%s missing ledger source %v", table, missing)) } if len(extraSources) > 0 { sort.Slice(extraSources, func(i, j int) bool { return extraSources[i] < extraSources[j] }) diffs = append(diffs, fmt.Sprintf("%s unexpected ledger source %v", table, extraSources)) } // 2) target 集合必须精确等于独立预测集合,而且确实存在这些目标行。 for _, target := range got { if !expectedTargets[target] { diffs = append(diffs, fmt.Sprintf("%s ledger target %d is not an independently predicted target", table, target)) } } for target := range expectedTargets { if _, ok := column.Actual[target]; !ok { diffs = append(diffs, fmt.Sprintf("%s predicted target %d does not exist in target storage", table, target)) } } if len(got) == 0 && len(column.Sources) > 0 { diffs = append(diffs, fmt.Sprintf("%s has no ledger mapping rows", table)) } } return diffs } // ds02FlipMappingTargets 互换同一张表两条映射的目标,模拟"配对写反"。 func ds02FlipMappingTargets(ledger []libraryrelease.LocalIDMapping, table string) []libraryrelease.LocalIDMapping { out := append([]libraryrelease.LocalIDMapping(nil), ledger...) first := -1 for index := range out { if out[index].Table != table { continue } if first < 0 { first = index continue } out[first].Target, out[index].Target = out[index].Target, out[first].Target return out } return out } // ds02ReplaceMapping 把同一张表第一条映射的某个字段替换成新值。 func ds02ReplaceMapping(ledger []libraryrelease.LocalIDMapping, table, field, value string) []libraryrelease.LocalIDMapping { out := append([]libraryrelease.LocalIDMapping(nil), ledger...) for index := range out { if out[index].Table != table { continue } switch field { case "source": out[index].Source = value case "target": out[index].Target = value case "column": out[index].Column = value } return out } return out } // ds02DropMapping 删掉同一张表的最后一条映射(少项)。 func ds02DropMapping(ledger []libraryrelease.LocalIDMapping, table string) []libraryrelease.LocalIDMapping { out := append([]libraryrelease.LocalIDMapping(nil), ledger...) for index := len(out) - 1; index >= 0; index-- { if out[index].Table != table { continue } return append(out[:index], out[index+1:]...) } return out } // ds02DuplicateMapping 复制同一张表的第一条映射(多项/重复来源与目标)。 func ds02DuplicateMapping(ledger []libraryrelease.LocalIDMapping, table string) []libraryrelease.LocalIDMapping { out := append([]libraryrelease.LocalIDMapping(nil), ledger...) for index := range out { if out[index].Table != table { continue } return append(out, out[index]) } return out }