Skip to content
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ jobs:

- name: Install native dependencies
if: matrix.language == 'go'
run: sudo apt-get update && sudo apt-get install -y uuid-dev
run: sudo apt-get update && sudo apt-get install -y uuid-dev libcurl4-openssl-dev

# The Go bridge needs headers and libtsfile under cpp/target/build.
- name: Build native library for Go
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/cpp-dependency-source.yml
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ jobs:
- name: Install bundled build prerequisites
run: |
sudo apt-get update
sudo apt-get install -y uuid-dev
sudo apt-get install -y uuid-dev libcurl4-openssl-dev

- name: Configure all dependencies from verified archives
shell: bash
Expand Down Expand Up @@ -240,7 +240,7 @@ jobs:
- name: Install offline build prerequisites
run: |
sudo apt-get update
sudo apt-get install -y uuid-dev
sudo apt-get install -y uuid-dev libcurl4-openssl-dev

- name: Configure all dependencies offline
shell: bash
Expand Down Expand Up @@ -280,7 +280,7 @@ jobs:
- name: Install SDK test prerequisites
run: |
sudo apt-get update
sudo apt-get install -y uuid-dev liblz4-dev pkg-config ninja-build
sudo apt-get install -y uuid-dev libcurl4-openssl-dev liblz4-dev pkg-config ninja-build

- name: Test installed CMake and pkg-config consumers and CLI RPATH
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/cppcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ jobs:
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y uuid-dev
sudo apt-get install -y uuid-dev libcurl4-openssl-dev

- name: Install Cppcheck
shell: bash
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/unit-test-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ jobs:
if [[ "$RUNNER_OS" == "Linux" ]]; then
if command -v apt-get >/dev/null 2>&1; then
sudo apt-get update
sudo apt-get install -y uuid-dev
sudo apt-get install -y uuid-dev libcurl4-openssl-dev
elif command -v yum >/dev/null 2>&1; then
sudo yum install -y libuuid-devel
sudo yum install -y libuuid-devel libcurl-devel
fi
python -m venv "$RUNNER_TEMP/clang-format"
"$RUNNER_TEMP/clang-format/bin/python" -m pip install --only-binary=:all: clang-format==17.0.6
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/unit-test-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ jobs:
"$RUNNER_TEMP/clang-format/bin/clang-format" --version
echo "$RUNNER_TEMP/clang-format/bin" >> "$GITHUB_PATH"
sudo apt-get update
sudo apt-get install -y uuid-dev
sudo apt-get install -y uuid-dev libcurl4-openssl-dev
elif [[ "$RUNNER_OS" == "Windows" ]]; then
choco install llvm --version 17.0.6 --force
else
Expand Down Expand Up @@ -176,7 +176,7 @@ jobs:
shell: bash
run: |
sudo apt-get update
sudo apt-get install -y uuid-dev binutils
sudo apt-get install -y uuid-dev libcurl4-openssl-dev binutils

