aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/core/scheme/scheme_tablecell.cpp
blob: 4c0650930f5f35349b628af776686bac76bdba8e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include <ydb/core/scheme/scheme_tablecell.h>
#include <ydb/core/scheme/scheme_type_registry.h>

#include <ydb/library/actors/core/actorid.h>
#include <util/string/escape.h>

namespace NKikimr {

void TOwnedCellVec::TData::operator delete(void* mem) noexcept {
    ::free(mem);
}

TOwnedCellVec::TInit TOwnedCellVec::Allocate(TOwnedCellVec::TCellVec cells) {
    size_t cellCount = cells.size();

    if (cellCount == 0) {
        // Leave the data field empty
        return TInit{
            TCellVec(),
            nullptr,
            0,
        };
    }

    size_t size = sizeof(TData) + sizeof(TCell) * cellCount;
    for (auto& x : cells) {
        if (!x.IsNull() && !x.IsInline()) {
            const size_t xsz = x.Size();
            size += AlignUp(xsz);
        }
    }

    void* mem = ::malloc(size);
    if (Y_UNLIKELY(!mem)) {
        throw std::bad_alloc();
    }

    TCell* ptrCell = (TCell*)((TData*)mem + 1);
    char* ptrData = (char*)(ptrCell + cellCount);

    TConstArrayRef<TCell> cellvec(ptrCell, ptrCell + cellCount);

    for (auto& x : cells) {
        if (x.IsNull()) {
            new (ptrCell) TCell();
        } else if (x.IsInline()) {
            new (ptrCell) TCell(x);
        } else {
            const size_t cellSize = x.Size();
            if (Y_LIKELY(cellSize > 0)) {
                ::memcpy(ptrData, x.Data(), cellSize);
            }
            new (ptrCell) TCell(ptrData, cellSize);
            ptrData += AlignUp(cellSize);
        }

        ++ptrCell;
    }

    return TInit {
        cellvec,
        new (mem) TData(),
        size,
    };
}

namespace {

    struct TCellHeader {
        TCellHeader() = default;

        TCellHeader(ui32 rawValue) : RawValue(rawValue) {}

        TCellHeader(ui32 cellSize, bool isNull)
            : RawValue(cellSize | (static_cast<ui32>(isNull) << 31))
        {}

        ui32 CellSize() const { return RawValue & ~(1ULL << 31); }

        bool IsNull() const { return RawValue & (1ULL << 31); };

        ui32 RawValue = 0;
    };

    static_assert(sizeof(TCellHeader) == sizeof(ui32));

    Y_FORCE_INLINE void SerializeCellVec(TConstArrayRef<TCell> cells, TString &resultBuffer, TVector<TCell> *resultCells) {
        resultBuffer.clear();
        if (resultCells)
            resultCells->clear();

        if (cells.empty()) {
            return;
        }

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

        resultBuffer.ReserveAndResize(size);
        char* resultBufferData = resultBuffer.Detach();

        ui16 cellsSize = cells.size();
        WriteUnaligned<ui16>(resultBufferData, cellsSize);
        resultBufferData += sizeof(cellsSize);

        if (resultCells) {
            resultCells->resize_uninitialized(cellsSize);
        }

        for (size_t i = 0; i < cellsSize; ++i) {
            TCellHeader header(cells[i].Size(), cells[i].IsNull());
            WriteUnaligned<ui32>(resultBufferData, header.RawValue);
            resultBufferData += sizeof(header);

            const auto & cell = cells[i];
            if (cell.Size() > 0) {
                cell.CopyDataInto(resultBufferData);
            }

            if (resultCells) {
                if (cell.IsNull()) {
                    new (resultCells->data() + i) TCell();
                } else {
                    new (resultCells->data() + i) TCell(resultBufferData, cell.Size());
                }
            }

            resultBufferData += cell.Size();
        }
    }

