zwhzzz0821 commented on code in PR #345:
URL: https://github.com/apache/tsfile/pull/345#discussion_r1923053778
##########
cpp/src/common/schema.h:
##########
@@ -21,68 +21,314 @@
#define COMMON_SCHEMA_H
#include <map> // use unordered_map instead
+#include <memory>
#include <string>
#include "common/db_common.h"
#include "writer/time_chunk_writer.h"
#include "writer/value_chunk_writer.h"
namespace storage {
-class ChunkWriter;
+ class ChunkWriter;
}
namespace storage {
+ /* schema information for one measurement */
+ struct MeasurementSchema {
+ std::string measurement_name_; // for example: "s1"
+ common::TSDataType data_type_;
+ common::TSEncoding encoding_;
+ common::CompressionType compression_type_;
+ storage::ChunkWriter *chunk_writer_;
+ ValueChunkWriter *value_chunk_writer_;
+ std::map<std::string, std::string> props_;
-/* schema information for one measurement */
-struct MeasurementSchema {
- std::string measurement_name_; // for example: "s1"
- common::TSDataType data_type_;
- common::TSEncoding encoding_;
- common::CompressionType compression_type_;
- storage::ChunkWriter *chunk_writer_;
- ValueChunkWriter *value_chunk_writer_;
-
- MeasurementSchema()
- : measurement_name_(),
- data_type_(common::INVALID_DATATYPE),
- encoding_(common::INVALID_ENCODING),
- compression_type_(common::INVALID_COMPRESSION),
- chunk_writer_(nullptr),
- value_chunk_writer_(nullptr) {}
-
- MeasurementSchema(const std::string &measurement_name,
- common::TSDataType data_type)
- : measurement_name_(measurement_name),
- data_type_(data_type),
- encoding_(get_default_encoding_for_type(data_type)),
- compression_type_(common::LZ4),
- chunk_writer_(nullptr),
- value_chunk_writer_(nullptr) {}
-
- MeasurementSchema(const std::string &measurement_name,
- common::TSDataType data_type, common::TSEncoding
encoding,
- common::CompressionType compression_type)
- : measurement_name_(measurement_name),
- data_type_(data_type),
- encoding_(encoding),
- compression_type_(compression_type),
- chunk_writer_(nullptr),
- value_chunk_writer_(nullptr) {}
-};
-
-typedef std::map<std::string, MeasurementSchema *> MeasurementSchemaMap;
-typedef std::map<std::string, MeasurementSchema *>::iterator
+ MeasurementSchema()
+ : measurement_name_(),
+ data_type_(common::INVALID_DATATYPE),
+ encoding_(common::INVALID_ENCODING),
+ compression_type_(common::INVALID_COMPRESSION),
+ chunk_writer_(nullptr),
+ value_chunk_writer_(nullptr) {
+ }
+
+ MeasurementSchema(const std::string &measurement_name,
+ common::TSDataType data_type)
+ : measurement_name_(measurement_name),
+ data_type_(data_type),
+ encoding_(get_default_encoding_for_type(data_type)),
+ compression_type_(common::UNCOMPRESSED),
+ chunk_writer_(nullptr),
+ value_chunk_writer_(nullptr) {
+ }
+
+ MeasurementSchema(const std::string &measurement_name,
+ common::TSDataType data_type, common::TSEncoding
encoding,
+ common::CompressionType compression_type)
+ : measurement_name_(measurement_name),
+ data_type_(data_type),
+ encoding_(encoding),
+ compression_type_(compression_type),
+ chunk_writer_(nullptr),
+ value_chunk_writer_(nullptr) {
+ }
+
+ int serialize_to(common::ByteStream &out) {
+ int ret = common::E_OK;
+ if (RET_FAIL(
+ common::SerializationUtil::write_str(measurement_name_, out)))
{
+ } else if (RET_FAIL(
+ common::SerializationUtil::write_ui8(data_type_, out))) {
+ } else if (RET_FAIL(
+ common::SerializationUtil::write_ui8(encoding_, out))) {
+ } else if (RET_FAIL(common::SerializationUtil::write_ui8(
+ compression_type_, out))) {
+ }
+ if (ret == common::E_OK) {
+ if
(RET_FAIL(common::SerializationUtil::write_ui32(props_.size(),
+ out))) {
+ for (const auto &prop: props_) {
+ if (RET_FAIL(common::SerializationUtil::write_str(
+ prop.first, out))) {
+ } else if
(RET_FAIL(common::SerializationUtil::write_str(
+ prop.second, out))) {
+ }
+ if (ret != common::E_OK) break;
+ }
+ }
+ }
+ return ret;
+ }
+
+ int deserialize_from(common::ByteStream &in) {
+ int ret = common::E_OK;
+ uint8_t data_type = common::TSDataType::INVALID_DATATYPE,
+ encoding = common::TSEncoding::INVALID_ENCODING,
+ compression_type =
common::CompressionType::INVALID_COMPRESSION;
+ if (RET_FAIL(
+ common::SerializationUtil::read_str(measurement_name_, in))) {
+ } else if (RET_FAIL(
+ common::SerializationUtil::read_ui8(data_type, in))) {
+ } else if (RET_FAIL(
+ common::SerializationUtil::read_ui8(encoding, in))) {
+ } else if (RET_FAIL(common::SerializationUtil::read_ui8(
+ compression_type, in))) {
+ }
+ data_type_ = static_cast<common::TSDataType>(data_type);
+ encoding_ = static_cast<common::TSEncoding>(encoding);
+ compression_type_ =
static_cast<common::CompressionType>(compression_type);
+ uint32_t props_size;
+ if (ret == common::E_OK) {
+ if (RET_FAIL(common::SerializationUtil::read_ui32(props_size,
+ in))) {
+ for (uint32_t i = 0; i < props_.size(); ++i) {
+ std::string key, value;
+ if (RET_FAIL(common::SerializationUtil::read_str(
+ key, in))) {
+ } else if
(RET_FAIL(common::SerializationUtil::read_str(
+ value, in))) {
+ }
+ props_.insert(std::make_pair(key, value));
+ if (ret != common::E_OK) break;
Review Comment:
```suggestion
if (IS_FAIL(ret)) break;
```
##########
cpp/src/common/schema.h:
##########
@@ -21,68 +21,314 @@
#define COMMON_SCHEMA_H
#include <map> // use unordered_map instead
+#include <memory>
#include <string>
#include "common/db_common.h"
#include "writer/time_chunk_writer.h"
#include "writer/value_chunk_writer.h"
namespace storage {
-class ChunkWriter;
+ class ChunkWriter;
}
namespace storage {
+ /* schema information for one measurement */
+ struct MeasurementSchema {
+ std::string measurement_name_; // for example: "s1"
+ common::TSDataType data_type_;
+ common::TSEncoding encoding_;
+ common::CompressionType compression_type_;
+ storage::ChunkWriter *chunk_writer_;
+ ValueChunkWriter *value_chunk_writer_;
+ std::map<std::string, std::string> props_;
-/* schema information for one measurement */
-struct MeasurementSchema {
- std::string measurement_name_; // for example: "s1"
- common::TSDataType data_type_;
- common::TSEncoding encoding_;
- common::CompressionType compression_type_;
- storage::ChunkWriter *chunk_writer_;
- ValueChunkWriter *value_chunk_writer_;
-
- MeasurementSchema()
- : measurement_name_(),
- data_type_(common::INVALID_DATATYPE),
- encoding_(common::INVALID_ENCODING),
- compression_type_(common::INVALID_COMPRESSION),
- chunk_writer_(nullptr),
- value_chunk_writer_(nullptr) {}
-
- MeasurementSchema(const std::string &measurement_name,
- common::TSDataType data_type)
- : measurement_name_(measurement_name),
- data_type_(data_type),
- encoding_(get_default_encoding_for_type(data_type)),
- compression_type_(common::LZ4),
- chunk_writer_(nullptr),
- value_chunk_writer_(nullptr) {}
-
- MeasurementSchema(const std::string &measurement_name,
- common::TSDataType data_type, common::TSEncoding
encoding,
- common::CompressionType compression_type)
- : measurement_name_(measurement_name),
- data_type_(data_type),
- encoding_(encoding),
- compression_type_(compression_type),
- chunk_writer_(nullptr),
- value_chunk_writer_(nullptr) {}
-};
-
-typedef std::map<std::string, MeasurementSchema *> MeasurementSchemaMap;
-typedef std::map<std::string, MeasurementSchema *>::iterator
+ MeasurementSchema()
+ : measurement_name_(),
+ data_type_(common::INVALID_DATATYPE),
+ encoding_(common::INVALID_ENCODING),
+ compression_type_(common::INVALID_COMPRESSION),
+ chunk_writer_(nullptr),
+ value_chunk_writer_(nullptr) {
+ }
+
+ MeasurementSchema(const std::string &measurement_name,
+ common::TSDataType data_type)
+ : measurement_name_(measurement_name),
+ data_type_(data_type),
+ encoding_(get_default_encoding_for_type(data_type)),
+ compression_type_(common::UNCOMPRESSED),
+ chunk_writer_(nullptr),
+ value_chunk_writer_(nullptr) {
+ }
+
+ MeasurementSchema(const std::string &measurement_name,
+ common::TSDataType data_type, common::TSEncoding
encoding,
+ common::CompressionType compression_type)
+ : measurement_name_(measurement_name),
+ data_type_(data_type),
+ encoding_(encoding),
+ compression_type_(compression_type),
+ chunk_writer_(nullptr),
+ value_chunk_writer_(nullptr) {
+ }
+
+ int serialize_to(common::ByteStream &out) {
+ int ret = common::E_OK;
+ if (RET_FAIL(
+ common::SerializationUtil::write_str(measurement_name_, out)))
{
+ } else if (RET_FAIL(
+ common::SerializationUtil::write_ui8(data_type_, out))) {
+ } else if (RET_FAIL(
+ common::SerializationUtil::write_ui8(encoding_, out))) {
+ } else if (RET_FAIL(common::SerializationUtil::write_ui8(
+ compression_type_, out))) {
+ }
+ if (ret == common::E_OK) {
+ if
(RET_FAIL(common::SerializationUtil::write_ui32(props_.size(),
+ out))) {
+ for (const auto &prop: props_) {
+ if (RET_FAIL(common::SerializationUtil::write_str(
+ prop.first, out))) {
+ } else if
(RET_FAIL(common::SerializationUtil::write_str(
+ prop.second, out))) {
+ }
+ if (ret != common::E_OK) break;
+ }
+ }
+ }
+ return ret;
+ }
+
+ int deserialize_from(common::ByteStream &in) {
+ int ret = common::E_OK;
+ uint8_t data_type = common::TSDataType::INVALID_DATATYPE,
+ encoding = common::TSEncoding::INVALID_ENCODING,
+ compression_type =
common::CompressionType::INVALID_COMPRESSION;
+ if (RET_FAIL(
+ common::SerializationUtil::read_str(measurement_name_, in))) {
+ } else if (RET_FAIL(
+ common::SerializationUtil::read_ui8(data_type, in))) {
+ } else if (RET_FAIL(
+ common::SerializationUtil::read_ui8(encoding, in))) {
+ } else if (RET_FAIL(common::SerializationUtil::read_ui8(
+ compression_type, in))) {
+ }
+ data_type_ = static_cast<common::TSDataType>(data_type);
+ encoding_ = static_cast<common::TSEncoding>(encoding);
+ compression_type_ =
static_cast<common::CompressionType>(compression_type);
+ uint32_t props_size;
+ if (ret == common::E_OK) {
+ if (RET_FAIL(common::SerializationUtil::read_ui32(props_size,
+ in))) {
+ for (uint32_t i = 0; i < props_.size(); ++i) {
+ std::string key, value;
+ if (RET_FAIL(common::SerializationUtil::read_str(
+ key, in))) {
+ } else if
(RET_FAIL(common::SerializationUtil::read_str(
+ value, in))) {
+ }
+ props_.insert(std::make_pair(key, value));
+ if (ret != common::E_OK) break;
+ }
+ }
+ }
+ return ret;
+ }
+ };
+
+ typedef std::map<std::string, MeasurementSchema *> MeasurementSchemaMap;
+ typedef std::map<std::string, MeasurementSchema *>::iterator
MeasurementSchemaMapIter;
-typedef std::pair<MeasurementSchemaMapIter, bool>
+ typedef std::pair<MeasurementSchemaMapIter, bool>
MeasurementSchemaMapInsertResult;
-/* schema information for a device */
-struct MeasurementSchemaGroup {
- // measurement_name -> MeasurementSchema
- MeasurementSchemaMap measurement_schema_map_;
- bool is_aligned_ = false;
- TimeChunkWriter *time_chunk_writer_ = nullptr;
-};
+ /* schema information for a device */
+ struct MeasurementSchemaGroup {
+ // measurement_name -> MeasurementSchema
+ MeasurementSchemaMap measurement_schema_map_;
+ bool is_aligned_ = false;
+ TimeChunkWriter *time_chunk_writer_ = nullptr;
+ };
+
+ enum class ColumnCategory { TAG, FIELD };
+
+ class TableSchema {
+ public:
+ static void to_lowercase_inplace(std::string &str) {
+ std::transform(str.begin(), str.end(), str.begin(),
+ [](unsigned char c) { return std::tolower(c); });
+ }
+
+ TableSchema() = default;
+
+ TableSchema(const std::string &table_name,
+ const std::vector<std::shared_ptr<MeasurementSchema> >
+ &column_schemas,
+ const std::vector<ColumnCategory> &column_categories)
+ : table_name_(table_name),
+ column_schemas_(column_schemas),
+ column_categories_(column_categories) {
+ to_lowercase_inplace(table_name_);
+ int idx = 0;
+ for (const auto &measurement_schema: column_schemas_) {
+ to_lowercase_inplace(measurement_schema->measurement_name_);
+ column_pos_index_.insert(
+ std::make_pair(measurement_schema->measurement_name_,
idx++));
+ }
+ }
+
+ TableSchema(TableSchema &&other) noexcept
+ : table_name_(std::move(other.table_name_)),
+ column_schemas_(std::move(other.column_schemas_)),
+ column_categories_(std::move(other.column_categories_)) {
+ }
+
+ TableSchema(const TableSchema &other) = default;
+
+ int serialize_to(common::ByteStream &out) {
+ int ret = common::E_OK;
+ if (RET_FAIL(common::SerializationUtil::write_var_uint(
+ column_schemas_.size(), out))) {
+ } else {
+ for (size_t i = 0; IS_SUCC(ret) && i < column_schemas_.size();
+ i++) {
+ auto column_schema = column_schemas_[i];
+ auto column_category = column_categories_[i];
+ if (RET_FAIL(column_schema->serialize_to(out))) {
+ } else if (RET_FAIL(common::SerializationUtil::write_i8(
+ static_cast<int8_t>(column_category), out))) {
+ }
+ }
+ }
+ return ret;
+ }
+
+ int deserialize(common::ByteStream &in) {
+ int ret = common::E_OK;
+ uint32_t num_columns;
+ if (RET_FAIL(common::SerializationUtil::read_var_uint(
+ num_columns, in))) {
+ } else {
+ for (size_t i = 0; IS_SUCC(ret) && i < num_columns;
+ i++) {
+ auto column_schema = std::make_shared<MeasurementSchema>();
+ int8_t column_category = 0;
+ if (RET_FAIL(column_schema->deserialize_from(in))) {
+ } else if (RET_FAIL(common::SerializationUtil::read_i8(
+ column_category, in))) {
+ }
+ column_schemas_.emplace_back(column_schema);
+
column_categories_.emplace_back(static_cast<ColumnCategory>(column_category));
+ }
+ }
+ return ret;
+ }
+
+ ~TableSchema() {
+ column_schemas_.clear();
+ }
+
+ const std::string &get_table_name() { return table_name_; }
+
+ std::vector<std::string> get_measurement_names() const {
+ std::vector<std::string> ret;
Review Comment:
ret.reserve(column_schemas_.size())
##########
cpp/src/common/device_id.h:
##########
@@ -0,0 +1,211 @@
+/*
+ * 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.
+ */
+
+#ifndef COMMON_DEVICE_ID_H
+#define COMMON_DEVICE_ID_H
+
+#include <algorithm>
+#include <cstdint>
+#include <cstring>
+#include <functional>
+#include <iostream>
+#include <memory>
+#include <numeric>
+#include <sstream>
+#include <stdexcept>
+#include <string>
+#include <vector>
+
+#include "common/allocator/byte_stream.h"
+#include "parser/path_nodes_generator.h"
+#include "utils/errno_define.h"
+
+class IDeviceID {
+ public:
+ virtual ~IDeviceID() = default;
+ virtual int serialize(common::ByteStream& write_stream) { return 0; }
+ virtual int deserialize(common::ByteStream& read_stream) { return 0; }
+ virtual std::string get_table_name() { return ""; }
+ virtual int segment_num() { return 0; }
+ virtual const std::vector<std::string>& get_segments() const {
+ return empty_segments_;
+ }
+ virtual std::string get_device_name() const { return ""; };
+ virtual bool operator<(const IDeviceID& other) { return 0; }
+ virtual bool operator==(const IDeviceID& other) { return false; }
+ virtual bool operator!=(const IDeviceID& other) { return false; }
+
+ protected:
+ IDeviceID() : empty_segments_() {}
+
+ private:
+ const std::vector<std::string> empty_segments_;
+};
+
+struct IDeviceIDComparator {
+ bool operator()(const std::shared_ptr<IDeviceID>& lhs,
+ const std::shared_ptr<IDeviceID>& rhs) const {
+ return *lhs < *rhs;
+ }
+};
+
+class StringArrayDeviceID : public IDeviceID {
+ public:
+ explicit StringArrayDeviceID(const std::vector<std::string>& segments)
+ : segments_(formalize(segments)) {}
+
+ explicit StringArrayDeviceID(const std::string& device_id_string)
+ : segments_(split_device_id_string(device_id_string)) {}
+
+ ~StringArrayDeviceID() override = default;
+
+ std::string get_device_name() const override {
+ return std::accumulate(std::next(segments_.begin()), segments_.end(),
+ segments_.front(),
+ [](std::string a, const std::string& b) {
+ return std::move(a) + "." + b;
+ });
+ };
+
+ int serialize(common::ByteStream& write_stream) override {
+ int ret = common::E_OK;
+ if (RET_FAIL(common::SerializationUtil::write_var_uint(segment_num(),
+ write_stream)))
{
+ return ret;
+ }
+ for (const auto& segment : segments_) {
+ if (RET_FAIL(common::SerializationUtil::write_str(segment,
+ write_stream))) {
+ return ret;
+ }
+ }
+ return ret;
+ }
+
+ int deserialize(common::ByteStream& read_stream) override {
+ int ret = common::E_OK;
+ uint32_t num_segments;
+ if (RET_FAIL(common::SerializationUtil::read_var_uint(num_segments,
read_stream))) {
+ return ret;
+ }
+ segments_.clear();
+ for (uint32_t i = 0; i < num_segments; ++i) {
+ std::string segment;
+ if (RET_FAIL(common::SerializationUtil::read_str(segment,
read_stream))) {
+ return ret;
+ }
+ segments_.push_back(segment);
+ }
+ return ret;
+ }
+
+ std::string get_table_name() override {
+ return segments_.empty() ? "" : segments_[0];
+ }
+
+ int segment_num() override { return static_cast<int>(segments_.size()); }
+
+ const std::vector<std::string>& get_segments() const override {
+ return segments_;
+ }
+
+ virtual bool operator<(const IDeviceID& other) override {
+ auto other_segments = other.get_segments();
+ return std::lexicographical_compare(segments_.begin(), segments_.end(),
+ other_segments.begin(),
+ other_segments.end());
+ }
+
+ bool operator==(const IDeviceID& other) override {
+ auto other_segments = other.get_segments();
+ return (segments_.size() == other_segments.size()) &&
+ std::equal(segments_.begin(), segments_.end(),
+ other_segments.begin());
+ }
+
+ bool operator!=(const IDeviceID& other) override {
+ return !(*this == other);
+ }
+
+ private:
+ std::vector<std::string> segments_;
+
+ std::vector<std::string> formalize(
+ const std::vector<std::string>& segments) {
+ auto it =
+ std::find_if(segments.rbegin(), segments.rend(),
+ [](const std::string& seg) { return !seg.empty(); });
+ return std::vector<std::string>(segments.begin(), it.base());
+ }
+
+ std::vector<std::string> split_device_id_string(
+ const std::string& device_id_string) {
+ auto splits =
+ storage::PathNodesGenerator::invokeParser(device_id_string);
+ return split_device_id_string(splits);
+ }
+
+ static const char PATH_SEPARATOR = '.';
+ static const int DEFAULT_SEGMENT_NUM_FOR_TABLE_NAME = 3;
+
+ std::vector<std::string> split_device_id_string(
+ const std::vector<std::string>& splits) {
+ size_t segment_cnt = splits.size();
+ std::vector<std::string> final_segments;
+
+ if (segment_cnt == 0) {
+ return final_segments;
+ }
+
+ if (segment_cnt == 1) {
+ // "root" -> {"root"}
+ final_segments.push_back(splits[0]);
+ } else if (segment_cnt < static_cast<size_t>(
+ DEFAULT_SEGMENT_NUM_FOR_TABLE_NAME + 1)) {
+ // "root.a" -> {"root", "a"}
+ // "root.a.b" -> {"root.a", "b"}
+ std::string table_name = std::accumulate(
+ splits.begin(), splits.end() - 1, std::string(),
+ [](const std::string& a, const std::string& b) {
+ return a.empty() ? b : a + PATH_SEPARATOR + b;
+ });
+ final_segments.push_back(table_name);
Review Comment:
```suggestion
final_segments.emplace_back(std::move(table_name));
```
##########
cpp/src/common/schema.h:
##########
@@ -21,68 +21,314 @@
#define COMMON_SCHEMA_H
#include <map> // use unordered_map instead
+#include <memory>
#include <string>
#include "common/db_common.h"
#include "writer/time_chunk_writer.h"
#include "writer/value_chunk_writer.h"
namespace storage {
-class ChunkWriter;
+ class ChunkWriter;
}
namespace storage {
+ /* schema information for one measurement */
+ struct MeasurementSchema {
+ std::string measurement_name_; // for example: "s1"
+ common::TSDataType data_type_;
+ common::TSEncoding encoding_;
+ common::CompressionType compression_type_;
+ storage::ChunkWriter *chunk_writer_;
+ ValueChunkWriter *value_chunk_writer_;
+ std::map<std::string, std::string> props_;
-/* schema information for one measurement */
-struct MeasurementSchema {
- std::string measurement_name_; // for example: "s1"
- common::TSDataType data_type_;
- common::TSEncoding encoding_;
- common::CompressionType compression_type_;
- storage::ChunkWriter *chunk_writer_;
- ValueChunkWriter *value_chunk_writer_;
-
- MeasurementSchema()
- : measurement_name_(),
- data_type_(common::INVALID_DATATYPE),
- encoding_(common::INVALID_ENCODING),
- compression_type_(common::INVALID_COMPRESSION),
- chunk_writer_(nullptr),
- value_chunk_writer_(nullptr) {}
-
- MeasurementSchema(const std::string &measurement_name,
- common::TSDataType data_type)
- : measurement_name_(measurement_name),
- data_type_(data_type),
- encoding_(get_default_encoding_for_type(data_type)),
- compression_type_(common::LZ4),
- chunk_writer_(nullptr),
- value_chunk_writer_(nullptr) {}
-
- MeasurementSchema(const std::string &measurement_name,
- common::TSDataType data_type, common::TSEncoding
encoding,
- common::CompressionType compression_type)
- : measurement_name_(measurement_name),
- data_type_(data_type),
- encoding_(encoding),
- compression_type_(compression_type),
- chunk_writer_(nullptr),
- value_chunk_writer_(nullptr) {}
-};
-
-typedef std::map<std::string, MeasurementSchema *> MeasurementSchemaMap;
-typedef std::map<std::string, MeasurementSchema *>::iterator
+ MeasurementSchema()
+ : measurement_name_(),
+ data_type_(common::INVALID_DATATYPE),
+ encoding_(common::INVALID_ENCODING),
+ compression_type_(common::INVALID_COMPRESSION),
+ chunk_writer_(nullptr),
+ value_chunk_writer_(nullptr) {
+ }
+
+ MeasurementSchema(const std::string &measurement_name,
+ common::TSDataType data_type)
+ : measurement_name_(measurement_name),
+ data_type_(data_type),
+ encoding_(get_default_encoding_for_type(data_type)),
+ compression_type_(common::UNCOMPRESSED),
+ chunk_writer_(nullptr),
+ value_chunk_writer_(nullptr) {
+ }
+
+ MeasurementSchema(const std::string &measurement_name,
+ common::TSDataType data_type, common::TSEncoding
encoding,
+ common::CompressionType compression_type)
+ : measurement_name_(measurement_name),
+ data_type_(data_type),
+ encoding_(encoding),
+ compression_type_(compression_type),
+ chunk_writer_(nullptr),
+ value_chunk_writer_(nullptr) {
+ }
+
+ int serialize_to(common::ByteStream &out) {
+ int ret = common::E_OK;
+ if (RET_FAIL(
+ common::SerializationUtil::write_str(measurement_name_, out)))
{
+ } else if (RET_FAIL(
+ common::SerializationUtil::write_ui8(data_type_, out))) {
+ } else if (RET_FAIL(
+ common::SerializationUtil::write_ui8(encoding_, out))) {
+ } else if (RET_FAIL(common::SerializationUtil::write_ui8(
+ compression_type_, out))) {
+ }
+ if (ret == common::E_OK) {
+ if
(RET_FAIL(common::SerializationUtil::write_ui32(props_.size(),
+ out))) {
+ for (const auto &prop: props_) {
+ if (RET_FAIL(common::SerializationUtil::write_str(
+ prop.first, out))) {
+ } else if
(RET_FAIL(common::SerializationUtil::write_str(
+ prop.second, out))) {
+ }
+ if (ret != common::E_OK) break;
Review Comment:
```suggestion
if (IS_FAIL(ret)) break;
```
##########
cpp/src/common/device_id.h:
##########
@@ -0,0 +1,211 @@
+/*
+ * 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.
+ */
+
+#ifndef COMMON_DEVICE_ID_H
+#define COMMON_DEVICE_ID_H
+
+#include <algorithm>
+#include <cstdint>
+#include <cstring>
+#include <functional>
+#include <iostream>
+#include <memory>
+#include <numeric>
+#include <sstream>
+#include <stdexcept>
+#include <string>
+#include <vector>
+
+#include "common/allocator/byte_stream.h"
+#include "parser/path_nodes_generator.h"
+#include "utils/errno_define.h"
+
+class IDeviceID {
+ public:
+ virtual ~IDeviceID() = default;
+ virtual int serialize(common::ByteStream& write_stream) { return 0; }
+ virtual int deserialize(common::ByteStream& read_stream) { return 0; }
+ virtual std::string get_table_name() { return ""; }
+ virtual int segment_num() { return 0; }
+ virtual const std::vector<std::string>& get_segments() const {
+ return empty_segments_;
+ }
+ virtual std::string get_device_name() const { return ""; };
+ virtual bool operator<(const IDeviceID& other) { return 0; }
+ virtual bool operator==(const IDeviceID& other) { return false; }
+ virtual bool operator!=(const IDeviceID& other) { return false; }
+
+ protected:
+ IDeviceID() : empty_segments_() {}
+
+ private:
+ const std::vector<std::string> empty_segments_;
+};
+
+struct IDeviceIDComparator {
+ bool operator()(const std::shared_ptr<IDeviceID>& lhs,
+ const std::shared_ptr<IDeviceID>& rhs) const {
+ return *lhs < *rhs;
+ }
+};
+
+class StringArrayDeviceID : public IDeviceID {
+ public:
+ explicit StringArrayDeviceID(const std::vector<std::string>& segments)
+ : segments_(formalize(segments)) {}
+
+ explicit StringArrayDeviceID(const std::string& device_id_string)
+ : segments_(split_device_id_string(device_id_string)) {}
+
+ ~StringArrayDeviceID() override = default;
+
+ std::string get_device_name() const override {
+ return std::accumulate(std::next(segments_.begin()), segments_.end(),
+ segments_.front(),
+ [](std::string a, const std::string& b) {
+ return std::move(a) + "." + b;
+ });
+ };
+
+ int serialize(common::ByteStream& write_stream) override {
+ int ret = common::E_OK;
+ if (RET_FAIL(common::SerializationUtil::write_var_uint(segment_num(),
+ write_stream)))
{
+ return ret;
+ }
+ for (const auto& segment : segments_) {
+ if (RET_FAIL(common::SerializationUtil::write_str(segment,
+ write_stream))) {
+ return ret;
+ }
+ }
+ return ret;
+ }
+
+ int deserialize(common::ByteStream& read_stream) override {
+ int ret = common::E_OK;
+ uint32_t num_segments;
+ if (RET_FAIL(common::SerializationUtil::read_var_uint(num_segments,
read_stream))) {
+ return ret;
+ }
+ segments_.clear();
+ for (uint32_t i = 0; i < num_segments; ++i) {
+ std::string segment;
+ if (RET_FAIL(common::SerializationUtil::read_str(segment,
read_stream))) {
+ return ret;
+ }
+ segments_.push_back(segment);
+ }
+ return ret;
+ }
+
+ std::string get_table_name() override {
+ return segments_.empty() ? "" : segments_[0];
+ }
+
+ int segment_num() override { return static_cast<int>(segments_.size()); }
+
+ const std::vector<std::string>& get_segments() const override {
+ return segments_;
+ }
+
+ virtual bool operator<(const IDeviceID& other) override {
+ auto other_segments = other.get_segments();
+ return std::lexicographical_compare(segments_.begin(), segments_.end(),
+ other_segments.begin(),
+ other_segments.end());
+ }
+
+ bool operator==(const IDeviceID& other) override {
+ auto other_segments = other.get_segments();
+ return (segments_.size() == other_segments.size()) &&
+ std::equal(segments_.begin(), segments_.end(),
+ other_segments.begin());
+ }
+
+ bool operator!=(const IDeviceID& other) override {
+ return !(*this == other);
+ }
+
+ private:
+ std::vector<std::string> segments_;
+
+ std::vector<std::string> formalize(
+ const std::vector<std::string>& segments) {
+ auto it =
+ std::find_if(segments.rbegin(), segments.rend(),
+ [](const std::string& seg) { return !seg.empty(); });
+ return std::vector<std::string>(segments.begin(), it.base());
+ }
+
+ std::vector<std::string> split_device_id_string(
+ const std::string& device_id_string) {
+ auto splits =
+ storage::PathNodesGenerator::invokeParser(device_id_string);
+ return split_device_id_string(splits);
+ }
+
+ static const char PATH_SEPARATOR = '.';
+ static const int DEFAULT_SEGMENT_NUM_FOR_TABLE_NAME = 3;
Review Comment:
include /common/constant/tsfile_constant
--
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]