Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 65 additions & 16 deletions ydb/core/tx/replication/service/json_change_record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,40 @@ NChangeExchange::IChangeRecord::EKind TChangeRecord::GetKind() const {
: EKind::CdcDataChange;
}

static bool ParseKey(TVector<TCell>& cells,
const NJson::TJsonValue::TArray& key, TLightweightSchema::TCPtr schema, TMemoryPool& pool, TString& error)
{
cells.resize(key.size());

Y_ABORT_UNLESS(key.size() == schema->KeyColumns.size());
for (ui32 i = 0; i < key.size(); ++i) {
if (!NFormats::MakeCell(cells[i], key[i], schema->KeyColumns[i], pool, error)) {
return false;
}
}

return true;
}

static bool ParseValue(TVector<NTable::TTag>& tags, TVector<TCell>& cells,
const NJson::TJsonValue::TMapType& value, TLightweightSchema::TCPtr schema, TMemoryPool& pool, TString& error)
{
tags.reserve(value.size());
cells.reserve(value.size());

for (const auto& [column, value] : value) {
auto it = schema->ValueColumns.find(column);
Y_ABORT_UNLESS(it != schema->ValueColumns.end());

tags.push_back(it->second.Tag);
if (!NFormats::MakeCell(cells.emplace_back(), value, it->second.Type, pool, error)) {
return false;
}
}

return true;
}

void TChangeRecord::Serialize(NKikimrTxDataShard::TEvApplyReplicationChanges::TChange& record) const {
record.SetSourceOffset(GetOrder());
// TODO: fill WriteTxId
Expand All @@ -42,13 +76,10 @@ void TChangeRecord::Serialize(NKikimrTxDataShard::TEvApplyReplicationChanges::TC

if (JsonBody.Has("key") && JsonBody["key"].IsArray()) {
const auto& key = JsonBody["key"].GetArray();
Y_ABORT_UNLESS(key.size() == Schema->KeyColumns.size());
TVector<TCell> cells;

TVector<TCell> cells(key.size());
for (ui32 i = 0; i < key.size(); ++i) {
auto res = NFormats::MakeCell(cells[i], key[i], Schema->KeyColumns[i], pool, error);
Y_ABORT_UNLESS(res);
}
auto res = ParseKey(cells, key, Schema, pool, error);
Y_ABORT_UNLESS(res);

record.SetKey(TSerializedCellVec::Serialize(cells));
} else {
Expand All @@ -57,18 +88,14 @@ void TChangeRecord::Serialize(NKikimrTxDataShard::TEvApplyReplicationChanges::TC

if (JsonBody.Has("update") && JsonBody["update"].IsMap()) {
const auto& update = JsonBody["update"].GetMap();
auto& upsert = *record.MutableUpsert();

TVector<TCell> cells(::Reserve(update.size()));
for (const auto& [column, value] : update) {
auto it = Schema->ValueColumns.find(column);
Y_ABORT_UNLESS(it != Schema->ValueColumns.end());
TVector<NTable::TTag> tags;
TVector<TCell> cells;

upsert.AddTags(it->second.Tag);
auto res = NFormats::MakeCell(cells.emplace_back(), value, it->second.Type, pool, error);
Y_ABORT_UNLESS(res);
}
auto res = ParseValue(tags, cells, update, Schema, pool, error);
Y_ABORT_UNLESS(res);

auto& upsert = *record.MutableUpsert();
*upsert.MutableTags() = {tags.begin(), tags.end()};
upsert.SetData(TSerializedCellVec::Serialize(cells));
} else if (JsonBody.Has("erase")) {
record.MutableErase();
Expand All @@ -77,4 +104,26 @@ void TChangeRecord::Serialize(NKikimrTxDataShard::TEvApplyReplicationChanges::TC
}
}

TConstArrayRef<TCell> TChangeRecord::GetKey() const {
if (!Key) {
TMemoryPool pool(256);
TString error;

if (JsonBody.Has("key") && JsonBody["key"].IsArray()) {
const auto& key = JsonBody["key"].GetArray();
TVector<TCell> cells;

auto res = ParseKey(cells, key, Schema, pool, error);
Y_ABORT_UNLESS(res);

Key.ConstructInPlace(cells);
} else {
Y_ABORT("Malformed json record");
}
}

Y_ABORT_UNLESS(Key);
return *Key;
}

}
6 changes: 6 additions & 0 deletions ydb/core/tx/replication/service/json_change_record.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

#include <ydb/core/change_exchange/change_record.h>
#include <ydb/core/protos/tx_datashard.pb.h>
#include <ydb/core/scheme/scheme_tablecell.h>
#include <ydb/core/scheme_types/scheme_type_info.h>
#include <ydb/core/tablet_flat/flat_row_eggs.h>

#include <library/cpp/json/json_reader.h>

#include <util/generic/hash.h>
#include <util/generic/maybe.h>
#include <util/generic/ptr.h>
#include <util/generic/vector.h>

Expand Down Expand Up @@ -39,10 +41,14 @@ class TChangeRecord: public NChangeExchange::TChangeRecordBase {

void Serialize(NKikimrTxDataShard::TEvApplyReplicationChanges::TChange& record) const;

TConstArrayRef<TCell> GetKey() const;

private:
NJson::TJsonValue JsonBody;
TLightweightSchema::TCPtr Schema;

mutable TMaybe<TOwnedCellVec> Key;

}; // TChangeRecord

class TChangeRecordBuilder: public NChangeExchange::TChangeRecordBuilder<TChangeRecord, TChangeRecordBuilder> {
Expand Down
Loading