|
5 | 5 | #include <ydb/core/change_exchange/change_record.h> |
6 | 6 | #include <ydb/core/protos/change_exchange.pb.h> |
7 | 7 | #include <ydb/core/protos/tx_datashard.pb.h> |
| 8 | +#include <ydb/core/protos/datashard_backup.pb.h> |
8 | 9 | #include <ydb/core/scheme/scheme_tablecell.h> |
9 | 10 | #include <ydb/core/tx/replication/service/lightweight_schema.h> |
10 | 11 | #include <ydb/library/yverify_stream/yverify_stream.h> |
@@ -71,58 +72,100 @@ class TChangeRecord: public NChangeExchange::TChangeRecordBase { |
71 | 72 |
|
72 | 73 | switch (ProtoBody.GetCdcDataChange().GetRowOperationCase()) { |
73 | 74 | case NKikimrChangeExchange::TDataChange::kUpsert: { |
74 | | - // Check if NewImage is available, otherwise fall back to Upsert |
75 | | - if (ProtoBody.GetCdcDataChange().HasNewImage()) { |
76 | | - *upsert.MutableTags() = { |
77 | | - ProtoBody.GetCdcDataChange().GetNewImage().GetTags().begin(), |
78 | | - ProtoBody.GetCdcDataChange().GetNewImage().GetTags().end()}; |
79 | | - auto it = Schema->ValueColumns.find("__ydb_incrBackupImpl_deleted"); |
80 | | - Y_ABORT_UNLESS(it != Schema->ValueColumns.end(), "Invariant violation"); |
81 | | - upsert.AddTags(it->second.Tag); |
82 | | - |
83 | | - TString serializedCellVec = ProtoBody.GetCdcDataChange().GetNewImage().GetData(); |
84 | | - Y_ABORT_UNLESS( |
85 | | - TSerializedCellVec::UnsafeAppendCells({TCell::Make<bool>(false)}, serializedCellVec), |
86 | | - "Invalid cell format, can't append cells"); |
| 75 | + TVector<NTable::TTag> tags; |
| 76 | + TVector<TCell> cells; |
| 77 | + NKikimrBackup::TColumnStateMap columnStateMap; |
| 78 | + |
| 79 | + const auto& upsertData = ProtoBody.GetCdcDataChange().GetUpsert(); |
| 80 | + TSerializedCellVec originalCells; |
| 81 | + Y_ABORT_UNLESS(TSerializedCellVec::TryParse(upsertData.GetData(), originalCells)); |
| 82 | + |
| 83 | + tags.assign(upsertData.GetTags().begin(), upsertData.GetTags().end()); |
| 84 | + cells.assign(originalCells.GetCells().begin(), originalCells.GetCells().end()); |
| 85 | + |
| 86 | + THashSet<NTable::TTag> presentTags(upsertData.GetTags().begin(), upsertData.GetTags().end()); |
| 87 | + for (const auto& [name, columnInfo] : Schema->ValueColumns) { |
| 88 | + if (name == "__ydb_incrBackupImpl_deleted" || name == "__ydb_incrBackupImpl_columnStates") { |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + auto* columnState = columnStateMap.AddColumnStates(); |
| 93 | + columnState->SetTag(columnInfo.Tag); |
| 94 | + |
| 95 | + if (presentTags.contains(columnInfo.Tag)) { |
| 96 | + auto it = std::find(upsertData.GetTags().begin(), upsertData.GetTags().end(), columnInfo.Tag); |
| 97 | + if (it != upsertData.GetTags().end()) { |
| 98 | + size_t idx = std::distance(upsertData.GetTags().begin(), it); |
| 99 | + if (idx < originalCells.GetCells().size()) { |
| 100 | + columnState->SetIsNull(originalCells.GetCells()[idx].IsNull()); |
| 101 | + } else { |
| 102 | + columnState->SetIsNull(true); |
| 103 | + } |
| 104 | + } else { |
| 105 | + columnState->SetIsNull(true); |
| 106 | + } |
| 107 | + columnState->SetIsChanged(true); |
| 108 | + } else { |
| 109 | + columnState->SetIsNull(false); |
| 110 | + columnState->SetIsChanged(false); |
| 111 | + } |
| 112 | + } |
87 | 113 |
|
88 | | - upsert.SetData(serializedCellVec); |
89 | | - } else { |
90 | | - *upsert.MutableTags() = { |
91 | | - ProtoBody.GetCdcDataChange().GetUpsert().GetTags().begin(), |
92 | | - ProtoBody.GetCdcDataChange().GetUpsert().GetTags().end()}; |
93 | | - auto it = Schema->ValueColumns.find("__ydb_incrBackupImpl_deleted"); |
94 | | - Y_ABORT_UNLESS(it != Schema->ValueColumns.end(), "Invariant violation"); |
95 | | - upsert.AddTags(it->second.Tag); |
| 114 | + auto deletedIt = Schema->ValueColumns.find("__ydb_incrBackupImpl_deleted"); |
| 115 | + Y_ABORT_UNLESS(deletedIt != Schema->ValueColumns.end(), "Invariant violation"); |
| 116 | + tags.push_back(deletedIt->second.Tag); |
| 117 | + cells.emplace_back(TCell::Make<bool>(false)); |
96 | 118 |
|
97 | | - TString serializedCellVec = ProtoBody.GetCdcDataChange().GetUpsert().GetData(); |
98 | | - Y_ABORT_UNLESS( |
99 | | - TSerializedCellVec::UnsafeAppendCells({TCell::Make<bool>(false)}, serializedCellVec), |
100 | | - "Invalid cell format, can't append cells"); |
| 119 | + auto columnStatesIt = Schema->ValueColumns.find("__ydb_incrBackupImpl_columnStates"); |
| 120 | + Y_ABORT_UNLESS(columnStatesIt != Schema->ValueColumns.end(), "Invariant violation"); |
| 121 | + tags.push_back(columnStatesIt->second.Tag); |
| 122 | + |
| 123 | + TString serializedColumnState; |
| 124 | + Y_ABORT_UNLESS(columnStateMap.SerializeToString(&serializedColumnState)); |
| 125 | + cells.emplace_back(TCell(serializedColumnState.data(), serializedColumnState.size())); |
101 | 126 |
|
102 | | - upsert.SetData(serializedCellVec); |
103 | | - } |
| 127 | + *upsert.MutableTags() = {tags.begin(), tags.end()}; |
| 128 | + upsert.SetData(TSerializedCellVec::Serialize(cells)); |
104 | 129 | break; |
105 | 130 | } |
106 | 131 | case NKikimrChangeExchange::TDataChange::kErase: { |
107 | 132 | size_t size = Schema->ValueColumns.size(); |
108 | 133 | TVector<NTable::TTag> tags; |
109 | 134 | TVector<TCell> cells; |
| 135 | + NKikimrBackup::TColumnStateMap columnStateMap; |
110 | 136 |
|
111 | 137 | tags.reserve(size); |
112 | 138 | cells.reserve(size); |
113 | 139 |
|
114 | | - for (const auto& [name, value] : Schema->ValueColumns) { |
115 | | - tags.push_back(value.Tag); |
116 | | - if (name != "__ydb_incrBackupImpl_deleted") { |
117 | | - cells.emplace_back(); |
118 | | - } else { |
119 | | - cells.emplace_back(TCell::Make<bool>(true)); |
| 140 | + for (const auto& [name, columnInfo] : Schema->ValueColumns) { |
| 141 | + if (name == "__ydb_incrBackupImpl_deleted" || name == "__ydb_incrBackupImpl_columnStates") { |
| 142 | + continue; |
120 | 143 | } |
| 144 | + |
| 145 | + tags.push_back(columnInfo.Tag); |
| 146 | + cells.emplace_back(); |
| 147 | + |
| 148 | + auto* columnState = columnStateMap.AddColumnStates(); |
| 149 | + columnState->SetTag(columnInfo.Tag); |
| 150 | + columnState->SetIsNull(true); |
| 151 | + columnState->SetIsChanged(true); |
121 | 152 | } |
122 | 153 |
|
| 154 | + auto deletedIt = Schema->ValueColumns.find("__ydb_incrBackupImpl_deleted"); |
| 155 | + Y_ABORT_UNLESS(deletedIt != Schema->ValueColumns.end(), "Invariant violation"); |
| 156 | + tags.push_back(deletedIt->second.Tag); |
| 157 | + cells.emplace_back(TCell::Make<bool>(true)); |
| 158 | + |
| 159 | + auto columnStatesIt = Schema->ValueColumns.find("__ydb_incrBackupImpl_columnStates"); |
| 160 | + Y_ABORT_UNLESS(columnStatesIt != Schema->ValueColumns.end(), "Invariant violation"); |
| 161 | + tags.push_back(columnStatesIt->second.Tag); |
| 162 | + |
| 163 | + TString serializedColumnState; |
| 164 | + Y_ABORT_UNLESS(columnStateMap.SerializeToString(&serializedColumnState)); |
| 165 | + cells.emplace_back(TCell(serializedColumnState.data(), serializedColumnState.size())); |
| 166 | + |
123 | 167 | *upsert.MutableTags() = {tags.begin(), tags.end()}; |
124 | 168 | upsert.SetData(TSerializedCellVec::Serialize(cells)); |
125 | | - |
126 | 169 | break; |
127 | 170 | } |
128 | 171 | case NKikimrChangeExchange::TDataChange::kReset: [[fallthrough]]; |
|
0 commit comments