diff --git a/cpp/README.md b/cpp/README.md index 4f731b1d2..3c657592f 100644 --- a/cpp/README.md +++ b/cpp/README.md @@ -33,44 +33,47 @@ ___________ ___________.__.__ ## Introduction -This directory contains the implementation of the tsfile in the C++ version. Currently, the C++ version has implemented the query and write functions of TsFile, and supports time filtering query. -You can find all source code at ./src, some c examples at ./examples, and a bench_mark of TsFile_cpp at ./bench_mark. -Besides you can find a C function wrapper at ./src/cwrapper, and the Python tool depends on this directory. +This directory contains the C++ implementation of TsFile. The C++ version currently supports the query and write functions of TsFile, including time filtering queries. +The source code can be found in the `./src` directory. C/C++ examples are located in the `./examples` directory, and a benchmark for TsFile_cpp can be found in the `./bench_mark` directory. Additionally, a C function wrapper is available in the `./src/cwrapper` directory, which the Python tool relies on. ## How to make contributions -We use clang-format to make C++ codes obey a consistent ruleset defined in ./clang-format. it's like Google style. +We use `clang-format` to ensure that our C++ code adheres to a consistent set of rules defined in `./clang-format`. This is similar to the Google style. **Feature List**: -- [ ] add some UTs about the reader、writer、compression etc. -- [ ] add UTs about cwrapper. -- [ ] supports multiple flushes of data. -- [ ] supports aligned timeseries. -- [ ] supports describe a table in tsfile. -- [ ] supports get all tables' schema/names. -- [ ] support automatic flush. -- [ ] supports out-of-order data writing -- [ ] support TsFile V4. TsFile CPP does not implement support for the table model, therefore there are differences in file output compared to the Java version. +- [ ] Add unit tests for the reader, writer, compression, etc. +- [ ] Add unit tests for the C wrapper. +- [ ] Support multiple data flushes. +- [ ] Support aligned timeseries. +- [ ] Support table description in tsfile. +- [ ] Retrieve all table schemas/names. +- [ ] Implement automatic flush. +- [ ] Support out-of-order data writing. +- [ ] Support TsFile V4. Note: TsFile CPP does not implement support for the table model, therefore there are differences in file output compared to the Java version. **Bug List**: - [ ] Flushing without writing after registering a timeseries will cause a core dump. - [ ] Misalignment in memory may lead to a bus error. -Any bugs report is welcome, you can open an issue with a title starting with [CPP] to describe the bug, like:https://github.com/apache/tsfile/issues/94 - +We welcome any bug reports. You can open an issue with a title starting with [CPP] to describe the bug, like: https://github.com/apache/tsfile/issues/94 ## Build -### requirement +### Requirements -``` +```bash sudo apt-get update sudo apt-get install -y cmake make g++ clang-format ``` +To build tsfile, you can run: `bash build.sh`. If you have Maven tools, you can run: `mvn package -P with-cpp clean verify`. Then, you can find the shared object at `./build`. + +Before you submit your code to GitHub, please ensure that the `mvn` compilation is correct. + +## Use TsFile -## Use TsFile \ No newline at end of file +You can find examples on how to read and write data in `demo_read.cpp` and `demo_write.cpp` located under `./examples/cpp_examples`. There are also examples under `./examples/c_examples`on how to use a C-style API to read and write data in a C environment \ No newline at end of file diff --git a/cpp/examples/CMakeLists.txt b/cpp/examples/CMakeLists.txt deleted file mode 100644 index fdaf5a725..000000000 --- a/cpp/examples/CMakeLists.txt +++ /dev/null @@ -1,23 +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 - - https://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. -]] -cmake_minimum_required(VERSION 3.10) -project(cwrapper_example) -message("Running in exampes directory") - -add_subdirectory(c_examples) \ No newline at end of file diff --git a/cpp/examples/build.sh b/cpp/examples/build.sh deleted file mode 100644 index c13a6d670..000000000 --- a/cpp/examples/build.sh +++ /dev/null @@ -1,35 +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. -# -build_type=Debug -use_cpp11=1 -if [ ${build_type} = "Debug" ] -then - mkdir -p build/Debug - cd build/Debug - use_sdk_debug=1 -else - mkdir -p build/Release - cd build/Release - use_sdk_debug=0 -fi - -echo "use_sdk_debug=${use_sdk_debug}" -cmake ../../ \ - -DUSE_CPP11=$use_cpp11 -make \ No newline at end of file diff --git a/cpp/examples/c_examples/CMakeLists.txt b/cpp/examples/c_examples/CMakeLists.txt deleted file mode 100644 index 74fb5b8cf..000000000 --- a/cpp/examples/c_examples/CMakeLists.txt +++ /dev/null @@ -1,48 +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 - - https://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. -]] -cmake_minimum_required(VERSION 3.12) -project(cwrapper_example) -set(INCLUDE_DIR ${CMAKE_SOURCE_DIR}/../src/cwrapper/) -include_directories(${INCLUDE_DIR}) -message(STATUS "INCLUDE_DIR: ${INCLUDE_DIR}") - -if (${USE_CPP11}) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - set(CMAKE_CXX_STANDARD 11) -else() - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++03") -endif() - -if (USE_SDK_DEBUG) - SET(CMAKE_CXX_FLAGS "-g -O0") -else() - SET(CMAKE_CXX_FLAGS "-O3") -endif() - -add_executable(cwrapper_example c_examples.c) - -set(SDK_LIB_DIR_RELEASE ${PROJECT_SOURCE_DIR}/../../build/Release/bin/libtsfile_sdk/lib) -message(STATUS "SDK_LIB_DIR_RELEASE: ${SDK_LIB_DIR_RELEASE}") -find_library(TSFILE_LIBRARY tsfile PATHS ${SDK_LIB_DIR_RELEASE}) -if(TSFILE_LIBRARY) - message(STATUS "Found tsfile library: ${TSFILE_LIBRARY}") -else() - message(FATAL_ERROR "Could not find tsfile library") -endif() -target_link_libraries(cwrapper_example ${TSFILE_LIBRARY}) \ No newline at end of file diff --git a/cpp/examples/cpp_examples/demo_read.cpp b/cpp/examples/cpp_examples/demo_read.cpp new file mode 100644 index 000000000..79a1be99f --- /dev/null +++ b/cpp/examples/cpp_examples/demo_read.cpp @@ -0,0 +1,107 @@ +/* + * 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 +#include +#include +#include "common/path.h" +#include "reader/filter/filter.h" +#include "reader/expression.h" +#include "reader/tsfile_reader.h" +#include "reader/qds_with_timegenerator.h" +#include "reader/qds_without_timegenerator.h" +#include "common/row_record.h" + +std::string field_to_string(storage::Field *value) +{ + if (value->type_ == common::TEXT) { + return std::string(value->value_.sval_); + } else { + std::stringstream ss; + switch (value->type_) { + case common::BOOLEAN: + ss << (value->value_.bval_ ? "true" : "false"); + break; + case common::INT32: + ss << value->value_.ival_; + break; + case common::INT64: + ss << value->value_.lval_; + break; + case common::FLOAT: + ss << value->value_.fval_; + break; + case common::DOUBLE: + ss << value->value_.dval_; + break; + case common::NULL_TYPE: + ss << "NULL"; + break; + default: + ASSERT(false); + break; + } + return ss.str(); + } +} + +int main (int argc, char **argv) +{ + std::cout<<"begin to read tsfile from /tmp/t1" << std::endl; + std::string device_name = "root.db001.dev001"; + std::string measurement_name = "m001"; + storage::Path p1(device_name, measurement_name); + std::vector select_list; + select_list.push_back(p1); + storage::QueryExpression *query_expr = + storage::QueryExpression::create(select_list, nullptr); + + common::init_config_value(); + storage::TsFileReader reader; + int ret = reader.open("/tmp/t1"); + + std::cout << "begin to query expr" << std::endl; + ASSERT(ret == 0); + storage::QueryDataSet *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 { + record = qds->get_next(); + if (record) { + std::cout<< "dump QDS : " << record->get_timestamp() << ","; + 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); + + return (0); +} + diff --git a/cpp/examples/cpp_examples/demo_write.cpp b/cpp/examples/cpp_examples/demo_write.cpp new file mode 100644 index 000000000..047da96fc --- /dev/null +++ b/cpp/examples/cpp_examples/demo_write.cpp @@ -0,0 +1,59 @@ +/* + * 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 +#include +#include +#include "writer/tsfile_writer.h" +#include "common/record.h" +#include "common/db_common.h" + +int main (int argc, char **argv) +{ + std::cout<<"get in."<