Skip to content

Commit 1f88488

Browse files
authored
GetSubmatrix (#556)
1 parent 9ee90d6 commit 1f88488

6 files changed

+54
-21
lines changed

ydb/core/scheme/scheme_tablecell.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,34 @@ TSerializedCellMatrix::TSerializedCellMatrix(TConstArrayRef<TCell> cells, ui32 r
262262
SerializeCellMatrix(cells, rowCount, colCount, Buf, &Cells);
263263
}
264264

265+
const TCell& TSerializedCellMatrix::GetCell(ui32 row, ui16 column) const {
266+
Y_ABORT_UNLESS(row < RowCount && column < ColCount);
267+
return Cells.at(CalcIndex(row, column));
268+
}
269+
270+
271+
void TSerializedCellMatrix::GetSubmatrix(ui32 firstRow, ui32 lastRow, ui16 firstColumn, ui16 lastColumn, TVector<TCell>& resultCells) const {
272+
Y_ABORT_UNLESS(firstColumn < ColCount &&
273+
lastColumn < ColCount &&
274+
firstRow < RowCount &&
275+
lastRow < RowCount &&
276+
firstColumn <= lastColumn &&
277+
firstRow <= lastRow);
278+
279+
ui32 rowCount = (lastRow - firstRow + 1);
280+
ui16 colCount = (lastColumn - firstColumn + 1);
281+
size_t cellCount = colCount * rowCount;
282+
resultCells.clear();
283+
resultCells.resize_uninitialized(cellCount);
284+
285+
for (ui32 row = firstRow; row <= lastRow; ++row) {
286+
for (ui16 col = firstColumn; col <= lastColumn; ++col) {
287+
resultCells[CalcIndex(row - firstRow, col - firstColumn, colCount)] = GetCell(row, col);
288+
}
289+
}
290+
}
291+
292+
265293
void TSerializedCellMatrix::Serialize(TString& res, TConstArrayRef<TCell> cells, ui32 rowCount, ui16 colCount) {
266294
SerializeCellMatrix(cells, rowCount, colCount, res, nullptr /*resultCells*/);
267295
}

ydb/core/scheme/scheme_tablecell.h

+6
Original file line numberDiff line numberDiff line change
@@ -611,10 +611,16 @@ class TSerializedCellMatrix {
611611
}
612612

613613
TConstArrayRef<TCell> GetCells() const { return Cells; }
614+
const TCell& GetCell(ui32 row, ui16 column) const;
614615

615616
ui32 GetRowCount() const { return RowCount; }
616617
ui16 GetColCount() const { return ColCount; }
617618

619+
static size_t CalcIndex(ui32 row, ui16 column, ui16 columnCount) { return row * columnCount + column; }
620+
size_t CalcIndex(ui32 row, ui16 column) const { return CalcIndex(row, column, ColCount); }
621+
622+
void GetSubmatrix(ui32 firstRow, ui32 lastRow, ui16 firstColumn, ui16 lastColumn, TVector<TCell>& resultCells) const;
623+
618624
static void Serialize(TString& res, TConstArrayRef<TCell> cells, ui32 rowCount, ui16 colCount);
619625

620626
static TString Serialize(TConstArrayRef<TCell> cells, ui32 rowCount, ui16 colCount);

ydb/core/scheme/scheme_tablecell_ut.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,13 @@ Y_UNIT_TEST_SUITE(Scheme) {
282282

283283
UNIT_ASSERT_VALUES_EQUAL(matrix.GetBuffer().size(), 2146);
284284

285+
//test submatrix
286+
{
287+
TVector<TCell> submatrix;
288+
matrix.GetSubmatrix(1, 2, 3, 5, submatrix);
289+
UNIT_ASSERT_VALUES_EQUAL(submatrix.size(), 6);
290+
}
291+
285292
TSerializedCellMatrix matrix2(matrix.GetBuffer());
286293
CompareTypedCellMatrix(matrix2, cells, types, hash);
287294

ydb/core/tx/datashard/datashard_write_operation.cpp

+4-8
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,14 @@ TVector<TEngineBay::TColumnWriteMeta> GetColumnWrites(const ::google::protobuf::
152152

153153
void TValidatedWriteTx::SetTxKeys(const ::google::protobuf::RepeatedField<::NProtoBuf::uint32>& columnTags, const NScheme::TTypeRegistry& typeRegistry, const TActorContext& ctx)
154154
{
155+
TVector<TCell> keyCells;
155156
for (ui32 rowIdx = 0; rowIdx < Matrix_.GetRowCount(); ++rowIdx)
156157
{
157-
//TODO zero copy necessary keys from TableInfo_->KeyColumnTypes
158-
KeyCells_.clear();
159-
for (ui16 colIdx = 0; colIdx < TableInfo_->KeyColumnIds.size(); ++colIdx)
160-
KeyCells_.push_back(Matrix_.GetCells()[rowIdx * columnTags.size() + colIdx]);
161-
162-
TTableRange tableRange(KeyCells_);
158+
Matrix_.GetSubmatrix(rowIdx, rowIdx, 0, TableInfo_->KeyColumnIds.size() - 1, keyCells);
163159

164160
LOG_TRACE_S(ctx, NKikimrServices::TX_DATASHARD, "Table " << TableInfo_->Path << ", shard: " << TabletId_ << ", "
165-
<< "write point " << DebugPrintPoint(TableInfo_->KeyColumnTypes, KeyCells_, typeRegistry));
166-
161+
<< "write point " << DebugPrintPoint(TableInfo_->KeyColumnTypes, keyCells, typeRegistry));
162+
TTableRange tableRange(keyCells);
167163
EngineBay.AddWriteRange(TableId_, tableRange, TableInfo_->KeyColumnTypes, GetColumnWrites(columnTags), false);
168164
}
169165
}

ydb/core/tx/datashard/datashard_write_operation.h

-5
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ class TValidatedWriteTx: TNonCopyable {
6363
return Matrix_;
6464
}
6565

66-
const TVector<TCell> KeyCells() const {
67-
return KeyCells_;
68-
}
69-
7066
ui64 LockTxId() const {
7167
return Record().locktxid();
7268
}
@@ -232,7 +228,6 @@ class TValidatedWriteTx: TNonCopyable {
232228
const TUserTable* TableInfo_;
233229
const NEvents::TDataEvents::TEvWrite::TPtr& Ev_;
234230
TSerializedCellMatrix Matrix_;
235-
TVector<TCell> KeyCells_;
236231
TActorId Source_;
237232
TEngineBay EngineBay;
238233
NKikimrTxDataShard::TError::EKind ErrCode;

ydb/core/tx/datashard/write_unit.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,26 @@ class TWriteUnit : public TExecutionUnit {
6060
TVector<TRawTypeValue> key;
6161
TVector<NTable::TUpdateOp> value;
6262

63+
TVector<TCell> keyCells;
64+
6365
const TSerializedCellMatrix& matrix = writeTx->Matrix();
64-
TConstArrayRef<TCell> cells = matrix.GetCells();
65-
const ui32 rowCount = matrix.GetRowCount();
66-
const ui16 colCount = matrix.GetColCount();
6766

68-
for (ui32 rowIdx = 0; rowIdx < rowCount; ++rowIdx)
67+
for (ui32 rowIdx = 0; rowIdx < matrix.GetRowCount(); ++rowIdx)
6968
{
7069
key.clear();
70+
keyCells.clear();
7171
ui64 keyBytes = 0;
7272
for (ui16 keyColIdx = 0; keyColIdx < TableInfo_.KeyColumnIds.size(); ++keyColIdx) {
7373
const auto& cellType = TableInfo_.KeyColumnTypes[keyColIdx];
74-
const TCell& cell = cells[rowIdx * colCount + keyColIdx];
74+
const TCell& cell = matrix.GetCell(rowIdx, keyColIdx);
7575
if (cellType.GetTypeId() == NScheme::NTypeIds::Uint8 && !cell.IsNull() && cell.AsValue<ui8>() > 127) {
7676
tx->SetError(NKikimrDataEvents::TEvWriteResult::STATUS_BAD_REQUEST, "Keys with Uint8 column values >127 are currently prohibited");
7777
return;
7878
}
7979

8080
keyBytes += cell.Size();
8181
key.emplace_back(TRawTypeValue(cell.AsRef(), cellType));
82+
keyCells.emplace_back(cell);
8283
}
8384

8485
if (keyBytes > NLimits::MaxWriteKeySize) {
@@ -87,9 +88,9 @@ class TWriteUnit : public TExecutionUnit {
8788
}
8889

8990
value.clear();
90-
for (ui16 valueColIdx = TableInfo_.KeyColumnIds.size(); valueColIdx < colCount; ++valueColIdx) {
91+
for (ui16 valueColIdx = TableInfo_.KeyColumnIds.size(); valueColIdx < matrix.GetColCount(); ++valueColIdx) {
9192
ui32 columnTag = writeTx->RecordOperation().GetColumnIds(valueColIdx);
92-
const TCell& cell = cells[rowIdx * colCount + valueColIdx];
93+
const TCell& cell = matrix.GetCell(rowIdx, valueColIdx);
9394
if (cell.Size() > NLimits::MaxWriteValueSize) {
9495
tx->SetError(NKikimrDataEvents::TEvWriteResult::STATUS_BAD_REQUEST, TStringBuilder() << "Row cell size of " << cell.Size() << " bytes is larger than the allowed threshold " << NLimits::MaxWriteValueSize);
9596
return;
@@ -102,7 +103,7 @@ class TWriteUnit : public TExecutionUnit {
102103
}
103104

104105
txc.DB.Update(writeTableId, NTable::ERowOp::Upsert, key, value, writeVersion);
105-
self->GetConflictsCache().GetTableCache(writeTableId).RemoveUncommittedWrites(writeTx->KeyCells(), txc.DB);
106+
self->GetConflictsCache().GetTableCache(writeTableId).RemoveUncommittedWrites(keyCells, txc.DB);
106107
}
107108
//TODO: Counters
108109
// self->IncCounter(COUNTER_UPLOAD_ROWS, rowCount);

0 commit comments

Comments
 (0)