    Y_FORCE_INLINE bool TryDeserializeCellVec(const TString & data, TString & resultBuffer, TVector<TCell> & resultCells) {
        resultBuffer.clear();
        resultCells.clear();

        if (data.empty())
            return true;

        const char* buf = data.data();
        const char* bufEnd = data.data() + data.size();
        if (Y_UNLIKELY(bufEnd - buf < static_cast<ptrdiff_t>(sizeof(ui16))))
            return false;

        ui16 cellsSize = ReadUnaligned<ui16>(buf);
        buf += sizeof(cellsSize);

        resultCells.resize_uninitialized(cellsSize);
        TCell* resultCellsData = resultCells.data();

        for (ui32 i = 0; i < cellsSize; ++i) {
            if (Y_UNLIKELY(bufEnd - buf < static_cast<ptrdiff_t>(sizeof(TCellHeader)))) {
                resultCells.clear();
                return false;
            }

            TCellHeader cellHeader = ReadUnaligned<TCellHeader>(buf);
            buf += sizeof(cellHeader);

            if (Y_UNLIKELY(bufEnd - buf < static_cast<ptrdiff_t>(cellHeader.CellSize()))) {
                resultCells.clear();
                return false;
            }

            if (cellHeader.IsNull()) {
                new (resultCellsData + i) TCell();
            } else {
                new (resultCellsData + i) TCell(buf, cellHeader.CellSize());
            }

            buf += cellHeader.CellSize();
        }

        resultBuffer = data;
        return true;
    }

}

TSerializedCellVec::TSerializedCellVec(TConstArrayRef<TCell> cells)
{
    SerializeCellVec(cells, Buf, &Cells);
}

void TSerializedCellVec::Serialize(TString& res, TConstArrayRef<TCell> cells) {
    SerializeCellVec(cells, res, nullptr /*resultCells*/);
}

TString TSerializedCellVec::Serialize(TConstArrayRef<TCell> cells) {
    TString result;
    SerializeCellVec(cells, result, nullptr /*resultCells*/);

    return result;
}

bool TSerializedCellVec::DoTryParse(const TString& data) {
    return TryDeserializeCellVec(data, Buf, Cells);
}

void TCellsStorage::Reset(TArrayRef<const TCell> cells)
{
    size_t cellsSize = cells.size();
    size_t cellsDataSize = sizeof(TCell) * cellsSize;

    for (size_t i = 0; i < cellsSize; ++i) {
        const auto & cell = cells[i];
        if (!cell.IsNull() && !cell.IsInline() && cell.Size() != 0) {
            cellsDataSize += AlignUp(static_cast<size_t>(cell.Size()));
        }
    }

    CellsData.resize(cellsDataSize);

    char *cellsData = CellsData.data();
    Cells = TArrayRef<TCell>{reinterpret_cast<TCell *>(cellsData), cellsSize};
    cellsData += sizeof(TCell) * cellsSize;

    for (size_t i = 0; i < cellsSize; ++i) {
        const auto & cell = cells[i];

        if (!cell.IsNull() && !cell.IsInline() && cell.Size() != 0) {
            memcpy(cellsData, cell.Data(), cell.Size());
            Cells[i] = TCell(cellsData, cell.Size());
            cellsData += AlignUp(static_cast<size_t>(cell.Size()));
        } else {
            Cells[i] = cell;
        }
    }
}

TOwnedCellVecBatch::TOwnedCellVecBatch()
    : Pool(std::make_unique<TMemoryPool>(InitialPoolSize)) {
}

size_t TOwnedCellVecBatch::Append(TConstArrayRef<TCell> cells) {
    size_t cellsSize = cells.size();
    if (cellsSize == 0) {
        CellVectors.emplace_back();
        return 0;
    }

    size_t size = sizeof(TCell) * cellsSize;
    for (auto& cell : cells) {
        if (!cell.IsNull() && !cell.IsInline()) {
            const size_t cellSize = cell.Size();
            size += AlignUp(cellSize);
        }
    }

    char * allocatedBuffer = reinterpret_cast<char *>(Pool->Allocate(size));

    TCell* ptrCell = reinterpret_cast<TCell*>(allocatedBuffer);
    char* ptrData = reinterpret_cast<char*>(ptrCell + cellsSize);

    TConstArrayRef<TCell> cellvec(ptrCell, ptrCell + cellsSize);

    for (auto& cell : cells) {
        if (cell.IsNull()) {
            new (ptrCell) TCell();
        } else if (cell.IsInline()) {
            new (ptrCell) TCell(cell);
        } else {
            const size_t cellSize = cell.Size();
            if (Y_LIKELY(cellSize > 0)) {
                ::memcpy(ptrData, cell.Data(), cellSize);
            }
            new (ptrCell) TCell(ptrData, cellSize);
            ptrData += AlignUp(cellSize);
        }

        ++ptrCell;
    }

    CellVectors.push_back(cellvec);
    return size;
}


TString DbgPrintCell(const TCell& r, NScheme::TTypeInfo typeInfo, const NScheme::TTypeRegistry &reg) {
    auto typeId = typeInfo.GetTypeId();
    TString res;

    if (typeId == NScheme::NTypeIds::Pg) {
        res = NPg::PgTypeNameFromTypeDesc(typeInfo.GetTypeDesc());
    } else {
        NScheme::ITypeSP t = reg.GetType(typeId);

        if (!t.IsKnownType())
            return Sprintf("Unknow typeId 0x%x", (ui32)typeId);

        res = t->GetName();
    }

    res += " : ";
    DbgPrintValue(res, r, typeInfo);

    return res;
}

void DbgPrintValue(TString &res, const TCell &r, NScheme::TTypeInfo typeInfo) {
    if (r.IsNull()) {
        res += "NULL";
    } else {
        switch (typeInfo.GetTypeId()) {
        case NScheme::NTypeIds::Bool:
            res += r.AsValue<bool>() ? "true" : "false";
            break;
        case NScheme::NTypeIds::Byte:
            res += ToString(r.AsValue<ui8>());
            break;
        case NScheme::NTypeIds::Int32:
            res += ToString(r.AsValue<i32>());
            break;
        case NScheme::NTypeIds::Uint32:
            res += ToString(r.AsValue<ui32>());
            break;
        case NScheme::NTypeIds::Int64:
            res += ToString(r.AsValue<i64>());
            break;
        case NScheme::NTypeIds::Uint64:
            res += ToString(r.AsValue<ui64>());
            break;
        case NScheme::NTypeIds::Float:
            res += ToString(r.AsValue<float>());
            break;
        case NScheme::NTypeIds::Double:
            res += ToString(r.AsValue<double>());
            break;
        case NScheme::NTypeIds::ActorId:
            res += ToString(r.AsValue<NActors::TActorId>());
            break;
        case NScheme::NTypeIds::Pg:
            // TODO: support pg types
            break;
        default:
            res += EscapeC(r.Data(), r.Size());
        }
    }
}

TString DbgPrintTuple(const TDbTupleRef& row, const NScheme::TTypeRegistry& typeRegistry) {
    TString res = "(";
    for (ui32 i = 0; i < row.ColumnCount; ++i) {
        res += DbgPrintCell(row.Columns[i], row.Types[i], typeRegistry);
        if (i < row.ColumnCount-1)
            res += ", ";
    }
    res += ")";
    return res;
}

} // namespace NKikimr