Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
5 changes: 4 additions & 1 deletion .github/workflows/unit-test-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,15 @@ jobs:
core.setOutput('platform_suffix', ``)
}

- name: Install clang-format
# Install dependencies
- name: Install dependencies
shell: bash
run: |
if [[ "$RUNNER_OS" == "Linux" ]]; then
sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-17 100
sudo update-alternatives --set clang-format /usr/bin/clang-format-17
sudo apt-get update
sudo apt-get install -y uuid-dev
elif [[ "$RUNNER_OS" == "Windows" ]]; then
choco install llvm --version 17.0.6 --force
else
Expand Down
17 changes: 17 additions & 0 deletions .github/workflows/unit-test-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ jobs:
core.setOutput('platform_suffix', ``)
}

# Install dependencies
- name: Install dependencies
shell: bash
run: |
if [[ "$RUNNER_OS" == "Linux" ]]; then
sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-17 100
sudo update-alternatives --set clang-format /usr/bin/clang-format-17
sudo apt-get update
sudo apt-get install -y uuid-dev
elif [[ "$RUNNER_OS" == "Windows" ]]; then
choco install llvm --version 17.0.6 --force
else
brew install llvm@17
ln -sf $(brew --prefix llvm@17)/bin/clang-format /opt/homebrew/bin/clang-format
fi


# Run the actual maven build including all tests.
- name: Build and test with Maven
shell: bash
Expand Down
3 changes: 1 addition & 2 deletions cpp/.clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: true
DisableFormat: false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: LogicalBlock
Expand Down Expand Up @@ -207,7 +206,7 @@ SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
BitFieldColonSpacing: Both
Standard: Auto
Standard: Cpp11
StatementAttributeLikeMacros:
- Q_EMIT
StatementMacros:
Expand Down
67 changes: 63 additions & 4 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,17 @@ under the License.
cmake_minimum_required(VERSION 3.11)
project(TsFile_CPP)

if (DEFINED ToolChain)
include(${CMAKE_SOURCE_DIR}/cmake/ToolChain.cmake)
message(STATUS "Using ToolChain: ${CMAKE_TOOLCHAIN_FILE}")
else()
message(STATUS "Not using ToolChain")
endif ()

cmake_policy(SET CMP0079 NEW)
set(TsFile_CPP_VERSION 2.2.0.dev)
set(CMAKE_CXX_FLAGS "$ENV{CXXFLAGS} -Wall")

if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wunused -Wuninitialized -D__STDC_FORMAT_MACROS")
endif ()
Expand Down Expand Up @@ -55,6 +63,18 @@ if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE)
endif ()

if (NOT DEFINED CMAKE_BUILD_PARALLEL_LEVEL)
include(ProcessorCount)
ProcessorCount(N)
if (N EQUAL 0)
set(N 1)
endif ()
set(CMAKE_BUILD_PARALLEL_LEVEL ${N} CACHE STRING "Number of parallel build jobs")
message("CMAKE BUILD PARALLEL LEVEL: ${CMAKE_BUILD_PARALLEL_LEVEL} (auto-detected)")
else ()
message("CMAKE BUILD PARALLEL LEVEL: ${CMAKE_BUILD_PARALLEL_LEVEL} (from environment)")
endif ()

message("CMAKE BUILD TYPE " ${CMAKE_BUILD_TYPE})
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g")
Expand Down Expand Up @@ -88,6 +108,40 @@ if (NOT WIN32)
endif ()
endif ()

option(BUILD_TEST "Build tests" ON)
message("cmake using: BUILD_TEST=${BUILD_TEST}")

option(ENABLE_ANTLR4 "Enable ANTLR4 runtime" ON)
message("cmake using: ENABLE_ANTLR4=${ENABLE_ANTLR4}")

option(ENABLE_SNAPPY "Enable Google Snappy compression" ON)
message("cmake using: ENABLE_SNAPPY=${ENABLE_SNAPPY}")

if (ENABLE_SNAPPY)
add_definitions(-DENABLE_SNAPPY)
endif()

option(ENABLE_LZ4 "Enable LZ4 compression" ON)
message("cmake using: ENABLE_LZ4=${ENABLE_LZ4}")

if (ENABLE_LZ4)
add_definitions(-DENABLE_LZ4)
endif()

option(ENABLE_LZOKAY "Enable LZOKAY compression" ON)
message("cmake using: ENABLE_LZOKAY=${ENABLE_LZOKAY}")

