This is an automated email from the ASF dual-hosted git repository.

colinlee pushed a commit to branch colin_example_c_cpp
in repository https://gitbox.apache.org/repos/asf/tsfile.git

commit 340473475eafd12260819eb1717584a77df5f9fc
Author: colin <[email protected]>
AuthorDate: Mon Feb 24 18:37:28 2025 +0800

    examples.
    
    tmpc ode.
    
    tmp code.
    
    fix issue.
---
 cpp/examples/CMakeLists.txt              |  15 +++-
 cpp/examples/build.sh                    |   2 +-
 cpp/examples/c_examples/c_examples.c     | 133 -------------------------------
 cpp/examples/c_examples/c_examples.h     |  14 +++-
 cpp/examples/c_examples/demo_read.c      |  86 ++++++++++++++++++++
 cpp/examples/c_examples/demo_write.c     |  70 ++++++++++++++++
 cpp/examples/cpp_examples/cpp_examples.h |  10 +++
 cpp/examples/cpp_examples/demo_read.cpp  |  43 ++--------
 cpp/examples/cpp_examples/demo_write.cpp | 104 +++++++-----------------
 cpp/src/cwrapper/tsfile_cwrapper.cc      |   8 +-
 cpp/src/cwrapper/tsfile_cwrapper.h       |  16 ++--
 cpp/src/utils/db_utils.h                 |   8 ++
 12 files changed, 250 insertions(+), 259 deletions(-)

diff --git a/cpp/examples/CMakeLists.txt b/cpp/examples/CMakeLists.txt
index 4b433c96..b0392c2d 100644
--- a/cpp/examples/CMakeLists.txt
+++ b/cpp/examples/CMakeLists.txt
@@ -20,13 +20,26 @@ cmake_minimum_required(VERSION 3.10)
 project(examples)
 message("Running in exampes directory")
 
+# TsFile include dir
 set(SDK_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/../src/)
 message("SDK_INCLUDE_DIR: ${SDK_INCLUDE_DIR}")
+
+# TsFile shared object dir
 set(SDK_LIB_DIR_RELEASE ${PROJECT_SOURCE_DIR}/../build/Release/lib)
 message("SDK_LIB_DIR_RELEASE: ${SDK_LIB_DIR_RELEASE}")
 
+set(SDK_LIB_DIR_DEBUG ${PROJECT_SOURCE_DIR}/../build/Debug/lib)
+message("SDK_LIB_DIR_DEBUG: ${SDK_LIB_DIR_DEBUG}")
+
+set(BUILD_TYPE "Release")
 include_directories(${SDK_INCLUDE_DIR})
-find_library(my_tsfile_lib NAMES tsfile PATHS ${SDK_LIB_DIR_RELEASE} 
NO_DEFAULT_PATH REQUIRED)
+
+# Find libtsfile in SDK_LIB_RIR
+if (BUILD_TYPE STREQUAL "Release")
+    find_library(my_tsfile_lib NAMES tsfile PATHS ${SDK_LIB_DIR_RELEASE} 
NO_DEFAULT_PATH REQUIRED)
+elseif(BUILD_TYPE STREQUAL "Debug")
+    find_library(my_tsfile_lib NAMES tsfile PATHS ${SDK_LIB_DIR_DEBUG} 
NO_DEFAULT_PATH REQUIRED)
+endif ()
 
 add_subdirectory(cpp_examples)
 add_subdirectory(c_examples)
diff --git a/cpp/examples/build.sh b/cpp/examples/build.sh
index bf4a3c45..e65ea81b 100644
--- a/cpp/examples/build.sh
+++ b/cpp/examples/build.sh
@@ -28,5 +28,5 @@ else
 fi
 
 
-cmake ../../ -DUSE_CPP11=$use_cpp11
+cmake ../../ -DUSE_CPP11=$use_cpp11 -DBUILD_TYPE=$build_type
 make
