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


##########
cpp/examples/cpp_examples/demo_write.cpp:
##########
@@ -27,88 +29,54 @@
 
 using namespace storage;
 
-long getNowTime() { return time(nullptr); }
-
-static std::string generate_random_string(int length) {
-    std::random_device rd;
-    std::mt19937 gen(rd());
-    std::uniform_int_distribution<> dis(0, 61);
-
-    const std::string chars =
-        "0123456789"
-        "abcdefghijklmnopqrstuvwxyz"
-        "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
-
-    std::string random_string;
-
-    for (int i = 0; i < length; ++i) {
-        random_string += chars[dis(gen)];
-    }
-
-    return random_string;
-}
-
 int demo_write() {
-    TsFileWriter* tsfile_writer_ = new TsFileWriter();
     libtsfile_init();
-    std::string file_name_ = std::string("tsfile_writer_test_") +
-                             generate_random_string(10) +
-                             std::string(".tsfile");
+    std::string table_name = "table1";
+    storage::WriteFile file;
     int flags = O_WRONLY | O_CREAT | O_TRUNC;
 #ifdef _WIN32
     flags |= O_BINARY;
 #endif
     mode_t mode = 0666;
-    tsfile_writer_->open(file_name_, flags, mode);
-    remove(file_name_.c_str());
-    const int device_num = 50;
-    const int measurement_num = 50;
-    std::vector<MeasurementSchema> schema_vec[50];
-    for (int i = 0; i < device_num; i++) {
-        std::string device_name = "test_device" + std::to_string(i);
-        schema_vec[i].reserve(measurement_num);
-        for (int j = 0; j < measurement_num; j++) {
-            std::string measure_name = "measurement" + std::to_string(j);
-            schema_vec[i].emplace_back(
-                MeasurementSchema(measure_name, common::TSDataType::INT32,
-                                  common::TSEncoding::PLAIN,
-                                  common::CompressionType::UNCOMPRESSED));
-            tsfile_writer_->register_timeseries(device_name, schema_vec[i][j]);
-        }
-    }
+    file.create("test_cpp.tsfile", flags, mode);
+    storage::TableSchema* schema = new storage::TableSchema(
+        table_name,
+        {
+            common::ColumnSchema("id1", common::STRING, common::UNCOMPRESSED,
+                                 common::PLAIN, common::ColumnCategory::TAG),
+            common::ColumnSchema("id2", common::STRING, common::UNCOMPRESSED,
+                                 common::PLAIN, common::ColumnCategory::TAG),
+            common::ColumnSchema("s1", common::INT64, common::UNCOMPRESSED,
+                                 common::PLAIN, common::ColumnCategory::FIELD),
+        });
 
-    std::cout << "input tablet size" << std::endl;
-    int tablet_size;
-    std::cin >> tablet_size;
+    TsFileTableWriter* writer = new TsFileTableWriter(&file, schema);
 
-    int max_rows = 100000;
-    int cur_row = 0;
-    long start = getNowTime();
-    for (; cur_row < max_rows;) {
-        if (cur_row + tablet_size > max_rows) {
-            tablet_size = max_rows - cur_row;
-        }
-        for (int i = 0; i < device_num; i++) {
-            std::string device_name = "test_device" + std::to_string(i);
-            Tablet tablet(device_name, &schema_vec[i], tablet_size);
-            tablet.init();
-            for (int row = 0; row < tablet_size; row++) {
-                tablet.set_timestamp(row, 16225600 + cur_row + row);
-            }
-            for (int j = 0; j < measurement_num; j++) {
-                for (int row = 0; row < tablet_size; row++) {
-                    tablet.set_value(row, j, row + cur_row);
-                }
-            }
-            tsfile_writer_->write_tablet(tablet);
-            tsfile_writer_->flush();
-        }
-        cur_row += tablet_size;
-        std::cout << "finish writing " << cur_row << " rows" << std::endl;
+    storage::Tablet tablet = storage::Tablet(
+        table_name, {"id1", "id2", "s1"},
+        {common::STRING, common::STRING, common::INT64},
+        {common::ColumnCategory::TAG, common::ColumnCategory::TAG,
+         common::ColumnCategory::FIELD},
+        10);
+    char* literal = new char[std::strlen("id1_filed_1") + 1];
+    std::strcpy(literal, "id1_filed_1");

Review Comment:
   fixed



##########
cpp/examples/cpp_examples/demo_write.cpp:
##########
@@ -27,88 +29,54 @@
 
 using namespace storage;
 
-long getNowTime() { return time(nullptr); }
-
-static std::string generate_random_string(int length) {
-    std::random_device rd;
-    std::mt19937 gen(rd());
-    std::uniform_int_distribution<> dis(0, 61);
-
-    const std::string chars =
-        "0123456789"
-        "abcdefghijklmnopqrstuvwxyz"
-        "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
-
-    std::string random_string;
-
-    for (int i = 0; i < length; ++i) {
-        random_string += chars[dis(gen)];
-    }
-
-    return random_string;
-}
-
 int demo_write() {
-    TsFileWriter* tsfile_writer_ = new TsFileWriter();
     libtsfile_init();
-    std::string file_name_ = std::string("tsfile_writer_test_") +
-                             generate_random_string(10) +
-                             std::string(".tsfile");
+    std::string table_name = "table1";
+    storage::WriteFile file;
     int flags = O_WRONLY | O_CREAT | O_TRUNC;
 #ifdef _WIN32
     flags |= O_BINARY;
 #endif
     mode_t mode = 0666;
-    tsfile_writer_->open(file_name_, flags, mode);
-    remove(file_name_.c_str());
-    const int device_num = 50;
-    const int measurement_num = 50;
-    std::vector<MeasurementSchema> schema_vec[50];
-    for (int i = 0; i < device_num; i++) {
-        std::string device_name = "test_device" + std::to_string(i);
-        schema_vec[i].reserve(measurement_num);
-        for (int j = 0; j < measurement_num; j++) {
-            std::string measure_name = "measurement" + std::to_string(j);
-            schema_vec[i].emplace_back(
-                MeasurementSchema(measure_name, common::TSDataType::INT32,
-                                  common::TSEncoding::PLAIN,
-                                  common::CompressionType::UNCOMPRESSED));
-            tsfile_writer_->register_timeseries(device_name, schema_vec[i][j]);
-        }
-    }
+    file.create("test_cpp.tsfile", flags, mode);
+    storage::TableSchema* schema = new storage::TableSchema(
+        table_name,
+        {
+            common::ColumnSchema("id1", common::STRING, common::UNCOMPRESSED,
+                                 common::PLAIN, common::ColumnCategory::TAG),
+            common::ColumnSchema("id2", common::STRING, common::UNCOMPRESSED,
+                                 common::PLAIN, common::ColumnCategory::TAG),
+            common::ColumnSchema("s1", common::INT64, common::UNCOMPRESSED,
+                                 common::PLAIN, common::ColumnCategory::FIELD),
+        });
 
-    std::cout << "input tablet size" << std::endl;
-    int tablet_size;
-    std::cin >> tablet_size;
+    TsFileTableWriter* writer = new TsFileTableWriter(&file, schema);
 
-    int max_rows = 100000;
-    int cur_row = 0;
-    long start = getNowTime();
-    for (; cur_row < max_rows;) {
-        if (cur_row + tablet_size > max_rows) {
-            tablet_size = max_rows - cur_row;
-        }
-        for (int i = 0; i < device_num; i++) {
-            std::string device_name = "test_device" + std::to_string(i);
-            Tablet tablet(device_name, &schema_vec[i], tablet_size);
-            tablet.init();
-            for (int row = 0; row < tablet_size; row++) {
-                tablet.set_timestamp(row, 16225600 + cur_row + row);
-            }
-            for (int j = 0; j < measurement_num; j++) {
-                for (int row = 0; row < tablet_size; row++) {
-                    tablet.set_value(row, j, row + cur_row);
-                }
-            }
-            tsfile_writer_->write_tablet(tablet);
-            tsfile_writer_->flush();
-        }
-        cur_row += tablet_size;
-        std::cout << "finish writing " << cur_row << " rows" << std::endl;
+    storage::Tablet tablet = storage::Tablet(
+        table_name, {"id1", "id2", "s1"},
+        {common::STRING, common::STRING, common::INT64},
+        {common::ColumnCategory::TAG, common::ColumnCategory::TAG,
+         common::ColumnCategory::FIELD},
+        10);
+    char* literal = new char[std::strlen("id1_filed_1") + 1];
+    std::strcpy(literal, "id1_filed_1");
+    common::String literal_str(literal, std::strlen("id1_filed_1"));
+    char* literal2 = new char[std::strlen("id1_filed_2") + 1];
+    std::strcpy(literal, "id1_filed_2");
+    common::String literal_str2(literal2, std::strlen("id1_filed_2"));
+    for (int row = 0; row < 5; row++) {
+        long timestamp = row;
+        tablet.add_timestamp(row, timestamp);
+        tablet.add_value(row, "id1", literal_str);
+        tablet.add_value(row, "id2", literal_str2);
+        tablet.add_value(row, "s1", static_cast<int64_t>(row));
     }
-
-    tsfile_writer_->close();
-    long end = getNowTime();
-    printf("interval waitForResults is %ld \n", end - start);
+    writer->write_table(tablet);

Review Comment:
   fixed



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