Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add unsafe append to tablecell #6696

Merged
Show file tree
Hide file tree
Changes from 2 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
37 changes: 37 additions & 0 deletions ydb/core/scheme/scheme_tablecell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,43 @@ 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();

if (Y_UNLIKELY(bufEnd - buf < 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();
Enjection marked this conversation as resolved.
Show resolved Hide resolved

serializedCellVec.ReserveAndResize(newSize);

char* mutableBuf = serializedCellVec.Detach();
char* oldBufEnd = mutableBuf + (bufEnd - buf);
Enjection marked this conversation as resolved.
Show resolved Hide resolved

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));
}
}
Loading