Skip to content

Commit

Permalink
Merge b1481ea into 0bf2ed6
Browse files Browse the repository at this point in the history
  • Loading branch information
Enjection authored Jul 15, 2024
2 parents 0bf2ed6 + b1481ea commit f724910
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
39 changes: 39 additions & 0 deletions ydb/core/scheme/scheme_tablecell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,45 @@ bool TSerializedCellVec::DoTryParse(const TString& data) {
return TryDeserializeCellVec(data, Buf, Cells);
}

bool TSerializedCellVec::UnsafeAppendCells(TConstArrayRef<TCell> cells, TString& serializedCellVec) {
if (Y_UNLIKELY(cells.size() == 0)) {
return true;
}

if (!serializedCellVec) {
TSerializedCellVec::Serialize(serializedCellVec, cells);
return true;
}

const char* buf = serializedCellVec.data();
const char* bufEnd = serializedCellVec.data() + serializedCellVec.size();
const size_t bufSize = bufEnd - buf;

if (Y_UNLIKELY(bufSize < static_cast<ptrdiff_t>(sizeof(ui16)))) {
return false;
}

ui16 cellCount = ReadUnaligned<ui16>(buf);
cellCount += cells.size();

size_t newSize = serializedCellVec.size();

for (auto& cell : cells) {
newSize += sizeof(TCellHeader) + cell.Size();
}

serializedCellVec.ReserveAndResize(newSize);

char* mutableBuf = serializedCellVec.Detach();
char* oldBufEnd = mutableBuf + bufSize;

WriteUnaligned<ui16>(mutableBuf, cellCount);

SerializeCellVecBody(cells, oldBufEnd, nullptr);

return true;
}

TSerializedCellMatrix::TSerializedCellMatrix(TConstArrayRef<TCell> cells, ui32 rowCount, ui16 colCount)
: RowCount(rowCount), ColCount(colCount)
{
Expand Down
3 changes: 3 additions & 0 deletions ydb/core/scheme/scheme_tablecell.h
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,9 @@ class TSerializedCellVec {
return Cells;
}

// read headers, assuming the buf is correct and append additional cells at the end
static bool UnsafeAppendCells(TConstArrayRef<TCell> cells, TString& serializedCellVec);

static void Serialize(TString& res, TConstArrayRef<TCell> cells);

static TString Serialize(TConstArrayRef<TCell> cells);
Expand Down
33 changes: 33 additions & 0 deletions ydb/core/scheme/scheme_tablecell_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,4 +507,37 @@ Y_UNIT_TEST_SUITE(Scheme) {
}
}
}

Y_UNIT_TEST(UnsafeAppend) {
TString appended = TSerializedCellVec::Serialize({});

UNIT_ASSERT(TSerializedCellVec::UnsafeAppendCells({}, appended));

UNIT_ASSERT_EQUAL(appended.size(), 0);

ui64 intVal = 42;
char bigStrVal[] = "This is a large string value that shouldn't be inlined";

TVector<TCell> cells;
cells.emplace_back(TCell::Make(intVal));
cells.emplace_back(bigStrVal, sizeof(bigStrVal));

UNIT_ASSERT(TSerializedCellVec::UnsafeAppendCells(cells, appended));
TString serialized = TSerializedCellVec::Serialize(cells);

UNIT_ASSERT_VALUES_EQUAL(appended, serialized);

UNIT_ASSERT(TSerializedCellVec::UnsafeAppendCells(cells, appended));

cells.emplace_back(TCell::Make(intVal));
cells.emplace_back(bigStrVal, sizeof(bigStrVal));

serialized = TSerializedCellVec::Serialize(cells);

UNIT_ASSERT_VALUES_EQUAL(appended, serialized);

appended.resize(1);

UNIT_ASSERT(!TSerializedCellVec::UnsafeAppendCells(cells, appended));
}
}

0 comments on commit f724910

Please sign in to comment.