- name: Build with ASan/LSan
shell: bash
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ python/tsfile/*.h
python/tsfile/*.cpp
python/tsfile/**/*.cpp
python/tsfile/**/*so*
!python/tsfile/python_reader_source.h
python/data
python/venv/*
python/tests/__pycache__/*
Expand Down
27 changes: 27 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,30 @@ if (ENABLE_SIMD)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/SIMDeDependency.cmake)
endif()

# Native S3/OSS object-storage reads need an HTTP client with TLS. libcurl is
# optional: when it is missing the Python and C++ APIs still exist but report
# that native object-storage support was not built. find_package(CURL) only
# defines the CURL::libcurl target on CMake 3.12 and newer, so the CMake 3.11
# baseline keeps the feature disabled rather than relying on a target that the
# link step cannot resolve.
option(ENABLE_S3 "Enable native S3/OSS reads when libcurl is available" ON)
message("cmake using: ENABLE_S3=${ENABLE_S3}")
set(TSFILE_S3_ENABLED OFF)
if (ENABLE_S3)
find_package(CURL 7.42 QUIET)
if (CURL_FOUND AND TARGET CURL::libcurl)
message(STATUS "native S3/OSS reads: enabled (libcurl ${CURL_VERSION_STRING})")
add_definitions(-DTSFILE_ENABLE_S3)
set(TSFILE_S3_ENABLED ON)
elseif (CURL_FOUND)
message(STATUS "native S3/OSS reads: disabled (CMake does not define CURL::libcurl)")
else ()
message(STATUS "native S3/OSS reads: disabled (libcurl not found)")
endif ()
else ()
message(STATUS "native S3/OSS reads: disabled by ENABLE_S3=OFF")
endif ()

# All libs will be stored here, including libtsfile, compress-encoding lib.
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

Expand Down Expand Up @@ -446,6 +470,9 @@ endif ()
if (ENABLE_SIMD)
list(APPEND _TSFILE_PROJECT_DEPENDENCIES TsFile::SIMDe)
endif ()
if (TSFILE_S3_ENABLED)
list(APPEND _TSFILE_PROJECT_DEPENDENCIES TsFile::CURL)
endif ()

if (NOT "${_TSFILE_PROJECT_DEPENDENCIES}" STREQUAL "")
get_property(_TSFILE_BASE_LINK_LIBRARIES
Expand Down
42 changes: 42 additions & 0 deletions cpp/README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,48 @@ C API 可通过 `tsfile_set_file_read_backend(TSFILE_READ_BACKEND_*)` 设置相
成功后会关闭原始文件描述符,映射本身会保持有效直至 reader 关闭。映射
reader 打开期间不得修改或截断文件。

### 对象存储读取后端

当以 `ENABLE_S3=ON`(默认)构建且 configure 阶段找到 libcurl 7.42 或更新版本时,SDK 可以直接
从 S3 兼容对象存储(AWS S3、阿里云 OSS、MinIO 等)读取 TsFile,内部通过带
SigV4 签名的 HTTP Range 请求完成。可用 `s3_support_available()` 判断当前构建
是否包含该能力。打开对象时需要强 ETag,每次 Range GET 都会携带对应的
`If-Match`。对象被覆盖后,请重新打开 reader 以读取新版本。缓存容量同时计入驻留块、正在传输的数据和活跃读取持有的块。预算用尽时,新的缓存未命中会等待空间。

```cpp
#include "file/cached_random_access_read_file.h"
#include "file/s3_random_access_read_file.h"
#include "reader/tsfile_reader.h"

storage::S3Options options;
options.endpoint = "https://s3.oss-cn-beijing.aliyuncs.com"; // 或 AWS/MinIO
options.path_style = false; // 阿里云 OSS 使用虚拟主机寻址
options.bucket = "bucket";
options.key = "path/example.tsfile";
options.access_key_id = "..."; // 两者都为空表示匿名访问
options.secret_access_key = "...";
options.region = "cn-beijing"; // 为空时从 endpoint 推导
std::string error;
std::unique_ptr<storage::RandomAccessReadFile> source =
storage::open_s3_read_file(options, &error);

storage::ReadCacheOptions cache;
cache.prefetch_threads = 4; // 后台按连续块组并发拉取
source = storage::make_cached_read_file(std::move(source), cache);

storage::TsFileReader reader;
reader.open(std::move(source));
```

每次范围读取都使用打开时取得的强 ETag。对于 HTTP 206,读取器会核对起止偏移和对象总大小。
若服务返回 HTTP 200,读取器跳过目标区间之前的字节,取得所需数据后即可结束传输。
使用 `ENABLE_THREADS=OFF` 构建时,预读同步执行。顺序预读按配置增长(最多额外 64 块),并受缓存容量和单次传输上限约束。
使用自定义端点或 S3 传输加速端点进行签名时,请显式指定存储桶所在区域。HTTP 408 和 429 可重试,其他 4xx 响应会立即结束该请求。

打开阶段的签名和 HTTP 错误通过 `error` 返回;`read()` 会按 `max_retries`
重试瞬时失败,重试耗尽后返回读取错误。块缓存层对远程源强烈建议启用;块大小、
缓存容量与预读量见 `ReadCacheOptions`。

---

## 使用 TsFile
Expand Down
47 changes: 47 additions & 0 deletions cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,53 @@ not be modified or truncated while a mapped reader is open. After a mapping is
created successfully, the original file descriptor is closed; the mapping
itself remains valid until the reader closes.

### Object Storage Read Backend

With `ENABLE_S3=ON` (the default) and libcurl 7.42 or newer available at configure time, the
SDK can read a TsFile directly from S3-compatible object storage (AWS S3,
Alibaba Cloud OSS, MinIO, ...) with signed HTTP range requests. Check
`s3_support_available()` when the build environment is unknown.

```cpp
#include "file/cached_random_access_read_file.h"
#include "file/s3_random_access_read_file.h"
#include "reader/tsfile_reader.h"

storage::S3Options options;
options.endpoint = "https://s3.oss-cn-beijing.aliyuncs.com"; // or AWS/MinIO
options.path_style = false; // Alibaba OSS uses virtual-hosted addressing
options.bucket = "bucket";
options.key = "path/example.tsfile";
options.access_key_id = "..."; // empty pair => anonymous access
options.secret_access_key = "...";
options.region = "cn-beijing"; // derived from the endpoint when empty
std::string error;
std::unique_ptr<storage::RandomAccessReadFile> source =
storage::open_s3_read_file(options, &error);

storage::ReadCacheOptions cache;
cache.prefetch_threads = 4; // background grouped range GETs
source = storage::make_cached_read_file(std::move(source), cache);

storage::TsFileReader reader;
reader.open(std::move(source));
```

Opening requires a strong ETag. Every range GET uses it with `If-Match`.
A 206 response must identify the requested offsets and object size in
`Content-Range`. For a 200 response the reader consumes the prefix through the
requested range and ends the transfer there. Single-thread builds perform
read-ahead synchronously. Reopen the reader to read a replacement object. The cache capacity includes in-flight range buffers and blocks held by active reads.
When the budget is full, a new cache miss waits for space. Sequential read-ahead grows up to the
configured limit (0–64 extra blocks), subject to capacity and transfer-size limits.

Opening reports signing failures and HTTP problems through `error`, and
`read()` retries transient failures `max_retries` times before returning a
read error. The block cache wrapper is optional but strongly recommended for
remote sources. Set the bucket's signing region explicitly for custom and S3 Transfer Acceleration endpoints. HTTP 408 and 429 are
retryable; other 4xx responses end the request immediately. See `ReadCacheOptions` for block size, capacity and
read-ahead.

## 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. The examples will be built automatically when you run the main build command.
Expand Down
11 changes: 11 additions & 0 deletions cpp/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ enable_lz4=ON
enable_lzokay=ON
enable_zlib=ON
enable_zstd=ON
enable_s3=ON
build_jobs=""
# Keep the default build compatible with CMake 3.11-era industrial toolchains.
# Bundled XZ Utils requires CMake 3.20+, so LZMA2 must be enabled explicitly.
Expand Down Expand Up @@ -92,6 +93,11 @@ Options:
--disable-zlib
--enable-zstd=<ON|OFF>
--disable-zstd
--enable-s3=<ON|OFF>
Enable native S3/OSS reads when libcurl is found
(default: ON; the feature stays off when libcurl is
unavailable).
--disable-s3
--enable-lzma2=<ON|OFF>
Enable LZMA2 explicitly (default: OFF; bundled XZ
requires CMake 3.20 or newer).
Expand Down Expand Up @@ -177,6 +183,8 @@ parse_options()
enable_zlib=$(get_key_value "$1");;
--enable-zstd=*)
enable_zstd=$(get_key_value "$1");;
--enable-s3=*)
enable_s3=$(get_key_value "$1");;
--enable-lzma2=*)
enable_lzma2=$(get_key_value "$1");;
--disable-antlr4)
Expand All @@ -191,6 +199,8 @@ parse_options()
enable_zlib=OFF;;
--disable-zstd)
enable_zstd=OFF;;
--disable-s3)
enable_s3=OFF;;
--disable-lzma2)
enable_lzma2=OFF;;
-h | --help)
Expand Down Expand Up @@ -270,6 +280,7 @@ cmake ../../ \
-DENABLE_LZOKAY=$enable_lzokay \
-DENABLE_ZLIB=$enable_zlib \
-DENABLE_ZSTD=$enable_zstd \
-DENABLE_S3=$enable_s3 \
-DENABLE_LZMA2=$enable_lzma2

VERBOSE=1 make -j"${build_jobs}"
Expand Down
10 changes: 10 additions & 0 deletions cpp/cmake/TsFileStaticDependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ tsfile_install_static_dependency(LZOKAY "${TSFILE_LZOKAY_SOURCE}" lzokay::lzokay
"find_dependency(lzokay ${TSFILE_LZOKAY_MIN_VERSION} CONFIG)")
tsfile_install_static_dependency(ZLIB "${TSFILE_ZLIB_SOURCE}" ZLIB::ZLIB
"find_dependency(ZLIB ${TSFILE_ZLIB_MIN_VERSION})")
if (TSFILE_S3_ENABLED)
tsfile_install_static_dependency(CURL "SYSTEM" CURL::libcurl
[=[find_dependency(CURL 7.42)
if (NOT TARGET CURL::libcurl)
add_library(CURL::libcurl INTERFACE IMPORTED)
set_target_properties(CURL::libcurl PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${CURL_INCLUDE_DIRS}"
INTERFACE_LINK_LIBRARIES "${CURL_LIBRARIES}")
endif ()]=])
endif ()
tsfile_install_static_dependency(ZSTD "${TSFILE_ZSTD_SOURCE}"
"${TSFILE_ZSTD_SYSTEM_TARGET}"
"find_dependency(zstd ${TSFILE_ZSTD_MIN_VERSION} CONFIG)")
Expand Down
8 changes: 7 additions & 1 deletion cpp/cmake/tests/projects/InstalledConsumer/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@
* under the License.
*/
#include <tsfile/cwrapper/tsfile_cwrapper.h>
#include <tsfile/file/s3_random_access_read_file.h>
#include <tsfile/reader/tsfile_reader.h>
#include <tsfile/writer/tsfile_table_writer.h>
#include <tsfile/writer/tsfile_tree_writer.h>