\ No newline at end of file
diff --git a/cpp/examples/c_examples/c_examples.c 
b/cpp/examples/c_examples/c_examples.c
deleted file mode 100644
index 8e5f8d2d..00000000
--- a/cpp/examples/c_examples/c_examples.c
+++ /dev/null
@@ -1,133 +0,0 @@
-/*
- * 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 "c_examples.h"
-
-#include <fcntl.h>
-#include <malloc.h>
-#include <stdbool.h>
-#include <stdio.h>
-#include <unistd.h>
-
-#define HANDLE_ERROR(err_no)                  \
-    do {                                      \
-        if (err_no != 0) {                    \
-            printf("get err no: %d", err_no); \
-            return err_no;                    \
-        }                                     \
-    } while (0)
-
-ErrorCode write_tsfile() {
-    ErrorCode err_code;
-    CTsFileWriter writer;
-    if (access("c_rw.tsfile", 0) == 0) {
-        if (remove("test.tsfile") != 0) {
-            printf("Failed to delete test.tsfile file\n");
-            return -1;
-        }
-    }
-    writer = ts_writer_open("c_rw.tsfile", &err_code);
-    if (NULL == writer) {
-        return err_code;
-    }
-    ColumnSchema columnSchema;
-    columnSchema.name = "temperature";
-    columnSchema.column_def = TS_TYPE_INT32;
-    err_code =
-        tsfile_register_table_column(writer, "test_table", &columnSchema);
-    HANDLE_ERROR(err_code);
-    TableSchema tableSchema;
-    tableSchema.column_num = 3;
-    tableSchema.table_name = "test_table";
-    tableSchema.column_schema =
-        (ColumnSchema **)malloc(tableSchema.column_num * sizeof(TableSchema 
*));
-    tableSchema.column_schema[0] = (ColumnSchema 
*)malloc(sizeof(ColumnSchema));
-    tableSchema.column_schema[0]->column_def = TS_TYPE_DOUBLE;
-    tableSchema.column_schema[0]->name = "level";
-    tableSchema.column_schema[1] = (ColumnSchema 
*)malloc(sizeof(ColumnSchema));
-    tableSchema.column_schema[1]->column_def = TS_TYPE_BOOLEAN;
-    tableSchema.column_schema[1]->name = "up";
-    tableSchema.column_schema[2] = (ColumnSchema 
*)malloc(sizeof(ColumnSchema));
-    tableSchema.column_schema[2]->column_def = TS_TYPE_FLOAT;
-    tableSchema.column_schema[2]->name = "humi";
-    err_code = tsfile_register_table(writer, &tableSchema);
-    free(tableSchema.column_schema[0]);
-    free(tableSchema.column_schema[1]);
-    free(tableSchema.column_schema[2]);
-    free(tableSchema.column_schema);
-    HANDLE_ERROR(err_code);
-    printf("register table success\n");
-    TsFileRowData rowData = create_tsfile_row("test_table", 1, 4);
-    insert_data_into_tsfile_row_double(rowData, "level", 10);
-    insert_data_into_tsfile_row_float(rowData, "humi", 10.0f);
-    insert_data_into_tsfile_row_boolean(rowData, "up", true);
-    insert_data_into_tsfile_row_int32(rowData, "temperature", 10);
-    err_code = tsfile_write_row_data(writer, rowData);
-
-    rowData = create_tsfile_row("test_table", 2, 4);
-    insert_data_into_tsfile_row_double(rowData, "level", 12);
-    err_code = tsfile_write_row_data(writer, rowData);
-
-    for (int ind = 10; ind < 2000; ind++) {
-        rowData = create_tsfile_row("test_table", ind, 4);
-        insert_data_into_tsfile_row_double(rowData, "level", 12 + ind);
-        insert_data_into_tsfile_row_float(rowData, "humi", 12.0f + ind);
-        insert_data_into_tsfile_row_boolean(rowData, "up", true);
-        insert_data_into_tsfile_row_int32(rowData, "temperature", 12 + ind);
-        err_code = tsfile_write_row_data(writer, rowData);
-    }
-    printf("writer row data success\n");
-    HANDLE_ERROR(err_code);
-    HANDLE_ERROR(tsfile_flush_data(writer));
-    printf("flush data success\n");
-    HANDLE_ERROR(ts_writer_close(writer));
-    printf("close writer success\n");
-    return 0;
-}
-
-ErrorCode read_tsfile() {
-    ErrorCode err_code;
-    CTsFileReader reader;
-    reader = ts_reader_open("c_rw.tsfile", &err_code);
-    if (NULL == reader) {
-        return err_code;
-    }
-    const char *columns[] = {"temperature", "level", "up", "humi"};
-    //  TimeFilterExpression* exp = create_andquery_timefilter();
-    //  TimeFilterExpression* time_filter = create_time_filter("test_table",
-    //  "temperature", GT, 11); TimeFilterExpression* time_filter2 =
-    //  create_time_filter("test_table", "humi", GT, 10); TimeFilterExpression*
-    //  time_filter3 = create_time_filter("test_table", "level", LE, 20);
-    //  add_time_filter_to_and_query(exp, time_filter);
-    //  add_time_filter_to_and_query(exp, time_filter2);
-    //  add_time_filter_to_and_query(exp, time_filter3);
-
-    QueryDataRet ret = ts_reader_query(reader, "test_table", columns, 4, NULL);
-    printf("query success\n");
-    DataResult *result = ts_next(ret, 20);
-    if (result == NULL) {
-        printf("get result failed\n");
-        return -1;
-    }
-    print_data_result(result);
-    //  destory_time_filter_query(exp);
-    HANDLE_ERROR(destory_query_dataret(ret));
-    HANDLE_ERROR(destory_tablet(result));
-    return 0;
-}
diff --git a/cpp/examples/c_examples/c_examples.h 
b/cpp/examples/c_examples/c_examples.h
index 3f63803f..7999aa7b 100644
--- a/cpp/examples/c_examples/c_examples.h
+++ b/cpp/examples/c_examples/c_examples.h
@@ -17,15 +17,21 @@
  * under the License.
  */
 
