ademakov commented on code in PR #988: URL: https://github.com/apache/ignite-3/pull/988#discussion_r945549162
########## modules/platforms/cpp/schema/BinaryTupleBuilder.h: ########## @@ -0,0 +1,397 @@ +/* + * 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. + */ + +#pragma once + +#include "BinaryTupleSchema.h" +#include "common/Types.h" + +#include <cassert> +#include <cstring> +#include <limits> +#include <type_traits> + +namespace ignite { + +/** + * @brief Binary tuple builder. + * + * A tuple builder is used to create one or more binary tuples for a given schema. + * + * Building a tuple takes two passes. On the first pass the builder finds the tuple + * layout. On the second pass it actually fills the tuple data. + * + * More precisely a tuple is built with the following call sequence: + * + * 1. Initialize the builder with the @ref start call. + * 2. Supply all elements with one or more @ref claim calls in the order that + * corresponds to the tuple schema. + * 3. Determine tuple layout with the @ref layout call. + * 4. Supply all elements again with one or more @ref append calls in the same + * order with the same values. + * 5. Finally, the resulting binary tuple is obtained with the @ref build call. + */ +class BinaryTupleBuilder { + IntT elementCount; /**< Total number of elements. */ + + IntT elementIndex; /**< Index of the next element to add. */ + + IntT nullElements; /**< The number of null elements. */ + + SizeT valueAreaSize; /**< Total size of all values. */ + + SizeT entrySize; /**< Size of an offset table entry. */ + + std::byte *nextEntry; /**< Position for the next offset table entry. */ + + std::byte *valueBase; /**< Position of the value area.*/ + + std::byte *nextValue; /**< Position for the next value. */ + + std::vector<std::byte> binaryTuple; /**< Internal buffer for tuple generation. */ + +public: + /** + * @brief constructs a new Tuple Builder object + * + * @param schema Binary tuple schema. + */ + explicit BinaryTupleBuilder(IntT elementCount) noexcept; + + /** + * @brief starts a new tuple + */ + void start() noexcept; + + /** + * @brief assigns a null value for the next element + */ + void claim(std::nullopt_t /*null*/) noexcept { + assert(elementIndex < elementCount); + nullElements++; + elementIndex++; + } + + /** + * @brief assigns a binary value for the next element + * + * @param valueSize required size for the value + */ + void claim(SizeT valueSize) noexcept { + assert(elementIndex < elementCount); + valueAreaSize += valueSize; + elementIndex++; + } + + /** + * @brief assigns a binary value for the next element + * + * @param type Element type. + * @param bytes Binary element value. + */ + void claim(DATA_TYPE type, const BytesView &bytes) noexcept { + claim(sizeOf(type, bytes)); + } + + /** + * @brief assigns a value or null for the next element + * + * @tparam BytesT Byte container for a single internal tuple field. + * @param type Element type. + * @param slice Optional value of an internal tuple field. + */ + template <typename BytesT> + void claim(DATA_TYPE type, const std::optional<BytesT> &slice) noexcept { + if (slice.has_value()) { + claim(type, slice.value()); + } else { + claim(std::nullopt); + } + } + + /** + * @brief assigns values for a number of elements + * + * @tparam BytesT Byte container for a single internal tuple field. + * @param schema Tuple schema. + * @param tuple Tuple in the internal form. + */ + template <typename BytesT> + void claim(const BinaryTupleSchema &schema, const std::vector<std::optional<BytesT>> &tuple) noexcept { + for (IntT i = 0; i < schema.numElements(); i++) { + claim(schema.getElement(i).dataType, tuple[i]); + } + } + + /** + * @brief performs binary tuple layout + */ + void layout(); Review Comment: I think I'll remove it in IGNITE-17401 by using the same technique as in Java version. -- 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]