int main() {
if (set_global_time_encoding(TS_ENCODING_TS_2DIFF) != 0) return 1;
return get_global_time_encoding() == TS_ENCODING_TS_2DIFF ? 0 : 1;
if (get_global_time_encoding() != TS_ENCODING_TS_2DIFF) return 1;
// Referencing the object-storage entry point pulls its translation unit
// out of a static archive, so an installed static package must propagate
// the libcurl link dependency (TsFile::Static_CURL).
(void)storage::s3_support_available();
return 0;
}
2 changes: 2 additions & 0 deletions cpp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<enable.lzokay>ON</enable.lzokay>
<enable.zlib>ON</enable.zlib>
<enable.zstd>ON</enable.zstd>
<enable.s3>ON</enable.s3>
<!--
Keep Maven's default build compatible with CMake 3.11-era
industrial toolchains. Bundled XZ Utils requires CMake 3.20+;
Expand Down Expand Up @@ -137,6 +138,7 @@
<option>-DENABLE_LZOKAY=${enable.lzokay}</option>
<option>-DENABLE_ZLIB=${enable.zlib}</option>
<option>-DENABLE_ZSTD=${enable.zstd}</option>
<option>-DENABLE_S3=${enable.s3}</option>
<option>-DENABLE_LZMA2=${enable.lzma2}</option>
<option>-DENABLE_SIMD=${enable.simd}</option>
<option>-DTSFILE_ENABLE_CPACK=${enable.cpack}</option>
Expand Down
3 changes: 2 additions & 1 deletion cpp/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ set(_TSFILE_OBJECT_TARGETS
dataset_obj
file_obj
read_obj
utils_obj
write_obj)
if (ENABLE_ANTLR4)
list(APPEND _TSFILE_OBJECT_TARGETS parser_obj)
Expand Down Expand Up @@ -164,7 +165,7 @@ unset(_TSFILE_OBJECT_SOURCES)
unset(_TSFILE_OBJECT_TARGET)
unset(_TSFILE_OBJECT_TARGETS)