-#include "cwrapper/TsFile-cwrapper.h"
+#include "cwrapper/tsfile_cwrapper.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
+ERRNO write_tsfile();
+ERRNO read_tsfile();
 
-ErrorCode write_tsfile();
-ErrorCode read_tsfile();
-
+#define HANDLE_ERROR(err_no)                  \
+    do {                                      \
+        if (err_no != 0) {                    \
+            printf("get err no: %d", err_no); \
+            return err_no;                    \
+        }                                     \
+    } while (0)
 #ifdef __cplusplus
 }
 #endif
diff --git a/cpp/examples/c_examples/demo_read.c 
b/cpp/examples/c_examples/demo_read.c
new file mode 100644
index 00000000..36a8c4c6
--- /dev/null
+++ b/cpp/examples/c_examples/demo_read.c
@@ -0,0 +1,86 @@
+/*
+ * 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 <stdbool.h>
+#include <stdio.h>
+#include <stdint.h>
+
+#include "c_examples.h"
+
+// This example shows you how to read tsfile.
+ERRNO read_tsfile() {
+    ERRNO code = 0;
+    char* table_name = "table1";
+
+    // Create tsfile reader with specify tsfile's path
+    TsFileReader reader = tsfile_reader_new("test.tsfile", &code);
+    HANDLE_ERROR(code);
+
+    ResultSet ret =
+        tsfile_reader_query_table(reader, table_name, (char*[]){"id1", "id2", 
"s1"}, 3, 0, 10);
+
+    // Get query result metadata: column name and datatype
+    ResultSetMetaData metadata = tsfile_result_set_get_metadata(ret);
+    int sensor_num = metadata.column_num;
+
+    for (int i = 0; i < sensor_num; i++) {
+        printf("column:%s, datatype:%d\n", metadata.column_names[i],
+               metadata.data_types[i]);
+    }
+
+    // Get data by column name or index.
+    while (tsfile_result_set_has_next(ret)) {
+        Timestamp timestamp = 
tsfile_result_set_get_value_by_index_int64_t(ret, 1);
+        printf("%ld ", timestamp);
+        for (int i = 1; i < sensor_num; i++) {
+            if (tsfile_result_set_is_null_by_index(ret, i)) {
+                printf("null ");
+            } else {
+                switch (metadata.data_types[i]) {
+                    case TS_DATATYPE_BOOLEAN:
+                        printf("%d", 
tsfile_result_set_get_value_by_index_bool(ret, i));
+                    break;
+                    case TS_DATATYPE_INT32:
+                        printf("%d", 
tsfile_result_set_get_value_by_index_int32_t(ret, i));
+                    break;
+                    case TS_DATATYPE_INT64:
+                        // 注意:int64_t 应使用 %lld(Linux)或 %I64d(Windows)
+                            printf("%lld", 
tsfile_result_set_get_value_by_index_int64_t(ret, i));
+                    break;
+                    case TS_DATATYPE_FLOAT:
+                        printf("%f", 
tsfile_result_set_get_value_by_index_float(ret, i));
+                    break;
+                    case TS_DATATYPE_DOUBLE:
+                        printf("%lf", 
tsfile_result_set_get_value_by_index_double(ret, i));
+                    break;
+                    case TS_DATATYPE_STRING:
+                        printf("%s", 
tsfile_result_set_get_value_by_index_string(ret, i));
+                    break;
+                    default:
+                        printf("unknown_type");
+                    break;
+                }
+            }
+        }
+    }
+
+    free_result_set_meta_data(metadata);
+    free_tsfile_result_set(ret);
+    return 0;
+}
diff --git a/cpp/examples/c_examples/demo_write.c 
b/cpp/examples/c_examples/demo_write.c
new file mode 100644
index 00000000..5a6ae015
--- /dev/null
+++ b/cpp/examples/c_examples/demo_write.c
@@ -0,0 +1,70 @@
+/*
+ * 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 <malloc.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "c_examples.h"
+
+ERRNO write_tsfile() {
+    ERRNO code = 0;
+    char* table_name = "table1";
+
+    // Create tsfile writer with specify path.
+    TsFileWriter writer = tsfile_writer_new("test.tsfile", &code);
+    HANDLE_ERROR(code);
+
+    // Table schema.
+    TableSchema table_schema = {
+        .table_name = table_name,
+        .column_schemas =
+            (ColumnSchema[]){ColumnSchema{"id1", TS_DATATYPE_TEXT, TAG},
+                             ColumnSchema{"id2", TS_DATATYPE_TEXT, TAG},
+                             ColumnSchema{"s1", TS_DATATYPE_INT32, FIELD}}};
+
+    // Register a table with tsfile writer.
+    code = tsfile_writer_register_table(writer, &table_schema);
+    HANDLE_ERROR(code);
+
+    // Create tablet to insert data.
+    Tablet tablet = tablet_new_with_device(
+        table_name, (char*[]){"id1", "id2", "s1"},
+        (TSDataType[]){TS_DATATYPE_TEXT, TS_DATATYPE_TEXT, TS_DATATYPE_INT32},
+        3, 1024);
+
+    for (int row = 0; row < 5; row++) {
+        Timestamp timestamp = row;
+        tablet_add_timestamp(tablet, row, timestamp);
+        tablet_add_value_by_name_string(tablet, row, "id1", "id_field_1");
+        tablet_add_value_by_name_string(tablet, row, "id2", "id_field_2");
+        tablet_add_value_by_name_int32_t(tablet, row, "s1", row);
+    }
+
+    // Write tablet data.
+    HANDLE_ERROR(tsfile_writer_write_tablet(writer, tablet));
+
+    // Flush tablet data.
+    HANDLE_ERROR(tsfile_writer_flush_data(writer));
+
+    // Close writer.
+    HANDLE_ERROR(tsfile_writer_close(writer));
+
+}
\ No newline at end of file
diff --git a/cpp/examples/cpp_examples/cpp_examples.h 
b/cpp/examples/cpp_examples/cpp_examples.h
index b2e52aad..7f877453 100644
--- a/cpp/examples/cpp_examples/cpp_examples.h
+++ b/cpp/examples/cpp_examples/cpp_examples.h
@@ -28,6 +28,16 @@
 #include "reader/qds_without_timegenerator.h"
 #include "reader/tsfile_reader.h"
 #include "writer/tsfile_writer.h"
+#include "writer/tsfile_table_writer.h"
+#include "file/write_file.h"
+
+#define HANDLE_ERROR(err_no)                  \
+    do {                                      \
+        if (err_no != 0) {                    \
+            printf("get err no: %d", err_no); \
+            return err_no;                    \
+        }                                     \
+    } while (0)
 
 int demo_read();
 int demo_write();
diff --git a/cpp/examples/cpp_examples/demo_read.cpp 
b/cpp/examples/cpp_examples/demo_read.cpp
index 8fab33f9..fabbc107 100644
--- a/cpp/examples/cpp_examples/demo_read.cpp
+++ b/cpp/examples/cpp_examples/demo_read.cpp
@@ -20,6 +20,7 @@
 #include <string>
 #include <vector>
 
+#include "../c_examples/c_examples.h"
 #include "cpp_examples.h"
 
 std::string field_to_string(storage::Field *value) {
@@ -55,43 +56,11 @@ std::string field_to_string(storage::Field *value) {
 }
 
 int demo_read() {
-    std::cout << "begin to read tsfile from demo_ts.tsfile" << std::endl;
-    std::string device_name = "root.db001.dev001";
-    std::string measurement_name = "m001";
-    storage::Path p1(device_name, measurement_name);
-    std::vector<storage::Path> select_list;
-    select_list.push_back(p1);
-    storage::QueryExpression *query_expr =
-        storage::QueryExpression::create(select_list, nullptr);
-
-    common::init_config_value();
+    ERRNO code = 0
+    std::string table_name = "table1";
     storage::TsFileReader reader;
-    int ret = reader.open("cpp_rw.tsfile");
-
-    std::cout << "begin to query expr" << std::endl;
-    ASSERT(ret == 0);
-    storage::ResultSet *qds = nullptr;
-    ret = reader.query(query_expr, qds);
-
-    storage::RowRecord *record;
-    std::cout << "begin to dump data from tsfile ---" << std::endl;
-    int row_cout = 0;
-    do {
-        if (qds->next()) {
-            std::cout << "dump QDS :  " << record->get_timestamp() << ",";
-            record = qds->get_row_record();
-            if (record) {
-                int size = record->get_fields()->size();
-                for (int i = 0; i < size; ++i) {
-                    std::cout << field_to_string(record->get_field(i)) << ",";
-                }
-                std::cout << std::endl;
-                row_cout++;
-            }
-        } else {
-            break;
-        }
-    } while (true);
+    reader.open("test.tsfile");
+    storage::ResultSet* ret = nullptr;
+    code = reader.query(table_name, {"id1", "id2", "s1"}, 0, 100, ret);
 
-    return (0);
 }
diff --git a/cpp/examples/cpp_examples/demo_write.cpp 
b/cpp/examples/cpp_examples/demo_write.cpp
index 17c5cb6b..6379a025 100644
--- a/cpp/examples/cpp_examples/demo_write.cpp
+++ b/cpp/examples/cpp_examples/demo_write.cpp
@@ -17,7 +17,9 @@
  * under the License.
  */
 