if (ENABLE_LZOKAY)
add_definitions(-DENABLE_LZOKAY)
endif()

option(ENABLE_ZLIB "Enable Zlib compression" ON)
message("cmake using: ENABLE_ZLIB=${ENABLE_ZLIB}")

if (ENABLE_ZLIB)
add_definitions(-DENABLE_ZLIB)
add_definitions(-DENABLE_GZIP)
endif()

# All libs will be stored here, including libtsfile, compress-encoding lib.
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
Expand All @@ -106,9 +160,14 @@ add_subdirectory(third_party)
set(CMAKE_CXX_FLAGS "${SAVED_CXX_FLAGS}")

add_subdirectory(src)
add_subdirectory(test)
add_subdirectory(examples)
if (TESTS_ENABLED)
add_dependencies(TsFile_Test tsfile)
if (BUILD_TEST)
add_subdirectory(test)
if (TESTS_ENABLED)
add_dependencies(TsFile_Test tsfile)
endif ()
else()
message("BUILD_TEST is OFF, skipping test directory")
endif ()

add_subdirectory(examples)

34 changes: 16 additions & 18 deletions cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,6 @@ The source code can be found in the `./src` directory. C/C++ examples are locate

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

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
Expand All @@ -67,7 +50,7 @@ We welcome any bug reports. You can open an issue with a title starting with [CP

```bash
sudo apt-get update
sudo apt-get install -y cmake make g++ clang-format
sudo apt-get install -y cmake make g++ clang-format libuuid-dev
```

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`.
Expand All @@ -81,6 +64,21 @@ If you compile using MinGW on windows and encounter an error, you can try replac
* GCC 12.2.0 + LLVM/Clang/LLD/LLDB 16.0.0 + MinGW-w64 10.0.0 (MSVCRT) - release 5
* GCC 11.2.0 + MinGW-w64 10.0.0 (MSVCRT) - release 1

### configure the cross-compilation toolchain

Modify the Toolchain File `cmake/ToolChain.cmake`, define the following variables:

- `CMAKE_C_COMPILER`: Specify the path to the C compiler.
- `CMAKE_CXX_COMPILER`: Specify the path to the C++ compiler.
- `CMAKE_FIND_ROOT_PATH`: Set the root path for the cross-compilation environment (e.g., the directory of the cross-compilation toolchain).

In the `cpp/` directory, run the following commands to create the build directory and start the compilation:
```
mkdir build && cd build
cmake .. -DToolChian=ON
make
```

## Use TsFile

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. You can run `bash build.sh` under `./examples` to generate an executable output under `./examples/build`.
66 changes: 50 additions & 16 deletions cpp/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,38 @@ use_cpp11=1
enable_cov=0
debug_se=0
run_cov_only=0
enable_antlr4=ON

enable_snappy=ON
enable_lz4=ON
enable_lzokay=ON
enable_zlib=ON

shell_dir=$(cd "$(dirname "$0")";pwd)

# 添加get_key_value函数
get_key_value() {
echo "${1#*=}"
}

function print_config()
{
echo "build_type=$build_type"
echo "build_test=$build_test"
echo "use_cpp11=$use_cpp11"
echo "enable_cov=$enable_cov"
echo "enable_asan=$enable_asan"
echo "enable_antlr4=$enable_antlr4"
echo "enable_snappy=$enable_snappy"
echo "enable_lz4=$enable_lz4"
echo "enable_lzokay=$enable_lzokay"
echo "enable_zlib=$enable_zlib"
}

function run_test_for_cov()
{
# sh ${shell_dir}/scripts/regression_unittest.sh
sh ${shell_dir}/test/libtsfile_test/run_sdk_tests.sh
sh ${shell_dir}/test/libtsfile_test/run_sdk_tests.sh
}

parse_options()
Expand All @@ -53,20 +69,40 @@ parse_options()
run_cov)
run_cov_only=1;;
-t=*)
build_type=`get_key_value "$1"`;;
build_type=$(get_key_value "$1");;
-t)
shift
build_type=`get_key_value "$1"`;;
build_type=$(get_key_value "$1");;
-a=*)
enable_asan=`get_key_value "$1"`;;
enable_asan=$(get_key_value "$1");;
-a)
shift
enable_asan=`get_key_value "$1"`;;
enable_asan=$(get_key_value "$1");;
-c=*)
enable_cov=`get_key_value "$1"`;;
enable_cov=$(get_key_value "$1");;
-c)
shift
enable_cov=`get_key_value "$1"`;;
enable_cov=$(get_key_value "$1");;
--enable-antlr4=*)
enable_antlr4=$(get_key_value "$1");;
--enable-snappy=*)
enable_snappy=$(get_key_value "$1");;
--enable-lz4=*)
enable_lz4=$(get_key_value "$1");;
--enable-lzokay=*)
enable_lzokay=$(get_key_value "$1");;
--enable-zlib=*)
enable_zlib=$(get_key_value "$1");;
--disable-antlr4)
enable_antlr4=OFF;;
--disable-snappy)
enable_snappy=OFF;;
--disable-lz4)
enable_lz4=OFF;;
--disable-lzokay)
enable_lzokay=OFF;;
--disable-zlib)
enable_zlib=OFF;;
#-h | --help)
# usage
# exit 0;;
Expand Down Expand Up @@ -127,14 +163,12 @@ cmake ../../ \
-DUSE_CPP11=$use_cpp11 \
-DENABLE_COV=$enable_cov \
-DDEBUG_SE=$debug_se \
-DBUILD_TSFILE_ONLY=$build_tsfile_only
-DENABLE_ANTLR4=$enable_antlr4 \
-DBUILD_TSFILE_ONLY=$build_tsfile_only \
-DENABLE_SNAPPY=$enable_snappy \
-DENABLE_LZ4=$enable_lz4 \
-DENABLE_LZOKAY=$enable_lzokay \
-DENABLE_ZLIB=$enable_zlib

VERBOSE=1 make
VERBOSE=1 make install







VERBOSE=1 make install
14 changes: 12 additions & 2 deletions cpp/examples/c_examples/demo_write.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
// This example shows you how to write tsfile.
ERRNO write_tsfile() {
ERRNO code = 0;
code = set_global_compression(TS_COMPRESSION_LZ4);
if (code != RET_OK) {
return code;
}
code = set_datatype_encoding(TS_DATATYPE_INT32, TS_ENCODING_TS_2DIFF);
if (code != RET_OK) {
return code;
}
char* table_name = "table1";

// Create table schema to describe a table in a tsfile.
Expand Down Expand Up @@ -67,8 +75,10 @@ ERRNO write_tsfile() {
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_string_with_len(
tablet, row, "id1", "id_field_1", strlen("id_field_1"));
tablet_add_value_by_name_string_with_len(
tablet, row, "id2", "id_field_2", strlen("id_field_2"));
tablet_add_value_by_name_int32_t(tablet, row, "s1", row);
}

Expand Down
1 change: 1 addition & 0 deletions cpp/examples/cpp_examples/cpp_examples.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "file/write_file.h"
#include "reader/expression.h"
#include "reader/filter/filter.h"
#include "reader/filter/tag_filter.h"
#include "reader/qds_with_timegenerator.h"
#include "reader/qds_without_timegenerator.h"
#include "reader/tsfile_reader.h"
Expand Down
12 changes: 11 additions & 1 deletion cpp/examples/cpp_examples/demo_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,16 @@ int demo_read() {
columns.emplace_back("id2");
columns.emplace_back("s1");

auto table_schema = reader.get_table_schema(table_name);
storage::Filter* tag_filter1 =
storage::TagFilterBuilder(table_schema.get()).eq("id1", "id1_filed_1");
storage::Filter* tag_filter2 =
storage::TagFilterBuilder(table_schema.get()).eq("id2", "id1_filed_2");
storage::Filter* tag_filter = storage::TagFilterBuilder(table_schema.get())
.and_filter(tag_filter1, tag_filter2);
// Column vector contains the columns you want to select.
HANDLE_ERROR(reader.query(table_name, columns, 0, 100, temp_ret));
HANDLE_ERROR(
reader.query(table_name, columns, 0, 100, temp_ret, tag_filter));

// Get query handler.
auto ret = dynamic_cast<storage::TableResultSet*>(temp_ret);
Expand All @@ -61,6 +69,7 @@ int demo_read() {
while ((code = ret->next(has_next)) == common::E_OK && has_next) {
// Timestamp at column 1 and column index begin from 1.
Timestamp timestamp = ret->get_value<Timestamp>(1);
std::cout << timestamp << std::endl;
for (int i = 1; i <= column_num; i++) {
if (ret->is_null(i)) {
std::cout << "null" << std::endl;
Expand Down Expand Up @@ -97,5 +106,6 @@ int demo_read() {

// Close reader.
reader.close();
delete tag_filter;
return 0;
}
Loading
Loading