add_dependencies(tsfile utils_obj encoding_obj)
add_dependencies(tsfile encoding_obj)

set_property(DIRECTORY PROPERTY LINK_LIBRARIES
"${_TSFILE_SRC_LINK_LIBRARIES}")
Expand Down
14 changes: 14 additions & 0 deletions cpp/src/common/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ typedef struct ConfigValue {
// being reset by init_config_value(), so a setter called before
// libtsfile_init() is not clobbered.
bool allow_non_decreasing_timestamps_ = false;
// Block cache for non-local byte sources: a Python file object today, a
// native object-storage backend later. A source opened from a path never
// goes through this cache (pread/mmap plus the OS page cache is cheaper).
// Remote reads arrive as small, scattered page reads; serving them out of
// block-aligned cache entries with sequential read-ahead collapses a full
// scan into roughly file_size / block_size requests.
//
// Like thread_count_/sync_on_close_ these keep their in-class defaults
// instead of being reset by init_config_value(), so a setter called before
// the first reader is not clobbered by init-time defaults.
bool read_cache_enabled_ = true;
uint32_t read_cache_block_size_ = 256 * 1024;
uint32_t read_cache_capacity_ = 256 * 1024 * 1024;
uint32_t read_cache_max_readahead_blocks_ = 8;
} ConfigValue;