+#include <cwrapper/tsfile_cwrapper.h>
 #include <time.h>
+#include <writer/tsfile_table_writer.h>
 
 #include <iostream>
 #include <random>
@@ -29,86 +31,42 @@ 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";
+    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.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::ColumnCategory::TAG),
+            common::ColumnSchema("s1", common::INT32),
+        });
 
-    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::INT32},
+        {common::ColumnCategory::TAG, common::ColumnCategory::TAG,
+         common::ColumnCategory::FIELD})
 
-    tsfile_writer_->close();
-    long end = getNowTime();
-    printf("interval waitForResults is %ld \n", end - start);
-    return 0;
+        for (int row = 0; row < 5; row++) {
+            long timestamp = row;
+            tablet.add_timestamp(row, timestamp);
+            tablet.add_value(row, "id1", "id1_filed_1");
+            tablet.add_value(row, "id2", "id2_filed_1");
+            tablet.add_value(row, "s1", row);
+        }
+    writer->write_table(tablet);
+    writer->flush();
+    writer->close();
+    delete writer;
 }
diff --git a/cpp/src/cwrapper/tsfile_cwrapper.cc 
b/cpp/src/cwrapper/tsfile_cwrapper.cc
index c6c52a16..2561b808 100644
--- a/cpp/src/cwrapper/tsfile_cwrapper.cc
+++ b/cpp/src/cwrapper/tsfile_cwrapper.cc
@@ -61,7 +61,7 @@ uint32_t tablet_get_cur_row_size(Tablet tablet) {
 }
 
 ERRNO tablet_add_timestamp(Tablet tablet, uint32_t row_index,
-                           timestamp timestamp) {
+                           Timestamp timestamp) {
     return static_cast<storage::Tablet *>(tablet)->add_timestamp(row_index,
                                                                  timestamp);
 }
