ColinLeeo commented on code in PR #345:
URL: https://github.com/apache/tsfile/pull/345#discussion_r1922743208


##########
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)

Review Comment:
   If you can make sure the owner of measurement schema, dont use shared_ptr.



-- 
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]

Reply via email to