extern void init_config_value();
Expand Down
21 changes: 19 additions & 2 deletions cpp/src/encoding/gorilla_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,19 @@ struct GorillaBitReader {
}

FORCE_INLINE uint8_t read_control_bits(int max_bits) {
// Gorilla control words stop at the first zero bit. The two-bit form
// is the only one the decoder uses, and peeking both bits at once
// avoids two read_bit() refill checks for the common '10'/'11' case.
if (LIKELY(max_bits == 2 && bits >= 2 && !exhausted && !invalid)) {
bits -= 2;
const uint8_t pair =
static_cast<uint8_t>((buffer >> bits) & uint64_t{0x3});
if (LIKELY(pair >= 2)) {
return pair;
}
bits += 1; // the first bit was a lone zero
return 0;
}
uint8_t value = 0x00;
for (int i = 0; i < max_bits; i++) {
value <<= 1;
Expand Down Expand Up @@ -522,8 +535,12 @@ class GorillaDecoder : public Decoder {
// gives the compiler one contiguous range to vectorize. Integer
// and floating-point outputs are copied without arithmetic, so
// special IEEE-754 bit patterns remain unchanged.
std::fill_n(out + actual, run_length, decoded);
actual += run_length;
if (LIKELY(run_length == 1)) {
out[actual++] = decoded;
} else {
std::fill_n(out + actual, run_length, decoded);
actual += run_length;
}

// Prime the next value even when the repeated run exactly fills the
// caller's buffer. This preserves the scalar decoder invariant that
Expand Down
Loading
Loading