@@ -104,7 +104,7 @@ void *tablet_get_value(Tablet tablet, uint32_t row_index, 
uint32_t schema_index,
 }
 
 // TsRecord API
-TsRecord ts_record_new(const char *device_id, timestamp timestamp,
+TsRecord ts_record_new(const char *device_id, Timestamp timestamp,
                        int timeseries_num) {
     auto *record = new storage::TsRecord(timestamp, device_id, timeseries_num);
     return record;
@@ -263,14 +263,14 @@ ERRNO tsfile_writer_flush_data(TsFileWriter writer) {
 
 ResultSet tsfile_reader_query_table(TsFileReader reader, const char 
*table_name,
 char **columns, uint32_t column_num,
-                                    timestamp start_time, timestamp end_time) {
+                                    Timestamp start_time, Timestamp end_time) {
     // TODO: Implement query table with tsfile reader.
     return nullptr;
 }
 
 ResultSet tsfile_reader_query_device(TsFileReader reader, const char* 
device_name,
     char** sensor_name, uint32_t sensor_num,
-                                   timestamp start_time, timestamp end_time) {
+                                   Timestamp start_time, Timestamp end_time) {
     auto *r = static_cast<storage::TsFileReader *>(reader);
     std::vector<std::string> selected_paths;
     selected_paths.reserve(sensor_num);
diff --git a/cpp/src/cwrapper/tsfile_cwrapper.h 
b/cpp/src/cwrapper/tsfile_cwrapper.h
index 46cdabbc..08b9cfa2 100644
--- a/cpp/src/cwrapper/tsfile_cwrapper.h
+++ b/cpp/src/cwrapper/tsfile_cwrapper.h
@@ -109,7 +109,7 @@ typedef void* TsRecord;
 typedef void* ResultSet;
 
 typedef int32_t ERRNO;
-typedef int64_t timestamp;
+typedef int64_t Timestamp;
 
 #ifdef __cplusplus
 extern "C" {
@@ -126,10 +126,10 @@ Tablet tablet_new(const char** column_name_list, 
TSDataType* data_types,
 uint32_t tablet_get_cur_row_size(Tablet tablet);
 
 ERRNO tablet_add_timestamp(Tablet tablet, uint32_t row_index,
-                           timestamp timestamp);
+                           Timestamp timestamp);
 
 #define TABLET_ADD_VALUE_BY_NAME(type)                                        \
-    ERRNO tablet_add_value_by_name_##type(Tablet* tablet, uint32_t row_index, \
+    ERRNO tablet_add_value_by_name_##type(Tablet tablet, uint32_t row_index, \
                                           char* column_name, type value);
 
 TABLET_ADD_VALUE_BY_NAME(int32_t);
@@ -138,6 +138,8 @@ TABLET_ADD_VALUE_BY_NAME(float);
 TABLET_ADD_VALUE_BY_NAME(double);
 TABLET_ADD_VALUE_BY_NAME(bool);
 
+ERRNO tablet_add_value_by_name_string(Tablet tablet, uint32_t row_index, char* 
column_name, char* value);
+
 #define TABLE_ADD_VALUE_BY_INDEX(type)                                        \
     ERRNO tablet_add_value_by_index_##type(Tablet tablet, uint32_t row_index, \
                                            uint32_t column_index, type value);
@@ -148,11 +150,13 @@ TABLE_ADD_VALUE_BY_INDEX(float);
 TABLE_ADD_VALUE_BY_INDEX(double);
 TABLE_ADD_VALUE_BY_INDEX(bool);
 
+ERRNO tablet_add_value_by_index_string(Tablet tablet, uint32_t row_index, 
uint32_t column_index, char* value);
+
 void* tablet_get_value(Tablet tablet, uint32_t row_index, uint32_t 
schema_index,
                        TSDataType* type);
 
 /*--------------------------TsRecord API------------------------ */
-TsRecord ts_record_new(const char* device_id, timestamp timestamp,
+TsRecord ts_record_new(const char* device_id, Timestamp timestamp,
                        int timeseries_num);
 
 #define INSERT_DATA_INTO_TS_RECORD_BY_NAME(type)     \
@@ -190,11 +194,11 @@ ERRNO tsfile_writer_flush_data(TsFileWriter writer);
 /*-------------------TsFile reader query data------------------ */
 ResultSet tsfile_reader_query_table(TsFileReader reader, const char* 
table_name,
                                     char** columns, uint32_t column_num,
-                                    timestamp start_time, timestamp end_time);
+                                    Timestamp start_time, Timestamp end_time);
 ResultSet tsfile_reader_query_device(TsFileReader reader,
                                      const char* device_name,
                                      char** sensor_name, uint32_t sensor_num,
-                                     timestamp start_time, timestamp end_time);
+                                     Timestamp start_time, Timestamp end_time);
 bool tsfile_result_set_has_next(ResultSet result_set);
 
 #define TSFILE_RESULT_SET_GET_VALUE_BY_NAME(type)                         \
diff --git a/cpp/src/utils/db_utils.h b/cpp/src/utils/db_utils.h
index 8f5f5620..508d3a29 100644
--- a/cpp/src/utils/db_utils.h
+++ b/cpp/src/utils/db_utils.h
@@ -281,6 +281,14 @@ struct ColumnSchema {
           encoding_(encoding),
           column_category_(column_category) {}
 
+    ColumnSchema(std::string column_name, TSDataType data_type,
+             ColumnCategory column_category = ColumnCategory::FIELD)
+    : column_name_(std::move(column_name)),
+      data_type_(data_type),
+      compression_(get_default_compression_for_type(data_type)),
+      encoding_(get_default_encoding_for_type(data_type)),
+      column_category_(column_category) {}
+
     const std::string &get_column_name() const { return column_name_; }
     const TSDataType &get_data_type() const { return data_type_; }
     const ColumnCategory &get_column_category() const {

Reply via email to