ademakov commented on code in PR #988: URL: https://github.com/apache/ignite-3/pull/988#discussion_r948383052
########## modules/platforms/cpp/schema/BinaryTupleParser.cpp: ########## @@ -0,0 +1,203 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "BinaryTupleParser.h" + +#include <cassert> +#include <cstring> +#include <stdexcept> + +namespace { + +template <typename T> +T as(ignite::BytesView bytes) noexcept { + T value; + std::memcpy(&value, bytes.data(), sizeof(T)); + return value; +} + +} // namespace + +namespace ignite { + +BinaryTupleParser::BinaryTupleParser(IntT numElements, BytesView data) + : binaryTuple(data) + , elementCount(numElements) + , elementIndex(0) + , hasNullmap(false) { + + BinaryTupleHeader header; + header.flags = binaryTuple[0]; + + entrySize = header.getVarLenEntrySize(); + hasNullmap = header.getNullMapFlag(); + + size_t nullmapSize = 0; + if (hasNullmap) { + nullmapSize = BinaryTupleSchema::getNullMapSize(elementCount); + } + + size_t tableSize = entrySize * elementCount; + nextEntry = binaryTuple.data() + BinaryTupleHeader::SIZE + nullmapSize; + valueBase = nextEntry + tableSize; + + nextValue = valueBase; + + // Fix tuple size if needed. + uint64_t offset = 0; + static_assert(BYTE_ORDER == LITTLE_ENDIAN); + memcpy(&offset, nextEntry + tableSize - entrySize, entrySize); + const std::byte *tupleEnd = valueBase + offset; + if (binaryTuple.end() > tupleEnd) { + binaryTuple.remove_suffix(binaryTuple.end() - tupleEnd); + } +} + +ElementView BinaryTupleParser::getNext() { + assert(numParsedElements() < numElements()); + + ++elementIndex; + + uint64_t offset = 0; + static_assert(BYTE_ORDER == LITTLE_ENDIAN); + memcpy(&offset, nextEntry, entrySize); + nextEntry += entrySize; + + const std::byte *value = nextValue; + nextValue = valueBase + offset; + + const size_t length = nextValue - value; + if (length == 0 && hasNullmap && BinaryTupleSchema::hasNull(binaryTuple, elementIndex - 1)) { + return {}; + } + + return BytesView(value, length); +} + +TupleView BinaryTupleParser::parse(IntT num) { + assert(elementIndex == 0); + + if (num == NO_NUM) { + num = numElements(); + } + + TupleView tuple; + tuple.reserve(num); + while (elementIndex < num) { + tuple.emplace_back(getNext()); + } + + return tuple; +} + +KeyView BinaryTupleParser::parseKey(IntT num) { Review Comment: This is a micro-optimization for primary key tuples that cannot have nulls by definition. This feature is not strictly needed but some C++ folks might want it in certain scenarios. Anyway it is isolated and does not harm anything. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
