diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 75684ce0c..b9d93b9af 100755 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -23,7 +23,7 @@ cmake_policy(SET CMP0079 NEW) set(TsFile_CPP_VERSION 2.1.0.dev) set(CMAKE_CXX_FLAGS "$ENV{CXXFLAGS} -Wall") if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=maybe-uninitialized -D__STDC_FORMAT_MACROS") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__STDC_FORMAT_MACROS") endif() message("cmake using: USE_CPP11=${USE_CPP11}") @@ -51,9 +51,7 @@ if (${COV_ENABLED}) endif() -if (NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE) -endif () +set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE) message("CMAKE BUILD TYPE " ${CMAKE_BUILD_TYPE}) if (CMAKE_BUILD_TYPE STREQUAL "Debug") diff --git a/cpp/src/CMakeLists.txt b/cpp/src/CMakeLists.txt index 86c67f2b4..3cc3c6dd7 100644 --- a/cpp/src/CMakeLists.txt +++ b/cpp/src/CMakeLists.txt @@ -31,6 +31,7 @@ if (${COV_ENABLED}) endif () add_definitions(-DANTLR4CPP_STATIC) set(ANTLR4_WITH_STATIC_CRT OFF) +add_compile_options(-Wall -Wextra -Wconversion -Wsign-conversion) set(PROJECT_INCLUDE_DIR @@ -79,6 +80,7 @@ set(LIBTSFILE_PROJECT_VERSION ${TsFile_CPP_VERSION}) set(LIBTSFILE_SO_VERSION ${TsFile_CPP_VERSION}) set_target_properties(tsfile PROPERTIES VERSION ${LIBTSFILE_PROJECT_VERSION}) set_target_properties(tsfile PROPERTIES SOVERSION ${LIBTSFILE_SO_VERSION}) +target_compile_options(tsfile PRIVATE -Wall -Wextra -Werror -Wconversion -Wsign-conversion) install(TARGETS tsfile LIBRARY DESTINATION ${LIBRARY_OUTPUT_PATH}) diff --git a/cpp/src/common/CMakeLists.txt b/cpp/src/common/CMakeLists.txt index 7ac55ab5c..7304f707b 100644 --- a/cpp/src/common/CMakeLists.txt +++ b/cpp/src/common/CMakeLists.txt @@ -24,15 +24,20 @@ aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/container common_container_SRC_ aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/tsblock common_tsblock_SRC_LIST) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/mutex common_mutex_SRC_LIST) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/datatype common_datatype_SRC_LIST) +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/error_info common_error_info_SRC_LIST) set(CMAKE_POSITION_INDEPENDENT_CODE ON) -add_library(common_obj OBJECT ${common_SRC_LIST} - ${common_allocator_SRC_LIST} - ${common_container_SRC_LIST} - ${common_tsblock_SRC_LIST} - ${common_mutex_SRC_LIST} - ${common_datatype_SRC_LIST}) +add_library(common_obj OBJECT ${common_SRC_LIST} + ${common_allocator_SRC_LIST} + ${common_container_SRC_LIST} + ${common_tsblock_SRC_LIST} + ${common_mutex_SRC_LIST} + ${common_datatype_SRC_LIST} + ${common_error_info_SRC_LIST} +) # install header files recursively -file(GLOB_RECURSE HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/*.h") +file(GLOB_RECURSE HEADER_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.h") +file(GLOB_RECURSE INC_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.inc") +set(HEADERS ${HEADER_FILES} ${INC_FILES}) copy_to_dir(${HEADERS} "common_obj") \ No newline at end of file diff --git a/cpp/src/common/allocator/alloc_base.h b/cpp/src/common/allocator/alloc_base.h index e9824b11e..2cca8e376 100644 --- a/cpp/src/common/allocator/alloc_base.h +++ b/cpp/src/common/allocator/alloc_base.h @@ -75,56 +75,15 @@ enum AllocModID { extern const char *g_mod_names[__LAST_MOD_ID]; /* very basic alloc/free interface in C style */ -void *mem_alloc(uint32_t size, AllocModID mid); +void *mem_alloc(size_t size, AllocModID mid); void mem_free(void *ptr); -void *mem_realloc(void *ptr, uint32_t size); - -class ModStat { - public: - ModStat() : stat_arr_(NULL) {} - - static ModStat &get_instance() { - /* - * This is the singleton of Mod Memory Statistic. - * gms is short for Global Mod Statistic - */ - static ModStat gms; - return gms; - } - void init(); - void destroy(); - INLINE void update_alloc(AllocModID mid, int32_t size) { - // ASSERT(mid < __LAST_MOD_ID); - // ATOMIC_FAA(get_item(mid), size); - } - void update_free(AllocModID mid, uint32_t size) { - // ASSERT(mid < __LAST_MOD_ID); - // ATOMIC_FAA(get_item(mid), 0 - size); - } - void print_stat(); - -#ifdef ENABLE_TEST - int32_t TEST_get_stat(int8_t mid) { return ATOMIC_FAA(get_item(mid), 0); } -#endif - - private: - INLINE int32_t *get_item(int8_t mid) { - return &(stat_arr_[mid * (ITEM_SIZE / sizeof(int32_t))]); - } - - private: - static const int32_t ITEM_SIZE = CACHE_LINE_SIZE; - static const int32_t ITEM_COUNT = __LAST_MOD_ID; - int32_t *stat_arr_; - - STATIC_ASSERT((ITEM_SIZE % sizeof(int32_t) == 0), ModStat_ITEM_SIZE_ERROR); -}; +void *mem_realloc(void *ptr, size_t size); /* base allocator */ class BaseAllocator { public: - void *alloc(uint32_t size, AllocModID mid) { return mem_alloc(size, mid); } - void free(void *ptr) { mem_free(ptr); } + static void *alloc(const size_t size, const AllocModID mid) { return mem_alloc(size, mid); } + static void free(void *ptr) { mem_free(ptr); } }; extern BaseAllocator g_base_allocator; diff --git a/cpp/src/common/allocator/byte_stream.h b/cpp/src/common/allocator/byte_stream.h index 47c5148f3..52d91851f 100644 --- a/cpp/src/common/allocator/byte_stream.h +++ b/cpp/src/common/allocator/byte_stream.h @@ -29,10 +29,12 @@ #include "common/allocator/alloc_base.h" #include "common/allocator/my_string.h" -#include "utils/errno_define.h" +#include "common/error_info/errno_define.h" namespace common { +using E_CODE = error_info::E_CODE; + template class OptionalAtomic { public: @@ -287,9 +289,9 @@ class ByteStream { /* ================ Part 0: wrap from outside buffer ================ */ // if you wrap a buffer as a ByteStream, you should // manage the outside buffer yourself. - void wrap_from(const char *buf, int32_t buf_len) { + void wrap_from(char *buf, const size_t buf_len) { wrapped_page_.next_.store(nullptr); - wrapped_page_.buf_ = (uint8_t *)buf; + wrapped_page_.buf_ = reinterpret_cast(buf); page_size_ = buf_len; head_.store(&wrapped_page_); @@ -302,18 +304,18 @@ class ByteStream { FORCE_INLINE bool is_wrapped() const { return head_.load() == &wrapped_page_; } - char *get_wrapped_buf() { return (char *)wrapped_page_.buf_; } + char *get_wrapped_buf() const { return (char *)wrapped_page_.buf_; } void clear_wrapped_buf() { wrapped_page_.buf_ = nullptr; } /* ================ Part 1: basic ================ */ - FORCE_INLINE uint32_t remaining_size() const { + FORCE_INLINE size_t remaining_size() const { ASSERT(total_size_.load() >= read_pos_); return total_size_.load() - read_pos_; } FORCE_INLINE bool has_remaining() const { return remaining_size() > 0; } FORCE_INLINE void mark_read_pos() { marked_read_pos_ = read_pos_; } - FORCE_INLINE uint32_t get_mark_len() const { + FORCE_INLINE size_t get_mark_len() const { ASSERT(marked_read_pos_ <= read_pos_); return read_pos_ - marked_read_pos_; } @@ -346,9 +348,9 @@ class ByteStream { this->total_size_.store(other.total_size_.load()); } - FORCE_INLINE uint32_t total_size() const { return total_size_.load(); } - FORCE_INLINE uint32_t read_pos() const { return read_pos_; }; - FORCE_INLINE void wrapped_buf_advance_read_pos(uint32_t size) { + FORCE_INLINE size_t total_size() const { return total_size_.load(); } + FORCE_INLINE size_t read_pos() const { return read_pos_; }; + FORCE_INLINE void wrapped_buf_advance_read_pos(size_t size) { if (size + read_pos_ > total_size_.load()) { read_pos_ = total_size_.load(); } else { @@ -358,16 +360,16 @@ class ByteStream { /* ================ Part 2: write_xxx and read_xxx ================ */ // writer @buf with length @len into this bytestream - int write_buf(const uint8_t *buf, const uint32_t len) { - int ret = common::E_OK; - uint32_t write_len = 0; + E_CODE write_buf(const uint8_t *buf, const size_t len) { + E_CODE ret = error_info::E_OK; + size_t write_len = 0; while (write_len < len) { if (RET_FAIL(prepare_space())) { - std::cout << "write_buf error " << ret << std::endl; - return ret; + RETURN_ERR(error_info::E_OOM, "prepare spec failed"); } - uint32_t remainder = page_size_ - (total_size_.load() % page_size_); - uint32_t copy_len = + const size_t remainder = + page_size_ - (total_size_.load() % page_size_); + const size_t copy_len = remainder < (len - write_len) ? remainder : (len - write_len); memcpy(tail_.load()->buf_ + total_size_.load() % page_size_, buf + write_len, copy_len); @@ -380,39 +382,34 @@ class ByteStream { // reader @want_len bytes to @buf, @read_len indicates real len we reader. // if ByteStream do not have so many bytes, it will return E_PARTIAL_READ if // no other error occure. - int read_buf(uint8_t *buf, const uint32_t want_len, uint32_t &read_len) { - int ret = common::E_OK; + E_CODE read_buf(uint8_t *buf, const size_t want_len, size_t &read_len) { + E_CODE ret = E_CODE::E_OK; bool partial_read = (read_pos_ + want_len > total_size_.load()); - uint32_t want_len_limited = + const size_t want_len_limited = partial_read ? (total_size_.load() - read_pos_) : want_len; read_len = 0; while (read_len < want_len_limited) { if (RET_FAIL(check_space())) { return ret; } - uint32_t remainder = page_size_ - (read_pos_ % page_size_); - uint32_t copy_len = remainder < want_len_limited - read_len - ? remainder - : want_len_limited - read_len; + const size_t remainder = page_size_ - (read_pos_ % page_size_); + const size_t copy_len = remainder < want_len_limited - read_len + ? remainder + : want_len_limited - read_len; memcpy(buf + read_len, read_page_->buf_ + (read_pos_ % page_size_), copy_len); read_len += copy_len; read_pos_ += copy_len; } - return partial_read ? common::E_PARTIAL_READ : common::E_OK; + return partial_read ? E_CODE::E_PARTIAL_READ : E_CODE::E_OK; } - FORCE_INLINE int write_buf(const char *buf, const uint32_t len) { - return write_buf((const uint8_t *)buf, len); + FORCE_INLINE E_CODE write_buf(const char *buf, const size_t len) { + return write_buf(reinterpret_cast(buf), len); } - FORCE_INLINE int read_buf(char *buf, const uint32_t want_len, - uint32_t &read_len) { - return read_buf((uint8_t *)buf, want_len, read_len); - } - FORCE_INLINE int read_buf(char *buf, const int32_t want_len, - int32_t &read_len) { - return read_buf((uint8_t *)buf, (uint32_t &)want_len, - (uint32_t &)read_len); + FORCE_INLINE E_CODE read_buf(char *buf, const size_t want_len, + size_t &read_len) { + return read_buf(reinterpret_cast(buf), want_len, read_len); } void purge_prev_pages(int purge_page_count = INT32_MAX) { @@ -435,18 +432,18 @@ class ByteStream { */ struct Buffer { char *buf_; - uint32_t len_; + size_t len_; Buffer() : buf_(nullptr), len_(0) {} }; Buffer acquire_buf() { Buffer b; - if (common::E_OK != prepare_space()) { + if (error_info::E_OK != prepare_space()) { return b; } - b.buf_ = - (char *)(tail_.load()->buf_ + (total_size_.load() % page_size_)); + b.buf_ = reinterpret_cast(tail_.load()->buf_ + + (total_size_.load() % page_size_)); b.len_ = page_size_ - (total_size_.load() % page_size_); return b; } @@ -468,7 +465,7 @@ class ByteStream { const ByteStream &host_; Page *cur_; Page *end_; - int64_t total_size_; + size_t total_size_; BufferIterator(const ByteStream &bs) : host_(bs) { cur_ = bs.head_.load(); end_ = bs.tail_.load(); @@ -503,8 +500,8 @@ class ByteStream { struct Consumer { const ByteStream &host_; Page *cur_; - uint32_t read_offset_within_cur_page_; - int64_t total_end_offset_; // for DEBUG + size_t read_offset_within_cur_page_; + size_t total_end_offset_; // for DEBUG Consumer(const ByteStream &bs) : host_(bs) { ASSERT(bs.head_.enable_atomic()); @@ -527,7 +524,7 @@ class ByteStream { // get tail position atomically Page *host_end = nullptr; - uint32_t host_total_size = 0; + size_t host_total_size = 0; while (true) { host_end = host_.tail_.load(); host_total_size = host_.total_size_.load(); @@ -555,7 +552,7 @@ class ByteStream { (host_total_size % host_.page_size_)) { return b; } else { - b.buf_ = ((char *)(cur_->buf_)) + + b.buf_ = reinterpret_cast(cur_->buf_) + read_offset_within_cur_page_; b.len_ = (host_total_size % host_.page_size_) - read_offset_within_cur_page_; @@ -587,8 +584,8 @@ class ByteStream { }; private: - FORCE_INLINE int prepare_space() { - int ret = common::E_OK; + FORCE_INLINE E_CODE prepare_space() { + E_CODE ret = error_info::E_OK; if (UNLIKELY(tail_.load() == nullptr || total_size_.load() % page_size_ == 0)) { Page *p = nullptr; @@ -601,9 +598,10 @@ class ByteStream { return ret; } - FORCE_INLINE int check_space() { + FORCE_INLINE E_CODE check_space() { if (UNLIKELY(read_pos_ >= total_size_.load())) { - return common::E_OUT_OF_RANGE; + RETURN_ERR(E_CODE::E_PARTIAL_READ, + "read pos is larger than totalsize") } if (UNLIKELY(read_page_ == nullptr)) { read_page_ = head_.load(); @@ -611,29 +609,28 @@ class ByteStream { read_page_ = read_page_->next_.load(); } if (UNLIKELY(read_page_ == nullptr)) { - return common::E_OUT_OF_RANGE; + RETURN_ERR(E_CODE::E_OUT_OF_RANGE, "current read page is null") } - return common::E_OK; + return E_CODE::E_OK; } - FORCE_INLINE int alloc_page(Page *&p) { - int ret = common::E_OK; - char *buf = (char *)allocator_.alloc(page_size_ + sizeof(Page), mid_); + FORCE_INLINE E_CODE alloc_page(Page *&p) { + char *buf = static_cast( + common::BaseAllocator::alloc(page_size_ + sizeof(Page), mid_)); if (UNLIKELY(buf == nullptr)) { - ret = common::E_OOM; + RETURN_ERR(E_CODE::E_OOM, "allocate page failed") + } + p = new (buf) Page(head_.enable_atomic()); + p->next_.store(nullptr); + if (head_.load()) { + tail_.load()->next_.store(p); + tail_.store(p); } else { - p = new (buf) Page(head_.enable_atomic()); - p->next_.store(nullptr); - if (head_.load()) { - tail_.load()->next_.store(p); - tail_.store(p); - } else { - head_.store(p); - tail_.store(p); - } + head_.store(p); + tail_.store(p); } // printf("\nByteStream alloc_page, this=%p, new_page=%p\n", this, p); - return ret; + return E_CODE::E_OK; } DISALLOW_COPY_AND_ASSIGN(ByteStream); @@ -643,17 +640,17 @@ class ByteStream { OptionalAtomic head_; OptionalAtomic tail_; Page *read_page_; // only one thread is allow to reader this ByteStream - OptionalAtomic total_size_; // total size in byte - uint32_t read_pos_; // current reader position - uint32_t marked_read_pos_; // current reader position - uint32_t page_size_; + OptionalAtomic total_size_; // total size in byte + size_t read_pos_; // current reader position + size_t marked_read_pos_; // current reader position + size_t page_size_; AllocModID mid_; Page wrapped_page_; }; FORCE_INLINE int merge_byte_stream(ByteStream &sea, ByteStream &river, bool purge_river = false) { - int ret = common::E_OK; + int ret = error_info::E_OK; ByteStream::BufferIterator buf_iter = river.init_buffer_iterator(); while (true) { ByteStream::Buffer buf = buf_iter.get_next_buf(); @@ -671,23 +668,23 @@ FORCE_INLINE int merge_byte_stream(ByteStream &sea, ByteStream &river, return ret; } -FORCE_INLINE int copy_bs_to_buf(ByteStream &bs, char *src_buf, - uint32_t src_buf_len) { +FORCE_INLINE E_CODE copy_bs_to_buf(ByteStream &bs, char *src_buf, + const size_t src_buf_len) { ByteStream::BufferIterator buf_iter = bs.init_buffer_iterator(); - uint32_t copyed_len = 0; + size_t copied_len = 0; while (true) { ByteStream::Buffer buf = buf_iter.get_next_buf(); if (buf.buf_ == nullptr) { break; } else { - if (src_buf_len - copyed_len < buf.len_) { - return E_BUF_NOT_ENOUGH; + if (src_buf_len - copied_len < buf.len_) { + RETURN_ERR(E_CODE::E_BUF_NOT_ENOUGH, "buffer not enough") } - memcpy(src_buf + copyed_len, buf.buf_, buf.len_); - copyed_len += buf.len_; + memcpy(src_buf + copied_len, buf.buf_, buf.len_); + copied_len += buf.len_; } } - return E_OK; + return E_CODE::E_OK; } FORCE_INLINE uint32_t get_var_uint_size( @@ -743,61 +740,61 @@ FORCE_INLINE void DEBUG_hex_dump_buf(const char *print_tag, const char *buf, class SerializationUtil { public: - FORCE_INLINE static int write_ui8(uint8_t ui8, ByteStream &out) { + FORCE_INLINE static E_CODE write_ui8(uint8_t ui8, ByteStream &out) { return out.write_buf(&ui8, 1); } - FORCE_INLINE static int write_ui16(uint16_t ui16, ByteStream &out) { + FORCE_INLINE static E_CODE write_ui16(uint16_t ui16, ByteStream &out) { uint8_t buf[2]; - buf[0] = (uint8_t)((ui16 >> 8) & 0xFF); - buf[1] = (uint8_t)((ui16) & 0xFF); + buf[0] = static_cast((ui16 >> 8) & 0xFF); + buf[1] = static_cast((ui16) & 0xFF); return out.write_buf(buf, 2); } - FORCE_INLINE static int write_ui32(uint32_t ui32, ByteStream &out) { + FORCE_INLINE static E_CODE write_ui32(uint32_t ui32, ByteStream &out) { uint8_t buf[4]; - buf[0] = (uint8_t)((ui32 >> 24) & 0xFF); - buf[1] = (uint8_t)((ui32 >> 16) & 0xFF); - buf[2] = (uint8_t)((ui32 >> 8) & 0xFF); - buf[3] = (uint8_t)((ui32) & 0xFF); + buf[0] = static_cast((ui32 >> 24) & 0xFF); + buf[1] = static_cast((ui32 >> 16) & 0xFF); + buf[2] = static_cast((ui32 >> 8) & 0xFF); + buf[3] = static_cast((ui32) & 0xFF); return out.write_buf(buf, 4); } - FORCE_INLINE static int write_ui64(uint64_t ui64, ByteStream &out) { + FORCE_INLINE static E_CODE write_ui64(uint64_t ui64, ByteStream &out) { // big-endian: most signification byte at smaller address // refer to tsfile.utils.BytesUtil uint8_t buf[8]; - buf[0] = (uint8_t)((ui64 >> 56) & 0xFF); - buf[1] = (uint8_t)((ui64 >> 48) & 0xFF); - buf[2] = (uint8_t)((ui64 >> 40) & 0xFF); - buf[3] = (uint8_t)((ui64 >> 32) & 0xFF); - buf[4] = (uint8_t)((ui64 >> 24) & 0xFF); - buf[5] = (uint8_t)((ui64 >> 16) & 0xFF); - buf[6] = (uint8_t)((ui64 >> 8) & 0xFF); - buf[7] = (uint8_t)((ui64) & 0xFF); + buf[0] = static_cast((ui64 >> 56) & 0xFF); + buf[1] = static_cast((ui64 >> 48) & 0xFF); + buf[2] = static_cast((ui64 >> 40) & 0xFF); + buf[3] = static_cast((ui64 >> 32) & 0xFF); + buf[4] = static_cast((ui64 >> 24) & 0xFF); + buf[5] = static_cast((ui64 >> 16) & 0xFF); + buf[6] = static_cast((ui64 >> 8) & 0xFF); + buf[7] = static_cast((ui64) & 0xFF); return out.write_buf(buf, 8); } - FORCE_INLINE static int read_ui8(uint8_t &ui8, ByteStream &in) { - int ret = common::E_OK; + FORCE_INLINE static E_CODE read_ui8(uint8_t &ui8, ByteStream &in) { + E_CODE ret = E_CODE::E_OK; char buf[1]; - uint32_t read_len = 0; + size_t read_len = 0; ret = in.read_buf(buf, 1, read_len); - ui8 = (uint8_t)buf[0]; + ui8 = static_cast(buf[0]); return ret; } - FORCE_INLINE static int read_ui16(uint16_t &ui16, ByteStream &in) { - int ret = common::E_OK; + FORCE_INLINE static E_CODE read_ui16(uint16_t &ui16, ByteStream &in) { + E_CODE ret = E_CODE::E_OK; uint8_t buf[2]; - uint32_t read_len = 0; + size_t read_len = 0; if (RET_FAIL(in.read_buf(buf, 2, read_len))) { return ret; } ui16 = buf[0]; - ui16 = (ui16 << 8) | buf[1]; + ui16 = static_cast((ui16 << 8) | buf[1]); return ret; } - FORCE_INLINE static int read_ui32(uint32_t &ui32, ByteStream &in) { - int ret = common::E_OK; + FORCE_INLINE static E_CODE read_ui32(uint32_t &ui32, ByteStream &in) { + E_CODE ret = E_CODE::E_OK; uint8_t buf[4]; - uint32_t read_len = 0; + size_t read_len = 0; if (RET_FAIL(in.read_buf(buf, 4, read_len))) { return ret; } @@ -807,10 +804,10 @@ class SerializationUtil { ui32 = (ui32 << 8) | (buf[3] & 0xFF); return ret; } - FORCE_INLINE static int read_ui64(uint64_t &ui64, ByteStream &in) { - int ret = common::E_OK; + FORCE_INLINE static E_CODE read_ui64(uint64_t &ui64, ByteStream &in) { + E_CODE ret = error_info::E_OK; uint8_t buf[8]; - uint32_t read_len = 0; + size_t read_len = 0; if (RET_FAIL(in.read_buf(buf, 8, read_len))) { return ret; } @@ -826,19 +823,19 @@ class SerializationUtil { } // caller guarantee buffer has at least 1 byte FORCE_INLINE static uint8_t read_ui8(char *buffer) { - return *(uint8_t *)buffer; + return *reinterpret_cast(buffer); } // caller guarantee buffer has at least 2 bytes FORCE_INLINE static uint16_t read_ui16(char *buffer) { - uint8_t *buf = (uint8_t *)buffer; + uint8_t *buf = reinterpret_cast(buffer); uint16_t ui16 = buf[0]; - ui16 = (ui16 << 8) | (buf[1] & 0xFF); + ui16 = static_cast((ui16 << 8) | (buf[1] & 0xFF)); return ui16; } // caller guarantee buffer has at least 4 bytes FORCE_INLINE static uint32_t read_ui32(char *buffer) { - uint8_t *buf = (uint8_t *)buffer; + uint8_t *buf = reinterpret_cast(buffer); uint32_t ui32 = buf[0]; ui32 = (ui32 << 8) | (buf[1] & 0xFF); ui32 = (ui32 << 8) | (buf[2] & 0xFF); @@ -847,7 +844,7 @@ class SerializationUtil { } // caller guarantee buffer has at least 8 bytes FORCE_INLINE static uint64_t read_ui64(char *buffer) { - uint8_t *buf = (uint8_t *)buffer; + uint8_t *buf = reinterpret_cast(buffer); uint64_t ui64 = buf[0]; ui64 = (ui64 << 8) | (buf[1] & 0xFF); ui64 = (ui64 << 8) | (buf[2] & 0xFF); @@ -859,78 +856,78 @@ class SerializationUtil { return ui64; } - FORCE_INLINE static int write_float(float f, ByteStream &out) { + FORCE_INLINE static E_CODE write_float(float f, ByteStream &out) { uint8_t bytes[4]; float_to_bytes(f, bytes); return out.write_buf(bytes, 4); } - FORCE_INLINE static int read_float(float &f, ByteStream &in) { - int ret = common::E_OK; + FORCE_INLINE static E_CODE read_float(float &f, ByteStream &in) { + E_CODE ret = error_info::E_OK; uint8_t bytes[4]; - uint32_t read_len = 0; + size_t read_len = 0; if (RET_FAIL(in.read_buf(bytes, 4, read_len))) { } else if (read_len != 4) { - ret = common::E_BUF_NOT_ENOUGH; + RETURN_ERR(E_CODE::E_BUF_NOT_ENOUGH, "") } else { f = bytes_to_float(bytes); } return ret; } FORCE_INLINE static float read_float(char *buffer) { - uint8_t *buf = (uint8_t *)buffer; + auto *buf = reinterpret_cast(buffer); return bytes_to_float(buf); } - FORCE_INLINE static int write_double(double d, ByteStream &out) { + FORCE_INLINE static E_CODE write_double(double d, ByteStream &out) { uint8_t bytes[8]; double_to_bytes(d, bytes); return out.write_buf(bytes, 8); } - FORCE_INLINE static int read_double(double &d, ByteStream &in) { - int ret = common::E_OK; - uint32_t read_len = 0; + FORCE_INLINE static E_CODE read_double(double &d, ByteStream &in) { + E_CODE ret = E_CODE::E_OK; + size_t read_len = 0; uint8_t bytes[8]; if (RET_FAIL(in.read_buf(bytes, 8, read_len))) { } else if (read_len != 8) { - ret = common::E_BUF_NOT_ENOUGH; + RETURN_ERR(E_CODE::E_BUF_NOT_ENOUGH, "") } else { d = bytes_to_double(bytes); } return ret; } FORCE_INLINE static double read_double(char *buffer) { - uint8_t *buf = (uint8_t *)buffer; + uint8_t *buf = reinterpret_cast(buffer); return bytes_to_double(buf); } - FORCE_INLINE static int write_i8(int8_t i8, ByteStream &out) { - return write_ui8((uint8_t)i8, out); + FORCE_INLINE static E_CODE write_i8(int8_t i8, ByteStream &out) { + return write_ui8(static_cast(i8), out); } - FORCE_INLINE static int write_i16(int16_t i16, ByteStream &out) { - return write_ui16((uint16_t)i16, out); + FORCE_INLINE static E_CODE write_i16(int16_t i16, ByteStream &out) { + return write_ui16(static_cast(i16), out); } - FORCE_INLINE static int write_i32(int32_t i32, ByteStream &out) { - return write_ui32((uint32_t)i32, out); + FORCE_INLINE static E_CODE write_i32(int32_t i32, ByteStream &out) { + return write_ui32(static_cast(i32), out); } - FORCE_INLINE static int write_i64(int64_t i64, ByteStream &out) { - return write_ui64((uint64_t)i64, out); + FORCE_INLINE static E_CODE write_i64(int64_t i64, ByteStream &out) { + return write_ui64(static_cast(i64), out); } - FORCE_INLINE static int read_i8(int8_t &i8, ByteStream &in) { - return read_ui8((uint8_t &)i8, in); + FORCE_INLINE static E_CODE read_i8(int8_t &i8, ByteStream &in) { + return read_ui8(reinterpret_cast(i8), in); } - FORCE_INLINE static int read_i16(int16_t &i16, ByteStream &in) { - return read_ui16((uint16_t &)i16, in); + FORCE_INLINE static E_CODE read_i16(int16_t &i16, ByteStream &in) { + return read_ui16(reinterpret_cast(i16), in); } - FORCE_INLINE static int read_i32(int32_t &i32, ByteStream &in) { - return read_ui32((uint32_t &)i32, in); + FORCE_INLINE static E_CODE read_i32(int32_t &i32, ByteStream &in) { + return read_ui32(reinterpret_cast(i32), in); } - FORCE_INLINE static int read_i64(int64_t &i64, ByteStream &in) { - return read_ui64((uint64_t &)i64, in); + FORCE_INLINE static E_CODE read_i64(int64_t &i64, ByteStream &in) { + return read_ui64(reinterpret_cast(i64), in); } // TODO more test on var_xxx - FORCE_INLINE static int do_write_var_uint(uint32_t ui32, ByteStream &out) { - int ret = common::E_OK; + FORCE_INLINE static E_CODE do_write_var_uint(uint32_t ui32, ByteStream &out) { + E_CODE ret = E_CODE::E_OK; while ((ui32 & 0xFFFFFF80) != 0) { if (RET_FAIL(write_ui8((ui32 & 0x7F) | 0x80, out))) { return ret; @@ -939,71 +936,71 @@ class SerializationUtil { } return write_ui8(ui32 & 0x7F, out); } - FORCE_INLINE static int do_write_var_uint(uint32_t ui32, char *out_buf, + FORCE_INLINE static E_CODE do_write_var_uint(uint32_t ui32, char *out_buf, const uint32_t out_buf_len) { uint32_t offset = 0; while ((ui32 & 0xFFFFFF80) != 0) { if (offset >= out_buf_len) { - return common::E_BUF_NOT_ENOUGH; + RETURN_ERR(E_CODE::E_BUF_NOT_ENOUGH, "") } - *(out_buf + offset) = (ui32 & 0x7F) | 0x80; + *reinterpret_cast(out_buf + offset) = (ui32 & 0x7F) | 0x80; ui32 = ui32 >> 7; offset++; } if (offset >= out_buf_len) { - return common::E_BUF_NOT_ENOUGH; + RETURN_ERR(E_CODE::E_BUF_NOT_ENOUGH, "") } - *(out_buf + offset) = (ui32 & 0x7F); - return common::E_OK; + *reinterpret_cast(out_buf + offset) = (0x7F & ui32); + return error_info::E_OK; } - FORCE_INLINE static int do_read_var_uint(uint32_t &ui32, ByteStream &in) { + FORCE_INLINE static E_CODE do_read_var_uint(uint32_t &ui32, ByteStream &in) { // Follow readUnsignedVarInt in ReadWriteForEncodingUtils.java - int ret = common::E_OK; + E_CODE ret = E_CODE::E_OK; ui32 = 0; - int i = 0; + uint32_t i = 0; uint8_t ui8 = 0; if (RET_FAIL(read_ui8(ui8, in))) { return ret; } while (ui8 != 0xF && (ui8 & 0x80) != 0) { - ui32 = ui32 | ((ui8 & 0x7F) << i); + ui32 |= static_cast(((ui8 & 0x7F) << i)); i = i + 7; if (RET_FAIL(read_ui8(ui8, in))) { return ret; } } - ui32 = ui32 | (ui8 << i); + ui32 |= static_cast(ui8 << i); return ret; } - FORCE_INLINE static int do_read_var_uint(uint32_t &ui32, char *in_buf, - int in_buf_len, int *ret_offset) { + FORCE_INLINE static E_CODE do_read_var_uint(uint32_t &ui32, char *in_buf, + size_t in_buf_len, size_t *ret_offset) { ui32 = 0; int i = 0; uint8_t ui8 = 0; - int offset = 0; + size_t offset = 0; if (offset < in_buf_len) { - ui8 = *(uint8_t *)(in_buf + offset); + ui8 = *reinterpret_cast(in_buf + offset); offset++; } else { - return common::E_BUF_NOT_ENOUGH; + RETURN_ERR(E_CODE::E_BUF_NOT_ENOUGH, "") } while (ui8 != 0xF && (ui8 & 0x80) != 0) { - ui32 = ui32 | ((ui8 & 0x7F) << i); + ui32 |= static_cast((ui8 & 0x7F) << i); i = i + 7; if (offset < in_buf_len) { - ui8 = *(uint8_t *)(in_buf + offset); + ui8 = *reinterpret_cast(in_buf + offset); offset++; } else { - return common::E_BUF_NOT_ENOUGH; + RETURN_ERR(E_CODE::E_BUF_NOT_ENOUGH, "") } } - ui32 = ui32 | (ui8 << i); + ui32 |= static_cast(ui8 << i); if (ret_offset != nullptr) { *ret_offset = offset; } - return common::E_OK; + return E_CODE::E_OK; } - FORCE_INLINE static int write_var_int(int32_t i32, ByteStream &out) { + FORCE_INLINE static E_CODE write_var_int(int32_t i32, ByteStream &out) { // TODO 8byte to 4byte. // but in IoTDB java, it has only write_var_uint(i32) int ui32 = i32 << 1; @@ -1012,8 +1009,8 @@ class SerializationUtil { } return do_write_var_uint(static_cast(ui32), out); } - FORCE_INLINE static int read_var_int(int32_t &i32, ByteStream &in) { - int ret = common::E_OK; + FORCE_INLINE static E_CODE read_var_int(int32_t &i32, ByteStream &in) { + E_CODE ret = E_CODE::E_OK; uint32_t ui32; if (RET_FAIL(do_read_var_uint(ui32, in))) { } else { @@ -1035,14 +1032,14 @@ class SerializationUtil { return do_read_var_uint(ui32, in); } FORCE_INLINE static int read_var_uint(uint32_t &ui32, char *in_buf, - int in_buf_len, - int *ret_offset = nullptr) { + size_t in_buf_len, + size_t *ret_offset = nullptr) { return do_read_var_uint(ui32, in_buf, in_buf_len, ret_offset); } FORCE_INLINE static int write_var_str(const std::string &str, ByteStream &out) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(write_var_int(((int32_t)str.size()), out))) { } else if (RET_FAIL(out.write_buf(str.c_str(), str.size()))) { } @@ -1052,13 +1049,13 @@ class SerializationUtil { // If the str is nullptr, NO_STR_TO_READ will be added instead. FORCE_INLINE static int write_var_char_ptr(const std::string *str, ByteStream &out) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (str == nullptr) { write_var_int(storage::NO_STR_TO_READ, out); return ret; } size_t str_len = str->length(); - if (RET_FAIL(write_var_int(str_len, out))) { + if (RET_FAIL(write_var_int(static_cast(str_len), out))) { return ret; } else if (RET_FAIL(out.write_buf(str->c_str(), str_len))) { return ret; @@ -1068,71 +1065,76 @@ class SerializationUtil { // If `str` is not a nullptr after calling `read_var_char_ptr`, it // indicates that memory has been allocated and must be freed. - FORCE_INLINE static int read_var_char_ptr(std::string *&str, + FORCE_INLINE static E_CODE read_var_char_ptr(std::string *&str, ByteStream &in) { - int ret = common::E_OK; + E_CODE ret = E_CODE::E_OK; int32_t len = 0; - int32_t read_len = 0; + if (RET_FAIL(read_var_int(len, in))) { return ret; + } + + if (len == storage::NO_STR_TO_READ) { + str = nullptr; + return ret; + } + + size_t read_len = 0; + size_t usize = static_cast(len); + char *tmp_buf = static_cast(malloc(usize)); + if (RET_FAIL( + in.read_buf(tmp_buf, usize, read_len))) { + free(tmp_buf); + return ret; + } else if (usize != read_len) { + free(tmp_buf); + ret = E_CODE::E_BUF_NOT_ENOUGH; } else { - if (len == storage::NO_STR_TO_READ) { - str = nullptr; - return ret; - } else { - char *tmp_buf = static_cast(malloc(len)); - if (RET_FAIL(in.read_buf(tmp_buf, len, read_len))) { - free(tmp_buf); - return ret; - } else if (len != read_len) { - free(tmp_buf); - ret = E_BUF_NOT_ENOUGH; - } else { - str = new std::string(tmp_buf, len); - free(tmp_buf); - } - } + str = new std::string(tmp_buf, usize); + free(tmp_buf); } return ret; } - FORCE_INLINE static int read_var_str(std::string &str, ByteStream &in) { - int ret = common::E_OK; + FORCE_INLINE static E_CODE read_var_str(std::string &str, ByteStream &in) { + E_CODE ret = E_CODE::E_OK; int32_t len = 0; - int32_t read_len = 0; if (RET_FAIL(read_var_int(len, in))) { } else { - char *tmp_buf = (char *)malloc(len + 1); + const auto usize = static_cast(len); + size_t read_len = 0; + char *tmp_buf = static_cast(malloc(usize + 1)); tmp_buf[len] = '\0'; - if (RET_FAIL(in.read_buf(tmp_buf, len, read_len))) { - } else if (len != read_len) { - ret = E_BUF_NOT_ENOUGH; + if (RET_FAIL(in.read_buf(tmp_buf, usize, read_len))) { + } else if (usize != read_len) { + SET_ERR(E_CODE::E_BUF_NOT_ENOUGH, "") } else { - str = std::string(tmp_buf); + str = std::string(tmp_buf, usize); } free(tmp_buf); } return ret; } - FORCE_INLINE static int write_str(const std::string &str, ByteStream &out) { - int ret = common::E_OK; + FORCE_INLINE static E_CODE write_str(const std::string &str, ByteStream &out) { + E_CODE ret = error_info::E_OK; if (RET_FAIL(write_i32((static_cast(str.size())), out))) { } else if (RET_FAIL(out.write_buf(str.c_str(), str.size()))) { } return ret; } - FORCE_INLINE static int read_str(std::string &str, ByteStream &in) { - int ret = common::E_OK; + FORCE_INLINE static E_CODE read_str(std::string &str, ByteStream &in) { + E_CODE ret = E_CODE::E_OK; int32_t len = 0; if (RET_FAIL(read_i32(len, in))) { } else { - int32_t read_len = 0; - char *tmp_buf = static_cast(malloc(len + 1)); - tmp_buf[len] = '\0'; - if (RET_FAIL(in.read_buf(tmp_buf, len, read_len))) { - } else if (len != read_len) { - ret = E_BUF_NOT_ENOUGH; + size_t read_len = 0; + auto usize = static_cast(len); + char *tmp_buf = static_cast(malloc(usize + 1)); + tmp_buf[usize] = '\0'; + if (RET_FAIL(in.read_buf(tmp_buf, usize, read_len))) { + } else if (usize != read_len) { + SET_ERR(E_CODE::E_BUF_NOT_ENOUGH, "") } else { str = std::string(tmp_buf); } @@ -1141,27 +1143,28 @@ class SerializationUtil { return ret; } - FORCE_INLINE static int write_str(const String &str, ByteStream &out) { - int ret = common::E_OK; + FORCE_INLINE static E_CODE write_str(const String &str, ByteStream &out) { + E_CODE ret = error_info::E_OK; if (RET_FAIL(write_i32((static_cast(str.len_)), out))) { - } else if (RET_FAIL(out.write_buf(str.buf_, str.len_))) { + } else if (RET_FAIL(out.write_buf(str.buf_, static_cast(str.len_)))) { } return ret; } - FORCE_INLINE static int read_str(String &str, common::PageArena *pa, + FORCE_INLINE static E_CODE read_str(String &str, common::PageArena *pa, ByteStream &in) { - int ret = common::E_OK; + E_CODE ret = E_CODE::E_OK; int32_t len = 0; - int32_t read_len = 0; if (RET_FAIL(read_i32(len, in))) { } else { - char *buf = (char *)pa->alloc(len); + size_t usize = static_cast(len); + size_t read_len = 0; + char *buf = pa->alloc(usize); if (IS_NULL(buf)) { - ret = common::E_OOM; + SET_ERR(E_CODE::E_OOM, "read str get nullptr"); } else { - if (RET_FAIL(in.read_buf(buf, len, read_len))) { - } else if (len != read_len) { - ret = E_BUF_NOT_ENOUGH; + if (RET_FAIL(in.read_buf(buf, usize, read_len))) { + } else if (usize != read_len) { + SET_ERR(E_CODE::E_BUF_NOT_ENOUGH, "") } else { str.buf_ = buf; str.len_ = len; @@ -1171,27 +1174,29 @@ class SerializationUtil { return ret; } - FORCE_INLINE static int write_mystring(const String &str, ByteStream &out) { - int ret = common::E_OK; + FORCE_INLINE static E_CODE write_mystring(const String &str, ByteStream &out) { + E_CODE ret = E_CODE::E_OK; if (RET_FAIL(write_var_int(str.len_, out))) { - } else if (RET_FAIL(out.write_buf(str.buf_, str.len_))) { + } else if (RET_FAIL(out.write_buf(str.buf_, static_cast(str.len_)))) { } return ret; } - FORCE_INLINE static int read_mystring(String &str, common::PageArena *pa, + FORCE_INLINE static E_CODE read_mystring(String &str, common::PageArena *pa, ByteStream &in) { - int ret = common::E_OK; + E_CODE ret = error_info::E_OK; int32_t len = 0; - int32_t read_len = 0; + if (RET_FAIL(read_var_int(len, in))) { } else { - char *buf = (char *)pa->alloc(len); + auto usize = static_cast(len); + char *buf = pa->alloc(usize); if (IS_NULL(buf)) { - ret = common::E_OOM; + SET_ERR(E_CODE::E_OOM, "read str get nullptr"); } else { - if (RET_FAIL(in.read_buf(buf, len, read_len))) { - } else if (len != read_len) { - ret = E_BUF_NOT_ENOUGH; + size_t read_len = 0; + if (RET_FAIL(in.read_buf(buf, usize, read_len))) { + } else if (usize != read_len) { + SET_ERR(E_CODE::E_BUF_NOT_ENOUGH, "") } else { str.buf_ = buf; str.len_ = len; @@ -1201,7 +1206,7 @@ class SerializationUtil { return ret; } FORCE_INLINE static int write_char(char ch, ByteStream &out) { - return write_ui8(ch, out); + return write_ui8(static_cast(ch), out); } FORCE_INLINE static int read_char(char &ch, ByteStream &in) { return read_ui8((uint8_t &)ch, in); @@ -1209,20 +1214,20 @@ class SerializationUtil { }; FORCE_INLINE bool deserialize_buf_not_enough(int ret) { - return ret == E_OUT_OF_RANGE || ret == E_PARTIAL_READ; + return ret == E_CODE::E_OUT_OF_RANGE || ret == E_CODE::E_PARTIAL_READ; } FORCE_INLINE char *get_bytes_from_bytestream(ByteStream &bs) { if (bs.total_size() == 0) { return nullptr; } - uint32_t size = bs.total_size(); - char *ret_buf = (char *)malloc(size); + size_t size = bs.total_size(); + char *ret_buf = static_cast(malloc(size)); if (ret_buf == nullptr) { return nullptr; } ByteStream::BufferIterator buf_iter = bs.init_buffer_iterator(); - uint32_t offset = 0; + size_t offset = 0; while (true) { ByteStream::Buffer buf = buf_iter.get_next_buf(); if (buf.buf_ == nullptr) { diff --git a/cpp/src/common/allocator/mem_alloc.cc b/cpp/src/common/allocator/mem_alloc.cc index a1e16d9bd..0bf846471 100644 --- a/cpp/src/common/allocator/mem_alloc.cc +++ b/cpp/src/common/allocator/mem_alloc.cc @@ -24,6 +24,7 @@ #include +#include "../../../cmake-build-debug/include/common/error_info/error_info.h" #include "alloc_base.h" #include "common/logger/elog.h" #include "stdio.h" @@ -63,42 +64,40 @@ const char *g_mod_names[__LAST_MOD_ID] = { /* 27 */ "HASH_TABLE", }; -const uint32_t HEADER_SIZE_4B = 4; -const uint32_t HEADER_SIZE_8B = 8; +constexpr uint32_t HEADER_SIZE_4B = 4; +constexpr uint32_t HEADER_SIZE_8B = 8; -void *mem_alloc(uint32_t size, AllocModID mid) { +void *mem_alloc(const size_t size, const AllocModID mid) { // use 7bit at most ASSERT(mid <= 127); if (size <= 0xFFFFFF) { // use 3B size + 1B mod - char *p = (char *)malloc(size + HEADER_SIZE_4B); + auto *p = static_cast(malloc(size + HEADER_SIZE_4B)); if (UNLIKELY(p == nullptr)) { return nullptr; } else { - uint32_t header = (size << 8) | ((uint32_t)mid); - *((uint32_t *)p) = header; - ModStat::get_instance().update_alloc(mid, size); - // cppcheck-suppress memleak - // cppcheck-suppress unmatchedSuppression + uint32_t header = (size << 8) | static_cast(mid); + *reinterpret_cast(p) = header; return p + HEADER_SIZE_4B; } } else { - char *p = (char *)malloc(size + HEADER_SIZE_8B); + if (size > UINT32_MAX - HEADER_SIZE_4B) { + SET_ERR(error_info::E_OOM, "Too large spec to allocate"); + return nullptr; + } + auto *p = static_cast(malloc(size + HEADER_SIZE_8B)); if (UNLIKELY(p == nullptr)) { - std::cout << "alloc big filed for size " << size + HEADER_SIZE_4B - << std::endl; + SET_ERR(error_info::E_OOM, "allocate failed"); return nullptr; } else { - uint64_t large_size = size; - uint64_t header = ((large_size) << 8) | (((uint32_t)mid) | (0x80)); - uint32_t low4b = (uint32_t)(header & 0xFFFFFFFF); - uint32_t high4b = (uint32_t)(header >> 32); - *(uint32_t *)p = high4b; - *(uint32_t *)(p + 4) = low4b; - ModStat::get_instance().update_alloc(mid, size); - // cppcheck-suppress unmatchedSuppression - // cppcheck-suppress memleak + const uint64_t large_size = size; + const uint64_t header = + ((large_size) << 8) | (static_cast(mid) | (0x80)); + const auto low4b = static_cast(header & 0xFFFFFFFF); + const auto high4b = static_cast(header >> 32); + *reinterpret_cast(p) = high4b; + *reinterpret_cast(p + 4) = low4b; return p + HEADER_SIZE_8B; } } @@ -107,7 +106,7 @@ void *mem_alloc(uint32_t size, AllocModID mid) { #ifndef _WIN32 void printCallers() { int layers = 0, i = 0; - char **symbols = NULL; + char **symbols = nullptr; const int64_t MAX_FRAMES = 32; @@ -133,21 +132,21 @@ void printCallers() { void mem_free(void *ptr) { // try as 4Byte header - char *p = (char *)ptr; - uint32_t header = *(uint32_t *)(p - HEADER_SIZE_4B); + char *p = static_cast(ptr); + uint32_t header = *reinterpret_cast(p - HEADER_SIZE_4B); if ((header & 0x80) == 0) { // 4Byte header - uint32_t size = header >> 8; - AllocModID mid = (AllocModID)(header & 0x7F); - ModStat::get_instance().update_free(mid, size); + // uint32_t size = header >> 8; + // AllocModID mid = (AllocModID)(header & 0x7F); + // // ModStat::get_instance().update_free(mid, size); ::free(p - HEADER_SIZE_4B); } else { // 8Byte header - uint64_t header8b = ((uint64_t)(*(uint32_t *)(p - 4))) | - ((uint64_t)(*(uint32_t *)(p - 8)) << 32); - AllocModID mid = (AllocModID)(header8b & 0x7F); - uint32_t size = (uint32_t)(header8b >> 8); - ModStat::get_instance().update_free(mid, size); + uint64_t header8b = static_cast(*reinterpret_cast(p - 4)) | + (static_cast(*reinterpret_cast(p - 8)) << 32); + // AllocModID mid = (AllocModID)(header8b & 0x7F); + // uint32_t size = (uint32_t)(header8b >> 8); + // ModStat::get_instance().update_free(mid, size); ::free(p - HEADER_SIZE_8B); } } @@ -247,10 +246,6 @@ void ModStat::init() { void ModStat::destroy() { ::free(stat_arr_); } -// TODO return to SQL -void ModStat::print_stat() { - // -} BaseAllocator g_base_allocator; diff --git a/cpp/src/common/allocator/my_string.h b/cpp/src/common/allocator/my_string.h index 1f7bf00aa..82c7edf2c 100644 --- a/cpp/src/common/allocator/my_string.h +++ b/cpp/src/common/allocator/my_string.h @@ -19,46 +19,56 @@ #ifndef COMMON_ALLOCATOR_MY_STRING_H #define COMMON_ALLOCATOR_MY_STRING_H -#include +#include +#include -#include +#include #include "common/allocator/page_arena.h" -#include "utils/errno_define.h" +#include "common/error_info/errno_define.h" namespace common { -// String that use PageArena +// String that use PageArena or system spec. struct String { char *buf_; - uint32_t len_; - - String() : buf_(nullptr), len_(0) {} - String(char *buf, uint32_t len) : buf_(buf), len_(len) {} + // if len_ == 0: equal to ""; + // if len_ == -1: equal to nullptr; + int32_t len_; + // if owned is true, buf_ should be free. + bool owned_; + + String() : buf_(nullptr), len_(0), owned_(false) {} + String(char *buf, int32_t len) : buf_(buf), len_(len), owned_(false) {} String(const std::string &str, common::PageArena &pa) - : buf_(nullptr), len_(0) { + : buf_(nullptr), len_(0), owned_(false) { dup_from(str, pa); } - String(const std::string &str) { - buf_ = (char *)str.c_str(); - len_ = str.size(); + explicit String(const std::string &str) { + buf_ = const_cast(str.c_str()); + len_ = static_cast(str.size()); + owned_ = false; } - FORCE_INLINE bool is_null() const { return buf_ == nullptr && len_ == 0; } + FORCE_INLINE bool is_null() const { return len_ == -1; } + FORCE_INLINE bool is_empty() const { return len_ == 0; } FORCE_INLINE void reset() { len_ = 0; buf_ = nullptr; } FORCE_INLINE int dup_from(const std::string &str, common::PageArena &pa) { - len_ = str.size(); + ASSERT(str.length() <= + static_cast(std::numeric_limits::max())); + len_ = static_cast(str.size()); if (UNLIKELY(len_ == 0)) { - return common::E_OK; + return error_info::E_OK; } - buf_ = pa.alloc(len_); + // len_ must >= 0; + buf_ = pa.alloc(static_cast(len_)); if (IS_NULL(buf_)) { - return common::E_OOM; + RETURN_ERR(error_info::E_OOM, "oom when dup from str: " + str); } - memcpy(buf_, str.c_str(), len_); - return common::E_OK; + memcpy(buf_, str.c_str(), static_cast(len_)); + return error_info::E_OK; } FORCE_INLINE bool operator==(const String &other) const { @@ -69,25 +79,28 @@ struct String { len_ = str.len_; if (UNLIKELY(len_ == 0)) { buf_ = nullptr; - return common::E_OK; + return error_info::E_OK; } - buf_ = pa.alloc(len_); + buf_ = pa.alloc(static_cast(len_)); if (IS_NULL(buf_)) { - return common::E_OOM; + RETURN_ERR(error_info::E_OOM, + "oom when dup from str: " + str.to_std_string()); } - memcpy(buf_, str.buf_, len_); - return common::E_OK; + memcpy(buf_, str.buf_, static_cast(len_)); + return error_info::E_OK; } FORCE_INLINE int build_from(const String &s1, const String &s2, common::PageArena &pa) { len_ = s1.len_ + s2.len_; - buf_ = pa.alloc(len_); + buf_ = pa.alloc(static_cast(len_)); if (IS_NULL(buf_)) { - return common::E_OOM; + RETURN_ERR(error_info::E_OOM, + "oom when build from str1: " + s1.to_std_string() + + ", str2: " + s2.to_std_string()); } - memcpy(buf_, s1.buf_, s1.len_); - memcpy(buf_ + s1.len_, s2.buf_, s2.len_); - return common::E_OK; + memcpy(buf_, s1.buf_, static_cast(s1.len_)); + memcpy(buf_ + s1.len_, s2.buf_, static_cast(s2.len_)); + return error_info::E_OK; } FORCE_INLINE void shallow_copy_from(const String &src) { buf_ = src.buf_; @@ -95,23 +108,28 @@ struct String { } FORCE_INLINE bool equal_to(const String &that) const { return (len_ == 0 && that.len_ == 0) || - ((len_ == that.len_) && (0 == memcmp(buf_, that.buf_, len_))); + (len_ == -1 && that.len_ == -1) || + ((len_ == that.len_) && (0 == memcmp(buf_, that.buf_, static_cast(len_)))); } - - // FORCE_INLINE bool equal_to(const std::string &that) - // { - // return (len_ == 0 && that.size() == 0) - // || ((len_ == that.len_) && (0 == memcmp(buf_, that.c_str(), - // len_))); - // } - // strict less than. If @this equals to @that, return false. FORCE_INLINE bool less_than(const String &that) const { - if (len_ == 0 || that.len_ == 0) { + if (len_ == -1) { + return that.len_ != -1; + } + if (that.len_ == -1) { + return false; + } + + if (len_ == 0) { + return that.len_ > 0; + } + + if (that.len_ == 0) { return false; } - uint32_t min_len = std::min(len_, that.len_); - int cmp_res = memcmp(buf_, that.buf_, min_len); + + const int32_t min_len = std::min(len_, that.len_); + const int cmp_res = memcmp(buf_, that.buf_, static_cast(min_len)); if (cmp_res < 0) { return true; } else if (cmp_res > 0) { @@ -125,16 +143,26 @@ struct String { // return < 0, if this < that // return > 0, if this > that FORCE_INLINE int compare(const String &that) const { - if (len_ == 0 || that.len_ == 0) { - return 0; - } - uint32_t min_len = std::min(len_, that.len_); - int cmp_res = memcmp(buf_, that.buf_, min_len); - if (cmp_res == 0) { - return len_ - that.len_; - } else { - return cmp_res; + // Rule 1: nullptr is less than everything + if (len_ == -1 && that.len_ == -1) return 0; + if (len_ == -1) return -1; + if (that.len_ == -1) return 1; + + // Rule 2: empty is less than normal string + if (len_ == 0 && that.len_ == 0) return 0; + if (len_ == 0) return -1; + if (that.len_ == 0) return 1; + + // Rule 3: normal string compare (lexicographical) + const int32_t min_len = std::min(len_, that.len_); + + const int cmp_res = memcmp(buf_, that.buf_, static_cast(min_len)); + if (cmp_res != 0) { + return cmp_res; // >0 / <0 } + + // Prefix equal, shorter one is less + return len_ - that.len_; } FORCE_INLINE void max(const String &that, common::PageArena &pa) { @@ -160,15 +188,17 @@ struct String { return false; } - int min_len = std::min(this->len_, other.len_); - int cmp = std::memcmp(this->buf_, other.buf_, min_len); + const int min_len = std::min(this->len_, other.len_); + const int cmp = std::memcmp(this->buf_, other.buf_, static_cast(min_len)); if (cmp != 0) { return cmp < 0; } return this->len_ < other.len_; } - std::string to_std_string() const { return std::string(buf_, len_); } + std::string to_std_string() const { + return {buf_, static_cast(len_)}; + } #ifndef NDEBUG friend std::ostream &operator<<(std::ostream &os, const String &s) { diff --git a/cpp/src/common/allocator/page_arena.cc b/cpp/src/common/allocator/page_arena.cc index 9cc9cc009..a53102df8 100644 --- a/cpp/src/common/allocator/page_arena.cc +++ b/cpp/src/common/allocator/page_arena.cc @@ -22,7 +22,7 @@ namespace common { -char *PageArena::alloc(uint32_t size) { +char *PageArena::alloc(size_t size) { if (LIKELY(size <= page_size_)) { Page *cur_page = dummy_head_.next_; if (LIKELY(cur_page != nullptr)) { diff --git a/cpp/src/common/allocator/page_arena.h b/cpp/src/common/allocator/page_arena.h index b8053cfb6..6fe935896 100644 --- a/cpp/src/common/allocator/page_arena.h +++ b/cpp/src/common/allocator/page_arena.h @@ -35,12 +35,12 @@ class PageArena { base_allocator_(base_allocator), dummy_head_() {} ~PageArena() { destroy(); } - void init(uint32_t page_size, AllocModID mid) { + void init(size_t page_size, AllocModID mid) { page_size_ = page_size; mid_ = mid; } - char *alloc(uint32_t size); + char *alloc(size_t size); FORCE_INLINE void destroy() { reset(); } void reset(); @@ -91,7 +91,7 @@ class PageArena { }; private: - uint32_t page_size_; + size_t page_size_; AllocModID mid_; BaseAllocator &base_allocator_; Page dummy_head_; diff --git a/cpp/src/common/cache/lru_cache.h b/cpp/src/common/cache/lru_cache.h index 048a16ef6..5a550dc37 100644 --- a/cpp/src/common/cache/lru_cache.h +++ b/cpp/src/common/cache/lru_cache.h @@ -24,7 +24,7 @@ #include #include -#include "utils/errno_define.h" +#include "common/error_info/errno_define.h" namespace common { diff --git a/cpp/src/common/container/bit_map.cc b/cpp/src/common/container/bit_map.cc index 407605e56..993b7714d 100644 --- a/cpp/src/common/container/bit_map.cc +++ b/cpp/src/common/container/bit_map.cc @@ -39,7 +39,7 @@ int BitMap::init(uint32_t item_size, bool init_as_zero) { memset(bitmap_, initial_char, size); size_ = size; init_as_zero_ = init_as_zero; - return common::E_OK; + return error_info::E_OK; } } // namespace common diff --git a/cpp/src/common/container/bit_map.h b/cpp/src/common/container/bit_map.h index 9d0367449..6e76b781b 100644 --- a/cpp/src/common/container/bit_map.h +++ b/cpp/src/common/container/bit_map.h @@ -21,7 +21,7 @@ #include -#include "utils/errno_define.h" +#include "common/error_info/errno_define.h" #include "utils/util_define.h" namespace common { @@ -41,7 +41,7 @@ class BitMap { uint32_t offset = index >> 3; ASSERT(offset < size_); - char *start_addr = bitmap_ + offset; + uint8_t *start_addr = bitmap_ + offset; uint8_t bit_mask = get_bit_mask(index); *start_addr = (*start_addr) | (bit_mask); } @@ -50,7 +50,7 @@ class BitMap { uint32_t offset = index >> 3; ASSERT(offset < size_); - char *start_addr = bitmap_ + offset; + uint8_t *start_addr = bitmap_ + offset; uint8_t bit_mask = get_bit_mask(index); *start_addr = (*start_addr) & (~bit_mask); } @@ -59,22 +59,24 @@ class BitMap { uint32_t offset = index >> 3; ASSERT(offset < size_); - char *start_addr = bitmap_ + offset; + uint8_t *start_addr = bitmap_ + offset; uint8_t bit_mask = get_bit_mask(index); return (*start_addr & bit_mask); } FORCE_INLINE uint32_t get_size() { return size_; } - FORCE_INLINE char *get_bitmap() { return bitmap_; } // for debug + FORCE_INLINE char *get_bitmap() { + return reinterpret_cast(bitmap_); + } // for debug private: FORCE_INLINE uint8_t get_bit_mask(uint32_t index) { - return 1 << (index & 7); + return static_cast(1 << (index & 7)); } private: - char *bitmap_; + uint8_t *bitmap_; uint32_t size_; bool init_as_zero_; }; diff --git a/cpp/src/common/container/byte_buffer.h b/cpp/src/common/container/byte_buffer.h index 967d36408..af77f1349 100644 --- a/cpp/src/common/container/byte_buffer.h +++ b/cpp/src/common/container/byte_buffer.h @@ -100,6 +100,7 @@ class ByteBuffer { // for fixed len value FORCE_INLINE char *read(uint32_t offset, uint32_t len) { + UNUSED(len); ASSERT((offset + len) <= real_data_size_); char *p = &data_[offset]; return p; diff --git a/cpp/src/common/container/list.h b/cpp/src/common/container/list.h index ea4f23c37..c3a68daaa 100644 --- a/cpp/src/common/container/list.h +++ b/cpp/src/common/container/list.h @@ -21,7 +21,9 @@ #define COMMON_CONTAINER_LIST_H #include "common/allocator/page_arena.h" -#include "utils/errno_define.h" +#include "common/error_info/error_info.h" + +using namespace error_info; namespace common { @@ -58,7 +60,7 @@ class SimpleList { Iterator() : cur_(INVALID_NODE_PTR) {} T &get() { return cur_->data_; } FORCE_INLINE bool is_inited() const { return cur_ != INVALID_NODE_PTR; } - FORCE_INLINE Iterator &operator++(int n) { + FORCE_INLINE Iterator &operator++(int) { if (LIKELY(cur_ != nullptr)) { cur_ = cur_->next_; } @@ -90,7 +92,7 @@ class SimpleList { int push_back(const T &data) { void *buf = page_arena_->alloc(sizeof(SimpleListNode)); if (UNLIKELY(buf == nullptr)) { - return common::E_OOM; + return E_CODE::E_OOM; } SimpleListNode *node = new (buf) SimpleListNode(data); if (head_ == nullptr) { @@ -102,7 +104,7 @@ class SimpleList { tail_ = node; } size_++; - return common::E_OK; + return error_info::E_OK; } FORCE_INLINE T &front() { @@ -112,7 +114,7 @@ class SimpleList { int remove(T target) { if (head_ == nullptr) { - return common::E_NOT_EXIST; + return E_NOT_EXIST; } SimpleListNode *prev = head_; SimpleListNode *cur = head_->next_; @@ -120,12 +122,12 @@ class SimpleList { cur = cur->next_; } if (!cur) { - return common::E_NOT_EXIST; + return E_NOT_EXIST; } prev->next_ = cur->next_; size_--; // cur is allocated from PageArena, it should not reclaim now - return common::E_OK; + return error_info::E_OK; } FORCE_INLINE Iterator begin() const { return Iterator(head_); } diff --git a/cpp/src/common/container/murmur_hash3.h b/cpp/src/common/container/murmur_hash3.h index 1e2bf44ba..f8af03c86 100644 --- a/cpp/src/common/container/murmur_hash3.h +++ b/cpp/src/common/container/murmur_hash3.h @@ -33,39 +33,42 @@ namespace common { class Murmur128Hash { public: FORCE_INLINE static int32_t hash(int32_t value, uint32_t seed) { - return inner_hash(reinterpret_cast(&value), 4, seed); + return static_cast( + inner_hash(reinterpret_cast(&value), 4, seed)); } FORCE_INLINE static int32_t hash(int64_t value, uint32_t seed) { - return inner_hash(reinterpret_cast(&value), 8, seed); + return static_cast( + inner_hash(reinterpret_cast(&value), 8, seed)); } FORCE_INLINE static int32_t hash(double value, uint32_t seed) { - return inner_hash(reinterpret_cast(&value), 8, seed); + return static_cast( + inner_hash(reinterpret_cast(&value), 8, seed)); } FORCE_INLINE static int32_t hash(std::string &value, uint32_t seed) { - return inner_hash(value.data(), static_cast(value.size()), - seed); + return static_cast(inner_hash( + value.data(), static_cast(value.size()), seed)); } FORCE_INLINE static int32_t hash(const common::String &buf, int32_t seed) { - return inner_hash(buf.buf_, buf.len_ - 1, seed); + return static_cast(inner_hash(buf.buf_, buf.len_ - 1, seed)); } private: FORCE_INLINE static int64_t rotl64(int64_t v, int n) { - uint64_t uv = (uint64_t)v; - return ((uv << n) | (uv >> (64 - n))); + auto uv = static_cast(v); + return static_cast((uv << n) | (uv >> (64 - n))); } FORCE_INLINE static int64_t fmix(int64_t k) { - uint64_t uk = (uint64_t)k; + auto uk = static_cast(k); uk ^= uk >> 33; uk *= 0xff51afd7ed558ccdL; uk ^= uk >> 33; uk *= 0xc4ceb9fe1a85ec53L; uk ^= uk >> 33; - return (int64_t)uk; + return static_cast(uk); } static int64_t get_block(const char *buf, int32_t index); static int64_t inner_hash(const char *buf, int32_t len, int64_t seed); diff --git a/cpp/src/common/datatype/value.h b/cpp/src/common/datatype/value.h index 29fd5706b..54e8a5d68 100644 --- a/cpp/src/common/datatype/value.h +++ b/cpp/src/common/datatype/value.h @@ -30,6 +30,7 @@ #include "common/logger/elog.h" #include "utils/db_utils.h" +using namespace error_info; namespace common { struct Value { @@ -191,9 +192,7 @@ FORCE_INLINE std::string value_to_string(Value *value) { // return true if cast succ. template -FORCE_INLINE int get_typed_data_from_value(Value *value, T &ret_data) { - return E_OK; -} +FORCE_INLINE int get_typed_data_from_value(Value *value, T &ret_data); template <> FORCE_INLINE int get_typed_data_from_value(Value *value, bool &ret_data) { diff --git a/cpp/src/common/device_id.h b/cpp/src/common/device_id.h index b3a173db1..b49131774 100644 --- a/cpp/src/common/device_id.h +++ b/cpp/src/common/device_id.h @@ -31,29 +31,20 @@ #include "common/allocator/byte_stream.h" #include "constant/tsfile_constant.h" #include "parser/path_nodes_generator.h" -#include "utils/errno_define.h" namespace storage { class IDeviceID { public: virtual ~IDeviceID() = default; - virtual int serialize(common::ByteStream& write_stream) { return 0; } - virtual int deserialize(common::ByteStream& read_stream) { return 0; } - virtual std::string get_table_name() { return ""; } - virtual int segment_num() { return 0; } - virtual const std::vector& get_segments() const { - return empty_segments_; - } - virtual std::string get_device_name() const { return ""; }; - virtual bool operator<(const IDeviceID& other) { return false; } - virtual bool operator==(const IDeviceID& other) { return false; } - virtual bool operator!=(const IDeviceID& other) { return false; } - - protected: - IDeviceID() : empty_segments_() {} - - private: - const std::vector empty_segments_; + virtual int serialize(common::ByteStream& write_stream); + virtual int deserialize(common::ByteStream& read_stream); + virtual std::string get_table_name(); + virtual size_t segment_num(); + virtual const std::vector& get_segments() const; + virtual std::string get_device_name() const; + virtual bool operator<(const IDeviceID& other) const; + virtual bool operator==(const IDeviceID& other) const; + virtual bool operator!=(const IDeviceID& other) const; }; struct IDeviceIDComparator { @@ -112,8 +103,8 @@ class StringArrayDeviceID : public IDeviceID { }; int serialize(common::ByteStream& write_stream) override { - int ret = common::E_OK; - if (RET_FAIL(common::SerializationUtil::write_var_uint(segment_num(), + int ret = E_OK; + if (RET_FAIL(common::SerializationUtil::write_var_uint(static_cast(segment_num()), write_stream))) { return ret; } @@ -127,7 +118,7 @@ class StringArrayDeviceID : public IDeviceID { } int deserialize(common::ByteStream& read_stream) override { - int ret = common::E_OK; + int ret = error_info::E_OK; uint32_t num_segments; if (RET_FAIL(common::SerializationUtil::read_var_uint(num_segments, read_stream))) { @@ -157,13 +148,13 @@ class StringArrayDeviceID : public IDeviceID { return segments_.empty() ? "" : *segments_[0]; } - int segment_num() override { return static_cast(segments_.size()); } + size_t segment_num() override { return segments_.size(); } const std::vector& get_segments() const override { return segments_; } - bool operator<(const IDeviceID& other) override { + bool operator<(const IDeviceID& other) const override { auto other_segments = other.get_segments(); return std::lexicographical_compare( segments_.begin(), segments_.end(), other_segments.begin(), @@ -176,7 +167,7 @@ class StringArrayDeviceID : public IDeviceID { }); } - bool operator==(const IDeviceID& other) override { + bool operator==(const IDeviceID& other) const override { auto other_segments = other.get_segments(); return (segments_.size() == other_segments.size()) && std::equal(segments_.begin(), segments_.end(), @@ -188,7 +179,7 @@ class StringArrayDeviceID : public IDeviceID { }); } - bool operator!=(const IDeviceID& other) override { + bool operator!=(const IDeviceID& other) const override { return !(*this == other); } diff --git a/cpp/src/common/error_info/CMakeLists.txt b/cpp/src/common/error_info/CMakeLists.txt new file mode 100644 index 000000000..7210f3c1e --- /dev/null +++ b/cpp/src/common/error_info/CMakeLists.txt @@ -0,0 +1,24 @@ +#[[ +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. +]] + + +message("Running in src/common/error_info directory") + +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} common_error_info_SRC_LIST) +add_library(common_error_info OBJECT ${common_error_info_SRC_LIST}) \ No newline at end of file diff --git a/cpp/src/common/error_info/errno_define.h b/cpp/src/common/error_info/errno_define.h new file mode 100644 index 000000000..3b11172d6 --- /dev/null +++ b/cpp/src/common/error_info/errno_define.h @@ -0,0 +1,68 @@ +/* + * 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. + */ +#ifndef UTILS_ERRNO_DEFINE_H +#define UTILS_ERRNO_DEFINE_H + +#include +namespace error_info { + +// +//const int E_OK = 0; +//const int E_OOM = 1; +//const int E_NOT_EXIST = 2; +//const int E_ALREADY_EXIST = 3; +//const int E_INVALID_ARG = 4; +//const int E_OUT_OF_RANGE = 5; +//const int E_PARTIAL_READ = 6; +//const int E_INVALID_SCHEMA = 8; +//const int E_MUTEX_ERR = 18; +//const int E_COND_ERR = 19; +//const int E_OVERFLOW = 20; +//const int E_NO_MORE_DATA = 21; +//const int E_OUT_OF_ORDER = 22; +//const int E_TSBLOCK_DATA_INCONSISTENCY = 24; +//const int E_DDL_UNKNOWN_TYPE = 25; +//const int E_TYPE_NOT_SUPPORTED = 26; +//const int E_TYPE_NOT_MATCH = 27; +//const int E_FILE_OPEN_ERR = 28; +//const int E_FILE_CLOSE_ERR = 29; +//const int E_FILE_WRITE_ERR = 30; +//const int E_FILE_READ_ERR = 31; +//const int E_FILE_SYNC_ERR = 32; +//const int E_TSFILE_WRITER_META_ERR = 33; +//const int E_FILE_STAT_ERR = 34; +//const int E_TSFILE_CORRUPTED = 35; +//const int E_BUF_NOT_ENOUGH = 36; +//const int E_INVALID_PATH = 37; +//const int E_NOT_MATCH = 38; +//const int E_NOT_SUPPORT = 40; +//const int E_INVALID_DATA_POINT = 43; +//const int E_DEVICE_NOT_EXIST = 44; +//const int E_MEASUREMENT_NOT_EXIST = 45; +//const int E_INVALID_QUERY = 46; +//const int E_SDK_QUERY_OPTIMIZE_ERR = 47; +//const int E_COMPRESS_ERR = 48; +//const int E_TABLE_NOT_EXIST = 49; +//const int E_COLUMN_NOT_EXIST = 50; +//const int E_UNSUPPORTED_ORDER = 51; +//const int E_INVALID_NODE_TYPE = 52; + +} // end namespace common + +#endif // UTILS_ERRNO_DEFINE_H diff --git a/cpp/src/common/error_info/error_define.inc b/cpp/src/common/error_info/error_define.inc new file mode 100644 index 000000000..c0b76b631 --- /dev/null +++ b/cpp/src/common/error_info/error_define.inc @@ -0,0 +1,40 @@ +/* +* 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. +*/ + +ERRNO(E_OK, 0, "success") + +// MEMORY AND BUF READ +ERRNO(E_OOM, 1, "out of memory") +ERRNO(E_PARTIAL_READ, 2, "partial read") +ERRNO(E_OUT_OF_RANGE, 3, "out of range") +ERRNO(E_BUF_NOT_ENOUGH, 4, "buf not enough") +ERRNO(E_OVERFLOW, 5, "overflow") +ERRNO(E_NOT_EXIST, 6, "not exist") +// COMPRESSION +ERRNO(E_COMPRESS_ERR, 10, "compression error") + +// DATATYPE +ERRNO(E_TYPE_NOT_MATCH, 20, "type not match") +ERRNO(E_INVALID_ARG, 21, "invalid argument") + +// TSIFLE +ERRNO(E_NOT_SUPPORT, 30, "not supported") + +// TSFILE SCHEMA +ERRNO(E_TABLE_NOT_EXIST, 40, "table not exist") \ No newline at end of file diff --git a/cpp/src/common/error_info/error_info.cc b/cpp/src/common/error_info/error_info.cc new file mode 100644 index 000000000..ef70defc0 --- /dev/null +++ b/cpp/src/common/error_info/error_info.cc @@ -0,0 +1,64 @@ +/* + * 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 "error_info.h" + +#include +#include + +namespace error_info { + +static std::string tsfile_err_msg; +static E_CODE tsfile_err_code; +static std::string tsfile_err_filename; +static int tsfile_err_line; +static std::string tsfile_err_function; + +static const std::unordered_map err_name_map = { +#define ERRNO(name, val, desc) {val, desc}, +#include "error_define.inc" + +#undef ERRNO +}; + +std::string error_name(E_CODE code) { + auto it = err_name_map.find(code); + return it == err_name_map.end() ? "unknown error" : it->second; +} + +void set_err_no(E_CODE error) { tsfile_err_code = error; } + +void set_err_msg(const std::string& msg) { tsfile_err_msg = msg; } + +void set_err_info(E_CODE error, const std::string& msg, const std::string& file, + int line, const std::string& function) { + tsfile_err_code = error; + tsfile_err_msg = msg; + tsfile_err_filename = file; + tsfile_err_line = line; + tsfile_err_function = function; +} + +void print_err_info() { + std::cout << " error no is " << tsfile_err_code << "(" + << error_name(tsfile_err_code) << ")"; + std::cout << " error message : " << tsfile_err_msg; +} + +} // namespace error_info diff --git a/cpp/src/common/error_info/error_info.h b/cpp/src/common/error_info/error_info.h new file mode 100644 index 000000000..b1e32503d --- /dev/null +++ b/cpp/src/common/error_info/error_info.h @@ -0,0 +1,55 @@ +/* + * 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. + */ + +#ifndef COMMON_ERROR_INFO_H +#define COMMON_ERROR_INFO_H + +#include + +namespace error_info { +enum E_CODE { +#define ERRNO(name, val, desc) name = val, +#include "error_define.inc" + +#undef ERRNO +}; + +std::string error_name(E_CODE code); +void print_err_info(); +void set_err_no(int error); +void set_err_msg(const std::string& msg); +void set_err_info(int error, const std::string& msg, const std::string& file, + int line, const std::string& function); + +#define RETURN_ERR(code, msg) \ + do { \ + ::error_info::set_err_info((code), msg, __FILE__, __LINE__, \ + __FUNCTION__); \ + return (code); \ + } while (0); + +#define SET_ERR(code, msg) \ + do { \ + ::error_info::set_err_info((code), msg, __FILE__, __LINE__, \ + __FUNCTION__); \ + } while (0); + +} // namespace error_info + +#endif \ No newline at end of file diff --git a/cpp/src/common/global.cc b/cpp/src/common/global.cc index 32b937120..784232323 100644 --- a/cpp/src/common/global.cc +++ b/cpp/src/common/global.cc @@ -26,6 +26,7 @@ #include "utils/injection.h" +using namespace error_info; namespace common { ColumnSchema g_time_column_schema; diff --git a/cpp/src/common/global.h b/cpp/src/common/global.h index 3f331460b..d67e4c259 100644 --- a/cpp/src/common/global.h +++ b/cpp/src/common/global.h @@ -24,6 +24,9 @@ #include "common/allocator/byte_stream.h" #include "common/config/config.h" + +using namespace error_info; + namespace common { extern ConfigValue g_config_value_; @@ -58,10 +61,9 @@ FORCE_INLINE int set_global_time_compression(uint8_t compression) { } FORCE_INLINE int set_datatype_encoding(uint8_t data_type, uint8_t encoding) { - int code = E_OK; - TSDataType dtype = static_cast(data_type); + auto dtype = static_cast(data_type); ASSERT(dtype >= BOOLEAN && dtype <= STRING); - TSEncoding encoding_type = static_cast(encoding); + auto encoding_type = static_cast(encoding); ASSERT(encoding >= PLAIN && encoding <= FREQ); switch (dtype) { case BOOLEAN: diff --git a/cpp/src/common/path.h b/cpp/src/common/path.h index 8318a473e..2072780f9 100644 --- a/cpp/src/common/path.h +++ b/cpp/src/common/path.h @@ -22,9 +22,9 @@ #include #include "common/device_id.h" +#include "common/error_info/errno_define.h" #include "parser/generated/PathParser.h" #include "parser/path_nodes_generator.h" -#include "utils/errno_define.h" namespace storage { diff --git a/cpp/src/common/record.h b/cpp/src/common/record.h index eed72e1f5..f55b4620e 100644 --- a/cpp/src/common/record.h +++ b/cpp/src/common/record.h @@ -25,7 +25,7 @@ #include "common/allocator/my_string.h" #include "common/db_common.h" -#include "utils/errno_define.h" +#include "common/error_info/errno_define.h" namespace storage { @@ -151,16 +151,15 @@ struct TsRecord { template int add_point(const std::string &measurement_name, T val) { - int ret = common::E_OK; + int ret = error_info::E_OK; points_.emplace_back(DataPoint(measurement_name, val)); return ret; } }; -template <> -inline int TsRecord::add_point(const std::string &measurement_name, - common::String val) { - int ret = common::E_OK; +template<> +inline int TsRecord::add_point(const std::string &measurement_name, common::String val) { + int ret = error_info::E_OK; points_.emplace_back(DataPoint(measurement_name, val, pa)); return ret; } diff --git a/cpp/src/common/schema.h b/cpp/src/common/schema.h index 06e7e7e42..bee6076ef 100644 --- a/cpp/src/common/schema.h +++ b/cpp/src/common/schema.h @@ -89,7 +89,7 @@ struct MeasurementSchema { } int serialize_to(common::ByteStream &out) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL( common::SerializationUtil::write_str(measurement_name_, out))) { } else if (RET_FAIL( @@ -99,7 +99,7 @@ struct MeasurementSchema { } else if (RET_FAIL(common::SerializationUtil::write_ui8( compression_type_, out))) { } - if (ret == common::E_OK) { + if (ret == error_info::E_OK) { if (RET_FAIL(common::SerializationUtil::write_ui32(props_.size(), out))) { for (const auto &prop : props_) { @@ -116,7 +116,7 @@ struct MeasurementSchema { } int deserialize_from(common::ByteStream &in) { - int ret = common::E_OK; + int ret = error_info::E_OK; uint8_t data_type = common::TSDataType::INVALID_DATATYPE, encoding = common::TSEncoding::INVALID_ENCODING, compression_type = common::CompressionType::INVALID_COMPRESSION; @@ -134,7 +134,7 @@ struct MeasurementSchema { compression_type_ = static_cast(compression_type); uint32_t props_size; - if (ret == common::E_OK) { + if (ret == error_info::E_OK) { if (RET_FAIL( common::SerializationUtil::read_ui32(props_size, in))) { for (uint32_t i = 0; i < props_.size(); ++i) { @@ -254,7 +254,7 @@ class TableSchema { } int serialize_to(common::ByteStream &out) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(common::SerializationUtil::write_var_uint( column_schemas_.size(), out))) { } else { @@ -272,7 +272,7 @@ class TableSchema { } int deserialize(common::ByteStream &in) { - int ret = common::E_OK; + int ret = error_info::E_OK; uint32_t num_columns; if (RET_FAIL( common::SerializationUtil::read_var_uint(num_columns, in))) { diff --git a/cpp/src/common/seq_tvlist.h b/cpp/src/common/seq_tvlist.h index 57f637601..c415e17cf 100644 --- a/cpp/src/common/seq_tvlist.h +++ b/cpp/src/common/seq_tvlist.h @@ -22,9 +22,9 @@ #include "common/allocator/alloc_base.h" #include "common/allocator/page_arena.h" +#include "common/error_info/errno_define.h" #include "common/mutex/mutex.h" #include "utils/db_utils.h" -#include "utils/errno_define.h" #include "utils/storage_utils.h" #include "utils/util_define.h" @@ -96,7 +96,7 @@ class SeqTVList : public SeqTVListBase { } tv = host_list_->at(read_idx_); read_idx_++; - return common::E_OK; + return error_info::E_OK; } }; diff --git a/cpp/src/common/seq_tvlist.inc b/cpp/src/common/seq_tvlist.inc index 0e723ea3f..2704fbb7e 100644 --- a/cpp/src/common/seq_tvlist.inc +++ b/cpp/src/common/seq_tvlist.inc @@ -53,7 +53,7 @@ int SeqTVList::init(int32_t primary_array_size, // TODO make it configurable page_arena_.init(sizeof(TV) * primary_array_size_ * 4, common::MOD_TVLIST_OBJ); } - return common::E_OK; + return error_info::E_OK; } template @@ -92,7 +92,7 @@ int SeqTVList::push_without_lock(int64_t time, Type value) tv_array_list_[list_idx][list_offset] = insert_tv; write_count_++; last_time_ = time; - return common::E_OK; + return error_info::E_OK; }; template diff --git a/cpp/src/common/statistic.h b/cpp/src/common/statistic.h index 9b8cf1137..d7f3bd71c 100644 --- a/cpp/src/common/statistic.h +++ b/cpp/src/common/statistic.h @@ -21,6 +21,7 @@ #define COMMON_STATISTIC_H #include +#include #include @@ -130,39 +131,28 @@ class Statistic { virtual void destroy() {} virtual FORCE_INLINE void reset() { count_ = 0; } - virtual FORCE_INLINE void update(int64_t time, bool value) { - ASSERT(false); - } - virtual FORCE_INLINE void update(int64_t time, int32_t value) { - ASSERT(false); - } - virtual FORCE_INLINE void update(int64_t time, int64_t value) { - ASSERT(false); - } - virtual FORCE_INLINE void update(int64_t time, float value) { - ASSERT(false); - } - virtual FORCE_INLINE void update(int64_t time, double value) { - ASSERT(false); - } - virtual FORCE_INLINE void update(int64_t time, common::String value) { - ASSERT(false); - } - virtual FORCE_INLINE void update(int64_t time) { ASSERT(false); } + virtual FORCE_INLINE void update(int64_t time, bool value); + virtual FORCE_INLINE void update(int64_t time, int32_t value); + virtual FORCE_INLINE void update(int64_t time, int64_t value); + virtual FORCE_INLINE void update(int64_t time, float value); + virtual FORCE_INLINE void update(int64_t time, double value); + virtual FORCE_INLINE void update(int64_t time, common::String value); + virtual FORCE_INLINE void update(int64_t time); virtual int serialize_to(common::ByteStream &out) { - int ret = common::E_OK; - if (RET_FAIL(common::SerializationUtil::write_var_uint(count_, out))) { - } else if (RET_FAIL(common::SerializationUtil::write_ui64(start_time_, + int ret = error_info::E_OK; + if (RET_FAIL(common::SerializationUtil::write_var_uint((uint32_t)count_, out))) { + } else if (RET_FAIL(common::SerializationUtil::write_ui64((uint64_t)start_time_, out))) { } else if (RET_FAIL( - common::SerializationUtil::write_ui64(end_time_, out))) { + common::SerializationUtil::write_ui64((uint64_t)end_time_, out))) { } else if (RET_FAIL(serialize_typed_stat(out))) { } return ret; } virtual int serialize_typed_stat(common::ByteStream &out) { ASSERT(false); + UNUSED(out); return 0; } @@ -171,7 +161,7 @@ class Statistic { int64_t get_end_time() const { return end_time_; } virtual int deserialize_from(common::ByteStream &in) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(common::SerializationUtil::read_var_uint( (uint32_t &)count_, in))) { } else if (RET_FAIL(common::SerializationUtil::read_ui64( @@ -184,22 +174,26 @@ class Statistic { } virtual int deserialize_typed_stat(common::ByteStream &in) { ASSERT(false); + UNUSED(in); return 0; } virtual int merge_with(Statistic *that) { ASSERT(false); + UNUSED(that); return 0; } virtual int deep_copy_from(Statistic *stat) { ASSERT(false); + UNUSED(stat); return 0; } virtual common::TSDataType get_type() { ASSERT(false); + UNUSED(stat); return common::INVALID_DATATYPE; } virtual std::string to_string() const { - return std::string("UNTYPED_STATISTIC"); + return {"UNTYPED_STATISTIC"}; } public: @@ -211,14 +205,14 @@ class Statistic { #define MERGE_BOOL_STAT_FROM(StatType, untyped_stat) \ do { \ if (UNLIKELY(untyped_stat == nullptr)) { \ - return common::E_INVALID_ARG; \ + return E_CODE::E_INVALID_ARG; \ } \ StatType *typed_stat = (StatType *)(untyped_stat); \ if (UNLIKELY(typed_stat == nullptr)) { \ - return common::E_TYPE_NOT_MATCH; \ + return E_CODE::E_TYPE_NOT_MATCH; \ } \ if (UNLIKELY(typed_stat->count_ == 0)) { \ - return common::E_OK; \ + return error_info::E_OK; \ } \ if (count_ == 0) { \ count_ = typed_stat->count_; \ @@ -239,20 +233,20 @@ class Statistic { } \ sum_value_ += typed_stat->sum_value_; \ } \ - return common::E_OK; \ + return error_info::E_OK; \ } while (false) #define MERGE_NUM_STAT_FROM(StatType, untyped_stat) \ do { \ if (UNLIKELY(untyped_stat == nullptr)) { \ - return common::E_INVALID_ARG; \ + return E_CODE::E_INVALID_ARG; \ } \ StatType *typed_stat = (StatType *)(untyped_stat); \ if (UNLIKELY(typed_stat == nullptr)) { \ - return common::E_TYPE_NOT_MATCH; \ + return E_CODE::E_TYPE_NOT_MATCH; \ } \ if (UNLIKELY(typed_stat->count_ == 0)) { \ - return common::E_OK; \ + return error_info::E_OK; \ } \ if (count_ == 0) { \ count_ = typed_stat->count_; \ @@ -277,20 +271,20 @@ class Statistic { min_value_ = std::min(min_value_, typed_stat->min_value_); \ max_value_ = std::max(max_value_, typed_stat->max_value_); \ } \ - return common::E_OK; \ + return error_info::E_OK; \ } while (false) #define MERGE_STRING_STAT_FROM(StatType, untyped_stat) \ do { \ if (UNLIKELY(untyped_stat == nullptr)) { \ - return common::E_INVALID_ARG; \ + return error_info::E_INVALID_ARG; \ } \ StatType *typed_stat = (StatType *)(untyped_stat); \ if (UNLIKELY(typed_stat == nullptr)) { \ - return common::E_TYPE_NOT_MATCH; \ + return error_info::E_TYPE_NOT_MATCH; \ } \ if (UNLIKELY(typed_stat->count_ == 0)) { \ - return common::E_OK; \ + return error_info::E_OK; \ } \ if (count_ == 0) { \ count_ = typed_stat->count_; \ @@ -313,20 +307,20 @@ class Statistic { min_value_.min(typed_stat->min_value_, *pa_); \ max_value_.max(typed_stat->max_value_, *pa_); \ } \ - return common::E_OK; \ + return error_info::E_OK; \ } while (false) #define MERGE_TIME_STAT_FROM(StatType, untyped_stat) \ do { \ if (UNLIKELY(untyped_stat == nullptr)) { \ - return common::E_INVALID_ARG; \ + return error_info::E_INVALID_ARG; \ } \ StatType *typed_stat = (StatType *)(untyped_stat); \ if (UNLIKELY(typed_stat == nullptr)) { \ - return common::E_TYPE_NOT_MATCH; \ + return error_info::E_TYPE_NOT_MATCH; \ } \ if (UNLIKELY(typed_stat->count_ == 0)) { \ - return common::E_OK; \ + return error_info::E_OK; \ } \ if (count_ == 0) { \ count_ = typed_stat->count_; \ @@ -341,17 +335,17 @@ class Statistic { end_time_ = typed_stat->end_time_; \ } \ } \ - return common::E_OK; \ + return error_info::E_OK; \ } while (false) #define DEEP_COPY_BOOL_STAT_FROM(StatType, untyped_stat) \ do { \ if (UNLIKELY(untyped_stat == nullptr)) { \ - return common::E_INVALID_ARG; \ + return E_CODE::E_INVALID_ARG; \ } \ StatType *typed_stat = (StatType *)(untyped_stat); \ if (UNLIKELY(typed_stat == nullptr)) { \ - return common::E_TYPE_NOT_MATCH; \ + return E_CODE::E_TYPE_NOT_MATCH; \ } \ count_ = typed_stat->count_; \ start_time_ = typed_stat->start_time_; \ @@ -359,17 +353,17 @@ class Statistic { sum_value_ = typed_stat->sum_value_; \ first_value_ = typed_stat->first_value_; \ last_value_ = typed_stat->last_value_; \ - return common::E_OK; \ + return error_info::E_OK; \ } while (false) #define DEEP_COPY_NUM_STAT_FROM(StatType, untyped_stat) \ do { \ if (UNLIKELY(untyped_stat == nullptr)) { \ - return common::E_INVALID_ARG; \ + return error_info::E_INVALID_ARG; \ } \ StatType *typed_stat = (StatType *)(untyped_stat); \ if (UNLIKELY(typed_stat == nullptr)) { \ - return common::E_TYPE_NOT_MATCH; \ + return error_info::E_TYPE_NOT_MATCH; \ } \ count_ = typed_stat->count_; \ start_time_ = typed_stat->start_time_; \ @@ -379,17 +373,17 @@ class Statistic { last_value_ = typed_stat->last_value_; \ min_value_ = typed_stat->min_value_; \ max_value_ = typed_stat->max_value_; \ - return common::E_OK; \ + return error_info::E_OK; \ } while (false) #define DEEP_COPY_STRING_STAT_FROM(StatType, untyped_stat) \ do { \ if (UNLIKELY(untyped_stat == nullptr)) { \ - return common::E_INVALID_ARG; \ + return error_info::E_INVALID_ARG; \ } \ StatType *typed_stat = (StatType *)(untyped_stat); \ if (UNLIKELY(typed_stat == nullptr)) { \ - return common::E_TYPE_NOT_MATCH; \ + return error_info::E_TYPE_NOT_MATCH; \ } \ count_ = typed_stat->count_; \ start_time_ = typed_stat->start_time_; \ @@ -398,22 +392,22 @@ class Statistic { last_value_.dup_from(typed_stat->last_value_, *pa_); \ min_value_.dup_from(typed_stat->min_value_, *pa_); \ max_value_.dup_from(typed_stat->max_value_, *pa_); \ - return common::E_OK; \ + return error_info::E_OK; \ } while (false) #define DEEP_COPY_TIME_STAT_FROM(StatType, untyped_stat) \ do { \ if (UNLIKELY(untyped_stat == nullptr)) { \ - return common::E_INVALID_ARG; \ + return error_info::E_INVALID_ARG; \ } \ StatType *typed_stat = (StatType *)(untyped_stat); \ if (UNLIKELY(typed_stat == nullptr)) { \ - return common::E_TYPE_NOT_MATCH; \ + return error_info::E_TYPE_NOT_MATCH; \ } \ count_ = typed_stat->count_; \ start_time_ = typed_stat->start_time_; \ end_time_ = typed_stat->end_time_; \ - return common::E_OK; \ + return error_info::E_OK; \ } while (false) /* ================ Typed Statistics ================*/ @@ -447,18 +441,18 @@ class BooleanStatistic : public Statistic { BOOL_STAT_UPDATE(time, value); } int serialize_typed_stat(common::ByteStream &out) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(common::SerializationUtil::write_ui8(first_value_ ? 1 : 0, out))) { } else if (RET_FAIL(common::SerializationUtil::write_ui8( last_value_ ? 1 : 0, out))) { - } else if (RET_FAIL(common::SerializationUtil::write_ui64(sum_value_, + } else if (RET_FAIL(common::SerializationUtil::write_ui64((uint64_t)sum_value_, out))) { } return ret; } int deserialize_typed_stat(common::ByteStream &in) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(common::SerializationUtil::read_ui8( (uint8_t &)first_value_, in))) { } else if (RET_FAIL(common::SerializationUtil::read_ui8( @@ -523,21 +517,21 @@ class Int32Statistic : public Statistic { FORCE_INLINE common::TSDataType get_type() { return common::INT32; } int serialize_typed_stat(common::ByteStream &out) { - int ret = common::E_OK; - if (RET_FAIL(common::SerializationUtil::write_ui32(min_value_, out))) { - } else if (RET_FAIL(common::SerializationUtil::write_ui32(max_value_, + int ret = error_info::E_OK; + if (RET_FAIL(common::SerializationUtil::write_ui32((uint32_t)min_value_, out))) { + } else if (RET_FAIL(common::SerializationUtil::write_ui32((uint32_t)max_value_, out))) { - } else if (RET_FAIL(common::SerializationUtil::write_ui32(first_value_, + } else if (RET_FAIL(common::SerializationUtil::write_ui32((uint32_t)first_value_, out))) { - } else if (RET_FAIL(common::SerializationUtil::write_ui32(last_value_, + } else if (RET_FAIL(common::SerializationUtil::write_ui32((uint32_t)last_value_, out))) { - } else if (RET_FAIL(common::SerializationUtil::write_ui64(sum_value_, + } else if (RET_FAIL(common::SerializationUtil::write_ui64((uint64_t)sum_value_, out))) { } return ret; } int deserialize_typed_stat(common::ByteStream &in) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(common::SerializationUtil::read_ui32( (uint32_t &)min_value_, in))) { } else if (RET_FAIL(common::SerializationUtil::read_ui32( @@ -619,13 +613,13 @@ class Int64Statistic : public Statistic { FORCE_INLINE common::TSDataType get_type() { return common::INT64; } int serialize_typed_stat(common::ByteStream &out) { - int ret = common::E_OK; - if (RET_FAIL(common::SerializationUtil::write_ui64(min_value_, out))) { - } else if (RET_FAIL(common::SerializationUtil::write_ui64(max_value_, + int ret = error_info::E_OK; + if (RET_FAIL(common::SerializationUtil::write_ui64((uint64_t)min_value_, out))) { + } else if (RET_FAIL(common::SerializationUtil::write_ui64((uint64_t)max_value_, out))) { - } else if (RET_FAIL(common::SerializationUtil::write_ui64(first_value_, + } else if (RET_FAIL(common::SerializationUtil::write_ui64((uint64_t)first_value_, out))) { - } else if (RET_FAIL(common::SerializationUtil::write_ui64(last_value_, + } else if (RET_FAIL(common::SerializationUtil::write_ui64((uint64_t)last_value_, out))) { } else if (RET_FAIL(common::SerializationUtil::write_double(sum_value_, out))) { @@ -633,7 +627,7 @@ class Int64Statistic : public Statistic { return ret; } int deserialize_typed_stat(common::ByteStream &in) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(common::SerializationUtil::read_ui64( (uint64_t &)min_value_, in))) { } else if (RET_FAIL(common::SerializationUtil::read_ui64( @@ -708,7 +702,7 @@ class FloatStatistic : public Statistic { FORCE_INLINE common::TSDataType get_type() { return common::FLOAT; } int serialize_typed_stat(common::ByteStream &out) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(common::SerializationUtil::write_float(min_value_, out))) { } else if (RET_FAIL(common::SerializationUtil::write_float(max_value_, out))) { @@ -722,7 +716,7 @@ class FloatStatistic : public Statistic { return ret; } int deserialize_typed_stat(common::ByteStream &in) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(common::SerializationUtil::read_float(min_value_, in))) { } else if (RET_FAIL( common::SerializationUtil::read_float(max_value_, in))) { @@ -785,7 +779,7 @@ class DoubleStatistic : public Statistic { FORCE_INLINE common::TSDataType get_type() { return common::DOUBLE; } int serialize_typed_stat(common::ByteStream &out) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL( common::SerializationUtil::write_double(min_value_, out))) { } else if (RET_FAIL(common::SerializationUtil::write_double(max_value_, @@ -800,7 +794,7 @@ class DoubleStatistic : public Statistic { return ret; } int deserialize_typed_stat(common::ByteStream &in) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(common::SerializationUtil::read_double(min_value_, in))) { } else if (RET_FAIL(common::SerializationUtil::read_double(max_value_, in))) { @@ -851,8 +845,14 @@ class TimeStatistic : public Statistic { FORCE_INLINE common::TSDataType get_type() { return common::VECTOR; } - int serialize_typed_stat(common::ByteStream &out) { return common::E_OK; } - int deserialize_typed_stat(common::ByteStream &in) { return common::E_OK; } + int serialize_typed_stat(common::ByteStream &out) { + UNUSED(out); + return error_info::E_OK; + } + int deserialize_typed_stat(common::ByteStream &in) { + UNUSED(in); + return error_info::E_OK; + } int merge_with(Statistic *stat) { MERGE_TIME_STAT_FROM(TimeStatistic, stat); } @@ -920,27 +920,28 @@ class StringStatistic : public Statistic { FORCE_INLINE common::TSDataType get_type() { return common::STRING; } int serialize_typed_stat(common::ByteStream &out) { - int ret = common::E_OK; - if (RET_FAIL(common::SerializationUtil::write_str(first_value_, out))) { - } else if (RET_FAIL(common::SerializationUtil::write_str(last_value_, - out))) { - } else if (RET_FAIL( - common::SerializationUtil::write_str(min_value_, out))) { - } else if (RET_FAIL( - common::SerializationUtil::write_str(max_value_, out))) { + int ret = error_info::E_OK; + if (RET_FAIL(common::SerializationUtil::write_str( + first_value_, out))) { + } else if (RET_FAIL(common::SerializationUtil::write_str( + last_value_, out))) { + } else if (RET_FAIL(common::SerializationUtil::write_str( + min_value_, out))) { + } else if (RET_FAIL(common::SerializationUtil::write_str( + max_value_, out))) { } return ret; } int deserialize_typed_stat(common::ByteStream &in) { - int ret = common::E_OK; - if (RET_FAIL( - common::SerializationUtil::read_str(first_value_, pa_, in))) { - } else if (RET_FAIL(common::SerializationUtil::read_str(last_value_, - pa_, in))) { - } else if (RET_FAIL(common::SerializationUtil::read_str(min_value_, pa_, - in))) { - } else if (RET_FAIL(common::SerializationUtil::read_str(max_value_, pa_, - in))) { + int ret = error_info::E_OK; + if (RET_FAIL(common::SerializationUtil::read_str( + first_value_, pa_, in))) { + } else if (RET_FAIL(common::SerializationUtil::read_str( + last_value_, pa_, in))) { + } else if (RET_FAIL(common::SerializationUtil::read_str( + min_value_, pa_, in))) { + } else if (RET_FAIL(common::SerializationUtil::read_str( + max_value_, pa_, in))) { } return ret; } diff --git a/cpp/src/common/tablet.cc b/cpp/src/common/tablet.cc index 086717932..675fcda16 100644 --- a/cpp/src/common/tablet.cc +++ b/cpp/src/common/tablet.cc @@ -21,7 +21,7 @@ #include -#include "utils/errno_define.h" +#include "common/error_info/errno_define.h" using namespace common; @@ -228,7 +228,7 @@ int Tablet::add_value(uint32_t row_index, uint32_t schema_index, T val) { if (err_code_ != E_OK) { return err_code_; } - int ret = common::E_OK; + int ret = error_info::E_OK; if (UNLIKELY(schema_index >= schema_vec_->size())) { ASSERT(false); ret = common::E_OUT_OF_RANGE; @@ -258,7 +258,7 @@ int Tablet::add_value(uint32_t row_index, uint32_t schema_index, if (err_code_ != E_OK) { return err_code_; } - int ret = common::E_OK; + int ret = error_info::E_OK; if (UNLIKELY(schema_index >= schema_vec_->size())) { ASSERT(false); ret = common::E_OUT_OF_RANGE; @@ -276,7 +276,7 @@ int Tablet::add_value(uint32_t row_index, uint32_t schema_index, template int Tablet::add_value(uint32_t row_index, const std::string &measurement_name, T val) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (err_code_ != E_OK) { return err_code_; } diff --git a/cpp/src/common/tablet.h b/cpp/src/common/tablet.h index b30fc512b..c64d4f9bc 100644 --- a/cpp/src/common/tablet.h +++ b/cpp/src/common/tablet.h @@ -59,7 +59,7 @@ class Tablet { public: static const uint32_t DEFAULT_MAX_ROWS = 1024; - int err_code_ = common::E_OK; + int err_code_ = error_info::E_OK; public: Tablet(const std::string &device_id, diff --git a/cpp/src/common/tsblock/tsblock.cc b/cpp/src/common/tsblock/tsblock.cc index 8b93a5013..256727bad 100644 --- a/cpp/src/common/tsblock/tsblock.cc +++ b/cpp/src/common/tsblock/tsblock.cc @@ -38,8 +38,7 @@ int TsBlock::init() { // max_row_count_ given, calculated with max_row_count_ capacity_ = row_size * max_row_count_; } - int colnum = tuple_desc_->get_column_count(); - for (int i = 0; i < colnum; ++i) { + for (auto i = 0; i < tuple_desc_->get_column_count(); ++i) { ret = build_vector(tuple_desc_->get_column_type(i), max_row_count_); if (ret != 0) { return ret; diff --git a/cpp/src/common/tsblock/tsblock.h b/cpp/src/common/tsblock/tsblock.h index 4316e8f63..c596e3946 100644 --- a/cpp/src/common/tsblock/tsblock.h +++ b/cpp/src/common/tsblock/tsblock.h @@ -19,15 +19,13 @@ #ifndef COMMON_TSBLOCK_TSBLOCK_H #define COMMON_TSBLOCK_TSBLOCK_H -#include +#include #include "common/allocator/byte_stream.h" #include "common/container/byte_buffer.h" #include "common/global.h" #include "common/logger/elog.h" #include "tuple_desc.h" -#include "vector/fixed_length_vector.h" -#include "vector/variable_length_vector.h" #include "vector/vector.h" namespace common { @@ -51,10 +49,9 @@ class TsBlock { tuple_desc_(tupledesc) {} ~TsBlock() { - int size = vectors_.size(); - for (int i = 0; i < size; ++i) { - delete vectors_[i]; - vectors_[i] = nullptr; + for (auto & vector : vectors_) { + delete vector; + vector = nullptr; } } @@ -65,7 +62,7 @@ class TsBlock { FORCE_INLINE Vector *get_vector(uint32_t index) { return vectors_[index]; } FORCE_INLINE uint32_t get_column_count() const { - return tuple_desc_->get_column_count(); + return static_cast(tuple_desc_->get_column_count()); } FORCE_INLINE uint32_t get_max_row_count() const { return max_row_count_; } @@ -76,18 +73,6 @@ class TsBlock { capacity_ += extend_size; } - // need to call flush_row_count after using colappender - FORCE_INLINE int flush_row_count(uint32_t row_count) { - int errnum = E_OK; - if (row_count_ == 0) { - row_count_ = row_count; - } else if (row_count_ != row_count) { - LOGE("Inconsistent number of rows in two columns"); - errnum = E_TSBLOCK_DATA_INCONSISTENCY; - } - return errnum; - } - FORCE_INLINE void fill_trailling_nulls() { for (uint32_t i = 0; i < get_column_count(); ++i) { for (uint32_t j = vectors_[i]->get_row_num(); j < row_count_; ++j) { @@ -97,9 +82,8 @@ class TsBlock { } FORCE_INLINE void reset() { - int size = vectors_.size(); - for (int i = 0; i < size; ++i) { - vectors_[i]->reset(); + for (const auto & vector : vectors_) { + vector->reset(); } row_count_ = 0; } @@ -107,7 +91,7 @@ class TsBlock { FORCE_INLINE static int create_tsblock(TupleDesc *tupledesc, TsBlock *&ret_tsblock, uint32_t max_row_count = 0) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (ret_tsblock == nullptr) { ret_tsblock = new TsBlock(tupledesc, max_row_count); } @@ -133,7 +117,7 @@ class TsBlock { uint32_t row_count_; // real row count uint32_t max_row_count_; - common::BitMap select_list_; + // common::BitMap select_list_; TupleDesc *tuple_desc_; std::vector vectors_; }; @@ -141,7 +125,7 @@ class TsBlock { class RowAppender { public: explicit RowAppender(TsBlock *tsblock) : tsblock_(tsblock) {} - ~RowAppender() {} + ~RowAppender() = default; // todo:(yanghao) maybe need to consider select-list FORCE_INLINE bool add_row() { @@ -181,7 +165,7 @@ class ColAppender { vec_ = tsblock_->vectors_[column_index]; } - ~ColAppender() {} + ~ColAppender() = default; // todo:(yanghao) maybe need to consider select-list FORCE_INLINE bool add_row() { @@ -200,8 +184,8 @@ class ColAppender { FORCE_INLINE void append_null() { vec_->set_null(column_row_count_ - 1); } - FORCE_INLINE uint32_t get_col_row_count() { return column_row_count_; } - FORCE_INLINE uint32_t get_column_index() { return column_index_; } + FORCE_INLINE uint32_t get_col_row_count() const { return column_row_count_; } + FORCE_INLINE uint32_t get_column_index() const { return column_index_; } FORCE_INLINE int fill_null(uint32_t end_index) { while (column_row_count_ < end_index) { if (!add_row()) { @@ -234,16 +218,16 @@ class ColAppender { class RowIterator { public: explicit RowIterator(TsBlock *tsblock) : tsblock_(tsblock), row_id_(0) { - column_count_ = tsblock_->tuple_desc_->get_column_count(); + column_count_ = static_cast(tsblock_->tuple_desc_->get_column_count()); } - ~RowIterator() {} + ~RowIterator() = default; FORCE_INLINE bool end() { return row_id_ >= tsblock_->row_count_; } FORCE_INLINE bool has_next() { return row_id_ < tsblock_->row_count_; } - FORCE_INLINE uint32_t get_column_count() { return column_count_; } + FORCE_INLINE uint32_t get_column_count() const { return column_count_; } FORCE_INLINE TSDataType get_data_type(uint32_t column_index) { ASSERT(column_index < column_count_); @@ -301,7 +285,7 @@ class ColIterator { FORCE_INLINE char *read(uint32_t *len) { return vec_->read(len); } - FORCE_INLINE uint32_t get_column_index() { return column_index_; } + FORCE_INLINE uint32_t get_column_index() const { return column_index_; } private: uint32_t column_index_; diff --git a/cpp/src/common/tsblock/tuple_desc.cc b/cpp/src/common/tsblock/tuple_desc.cc index be0abf71d..617ed0129 100644 --- a/cpp/src/common/tsblock/tuple_desc.cc +++ b/cpp/src/common/tsblock/tuple_desc.cc @@ -20,7 +20,7 @@ namespace common { uint32_t TupleDesc::get_single_row_len(int *erro_code) { - int size = get_column_count(); + size_t size = get_column_count(); int total_len = 0; for (int i = 0; i < size; ++i) { switch (column_list_[i].data_type_) { diff --git a/cpp/src/common/tsblock/tuple_desc.h b/cpp/src/common/tsblock/tuple_desc.h index 98bd341d5..b07efec08 100644 --- a/cpp/src/common/tsblock/tuple_desc.h +++ b/cpp/src/common/tsblock/tuple_desc.h @@ -25,8 +25,8 @@ #include #include +#include "common/error_info/errno_define.h" #include "utils/db_utils.h" -#include "utils/errno_define.h" namespace common { @@ -53,7 +53,7 @@ class TupleDesc { column_list_.push_back(schema); } - FORCE_INLINE uint32_t get_column_count() const { + FORCE_INLINE size_t get_column_count() const { return column_list_.size(); } diff --git a/cpp/src/common/tsfile_common.cc b/cpp/src/common/tsfile_common.cc index 06c415bcc..d9bccd000 100644 --- a/cpp/src/common/tsfile_common.cc +++ b/cpp/src/common/tsfile_common.cc @@ -210,7 +210,7 @@ int TsFileMeta::serialize_to(common::ByteStream &out) { } int TsFileMeta::deserialize_from(common::ByteStream &in) { - int ret = common::E_OK; + int ret = error_info::E_OK; void *index_node_buf = page_arena_->alloc(sizeof(MetaIndexNode)); void *bloom_filter_buf = page_arena_->alloc(sizeof(BloomFilter)); if (IS_NULL(index_node_buf) || IS_NULL(bloom_filter_buf)) { diff --git a/cpp/src/common/tsfile_common.h b/cpp/src/common/tsfile_common.h index 3ad5bb1b1..321188ecb 100644 --- a/cpp/src/common/tsfile_common.h +++ b/cpp/src/common/tsfile_common.h @@ -20,12 +20,9 @@ #ifndef COMMON_TSFILE_COMMON_H #define COMMON_TSFILE_COMMON_H -#include -#include #include #include #include -#include #include "common/allocator/my_string.h" #include "common/allocator/page_arena.h" @@ -75,7 +72,7 @@ struct PageHeader { } int deserialize_from(common::ByteStream &in, bool deserialize_stat, common::TSDataType data_type) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(common::SerializationUtil::read_var_uint( uncompressed_size_, in))) { } else if (RET_FAIL(common::SerializationUtil::read_var_uint( @@ -83,7 +80,7 @@ struct PageHeader { } else if (deserialize_stat) { statistic_ = StatisticFactory::alloc_statistic(data_type); if (IS_NULL(statistic_)) { - return common::E_OOM; + return E_OOM; } else if (RET_FAIL(statistic_->deserialize_from(in))) { } } @@ -133,23 +130,23 @@ struct ChunkHeader { ~ChunkHeader() = default; int serialize_to(common::ByteStream &out) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(common::SerializationUtil::write_char(chunk_type_, out))) { } else if (RET_FAIL(common::SerializationUtil::write_var_str( measurement_name_, out))) { } else if (RET_FAIL(common::SerializationUtil::write_var_uint( data_size_, out))) { - } else if (RET_FAIL(common::SerializationUtil::write_char(data_type_, + } else if (RET_FAIL(common::SerializationUtil::write_ui8(data_type_, out))) { - } else if (RET_FAIL(common::SerializationUtil::write_char( + } else if (RET_FAIL(common::SerializationUtil::write_ui8( compression_type_, out))) { - } else if (RET_FAIL(common::SerializationUtil::write_char( + } else if (RET_FAIL(common::SerializationUtil::write_ui8( encoding_type_, out))) { } return ret; } int deserialize_from(common::ByteStream &in) { - int ret = common::E_OK; + int ret = error_info::E_OK; in.mark_read_pos(); if (RET_FAIL(common::SerializationUtil::read_char(chunk_type_, in))) { } else if (RET_FAIL(common::SerializationUtil::read_var_str( @@ -163,21 +160,13 @@ struct ChunkHeader { } else if (RET_FAIL(common::SerializationUtil::read_char( (char &)encoding_type_, in))) { } else { - serialized_size_ = in.get_mark_len(); + // There will meet data shortcut. + serialized_size_ = static_cast(in.get_mark_len()); } return ret; } #ifndef NDEBUG - friend std::ostream &operator<<(std::ostream &os, const ChunkHeader &h) { - os << "{measurement_name=" << h.measurement_name_ - << ", data_size=" << h.data_size_ << ", data_type=" << h.data_type_ - << ", compression_type=" << h.compression_type_ - << ", encoding_type=" << h.encoding_type_ - << ", num_of_pages=" << h.num_of_pages_ - << ", serialized_size=" << h.serialized_size_ - << ", chunk_type=" << (int)h.chunk_type_ << "}"; - return os; - } + #endif std::string measurement_name_; @@ -221,13 +210,13 @@ struct ChunkMeta { mask_ = mask; encoding_ = encoding; compression_type_ = compression_type; - return common::E_OK; + return error_info::E_OK; } FORCE_INLINE void clone_statistic_from(Statistic *stat) { clone_statistic(stat, statistic_, data_type_); } FORCE_INLINE int clone_from(ChunkMeta &that, common::PageArena *pa) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(measurement_name_.dup_from(that.measurement_name_, *pa))) { return ret; } @@ -237,7 +226,7 @@ struct ChunkMeta { statistic_ = StatisticFactory::alloc_statistic_with_pa(data_type_, pa); if (IS_NULL(statistic_)) { - return common::E_OOM; + return E_OOM; } clone_statistic_from(that.statistic_); } @@ -245,7 +234,7 @@ struct ChunkMeta { return ret; } int serialize_to(common::ByteStream &out, bool serialize_statistic) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(common::SerializationUtil::write_i64( offset_of_chunk_header_, out))) { } else if (serialize_statistic) { @@ -255,14 +244,14 @@ struct ChunkMeta { } int deserialize_from(common::ByteStream &in, bool deserialize_stat, common::PageArena *pa) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(common::SerializationUtil::read_i64( offset_of_chunk_header_, in))) { } else if (deserialize_stat) { statistic_ = StatisticFactory::alloc_statistic_with_pa(data_type_, pa); if (IS_NULL(statistic_)) { - ret = common::E_OOM; + ret = E_OOM; } else { ret = statistic_->deserialize_from(in); } @@ -400,10 +389,10 @@ class TimeseriesIndex : public ITimeseriesIndex { } statistic_ = StatisticFactory::alloc_statistic(data_type); if (IS_NULL(statistic_)) { - return common::E_OOM; + return E_OOM; } statistic_->reset(); - return common::E_OK; + return error_info::E_OK; } virtual Statistic *get_statistic() const { return statistic_; } common::TsID get_ts_id() const { return ts_id_; } @@ -414,7 +403,7 @@ class TimeseriesIndex : public ITimeseriesIndex { } int serialize_to(common::ByteStream &out) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(common::SerializationUtil::write_char( timeseries_meta_type_, out))) { } else if (RET_FAIL(common::SerializationUtil::write_mystring( @@ -431,7 +420,7 @@ class TimeseriesIndex : public ITimeseriesIndex { } int deserialize_from(common::ByteStream &in, common::PageArena *pa) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(common::SerializationUtil::read_char(timeseries_meta_type_, in))) { } else if (RET_FAIL(common::SerializationUtil::read_mystring( @@ -443,13 +432,13 @@ class TimeseriesIndex : public ITimeseriesIndex { } else if (nullptr == (statistic_ = StatisticFactory::alloc_statistic_with_pa( data_type_, pa))) { - ret = common::E_OOM; + ret = E_OOM; } else if (RET_FAIL(statistic_->deserialize_from(in))) { } else { statistic_from_pa_ = true; void *chunk_meta_list_buf = pa->alloc(sizeof(*chunk_meta_list_)); if (IS_NULL(chunk_meta_list_buf)) { - return common::E_OOM; + return E_OOM; } const bool deserialize_chunk_meta_statistic = (timeseries_meta_type_ & 0x3F); // TODO @@ -460,7 +449,7 @@ class TimeseriesIndex : public ITimeseriesIndex { in.read_pos() < start_pos + chunk_meta_list_data_size_) { void *cm_buf = pa->alloc(sizeof(ChunkMeta)); if (IS_NULL(cm_buf)) { - ret = common::E_OOM; + ret = E_OOM; } else { ChunkMeta *cm = new (cm_buf) ChunkMeta; cm->measurement_name_.shallow_copy_from( @@ -478,7 +467,7 @@ class TimeseriesIndex : public ITimeseriesIndex { } int clone_from(const TimeseriesIndex &that, common::PageArena *pa) { - int ret = common::E_OK; + int ret = error_info::E_OK; timeseries_meta_type_ = that.timeseries_meta_type_; chunk_meta_list_data_size_ = that.chunk_meta_list_data_size_; ts_id_ = that.ts_id_; @@ -486,7 +475,7 @@ class TimeseriesIndex : public ITimeseriesIndex { statistic_ = StatisticFactory::alloc_statistic_with_pa(data_type_, pa); if (IS_NULL(statistic_)) { - return common::E_OOM; + return E_OOM; } clone_statistic(that.statistic_, this->statistic_, data_type_); statistic_from_pa_ = true; @@ -498,7 +487,7 @@ class TimeseriesIndex : public ITimeseriesIndex { if (that.chunk_meta_list_ != nullptr) { void *buf = pa->alloc(sizeof(*chunk_meta_list_)); if (IS_NULL(buf)) { - return common::E_OOM; + return E_OOM; } chunk_meta_list_ = new (buf) common::SimpleList(pa); common::SimpleList::Iterator it; @@ -507,7 +496,7 @@ class TimeseriesIndex : public ITimeseriesIndex { ChunkMeta *cm = it.get(); void *cm_buf = pa->alloc(sizeof(ChunkMeta)); if (IS_NULL(cm_buf)) { - return common::E_OOM; + return E_OOM; } else { ChunkMeta *my_cm = new (cm_buf) ChunkMeta; if (RET_FAIL(my_cm->clone_from(*cm, pa))) { @@ -731,18 +720,14 @@ struct IMetaIndexEntry { IMetaIndexEntry() = default; virtual ~IMetaIndexEntry() = default; - virtual int serialize_to(common::ByteStream &out) { return common::E_OK; } + virtual int serialize_to(common::ByteStream &out); virtual int deserialize_from(common::ByteStream &out, - common::PageArena *pa) { - return common::E_NOT_SUPPORT; - } + common::PageArena *pa); virtual int64_t get_offset() const = 0; - virtual bool is_device_level() const { return false; } - virtual std::shared_ptr get_compare_key() const { - return std::shared_ptr(); - } - virtual common::String get_name() const { return {}; } - virtual std::shared_ptr get_device_id() const { return nullptr; } + virtual bool is_device_level() const = 0; + virtual std::shared_ptr get_compare_key() const; + virtual common::String get_name() const; + virtual std::shared_ptr get_device_id() const; virtual std::shared_ptr clone(common::PageArena *pa) = 0; #ifndef NDEBUG virtual void print(std::ostream &os) const {} @@ -773,7 +758,7 @@ struct DeviceMetaIndexEntry : IMetaIndexEntry { } int serialize_to(common::ByteStream &out) override { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(device_id_->serialize(out))) { } else if (RET_FAIL( common::SerializationUtil::write_i64(offset_, out))) { @@ -785,7 +770,7 @@ struct DeviceMetaIndexEntry : IMetaIndexEntry { int deserialize_from(common::ByteStream &in, common::PageArena *pa) override { - int ret = common::E_OK; + int ret = error_info::E_OK; device_id_ = std::make_shared("init"); if (RET_FAIL(device_id_->deserialize(in))) { } else if (RET_FAIL(common::SerializationUtil::read_i64(offset_, in))) { @@ -834,7 +819,7 @@ struct MeasurementMetaIndexEntry : IMetaIndexEntry { } int serialize_to(common::ByteStream &out) override { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(common::SerializationUtil::write_mystring(name_, out))) { } else if (RET_FAIL( common::SerializationUtil::write_i64(offset_, out))) { @@ -844,7 +829,7 @@ struct MeasurementMetaIndexEntry : IMetaIndexEntry { int deserialize_from(common::ByteStream &in, common::PageArena *pa) override { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(common::SerializationUtil::read_mystring(name_, pa, in))) { } else if (RET_FAIL(common::SerializationUtil::read_i64(offset_, in))) { } @@ -918,7 +903,7 @@ struct MetaIndexNode { int64_t &ret_end_offset); int serialize_to(common::ByteStream &out) { - int ret = common::E_OK; + int ret = error_info::E_OK; #if DEBUG_SE int64_t start_pos = out.total_size(); #endif @@ -946,13 +931,13 @@ struct MetaIndexNode { return ret; } - int deserialize_from(const char *buf, int len) { + int deserialize_from(char *buf, int len) { common::ByteStream bs; bs.wrap_from(buf, len); return deserialize_from(bs); } int deserialize_from(common::ByteStream &in) { - int ret = common::E_OK; + int ret = error_info::E_OK; uint32_t children_size = 0; if (RET_FAIL( common::SerializationUtil::read_var_uint(children_size, in))) { @@ -961,7 +946,7 @@ struct MetaIndexNode { for (uint32_t i = 0; i < children_size && IS_SUCC(ret); i++) { void *entry_buf = pa_->alloc(sizeof(MeasurementMetaIndexEntry)); if (IS_NULL(entry_buf)) { - return common::E_OOM; + return E_OOM; } auto entry = new (entry_buf) MeasurementMetaIndexEntry; @@ -987,13 +972,13 @@ struct MetaIndexNode { #endif return ret; } - int device_deserialize_from(const char *buf, int len) { + int device_deserialize_from(char *buf, int len) { common::ByteStream bs; bs.wrap_from(buf, len); return device_deserialize_from(bs); } int device_deserialize_from(common::ByteStream &in) { - int ret = common::E_OK; + int ret = error_info::E_OK; uint32_t children_size = 0; if (RET_FAIL( common::SerializationUtil::read_var_uint(children_size, in))) { @@ -1002,7 +987,7 @@ struct MetaIndexNode { for (uint32_t i = 0; i < children_size && IS_SUCC(ret); i++) { void *entry_buf = pa_->alloc(sizeof(DeviceMetaIndexEntry)); if (IS_NULL(entry_buf)) { - return common::E_OOM; + return E_OOM; } auto* entry_ptr = new(entry_buf) DeviceMetaIndexEntry(); auto entry = std::shared_ptr( @@ -1057,7 +1042,7 @@ struct MetaIndexNode { std::cout << "MetaIndexNode.push_entry(" << *entry << ")" << std::endl; #endif children_.push_back(entry); - return common::E_OK; + return error_info::E_OK; } FORCE_INLINE void destroy() { // std::vector().swap(children_); @@ -1086,20 +1071,20 @@ struct TsFileMeta { std::map>::iterator it = table_metadata_index_node_map_.find(table_name); if (it == table_metadata_index_node_map_.end()) { - return common::E_TABLE_NOT_EXIST; + return E_TABLE_NOT_EXIST; } ret_node = it->second.get(); - return common::E_OK; + return error_info::E_OK; } int get_table_schema(const std::string &table_name, std::shared_ptr &ret_schema) { TableSchemasMap::iterator it = table_schemas_.find(table_name); if (it == table_schemas_.end()) { - return common::E_TABLE_NOT_EXIST; + return E_TABLE_NOT_EXIST; } ret_schema = it->second; - return common::E_OK; + return error_info::E_OK; } TsFileMeta() diff --git a/cpp/src/common/tsfile_mgr.cc b/cpp/src/common/tsfile_mgr.cc deleted file mode 100644 index 88fcfd08d..000000000 --- a/cpp/src/common/tsfile_mgr.cc +++ /dev/null @@ -1,139 +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 "tsfile_mgr.h" - -#include // std::sort -#include -#include - -#include "utils/errno_define.h" - -using namespace common; - -namespace storage { - -TsFileMgr &TsFileMgr::get_instance() { - static TsFileMgr g_s_tsfile_mgr; - return g_s_tsfile_mgr; -} - -int TsFileMgr::init() { - int ret = E_OK; - return ret; -} - -// used when recover -int TsFileMgr::add_new_file(const std::string &file_path) { - int ret = E_OK; - MutexGuard mg(all_open_files_mutex_); - // TODO - return ret; -} - -int TsFileMgr::add_new_file(const FileID &file_id, OpenFile *open_file) { - MutexGuard mg(all_open_files_mutex_); - AllOpenFileMapIter find_iter = all_open_files_.find(file_id); - if (find_iter != all_open_files_.end()) { - return E_ALREADY_EXIST; - } - std::pair pair; - pair.first = file_id; - pair.second = open_file; - std::pair ins_res = all_open_files_.insert(pair); - if (!ins_res.second) { - ASSERT(false); - } - version_++; - return E_OK; -} - -/* - * Currently, we only allow sequence data writing, - * So we have only one DataRun returned. - */ -int TsFileMgr::get_files_for_query(const TsID &ts_id, - const TimeFilter *time_filter, - DataRun *ret_data_run, - int64_t &ret_version) { - int ret = E_OK; - - // Step 1: get all tsfiles that contain this ts_id, store them in tsfile_vec - std::vector tsfile_vec; - - all_open_files_mutex_.lock(); - for (AllOpenFileMapIter iter = all_open_files_.begin(); - iter != all_open_files_.end() && IS_SUCC(ret); iter++) { - OpenFile *open_file = iter->second; - TimeRange time_range; - int tmp_ret = open_file->get_time_range(ts_id, time_range); - if (tmp_ret == E_OK) { - if (time_range_stasify(time_filter, time_range)) { - TimeRangeOpenFilePair pair; - pair.open_file_ = open_file; - pair.time_range_ = time_range; - tsfile_vec.push_back(pair); - } - } else if (tmp_ret == E_NOT_EXIST) { - // continue next - } else { - ret = tmp_ret; - // log_err("get time range for ts_id error, ret=%d, ts_id=%s", ret, - // ts_id.to_string().c_str()); - } - } // end for - ret_version = version_; - all_open_files_mutex_.unlock(); - - // Step 2: since we have only one DataRun, sort these tsfiles - std::sort(tsfile_vec.begin(), tsfile_vec.end(), - compare_timerange_openfile_pair); - - // Step 3: wrap them as DataRun - for (size_t i = 0; i < tsfile_vec.size() && IS_SUCC(ret); i++) { - merge_time_range(ret_data_run->time_range_, tsfile_vec[i].time_range_); - ret = ret_data_run->tsfile_list_.push_back(tsfile_vec[i].open_file_); - } - return ret; -} - -bool TsFileMgr::time_range_stasify(const TimeFilter *time_filter, - const TimeRange &time_range) { - // TODO - UNUSED(time_filter); - UNUSED(time_range); - return true; -} - -#ifndef NDEBUG -void TsFileMgr::DEBUG_dump(const char *tag) { - MutexGuard mg(all_open_files_mutex_); - AllOpenFileMapIter it; - std::cout << tag << "Dump TsFileMgr Start" << std::endl; - int count = 0; - for (it = all_open_files_.begin(); it != all_open_files_.end(); it++) { - std::cout << tag << "Dump TsFileMgr:\n [" << std::setw(3) - << std::setfill(' ') << count << "]\n file_id=" << it->first - << "\n open_file=" << *it->second; - } - std::cout << tag << "Dump TsFileMgr End" << std::endl; -} -#endif - -} // end namespace storage \ No newline at end of file diff --git a/cpp/src/common/tsfile_mgr.h b/cpp/src/common/tsfile_mgr.h deleted file mode 100644 index 3772769a1..000000000 --- a/cpp/src/common/tsfile_mgr.h +++ /dev/null @@ -1,95 +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. - */ - -#ifndef COMMON_TSFILE_MGR_H -#define COMMON_TSFILE_MGR_H - -#include "common/db_common.h" -#include "common/mutex/mutex.h" -#include "reader/scan_iterator.h" - -namespace storage { - -// TODO use header file instead -class TimeFilter; - -struct TimeRangeOpenFilePair { - TimeRange time_range_; - OpenFile *open_file_; -}; - -FORCE_INLINE bool compare_timerange_openfile_pair( - const TimeRangeOpenFilePair &x, const TimeRangeOpenFilePair &y) { - return x.time_range_.start_time_ < y.time_range_.start_time_; -} - -FORCE_INLINE void merge_time_range(TimeRange &dest, const TimeRange &src) { - dest.start_time_ = UTIL_MIN(dest.start_time_, src.start_time_); - dest.end_time_ = UTIL_MAX(dest.end_time_, src.end_time_); -} - -class TsFileMgr { - public: - typedef std::map AllOpenFileMap; - typedef AllOpenFileMap::iterator AllOpenFileMapIter; - - public: - TsFileMgr() : all_open_files_(), version_(0), all_open_files_mutex_() {} - static TsFileMgr &get_instance(); - int init(); - void destroy() { all_open_files_.clear(); } - - int add_new_file(const std::string &file_path); - int add_new_file(const common::FileID &file_id, OpenFile *open_file); - - // int get_files_for_query(const common::TsID &ts_id, - // const TimeFilter &time_filter, - // common::SimpleList &ret_data_runs); - int get_files_for_query(const common::TsID &ts_id, - const TimeFilter *time_filter, - DataRun *ret_data_run, int64_t &ret_version); - int64_t get_version() { - common::MutexGuard mg(all_open_files_mutex_); - return version_; - } - -#ifndef NDEBUG - void DEBUG_dump(const char *tag); -#endif - - private: - bool time_range_stasify(const TimeFilter *time_filter, - const TimeRange &time_range); - - private: - // Map - AllOpenFileMap all_open_files_; - int64_t version_; - common::Mutex all_open_files_mutex_; -}; - -#ifndef NDEBUG -#define DUMP_TSFILE_MGR(tag) TsFileMgr::get_instance().DEBUG_dump(tag) -#else -#define DUMP_TSFILE_MGR(tag) (void) -#endif - -} // namespace storage - -#endif // COMMON_TSFILE_MGR_H diff --git a/cpp/src/compress/gzip_compressor.cc b/cpp/src/compress/gzip_compressor.cc index 4586975a8..e9dab9d32 100644 --- a/cpp/src/compress/gzip_compressor.cc +++ b/cpp/src/compress/gzip_compressor.cc @@ -19,271 +19,250 @@ #include "gzip_compressor.h" -using namespace common; +using namespace common; +using namespace error_info; +namespace storage { +GzipCompressor::GzipCompressor() : compressed_buf() { zstream_valid_ = false; } -namespace storage -{ +GzipCompressor::~GzipCompressor() { end_zstream(); } -GzipCompressor::GzipCompressor() : compressed_buf() -{ - zstream_valid_ = false; -} - -GzipCompressor::~GzipCompressor() -{ - end_zstream(); -} - -int GzipCompressor::reset() -{ - int ret = E_OK; - if (RET_FAIL(end_zstream())) { - } else if (RET_FAIL(init_zstream())) { - } - return ret; +int GzipCompressor::reset() { + int ret = E_CODE::E_OK; + if (RET_FAIL(end_zstream())) { + } else if (RET_FAIL(init_zstream())) { + } + return ret; } -int GzipCompressor::init_zstream() -{ - if (zstream_valid_) { +int GzipCompressor::init_zstream() { + if (zstream_valid_) { + return E_OK; + } + compress_stream_.zalloc = (alloc_func)0; // Z_NULL + compress_stream_.zfree = (free_func)0; + compress_stream_.opaque = (voidpf)0; + compress_stream_.next_in = 0; + compress_stream_.avail_in = 0; + compress_stream_.next_out = 0; + compress_stream_.avail_out = 0; + + memset(compressed_buf, 0, DEFLATE_BUFFER_SIZE); + + if (deflateInit2(&compress_stream_, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 31, + 8, Z_DEFAULT_STRATEGY) != Z_OK) { + // log_err("gzip deflateInit2 failed"); + return E_COMPRESS_ERR; + } + zstream_valid_ = true; return E_OK; - } - compress_stream_.zalloc = (alloc_func)0; // Z_NULL - compress_stream_.zfree = (free_func)0; - compress_stream_.opaque = (voidpf)0; - compress_stream_.next_in = 0; - compress_stream_.avail_in = 0; - compress_stream_.next_out = 0; - compress_stream_.avail_out = 0; - - memset(compressed_buf, 0, DEFLATE_BUFFER_SIZE); - - if (deflateInit2(&compress_stream_, - Z_DEFAULT_COMPRESSION, - Z_DEFLATED, - 31, - 8, - Z_DEFAULT_STRATEGY) != Z_OK) { - //log_err("gzip deflateInit2 failed"); - return E_COMPRESS_ERR; - } - zstream_valid_ = true; - return E_OK; } -int GzipCompressor::end_zstream() -{ - if (!zstream_valid_) { +int GzipCompressor::end_zstream() { + if (!zstream_valid_) { + return E_OK; + } + if (deflateEnd(&compress_stream_) != Z_OK) { + // log_err("deflateEnd failed"); + return E_COMPRESS_ERR; + } + zstream_valid_ = false; return E_OK; - } - if(deflateEnd(&compress_stream_) != Z_OK) { - //log_err("deflateEnd failed"); - return E_COMPRESS_ERR; - } - zstream_valid_ = false; - return E_OK; } int GzipCompressor::compress_into_bytestream(char *uncompressed_buf, uint32_t uncompressed_buf_len, - ByteStream &out) -{ - int ret = Z_OK; - - compress_stream_.next_in = (Bytef *)uncompressed_buf; - compress_stream_.avail_in = uncompressed_buf_len; - compress_stream_.next_out = (Bytef *)compressed_buf; - compress_stream_.avail_out = DEFLATE_BUFFER_SIZE; - - if (uncompressed_buf == nullptr || uncompressed_buf_len == 0) { // no more - if (compress_stream_.next_out) { - while (ret != Z_STREAM_END) { - ret = deflate(&compress_stream_, Z_FINISH); - if(ret != Z_OK && ret != Z_STREAM_END) { - //log_err("deflate failed"); - return E_COMPRESS_ERR; + ByteStream &out) { + int ret = Z_OK; + + compress_stream_.next_in = (Bytef *)uncompressed_buf; + compress_stream_.avail_in = uncompressed_buf_len; + compress_stream_.next_out = (Bytef *)compressed_buf; + compress_stream_.avail_out = DEFLATE_BUFFER_SIZE; + + if (uncompressed_buf == nullptr || uncompressed_buf_len == 0) { // no more + if (compress_stream_.next_out) { + while (ret != Z_STREAM_END) { + ret = deflate(&compress_stream_, Z_FINISH); + if (ret != Z_OK && ret != Z_STREAM_END) { + // log_err("deflate failed"); + return E_COMPRESS_ERR; + } + out.write_buf(compressed_buf, + DEFLATE_BUFFER_SIZE - compress_stream_.avail_out); + compress_stream_.next_out = (Bytef *)compressed_buf; + compress_stream_.avail_out = DEFLATE_BUFFER_SIZE; + } } - out.write_buf(compressed_buf, DEFLATE_BUFFER_SIZE - - compress_stream_.avail_out); compress_stream_.next_out = (Bytef - *)compressed_buf; compress_stream_.avail_out = DEFLATE_BUFFER_SIZE; - } + return E_OK; } - return E_OK; - } - for (;;) { - ret = deflate(&compress_stream_, Z_NO_FLUSH); - if (ret != Z_OK) { - //log_err("deflate failed"); - return E_COMPRESS_ERR; - } + for (;;) { + ret = deflate(&compress_stream_, Z_NO_FLUSH); + if (ret != Z_OK) { + // log_err("deflate failed"); + return E_COMPRESS_ERR; + } - if (compress_stream_.avail_in == 0) { // current input data are all - out.write_buf(compressed_buf, DEFLATE_BUFFER_SIZE - - compress_stream_.avail_out); compress_stream_.next_out = (Bytef - *)compressed_buf; compress_stream_.avail_out = DEFLATE_BUFFER_SIZE; - break; - } - else if (compress_stream_.avail_out == 0) { // no more space for output - out.write_buf(compressed_buf, DEFLATE_BUFFER_SIZE); - compress_stream_.next_out = (Bytef *)compressed_buf; - compress_stream_.avail_out = DEFLATE_BUFFER_SIZE; + if (compress_stream_.avail_in == 0) { // current input data are all + out.write_buf(compressed_buf, + DEFLATE_BUFFER_SIZE - compress_stream_.avail_out); + compress_stream_.next_out = (Bytef *)compressed_buf; + compress_stream_.avail_out = DEFLATE_BUFFER_SIZE; + break; + } else if (compress_stream_.avail_out == + 0) { // no more space for output + out.write_buf(compressed_buf, DEFLATE_BUFFER_SIZE); + compress_stream_.next_out = (Bytef *)compressed_buf; + compress_stream_.avail_out = DEFLATE_BUFFER_SIZE; + } } - } - return E_OK; + return E_OK; } int GzipCompressor::compress(char *uncompressed_buf, uint32_t uncompressed_buf_len, char *&compressed_buf, - uint32_t &compressed_buf_len) -{ - int ret = E_OK; - ByteStream out(DEFLATE_BUFFER_SIZE, MOD_COMPRESSOR_OBJ); - if (RET_FAIL(compress_into_bytestream(uncompressed_buf, - uncompressed_buf_len, out))) { - return ret; - } - if (RET_FAIL(compress_into_bytestream(nullptr, 0, out))) { + uint32_t &compressed_buf_len) { + int ret = E_OK; + ByteStream out(DEFLATE_BUFFER_SIZE, MOD_COMPRESSOR_OBJ); + if (RET_FAIL(compress_into_bytestream(uncompressed_buf, + uncompressed_buf_len, out))) { + return ret; + } + if (RET_FAIL(compress_into_bytestream(nullptr, 0, out))) { + return ret; + } + compressed_buf = get_bytes_from_bytestream(out); + // Assume the length is legal. + compressed_buf_len = static_cast(out.total_size()); + out.destroy(); return ret; - } - compressed_buf = get_bytes_from_bytestream(out); - compressed_buf_len = out.total_size(); - out.destroy(); - return ret; } -GzipDeCompressor::GzipDeCompressor() : decompressed_buf() -{ - zstream_valid_ = false; +GzipDeCompressor::GzipDeCompressor() : decompressed_buf() { + zstream_valid_ = false; } -GzipDeCompressor::~GzipDeCompressor() -{ - end_zstream(); -} +GzipDeCompressor::~GzipDeCompressor() { end_zstream(); } -int GzipDeCompressor::init_zstream() -{ - if (zstream_valid_) { +int GzipDeCompressor::init_zstream() { + if (zstream_valid_) { + return E_OK; + } + decompress_stream_.zalloc = (alloc_func)0; // Z_NULL + decompress_stream_.zfree = (free_func)0; + decompress_stream_.opaque = (voidpf)0; + decompress_stream_.next_in = 0; + decompress_stream_.avail_in = 0; + decompress_stream_.next_out = 0; + decompress_stream_.avail_out = 0; + + memset(decompressed_buf, 0, INFLATE_BUFFER_SIZE); + + if (inflateInit2(&decompress_stream_, 31) != Z_OK) { + // log_err("inflateInit2 failed"); + return E_COMPRESS_ERR; + } + zstream_valid_ = true; return E_OK; - } - decompress_stream_.zalloc = (alloc_func)0; // Z_NULL - decompress_stream_.zfree = (free_func)0; - decompress_stream_.opaque = (voidpf)0; - decompress_stream_.next_in = 0; - decompress_stream_.avail_in = 0; - decompress_stream_.next_out = 0; - decompress_stream_.avail_out = 0; - - memset(decompressed_buf, 0, INFLATE_BUFFER_SIZE); - - if (inflateInit2(&decompress_stream_, 31) != Z_OK) { - //log_err("inflateInit2 failed"); - return E_COMPRESS_ERR; - } - zstream_valid_ = true; - return E_OK; } -int GzipDeCompressor::end_zstream() -{ - if (!zstream_valid_) { +int GzipDeCompressor::end_zstream() { + if (!zstream_valid_) { + return E_OK; + } + if (inflateEnd(&decompress_stream_) != Z_OK) { + // log_err("inflateEnd failed"); + return E_COMPRESS_ERR; + } + zstream_valid_ = false; return E_OK; - } - if(inflateEnd(&decompress_stream_) != Z_OK) { - //log_err("inflateEnd failed"); - return E_COMPRESS_ERR; - } - zstream_valid_ = false; - return E_OK; } -int GzipDeCompressor::reset() -{ - int ret = E_OK; - if (RET_FAIL(end_zstream())) { - } else if (RET_FAIL(init_zstream())) { - } - return ret; +int GzipDeCompressor::reset() { + int ret = E_OK; + if (RET_FAIL(end_zstream())) { + } else if (RET_FAIL(init_zstream())) { + } + return ret; } int GzipDeCompressor::decompress_into_bytestream(char *compressed_buf, uint32_t compressed_buf_len, - ByteStream &out) -{ - int ret = Z_OK; - - decompress_stream_.next_in = (Bytef *)compressed_buf; - decompress_stream_.avail_in = compressed_buf_len; - decompress_stream_.next_out = (Bytef *)decompressed_buf; - decompress_stream_.avail_out = INFLATE_BUFFER_SIZE; - - if (compressed_buf == nullptr || compressed_buf_len == 0) { - if (decompress_stream_.next_out) { - while (ret != Z_STREAM_END) { - ret = inflate(&decompress_stream_, Z_FINISH); - if(ret != Z_OK && ret != Z_STREAM_END) { - //log_err("inflate failed"); - return E_COMPRESS_ERR; + ByteStream &out) { + int ret = Z_OK; + + decompress_stream_.next_in = (Bytef *)compressed_buf; + decompress_stream_.avail_in = compressed_buf_len; + decompress_stream_.next_out = (Bytef *)decompressed_buf; + decompress_stream_.avail_out = INFLATE_BUFFER_SIZE; + + if (compressed_buf == nullptr || compressed_buf_len == 0) { + if (decompress_stream_.next_out) { + while (ret != Z_STREAM_END) { + ret = inflate(&decompress_stream_, Z_FINISH); + if (ret != Z_OK && ret != Z_STREAM_END) { + // log_err("inflate failed"); + return E_COMPRESS_ERR; + } + out.write_buf( + decompressed_buf, + INFLATE_BUFFER_SIZE - decompress_stream_.avail_out); + decompress_stream_.next_out = (Bytef *)decompressed_buf; + decompress_stream_.avail_out = INFLATE_BUFFER_SIZE; + } } - out.write_buf(decompressed_buf, INFLATE_BUFFER_SIZE - - decompress_stream_.avail_out); decompress_stream_.next_out = (Bytef - *)decompressed_buf; decompress_stream_.avail_out = - INFLATE_BUFFER_SIZE; - } + return E_OK; } - return E_OK; - } - for (;;) { - ret = inflate(&decompress_stream_, Z_NO_FLUSH); - if (ret == Z_STREAM_END) { - out.write_buf(decompressed_buf, INFLATE_BUFFER_SIZE - - decompress_stream_.avail_out); break; - } - if (ret != Z_OK) { - //log_err("inflate failed"); - return E_COMPRESS_ERR; - } - if (decompress_stream_.avail_in == 0) { - out.write_buf(decompressed_buf, INFLATE_BUFFER_SIZE - - decompress_stream_.avail_out); decompress_stream_.next_out = (Bytef - *)decompressed_buf; decompress_stream_.avail_out = INFLATE_BUFFER_SIZE; - break; - } - else if (decompress_stream_.avail_out == 0) { - out.write_buf(decompressed_buf, INFLATE_BUFFER_SIZE); - decompress_stream_.next_out = (Bytef *)decompressed_buf; - decompress_stream_.avail_out = INFLATE_BUFFER_SIZE; + for (;;) { + ret = inflate(&decompress_stream_, Z_NO_FLUSH); + if (ret == Z_STREAM_END) { + out.write_buf(decompressed_buf, + INFLATE_BUFFER_SIZE - decompress_stream_.avail_out); + break; + } + if (ret != Z_OK) { + // log_err("inflate failed"); + return E_COMPRESS_ERR; + } + if (decompress_stream_.avail_in == 0) { + out.write_buf(decompressed_buf, + INFLATE_BUFFER_SIZE - decompress_stream_.avail_out); + decompress_stream_.next_out = (Bytef *)decompressed_buf; + decompress_stream_.avail_out = INFLATE_BUFFER_SIZE; + break; + } else if (decompress_stream_.avail_out == 0) { + out.write_buf(decompressed_buf, INFLATE_BUFFER_SIZE); + decompress_stream_.next_out = (Bytef *)decompressed_buf; + decompress_stream_.avail_out = INFLATE_BUFFER_SIZE; + } } - } - return E_OK; + return E_OK; } int GzipDeCompressor::uncompress(char *compressed_buf, uint32_t compressed_buf_len, char *&uncompressed_buf, - uint32_t &uncompressed_buf_len) -{ - int ret = E_OK; - ByteStream out(INFLATE_BUFFER_SIZE, MOD_COMPRESSOR_OBJ); - if(RET_FAIL(decompress_into_bytestream(compressed_buf, compressed_buf_len, - out))) { - return ret; - } - if (RET_FAIL(decompress_into_bytestream(nullptr, 0, out))) { + uint32_t &uncompressed_buf_len) { + int ret = E_OK; + ByteStream out(INFLATE_BUFFER_SIZE, MOD_COMPRESSOR_OBJ); + if (RET_FAIL(decompress_into_bytestream(compressed_buf, compressed_buf_len, + out))) { + return ret; + } + if (RET_FAIL(decompress_into_bytestream(nullptr, 0, out))) { + return ret; + } + uncompressed_buf = get_bytes_from_bytestream(out); + uncompressed_buf_len = static_cast(out.total_size()); + // uncompressed_buf[uncompressed_buf_len] = '\0'; + out.destroy(); return ret; - } - uncompressed_buf = get_bytes_from_bytestream(out); - uncompressed_buf_len = out.total_size(); -// uncompressed_buf[uncompressed_buf_len] = '\0'; - out.destroy(); - return ret; } -} // end namespace storage +} // end namespace storage diff --git a/cpp/src/compress/gzip_compressor.h b/cpp/src/compress/gzip_compressor.h index 69680421b..f5a8ee479 100644 --- a/cpp/src/compress/gzip_compressor.h +++ b/cpp/src/compress/gzip_compressor.h @@ -30,7 +30,6 @@ #include "common/allocator/byte_stream.h" #include "common/logger/elog.h" #include "compressor.h" -#include "utils/errno_define.h" #include "utils/util_define.h" #define DEFLATE_BUFFER_SIZE 512 diff --git a/cpp/src/compress/lz4_compressor.cc b/cpp/src/compress/lz4_compressor.cc index 06bd10cfd..dcbecc170 100644 --- a/cpp/src/compress/lz4_compressor.cc +++ b/cpp/src/compress/lz4_compressor.cc @@ -21,7 +21,7 @@ #include "common/allocator/alloc_base.h" using namespace common; - +using namespace error_info; namespace storage { void LZ4Compressor::destroy() { @@ -37,7 +37,7 @@ void LZ4Compressor::destroy() { } int LZ4Compressor::reset(bool for_compress) { - // Nothing to do + UNUSED(for_compress); return E_OK; } @@ -46,7 +46,7 @@ int LZ4Compressor::compress(char *uncompressed_buf, char *&compressed_buf, uint32_t &compressed_buf_len) { int ret = E_OK; - int max_dst_size = LZ4_compressBound(uncompressed_buf_len); + int max_dst_size = LZ4_compressBound(static_cast(uncompressed_buf_len)); compressed_buf_ = (char *)mem_alloc((size_t)max_dst_size, MOD_COMPRESSOR_OBJ); diff --git a/cpp/src/compress/lz4_compressor.h b/cpp/src/compress/lz4_compressor.h index e7f47a872..62bce4983 100644 --- a/cpp/src/compress/lz4_compressor.h +++ b/cpp/src/compress/lz4_compressor.h @@ -26,10 +26,10 @@ #include #include "common/allocator/byte_stream.h" +#include "common/error_info/errno_define.h" #include "common/logger/elog.h" #include "compressor.h" #include "lz4.h" -#include "utils/errno_define.h" #include "utils/util_define.h" #define UNCOMPRESSED_TIME 5 diff --git a/cpp/src/compress/lzo_compressor.cc b/cpp/src/compress/lzo_compressor.cc index 0ccde9f6c..be7278988 100644 --- a/cpp/src/compress/lzo_compressor.cc +++ b/cpp/src/compress/lzo_compressor.cc @@ -21,6 +21,7 @@ #include "common/allocator/alloc_base.h" using namespace common; +using namespace error_info; namespace storage { @@ -38,6 +39,7 @@ void LZOCompressor::destroy() { int LZOCompressor::reset(bool for_compress) { // Nothing to do + UNUSED(for_compress); return E_OK; } @@ -64,7 +66,7 @@ int LZOCompressor::compress(char *uncompressed_buf, } else { compressed_buf = compress_data; compressed_buf_ = compress_data; - compressed_buf_len = compressed_len; + compressed_buf_len = static_cast(compressed_len); } } else { ret = E_COMPRESS_ERR; @@ -87,8 +89,8 @@ int LZOCompressor::uncompress(char *compressed_buf, uint32_t compressed_buf_len, size_t ulength; constexpr float ratio[] = {1.5, 2.5, 3.5, 4.5, 255}; for (uint8_t i = 0; i < UNCOMPRESSED_TIME; ++i) { - regen_buffer = (char *)mem_alloc(compressed_buf_len * ratio[i], - MOD_COMPRESSOR_OBJ); + regen_buffer = static_cast( + mem_alloc(static_cast(compressed_buf_len * ratio[i]), MOD_COMPRESSOR_OBJ)); if (regen_buffer == nullptr) { ret = E_OOM; } else { diff --git a/cpp/src/compress/lzo_compressor.h b/cpp/src/compress/lzo_compressor.h index f80b6117b..f718bd8d5 100644 --- a/cpp/src/compress/lzo_compressor.h +++ b/cpp/src/compress/lzo_compressor.h @@ -26,10 +26,10 @@ #include #include "common/allocator/byte_stream.h" +#include "common/error_info/errno_define.h" #include "common/logger/elog.h" #include "compressor.h" #include "lzokay.hpp" -#include "utils/errno_define.h" #include "utils/util_define.h" #define UNCOMPRESSED_TIME 5 diff --git a/cpp/src/compress/snappy_compressor.cc b/cpp/src/compress/snappy_compressor.cc index 2dbba4ec7..80ff1d190 100644 --- a/cpp/src/compress/snappy_compressor.cc +++ b/cpp/src/compress/snappy_compressor.cc @@ -21,7 +21,7 @@ #include "common/allocator/alloc_base.h" using namespace common; - +using namespace error_info; namespace storage { void SnappyCompressor::destroy() { diff --git a/cpp/src/compress/snappy_compressor.h b/cpp/src/compress/snappy_compressor.h index af0e86bf0..4b2eb9349 100644 --- a/cpp/src/compress/snappy_compressor.h +++ b/cpp/src/compress/snappy_compressor.h @@ -26,10 +26,10 @@ #include #include "common/allocator/byte_stream.h" +#include "common/error_info/errno_define.h" #include "common/logger/elog.h" #include "compressor.h" #include "snappy.h" -#include "utils/errno_define.h" #include "utils/util_define.h" namespace storage { diff --git a/cpp/src/compress/uncompressed_compressor.h b/cpp/src/compress/uncompressed_compressor.h index 1d698e9a3..69125f000 100644 --- a/cpp/src/compress/uncompressed_compressor.h +++ b/cpp/src/compress/uncompressed_compressor.h @@ -30,14 +30,14 @@ class UncompressedCompressor : public Compressor { virtual ~UncompressedCompressor() {} int reset(bool for_compress) { UNUSED(for_compress); - return common::E_OK; + return error_info::E_OK; } void destroy() {} int compress(char *uncompressed_buf, uint32_t uncompressed_buf_len, char *&compressed_buf, uint32_t &compressed_buf_len) { compressed_buf = uncompressed_buf; compressed_buf_len = uncompressed_buf_len; - return common::E_OK; + return error_info::E_OK; } void after_compress(char *compressed_buf) { UNUSED(compressed_buf); } @@ -45,7 +45,7 @@ class UncompressedCompressor : public Compressor { char *&uncompressed_buf, uint32_t &uncompressed_buf_len) { uncompressed_buf = compressed_buf; uncompressed_buf_len = compressed_buf_len; - return common::E_OK; + return error_info::E_OK; } void after_uncompress(char *uncompressed_buf) { UNUSED(uncompressed_buf); } }; diff --git a/cpp/src/cwrapper/tsfile_cwrapper.cc b/cpp/src/cwrapper/tsfile_cwrapper.cc index 0095dd139..71aee2009 100644 --- a/cpp/src/cwrapper/tsfile_cwrapper.cc +++ b/cpp/src/cwrapper/tsfile_cwrapper.cc @@ -96,7 +96,7 @@ TsFileWriter tsfile_writer_new(WriteFile file, TableSchema *schema, auto table_writer = new storage::TsFileTableWriter( static_cast(file), table_schema); delete table_schema; - *err_code = common::E_OK; + *err_code = error_info::E_OK; return table_writer; } @@ -130,7 +130,7 @@ TsFileWriter tsfile_writer_new_with_memory_threshold(WriteFile file, auto table_writer = new storage::TsFileTableWriter(static_cast(file), table_schema, memory_threshold); - *err_code = common::E_OK; + *err_code = error_info::E_OK; delete table_schema; return table_writer; } @@ -138,7 +138,7 @@ TsFileReader tsfile_reader_new(const char *pathname, ERRNO *err_code) { init_tsfile_config(); auto reader = new storage::TsFileReader(); int ret = reader->open(pathname); - if (ret != common::E_OK) { + if (ret != error_info::E_OK) { *err_code = ret; delete reader; return nullptr; @@ -148,15 +148,15 @@ TsFileReader tsfile_reader_new(const char *pathname, ERRNO *err_code) { ERRNO tsfile_writer_close(TsFileWriter writer) { if (writer == nullptr) { - return common::E_OK; + return error_info::E_OK; } auto *w = static_cast(writer); int ret = w->flush(); - if (ret != common::E_OK) { + if (ret != error_info::E_OK) { return ret; } ret = w->close(); - if (ret != common::E_OK) { + if (ret != error_info::E_OK) { return ret; } delete w; @@ -166,7 +166,7 @@ ERRNO tsfile_writer_close(TsFileWriter writer) { ERRNO tsfile_reader_close(TsFileReader reader) { auto *ts_reader = static_cast(reader); delete ts_reader; - return common::E_OK; + return error_info::E_OK; } Tablet tablet_new(char **column_name_list, TSDataType *data_types, @@ -247,7 +247,7 @@ TsRecord _ts_record_new(const char *device_id, Timestamp timestamp, if (record->points_.size() + 1 > record->points_.capacity()) \ return common::E_BUF_NOT_ENOUGH; \ record->points_.push_back(point); \ - return common::E_OK; \ + return error_info::E_OK; \ } INSERT_DATA_INTO_TS_RECORD_BY_NAME_DEF(int32_t); @@ -262,7 +262,7 @@ TsFileWriter tsfile_writer_new_with_conf(const char *pathname, init_tsfile_config(); auto *writer = new storage::TsFileWriter(); const int ret = writer->open(pathname, O_CREAT | O_RDWR, flag); -if (ret != common::E_OK) { +if (ret != error_info::E_OK) { delete writer; *err_code = ret; return nullptr; @@ -302,10 +302,10 @@ ResultSet tsfile_query_table(TsFileReader reader, const char *table_name, bool tsfile_result_set_next(ResultSet result_set, ERRNO *err_code) { auto *r = static_cast(result_set); bool has_next = true; - int ret = common::E_OK; + int ret = error_info::E_OK; ret = r->next(has_next); *err_code = ret; - if (ret != common::E_OK) { + if (ret != error_info::E_OK) { return false; } return has_next; @@ -571,7 +571,7 @@ TsFileWriter _tsfile_writer_new(const char *pathname, uint64_t memory_threshold, #endif int ret = writer->open(pathname, flags, 0644); common::g_config_value_.chunk_group_size_threshold_ = memory_threshold; - if (ret != common::E_OK) { + if (ret != error_info::E_OK) { delete writer; *err_code = ret; return nullptr; @@ -643,11 +643,11 @@ ERRNO _tsfile_writer_register_device(TsFileWriter writer, static_cast(schema.data_type), static_cast(schema.encoding), static_cast(schema.compression))); - if (ret != common::E_OK) { + if (ret != error_info::E_OK) { return ret; } } - return common::E_OK; + return error_info::E_OK; } ERRNO _tsfile_writer_write_tablet(TsFileWriter writer, Tablet tablet) { @@ -672,11 +672,11 @@ ERRNO _tsfile_writer_write_ts_record(TsFileWriter writer, TsRecord data) { ERRNO _tsfile_writer_close(TsFileWriter writer) { auto *w = static_cast(writer); int ret = w->flush(); - if (ret != common::E_OK) { + if (ret != error_info::E_OK) { return ret; } ret = w->close(); - if (ret != common::E_OK) { + if (ret != error_info::E_OK) { return ret; } delete w; diff --git a/cpp/src/cwrapper/tsfile_cwrapper_expression.cc b/cpp/src/cwrapper/tsfile_cwrapper_expression.cc deleted file mode 100644 index d9b622a12..000000000 --- a/cpp/src/cwrapper/tsfile_cwrapper_expression.cc +++ /dev/null @@ -1,207 +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 "tsfile_cwrapper_expression.h" -// -// #include "common/global.h" -// #include "reader/expression.h" -// #include "reader/filter/filter.h" -// #include "reader/filter/time_filter.h" -// #include "reader/filter/time_operator.h" -// #include "reader/result_set.h" -// #include "reader/tsfile_reader.h" -// #include "utils/errno_define_c.h" -// #include "writer/tsfile_writer.h" -// -// #define E_OK common::E_OK -// -// #define CONSTRUCT_EXP_INTERNAL(exp, column_name) \ -// do { \ -// exp.column_name = column_name; \ -// exp.operate_type = oper; \ -// exp.children_length = 0; \ -// } while (0) -// -// Expression create_column_filter(const char* column_name, OperatorType oper, -// int32_t int32_value) { -// Expression exp; -// CONSTRUCT_EXP_INTERNAL(exp, column_name); -// exp.const_condition.value_condition = int32_value; -// exp.const_condition.type = TSDataType::TS_DATATYPE_INT32; -// return exp; -// } -// -// Expression create_column_filter(const char* column_name, OperatorType oper, -// int64_t int64_value) { -// Expression exp; -// CONSTRUCT_EXP_INTERNAL(exp, column_name); -// exp.const_condition.value_condition = int64_value; -// exp.const_condition.type = TSDataType::TS_DATATYPE_INT64; -// return exp; -// } -// Expression create_column_filter(const char* column_name, OperatorType oper, -// bool bool_value) { -// Expression exp; -// CONSTRUCT_EXP_INTERNAL(exp, column_name); -// exp.const_condition.value_condition = bool_value ? 1 : 0; -// exp.const_condition.type = TSDataType::TS_DATATYPE_BOOLEAN; -// return exp; -// } -// Expression create_column_filter(const char* column_name, OperatorType oper, -// float float_value) { -// Expression exp; -// CONSTRUCT_EXP_INTERNAL(exp, column_name); -// memcpy(&exp.const_condition.value_condition, &float_value, sizeof(float)); -// exp.const_condition.type = TSDataType::TS_DATATYPE_FLOAT; -// return exp; -// } -// Expression create_column_filter(const char* column_name, OperatorType oper, -// double double_value) { -// Expression exp; -// CONSTRUCT_EXP_INTERNAL(exp, column_name); -// exp.const_condition.value_condition = double_value; -// exp.const_condition.type = TSDataType::TS_DATATYPE_DOUBLE; -// return exp; -// } -// Expression create_column_filter(const char* column_name, OperatorType oper, -// const char* char_value) { -// Expression exp; -// CONSTRUCT_EXP_INTERNAL(exp, column_name); -// exp.const_condition.value_condition = reinterpret_cast(char_value); -// exp.const_condition.type = TSDataType::TS_DATATYPE_TEXT; -// return exp; -// } -// -// TimeFilterExpression* create_andquery_timefilter() { -// storage::Expression* exp = new storage::Expression(storage::AND_EXPR); -// return (TimeFilterExpression*)exp; -// } -// -// TimeFilterExpression* create_time_filter(const char* table_name, -// const char* column_name, -// OperatorType oper, int64_t timestamp) { -// std::string table_name_str(table_name); -// std::string column_name_str(column_name); -// storage::Path path(table_name_str, column_name_str); -// storage::Filter* filter; -// switch (oper) { -// case GT: -// filter = storage::TimeFilter::gt(timestamp); -// break; -// case LT: -// filter = storage::TimeFilter::lt(timestamp); -// break; -// case EQ: -// filter = storage::TimeFilter::eq(timestamp); -// break; -// case NOTEQ: -// filter = storage::TimeFilter::not_eqt(timestamp); -// break; -// case GE: -// filter = storage::TimeFilter::gt_eq(timestamp); -// break; -// case LE: -// filter = storage::TimeFilter::lt_eq(timestamp); -// break; -// default: -// filter = nullptr; -// break; -// } -// storage::Expression* exp = -// new storage::Expression(storage::SERIES_EXPR, path, filter); -// return (TimeFilterExpression*)exp; -// } -// -// TimeFilterExpression* add_time_filter_to_and_query( -// TimeFilterExpression* exp_and, TimeFilterExpression* exp) { -// storage::Expression* and_exp = (storage::Expression*)exp_and; -// storage::Expression* time_exp = (storage::Expression*)exp; -// if (and_exp->left_ == nullptr) { -// and_exp->left_ = time_exp; -// } else if (and_exp->right_ == nullptr) { -// and_exp->right_ = time_exp; -// } else { -// storage::Expression* new_exp = -// new storage::Expression(storage::AND_EXPR); -// new_exp->left_ = and_exp->right_; -// and_exp->right_ = new_exp; -// add_time_filter_to_and_query((TimeFilterExpression*)new_exp, exp); -// } -// return exp_and; -// } -// -// void destory_time_filter_query(TimeFilterExpression* expression) { -// if (expression == nullptr) { -// return; -// } -// -// destory_time_filter_query( -// (TimeFilterExpression*)((storage::Expression*)expression)->left_); -// destory_time_filter_query( -// (TimeFilterExpression*)((storage::Expression*)expression)->right_); -// storage::Expression* exp = (storage::Expression*)expression; -// if (exp->type_ == storage::ExpressionType::SERIES_EXPR) { -// delete exp->filter_; -// } else { -// delete exp; -// } -// } -// -// Expression create_global_time_expression(OperatorType oper, int64_t timestamp) { -// Expression exp; -// exp.operate_type = oper; -// exp.expression_type = GLOBALTIME; -// exp.const_condition.value_condition = timestamp; -// exp.const_condition.type = TSDataType::TS_DATATYPE_INT64; -// return exp; -// } -// -// Expression* and_filter_to_and_query(Expression* exp_and, Expression* exp) { -// if (exp_and->children_length >= MAX_COLUMN_FILTER_NUM - 1) { -// return nullptr; -// } -// exp_and->children[exp_and->children_length++] = exp; -// return exp_and; -// } -// -// QueryDataRet ts_reader_query(TsFileReader reader, const char* table_name, -// const char** columns_name, int column_num, -// TimeFilterExpression* expression) { -// auto* r = (storage::TsFileReader*)reader; -// std::string table_name_str(table_name); -// std::vector selected_paths; -// for (int i = 0; i < column_num; i++) { -// std::string column_name(columns_name[i]); -// selected_paths.push_back(storage::Path(table_name_str, column_name)); -// } -// -// storage::ResultSet* qds = nullptr; -// storage::QueryExpression* query_expression = -// storage::QueryExpression::create(selected_paths, -// (storage::Expression*)expression); -// r->query(query_expression, qds); -// QueryDataRet ret = (QueryDataRet)malloc(sizeof(struct query_data_ret)); -// ret->data = qds; -// ret->column_names = (char**)malloc(column_num * sizeof(char*)); -// ret->column_num = column_num; -// for (int i = 0; i < column_num; i++) { -// ret->column_names[i] = strdup(columns_name[i]); -// } -// return ret; -// } \ No newline at end of file diff --git a/cpp/src/cwrapper/tsfile_cwrapper_expression.h b/cpp/src/cwrapper/tsfile_cwrapper_expression.h deleted file mode 100644 index 692be87dd..000000000 --- a/cpp/src/cwrapper/tsfile_cwrapper_expression.h +++ /dev/null @@ -1,103 +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. - */ -// -// #ifndef CWRAPPER_TSFILE_CWRAPPER_H -// #define CWRAPPER_TSFILE_CWRAPPER_H -// -// #include -// #include -// #include -// #include -// #ifdef _WIN32 -// #include -// #endif -// -// #include "tsfile_cwrapper.h" -// -// typedef void* TimeFilterExpression; -// -// #define MAX_COLUMN_FILTER_NUM 10 -// typedef enum operator_type { -// LT, -// LE, -// EQ, -// GT, -// GE, -// NOTEQ, -//} OperatorType; -// -// typedef enum expression_type { -// OR, -// AND, -// GLOBALTIME, -//} ExpressionType; -// -// typedef struct constant { -// int64_t value_condition; -// int type; -//} Constant; -// -// typedef struct expression { -// const char* column_name; -// Constant const_condition; -// ExpressionType expression_type; -// OperatorType operate_type; -// struct expression* children[MAX_COLUMN_FILTER_NUM]; -// int children_length; -//} Expression; -// -// typedef void* QueryDataRetINTERNAL; -// typedef struct query_data_ret { -// char** column_names; -// int column_num; -// QueryDataRetINTERNAL data; -//}* QueryDataRet; -// -// #ifdef __cplusplus -// extern "C" { -// #endif -// -// TimeFilterExpression* create_query_and_time_filter(); -// -// TimeFilterExpression* create_time_filter(const char* table_name, -// const char* column_name, -// OperatorType oper, -// timestamp timestamp); -// -// TimeFilterExpression* add_time_filter_to_and_query( -// TimeFilterExpression* exp_and, TimeFilterExpression* exp); -// -// void destroy_time_filter_query(TimeFilterExpression* expression); -// -// Expression* create_time_expression(const char* column_name, OperatorType -// oper, -// timestamp timestamp); -// -// Expression* add_and_filter_to_and_query(Expression* exp_and, Expression* -// exp); -// -// QueryDataRet ts_reader_query(TsFileReader reader, const char* table_name, -// const char** columns, int colum_num, -// TimeFilterExpression* expression); -// -// -// #ifdef __cplusplus -//} -// #endif -// #endif // CWRAPPER_TSFILE_CWRAPPER_H diff --git a/cpp/src/encoding/bitpack_decoder.h b/cpp/src/encoding/bitpack_decoder.h index 099afc790..e33d79afe 100644 --- a/cpp/src/encoding/bitpack_decoder.h +++ b/cpp/src/encoding/bitpack_decoder.h @@ -78,7 +78,7 @@ class BitPackDecoder { } if (current_count_ == 0) { uint8_t header; - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL( common::SerializationUtil::read_ui8(header, byte_cache_))) { return ret; @@ -98,7 +98,7 @@ class BitPackDecoder { // in last bit-packing group, there may be some useless value, // lastBitPackedNum indicates how many values is useful uint8_t last_bit_packed_num; - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(common::SerializationUtil::read_ui8(last_bit_packed_num, byte_cache_))) { return ret; @@ -139,7 +139,7 @@ class BitPackDecoder { } int read_length_and_bitwidth(common::ByteStream &buffer) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL( common::SerializationUtil::read_var_uint(length_, buffer))) { return common::E_PARTIAL_READ; diff --git a/cpp/src/encoding/dictionary_decoder.h b/cpp/src/encoding/dictionary_decoder.h index 4ac846db8..f9f21fe3e 100644 --- a/cpp/src/encoding/dictionary_decoder.h +++ b/cpp/src/encoding/dictionary_decoder.h @@ -58,7 +58,7 @@ class DictionaryDecoder { } int init_map(common::ByteStream &buffer) { - int ret = common::E_OK; + int ret = error_info::E_OK; int length = 0; if (RET_FAIL(common::SerializationUtil::read_var_int(length, buffer))) { return common::E_PARTIAL_READ; diff --git a/cpp/src/encoding/dictionary_encoder.h b/cpp/src/encoding/dictionary_encoder.h index f40df5486..6ea3644ef 100644 --- a/cpp/src/encoding/dictionary_encoder.h +++ b/cpp/src/encoding/dictionary_encoder.h @@ -63,21 +63,21 @@ class DictionaryEncoder { } int flush(common::ByteStream &out) { - int ret = common::E_OK; + int ret = error_info::E_OK; ret = write_map(out); - if (ret != common::E_OK) { + if (ret != error_info::E_OK) { return ret; } else { write_encoded_data(out); } - if (ret != common::E_OK) { + if (ret != error_info::E_OK) { return ret; } - return common::E_OK; + return error_info::E_OK; } int write_map(common::ByteStream &out) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(common::SerializationUtil::write_var_int( (int)index_entry_.size(), out))) { return ret; @@ -89,7 +89,7 @@ class DictionaryEncoder { } } } - return common::E_OK; + return error_info::E_OK; } void write_encoded_data(common::ByteStream &out) { diff --git a/cpp/src/encoding/gorilla_decoder.h b/cpp/src/encoding/gorilla_decoder.h index f374b32eb..c7d384f18 100644 --- a/cpp/src/encoding/gorilla_decoder.h +++ b/cpp/src/encoding/gorilla_decoder.h @@ -308,7 +308,7 @@ template <> FORCE_INLINE int IntGorillaDecoder::read_int32(int32_t &ret_value, common::ByteStream &in) { ret_value = decode(in); - return common::E_OK; + return error_info::E_OK; } template <> FORCE_INLINE int IntGorillaDecoder::read_int64(int64_t &ret_value, @@ -351,7 +351,7 @@ template <> FORCE_INLINE int LongGorillaDecoder::read_int64(int64_t &ret_value, common::ByteStream &in) { ret_value = decode(in); - return common::E_OK; + return error_info::E_OK; } template <> FORCE_INLINE int LongGorillaDecoder::read_float(float &ret_value, @@ -390,7 +390,7 @@ FORCE_INLINE int FloatGorillaDecoder::read_int64(int64_t &ret_value, FORCE_INLINE int FloatGorillaDecoder::read_float(float &ret_value, common::ByteStream &in) { ret_value = decode(in); - return common::E_OK; + return error_info::E_OK; } FORCE_INLINE int FloatGorillaDecoder::read_double(double &ret_value, common::ByteStream &in) { @@ -420,7 +420,7 @@ FORCE_INLINE int DoubleGorillaDecoder::read_float(float &ret_value, FORCE_INLINE int DoubleGorillaDecoder::read_double(double &ret_value, common::ByteStream &in) { ret_value = decode(in); - return common::E_OK; + return error_info::E_OK; } } // end namespace storage diff --git a/cpp/src/encoding/gorilla_encoder.h b/cpp/src/encoding/gorilla_encoder.h index 5ae470112..64578ae23 100644 --- a/cpp/src/encoding/gorilla_encoder.h +++ b/cpp/src/encoding/gorilla_encoder.h @@ -261,7 +261,7 @@ FORCE_INLINE int GorillaEncoder::do_encode(T value, write_first(value, out); first_value_was_written_ = true; } - return common::E_OK; + return error_info::E_OK; } template <> @@ -276,7 +276,7 @@ FORCE_INLINE int GorillaEncoder::flush(common::ByteStream &out) { // the encoder may be reused, so let us reset it reset(); - return common::E_OK; + return error_info::E_OK; } template <> @@ -291,7 +291,7 @@ FORCE_INLINE int GorillaEncoder::flush(common::ByteStream &out) { // the encoder may be reused, so let us reset it reset(); - return common::E_OK; + return error_info::E_OK; } class FloatGorillaEncoder : public GorillaEncoder { @@ -313,7 +313,7 @@ class FloatGorillaEncoder : public GorillaEncoder { // the encoder may be reused, so let us reset it reset(); - return common::E_OK; + return error_info::E_OK; } int encode(bool value, common::ByteStream &out_stream); @@ -342,7 +342,7 @@ class DoubleGorillaEncoder : public GorillaEncoder { // the encoder may be reused, so let us reset it reset(); - return common::E_OK; + return error_info::E_OK; } int encode(bool value, common::ByteStream &out_stream); diff --git a/cpp/src/encoding/plain_encoder.h b/cpp/src/encoding/plain_encoder.h index a0b66c064..27ee67321 100644 --- a/cpp/src/encoding/plain_encoder.h +++ b/cpp/src/encoding/plain_encoder.h @@ -58,7 +58,7 @@ class PlainEncoder : public Encoder { int flush(common::ByteStream &out_stream) { // do nothing for PlainEncoder - return common::E_OK; + return error_info::E_OK; } int get_max_byte_size() { return 0; } diff --git a/cpp/src/encoding/ts2diff_decoder.h b/cpp/src/encoding/ts2diff_decoder.h index 5ad0e89cd..be58bbd9e 100644 --- a/cpp/src/encoding/ts2diff_decoder.h +++ b/cpp/src/encoding/ts2diff_decoder.h @@ -211,7 +211,7 @@ template <> FORCE_INLINE int IntTS2DIFFDecoder::read_int32(int32_t &ret_value, common::ByteStream &in) { ret_value = decode(in); - return common::E_OK; + return error_info::E_OK; } template <> FORCE_INLINE int IntTS2DIFFDecoder::read_int64(int64_t &ret_value, @@ -254,7 +254,7 @@ template <> FORCE_INLINE int LongTS2DIFFDecoder::read_int64(int64_t &ret_value, common::ByteStream &in) { ret_value = decode(in); - return common::E_OK; + return error_info::E_OK; } template <> FORCE_INLINE int LongTS2DIFFDecoder::read_float(float &ret_value, @@ -293,7 +293,7 @@ FORCE_INLINE int FloatTS2DIFFDecoder::read_int64(int64_t &ret_value, FORCE_INLINE int FloatTS2DIFFDecoder::read_float(float &ret_value, common::ByteStream &in) { ret_value = decode(in); - return common::E_OK; + return error_info::E_OK; } FORCE_INLINE int FloatTS2DIFFDecoder::read_double(double &ret_value, common::ByteStream &in) { @@ -323,7 +323,7 @@ FORCE_INLINE int DoubleTS2DIFFDecoder::read_float(float &ret_value, FORCE_INLINE int DoubleTS2DIFFDecoder::read_double(double &ret_value, common::ByteStream &in) { ret_value = decode(in); - return common::E_OK; + return error_info::E_OK; } } // end namespace storage diff --git a/cpp/src/encoding/ts2diff_encoder.h b/cpp/src/encoding/ts2diff_encoder.h index db3a28303..8bd35d724 100644 --- a/cpp/src/encoding/ts2diff_encoder.h +++ b/cpp/src/encoding/ts2diff_encoder.h @@ -196,7 +196,7 @@ int TS2DIFFEncoder::do_encode(T value, common::ByteStream &out_stream) { first_value_ = value; previous_value_ = first_value_; write_index_++; - return common::E_OK; + return error_info::E_OK; } // Calculate the delta between the current value and the previous_value_ T delta = value - previous_value_; @@ -218,14 +218,14 @@ int TS2DIFFEncoder::do_encode(T value, common::ByteStream &out_stream) { if (write_index_ >= block_size_) { return flush(out_stream); } - return common::E_OK; + return error_info::E_OK; } template <> inline int TS2DIFFEncoder::flush(common::ByteStream &out_stream) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (write_index_ == -1) { - return common::E_OK; + return error_info::E_OK; } // Subtract the minimum value for each delta_arr_ item SIMDOps::rebase(delta_arr_, delta_arr_min_, write_index_); @@ -247,9 +247,9 @@ inline int TS2DIFFEncoder::flush(common::ByteStream &out_stream) { template <> inline int TS2DIFFEncoder::flush(common::ByteStream &out_stream) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (write_index_ == -1) { - return common::E_OK; + return error_info::E_OK; } // Subtract the minimum value for each delta_arr_ item SIMDOps::rebase(delta_arr_, delta_arr_min_, write_index_); diff --git a/cpp/src/encoding/zigzag_encoder.h b/cpp/src/encoding/zigzag_encoder.h index 887ba07dc..929a72994 100644 --- a/cpp/src/encoding/zigzag_encoder.h +++ b/cpp/src/encoding/zigzag_encoder.h @@ -107,7 +107,7 @@ int ZigzagEncoder::encode(int32_t value) { write_byte_without_subsequence(value_zigzag); value_zigzag = (uint32_t)value_zigzag >> 7; - return common::E_OK; + return error_info::E_OK; } template <> @@ -130,7 +130,7 @@ int ZigzagEncoder::encode(int64_t value) { write_byte_without_subsequence(value_zigzag); value_zigzag = (uint64_t)value_zigzag >> 7; - return common::E_OK; + return error_info::E_OK; } template <> @@ -144,7 +144,7 @@ int ZigzagEncoder::flush(common::ByteStream &out) { flush_byte(out); } reset(); - return common::E_OK; + return error_info::E_OK; } template <> @@ -158,7 +158,7 @@ int ZigzagEncoder::flush(common::ByteStream &out) { flush_byte(out); } reset(); - return common::E_OK; + return error_info::E_OK; } typedef ZigzagEncoder IntZigzagEncoder; diff --git a/cpp/src/file/read_file.cc b/cpp/src/file/read_file.cc index cc08bf25b..c47268d65 100644 --- a/cpp/src/file/read_file.cc +++ b/cpp/src/file/read_file.cc @@ -65,7 +65,7 @@ int ReadFile::open(const std::string &file_path) { return ret; } -int ReadFile::get_file_size(int32_t &file_size) { +int64_t ReadFile::get_file_size(int64_t &file_size) { struct stat s; if (fstat(fd_, &s) < 0) { LOGE("fstat error, file_path=" << file_path_.c_str() << "fd=" << fd_ @@ -109,7 +109,7 @@ int ReadFile::check_file_magic() { return ret; } -int ReadFile::read(int32_t offset, char *buf, int32_t buf_size, +int ReadFile::read(int64_t offset, char *buf, int32_t buf_size, int32_t &read_len) { int ret = E_OK; read_len = 0; diff --git a/cpp/src/file/read_file.h b/cpp/src/file/read_file.h index a06482842..e5fa32723 100644 --- a/cpp/src/file/read_file.h +++ b/cpp/src/file/read_file.h @@ -24,7 +24,7 @@ #include -#include "utils/errno_define.h" +#include "common/error_info/errno_define.h" #include "utils/util_define.h" namespace storage { @@ -37,19 +37,19 @@ class ReadFile { int open(const std::string &file_path); FORCE_INLINE bool is_opened() const { return fd_ > 0; } - FORCE_INLINE int32_t file_size() const { return file_size_; } + FORCE_INLINE int64_t file_size() const { return file_size_; } FORCE_INLINE const std::string &file_path() const { return file_path_; } /* * try to reader @buf_size bytes from @offset of this file * into @buf. @read_len return the actual len reader. */ - int read(int32_t offset, char *buf, int32_t buf_size, + int read(int64_t offset, char *buf, int32_t buf_size, int32_t &ret_read_len); void close(); private: - int get_file_size(int32_t &file_size); + int64_t get_file_size(int64_t &file_size); int check_file_magic(); private: @@ -59,7 +59,7 @@ class ReadFile { private: std::string file_path_; int fd_; - int32_t file_size_; + int64_t file_size_; }; } // end namespace storage diff --git a/cpp/src/file/tsfile_io_reader.cc b/cpp/src/file/tsfile_io_reader.cc index 994c2ace6..71a2cbf4d 100644 --- a/cpp/src/file/tsfile_io_reader.cc +++ b/cpp/src/file/tsfile_io_reader.cc @@ -136,7 +136,7 @@ int TsFileIOReader::load_tsfile_meta() { int ret = E_OK; uint32_t tsfile_meta_size = 0; - int32_t read_offset = 0; + int64_t read_offset = 0; int32_t ret_read_len = 0; // Step 1: reader the tsfile_meta_size @@ -386,8 +386,8 @@ int TsFileIOReader::load_all_measurement_index_entry( return ret; } -int TsFileIOReader::read_device_meta_index(int32_t start_offset, - int32_t end_offset, +int TsFileIOReader::read_device_meta_index(int64_t start_offset, + int64_t end_offset, common::PageArena &pa, MetaIndexNode *&device_meta_index, bool leaf) { diff --git a/cpp/src/file/tsfile_io_reader.h b/cpp/src/file/tsfile_io_reader.h index 709dd3543..13978cd34 100644 --- a/cpp/src/file/tsfile_io_reader.h +++ b/cpp/src/file/tsfile_io_reader.h @@ -75,7 +75,7 @@ class TsFileIOReader { int get_chunk_metadata_list(IDeviceID device_id, std::string measurement, std::vector &chunk_meta_list); - int read_device_meta_index(int32_t start_offset, int32_t end_offset, + int read_device_meta_index(int64_t start_offset, int64_t end_offset, common::PageArena &pa, MetaIndexNode *&device_meta_index, bool leaf); int get_timeseries_indexes( @@ -85,7 +85,7 @@ class TsFileIOReader { common::PageArena &pa); private: - FORCE_INLINE int32_t file_size() const { return read_file_->file_size(); } + FORCE_INLINE int64_t file_size() const { return read_file_->file_size(); } int load_tsfile_meta(); diff --git a/cpp/src/file/tsfile_io_writer.cc b/cpp/src/file/tsfile_io_writer.cc index f65aebc2d..4b058a24f 100644 --- a/cpp/src/file/tsfile_io_writer.cc +++ b/cpp/src/file/tsfile_io_writer.cc @@ -106,11 +106,11 @@ int TsFileIOWriter::start_file() { int TsFileIOWriter::start_flush_chunk_group( std::shared_ptr device_name, bool is_aligned) { int ret = write_byte(CHUNK_GROUP_HEADER_MARKER); - if (ret != common::E_OK) { + if (ret != error_info::E_OK) { return ret; } ret = device_name->serialize(write_stream_); - if (ret != common::E_OK) { + if (ret != error_info::E_OK) { return ret; } is_aligned_ = is_aligned; @@ -243,7 +243,7 @@ int TsFileIOWriter::end_flush_chunk_group(bool is_aligned) { if (use_prev_alloc_cgm_) { cur_chunk_group_meta_ = nullptr; - return common::E_OK; + return error_info::E_OK; } int ret = chunk_group_meta_list_.push_back(cur_chunk_group_meta_); cur_chunk_group_meta_ = nullptr; diff --git a/cpp/src/file/write_file.cc b/cpp/src/file/write_file.cc index c8c2cbbaf..03c13b8ed 100644 --- a/cpp/src/file/write_file.cc +++ b/cpp/src/file/write_file.cc @@ -28,8 +28,8 @@ #include #include "common/config/config.h" +#include "common/error_info/errno_define.h" #include "common/logger/elog.h" -#include "utils/errno_define.h" #ifdef _WIN32 int fsync(int); diff --git a/cpp/src/file/write_file.h b/cpp/src/file/write_file.h index 3346d7ad5..e2112ee7b 100644 --- a/cpp/src/file/write_file.h +++ b/cpp/src/file/write_file.h @@ -42,7 +42,7 @@ class WriteFile { int create(const std::string &file_name, int flags, mode_t mode); bool file_opened() const { return fd_ > 0; } int write(const char *buf, uint32_t len); - // int flush() { return common::E_OK; } // TODO + // int flush() { return error_info::E_OK; } // TODO int sync(); int close(); FORCE_INLINE std::string get_file_path() { return path_; } diff --git a/cpp/src/parser/path_nodes_generator.cpp b/cpp/src/parser/path_nodes_generator.cpp index 55fe03547..0f2b7e431 100644 --- a/cpp/src/parser/path_nodes_generator.cpp +++ b/cpp/src/parser/path_nodes_generator.cpp @@ -20,12 +20,11 @@ #include #include - #include "generated/PathLexer.h" #include "generated/PathParser.h" #include "path_parser_error.h" #include "path_visitor.h" -#include "utils/errno_define.h" +#include "common/error_info/error_info.h" namespace storage { std::vector PathNodesGenerator::invokeParser( diff --git a/cpp/src/reader/aligned_chunk_reader.cc b/cpp/src/reader/aligned_chunk_reader.cc index 230661f3b..3a8b0f3a7 100644 --- a/cpp/src/reader/aligned_chunk_reader.cc +++ b/cpp/src/reader/aligned_chunk_reader.cc @@ -50,14 +50,18 @@ void AlignedChunkReader::reset() { cur_time_page_header_.reset(); cur_value_page_header_.reset(); - char *file_data_buf = time_in_stream_.get_wrapped_buf(); - if (file_data_buf != nullptr) { - mem_free(file_data_buf); + if (time_in_stream_.total_size() != 0) { + char *file_data_buf = time_in_stream_.get_wrapped_buf(); + if (file_data_buf != nullptr) { + mem_free(file_data_buf); + } } time_in_stream_.reset(); - file_data_buf = value_in_stream_.get_wrapped_buf(); - if (file_data_buf != nullptr) { - mem_free(file_data_buf); + if (value_in_stream_.total_size() != 0) { + char *file_data_buf = value_in_stream_.get_wrapped_buf(); + if (file_data_buf != nullptr) { + mem_free(file_data_buf); + } } value_in_stream_.reset(); file_data_time_buf_size_ = 0; @@ -87,17 +91,23 @@ void AlignedChunkReader::destroy() { CompressorFactory::free(value_compressor_); value_compressor_ = nullptr; } - char *buf = time_in_stream_.get_wrapped_buf(); - if (buf != nullptr) { - mem_free(buf); + + if (time_in_stream_.total_size() != 0) { + char *file_data_buf = time_in_stream_.get_wrapped_buf(); + if (file_data_buf != nullptr) { + mem_free(file_data_buf); + } time_in_stream_.clear_wrapped_buf(); } - cur_time_page_header_.reset(); - buf = value_in_stream_.get_wrapped_buf(); - if (buf != nullptr) { - mem_free(buf); + time_in_stream_.reset(); + if (value_in_stream_.total_size() != 0) { + char *file_data_buf = value_in_stream_.get_wrapped_buf(); + if (file_data_buf != nullptr) { + mem_free(file_data_buf); + } value_in_stream_.clear_wrapped_buf(); } + cur_time_page_header_.reset(); cur_value_page_header_.reset(); chunk_header_.~ChunkHeader(); } @@ -299,10 +309,11 @@ int AlignedChunkReader::read_from_file_and_rewrap( int ret = E_OK; const int DEFAULT_READ_SIZE = 4096; // may use page_size + page_header_size char *file_data_buf = in_stream_.get_wrapped_buf(); - int offset = chunk_meta->offset_of_chunk_header_ + chunk_visit_offset; + int64_t offset = chunk_meta->offset_of_chunk_header_ + chunk_visit_offset; int read_size = (want_size < DEFAULT_READ_SIZE ? DEFAULT_READ_SIZE : want_size); - if (file_data_buf_size < read_size || (may_shrink && read_size < file_data_buf_size / 10)) { + if (file_data_buf_size < read_size || + (may_shrink && read_size < file_data_buf_size / 10)) { file_data_buf = (char *)mem_realloc(file_data_buf, read_size); if (IS_NULL(file_data_buf)) { return E_OOM; @@ -366,7 +377,6 @@ int AlignedChunkReader::decode_cur_time_page_data() { uint32_t time_compressed_buf_size = 0; uint32_t time_uncompressed_buf_size = 0; - // Step 2: do uncompress if (IS_SUCC(ret)) { time_compressed_buf = @@ -485,7 +495,7 @@ int AlignedChunkReader::decode_cur_value_page_data() { int AlignedChunkReader::decode_time_value_buf_into_tsblock( TsBlock *&ret_tsblock, Filter *filter) { - int ret = common::E_OK; + int ret = error_info::E_OK; ret = decode_tv_buf_into_tsblock_by_datatype(time_in_, value_in_, ret_tsblock, filter); // if we return during @decode_tv_buf_into_tsblock, we should keep @@ -519,9 +529,9 @@ int AlignedChunkReader::decode_time_value_buf_into_tsblock( uint32_t mask = 1 << 7; \ int64_t time = 0; \ CppType value; \ - while ((time_decoder_->has_remaining() || time_in.has_remaining()) \ - && (value_decoder_->has_remaining() || \ - value_in.has_remaining())){ \ + while ( \ + (time_decoder_->has_remaining() || time_in.has_remaining()) && \ + (value_decoder_->has_remaining() || value_in.has_remaining())) { \ cur_value_index++; \ if (((value_page_col_notnull_bitmap_[cur_value_index / 8] & \ 0xFF) & \ @@ -530,8 +540,7 @@ int AlignedChunkReader::decode_time_value_buf_into_tsblock( if (ret != E_OK) { \ break; \ } \ - ret = value_decoder_->read_##ReadType(value, \ - value_in); \ + ret = value_decoder_->read_##ReadType(value, value_in); \ if (ret != E_OK) { \ break; \ } \ @@ -539,7 +548,7 @@ int AlignedChunkReader::decode_time_value_buf_into_tsblock( } \ if (UNLIKELY(!row_appender.add_row())) { \ ret = E_OVERFLOW; \ - cur_value_index--; \ + cur_value_index--; \ break; \ } else if (RET_FAIL(time_decoder_->read_int64(time, time_in))) { \ } else if (RET_FAIL(value_decoder_->read_##ReadType(value, \ diff --git a/cpp/src/reader/aligned_chunk_reader.h b/cpp/src/reader/aligned_chunk_reader.h index 58898f7d4..d39dd407e 100644 --- a/cpp/src/reader/aligned_chunk_reader.h +++ b/cpp/src/reader/aligned_chunk_reader.h @@ -76,6 +76,15 @@ class AlignedChunkReader : public IChunkReader { int get_next_page(common::TsBlock *tsblock, Filter *oneshoot_filter, common::PageArena &pa) override; + bool should_skip(Filter *filter) override { + if (filter != nullptr && time_chunk_meta_ != nullptr && + time_chunk_meta_->statistic_ != nullptr && + !filter->satisfy(time_chunk_meta_->statistic_)) { + return true; + } + return false; + } + private: FORCE_INLINE bool chunk_has_only_one_page( const ChunkHeader &chunk_header) const { diff --git a/cpp/src/reader/block/device_ordered_tsblock_reader.cc b/cpp/src/reader/block/device_ordered_tsblock_reader.cc index 57f0bfdcc..67f67ae89 100644 --- a/cpp/src/reader/block/device_ordered_tsblock_reader.cc +++ b/cpp/src/reader/block/device_ordered_tsblock_reader.cc @@ -22,9 +22,9 @@ namespace storage { int DeviceOrderedTsBlockReader::has_next(bool &has_next) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (current_reader_ != nullptr && IS_SUCC(current_reader_->has_next(has_next)) && has_next) { - return common::E_OK; + return error_info::E_OK; } if (current_reader_ != nullptr) { delete current_reader_; @@ -71,7 +71,7 @@ int DeviceOrderedTsBlockReader::has_next(bool &has_next) { } int DeviceOrderedTsBlockReader::next(common::TsBlock *&ret_block) { - int ret = common::E_OK; + int ret = error_info::E_OK; bool next = false; if (RET_FAIL(has_next(next)) || !next) { } else if (RET_FAIL(current_reader_->next(ret_block))) { diff --git a/cpp/src/reader/block/single_device_tsblock_reader.cc b/cpp/src/reader/block/single_device_tsblock_reader.cc index 507597c01..c4501cd66 100644 --- a/cpp/src/reader/block/single_device_tsblock_reader.cc +++ b/cpp/src/reader/block/single_device_tsblock_reader.cc @@ -35,7 +35,7 @@ SingleDeviceTsBlockReader::SingleDeviceTsBlockReader( int SingleDeviceTsBlockReader::init(DeviceQueryTask* device_query_task, uint32_t block_size, Filter* time_filter, Filter* field_filter) { - int ret = common::E_OK; + int ret = error_info::E_OK; pa_.init(512, common::AllocModID::MOD_TSFILE_READER); tuple_desc_.reset(); auto table_schema = device_query_task->get_table_schema(); @@ -76,7 +76,7 @@ int SingleDeviceTsBlockReader::init(DeviceQueryTask* device_query_task, if (field_column_contexts_.empty()) { delete current_block_; current_block_ = nullptr; - return common::E_OK; + return error_info::E_OK; } for (const auto& id_column : @@ -95,12 +95,12 @@ int SingleDeviceTsBlockReader::init(DeviceQueryTask* device_query_task, int SingleDeviceTsBlockReader::has_next(bool& has_next) { if (!last_block_returned_) { has_next = true; - return common::E_OK; + return error_info::E_OK; } if (field_column_contexts_.empty()) { has_next = false; - return common::E_OK; + return error_info::E_OK; } for (auto col_appender : col_appenders_) { @@ -130,7 +130,7 @@ int SingleDeviceTsBlockReader::has_next(bool& has_next) { } if (IS_FAIL(fill_measurements(min_time_columns))) { has_next = false; - return common::E_OK; + return error_info::E_OK; } else { next_time_set = false; next_time_ = -1; @@ -140,7 +140,7 @@ int SingleDeviceTsBlockReader::has_next(bool& has_next) { break; } } - int ret = common::E_OK; + int ret = error_info::E_OK; if (current_block_->get_row_count() > 0) { if (RET_FAIL(fill_ids())) { return ret; @@ -156,7 +156,7 @@ int SingleDeviceTsBlockReader::has_next(bool& has_next) { int SingleDeviceTsBlockReader::fill_measurements( std::vector& column_contexts) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (field_filter_ == nullptr /*TODO: || field_filter_->satisfy(column_contexts)*/) { row_appender_->add_row(); @@ -180,7 +180,7 @@ int SingleDeviceTsBlockReader::advance_column( int ret = column_context->move_iter(); if (ret == common::E_NO_MORE_DATA) { column_context->remove_from(field_column_contexts_); - ret = common::E_OK; + ret = error_info::E_OK; } return ret; } @@ -195,7 +195,7 @@ void SingleMeasurementColumnContext::remove_from( } int SingleDeviceTsBlockReader::fill_ids() { - int ret = common::E_OK; + int ret = error_info::E_OK; for (const auto& entry : id_column_contexts_) { const auto& id_column_context = entry.second; for (int32_t pos : id_column_context.pos_in_result_) { @@ -205,7 +205,7 @@ int SingleDeviceTsBlockReader::fill_ids() { if (device_tag == nullptr) { ret = col_appenders_[pos + 1]->fill_null( current_block_->get_row_count()); - if (ret != common::E_OK) { + if (ret != error_info::E_OK) { return ret; } continue; @@ -229,7 +229,7 @@ int SingleDeviceTsBlockReader::next(common::TsBlock*& ret_block) { } last_block_returned_ = true; ret_block = current_block_; - return common::E_OK; + return error_info::E_OK; } void SingleDeviceTsBlockReader::close() { @@ -257,7 +257,7 @@ void SingleDeviceTsBlockReader::close() { int SingleDeviceTsBlockReader::construct_column_context( const ITimeseriesIndex* time_series_index, Filter* time_filter) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (time_series_index == nullptr || (time_series_index->get_data_type() != common::TSDataType::VECTOR && time_series_index->get_chunk_meta_list()->empty())) { @@ -307,7 +307,7 @@ int SingleMeasurementColumnContext::init( DeviceQueryTask* device_query_task, const ITimeseriesIndex* time_series_index, Filter* time_filter, const std::vector& pos_in_result, common::PageArena& pa) { - int ret = common::E_OK; + int ret = error_info::E_OK; pos_in_result_ = pos_in_result; column_name_ = time_series_index->get_measurement_name().to_std_string(); if (RET_FAIL(tsfile_io_reader_->alloc_ssi( @@ -320,7 +320,7 @@ int SingleMeasurementColumnContext::init( } int SingleMeasurementColumnContext::get_next_tsblock(bool alloc_mem) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (tsblock_ != nullptr) { if (time_iter_) { delete time_iter_; @@ -358,7 +358,7 @@ int SingleMeasurementColumnContext::get_current_time(int64_t& time) { } uint32_t len = 0; time = *(int64_t*)(time_iter_->read(&len)); - return common::E_OK; + return error_info::E_OK; } int SingleMeasurementColumnContext::get_current_value(char*& value, @@ -368,11 +368,11 @@ int SingleMeasurementColumnContext::get_current_value(char*& value, } value = value_iter_->read(&len); assert(value != nullptr); - return common::E_OK; + return error_info::E_OK; } int SingleMeasurementColumnContext::move_iter() { - int ret = common::E_OK; + int ret = error_info::E_OK; time_iter_->next(); value_iter_->next(); if (time_iter_->end()) { diff --git a/cpp/src/reader/block/tsblock_reader.h b/cpp/src/reader/block/tsblock_reader.h index de6d98b5c..ce6de8f9a 100644 --- a/cpp/src/reader/block/tsblock_reader.h +++ b/cpp/src/reader/block/tsblock_reader.h @@ -35,10 +35,10 @@ class EmptyTsBlockReader : public TsBlockReader { EmptyTsBlockReader() = default; int has_next(bool &has_next) override { has_next = false; - return common::E_OK; + return error_info::E_OK; } - int next(common::TsBlock *&ret_block) override { return common::E_OK; } + int next(common::TsBlock *&ret_block) override { return error_info::E_OK; } }; } // namespace storage diff --git a/cpp/src/reader/bloom_filter.cc b/cpp/src/reader/bloom_filter.cc index 174791dde..5093615c7 100644 --- a/cpp/src/reader/bloom_filter.cc +++ b/cpp/src/reader/bloom_filter.cc @@ -70,7 +70,7 @@ double math_log(double in) { #endif /* ================ BitSet ================ */ -void BitSet::to_bytes(uint8_t *&ret_bytes, int32_t &ret_len) const { +void BitSet::to_bytes(uint8_t *&ret_bytes, size_t &ret_len) const { int32_t words_in_use = get_words_in_use(); if (words_in_use == 0) { return; @@ -83,20 +83,21 @@ void BitSet::to_bytes(uint8_t *&ret_bytes, int32_t &ret_len) const { x = x >> 8; } - uint8_t *res = - (uint8_t *)mem_alloc(sizeof(uint8_t) * len, MOD_BLOOM_FILTER); + auto *res = + static_cast( + mem_alloc(sizeof(uint8_t) * len, MOD_BLOOM_FILTER)); int32_t res_pos = 0; for (int32_t w = 0; w < words_in_use - 1; w++) { uint64_t word = words_[w]; for (int b = 0; b < 8; b++) { - *(res + res_pos) = (uint8_t)(word & 0xFF); + *(res + res_pos) = static_cast(word & 0xFF); word = word >> 8; res_pos++; } } uint64_t last_word = words_[words_in_use - 1]; for (; res_pos < len; res_pos++) { - *(res + res_pos) = (uint8_t)(last_word & 0xFF); + *(res + res_pos) = static_cast(last_word & 0xFF); last_word = last_word >> 8; } @@ -195,7 +196,7 @@ int BloomFilter::add_path_entry(const String &device_name, String entry = get_entry_string(device_name, measurement_name); if (IS_NULL(entry.buf_)) { - return E_OOM; + return error_info::E_OOM; } for (uint32_t i = 0; i < hash_func_count_; i++) { @@ -204,13 +205,13 @@ int BloomFilter::add_path_entry(const String &device_name, bitset_.set(hv); } free_entry_buf(entry.buf_); - return E_OK; + return error_info::E_OK; } int BloomFilter::serialize_to(ByteStream &out) { - int ret = E_OK; + int ret = error_info::E_OK; uint8_t *filter_data_bytes = nullptr; - int32_t filter_data_bytes_len = 0; + size_t filter_data_bytes_len = 0; bitset_.to_bytes(filter_data_bytes, filter_data_bytes_len); if (RET_FAIL( SerializationUtil::write_var_uint(filter_data_bytes_len, out))) { @@ -227,7 +228,7 @@ int BloomFilter::serialize_to(ByteStream &out) { } int BloomFilter::deserialize_from(ByteStream &in) { - int ret = E_OK; + int ret = error_info::E_OK; uint32_t filter_data_bytes_len = 0; uint32_t ret_read_len = 0; uint8_t *filter_data = nullptr; diff --git a/cpp/src/reader/bloom_filter.h b/cpp/src/reader/bloom_filter.h index a43b264a0..756358d40 100644 --- a/cpp/src/reader/bloom_filter.h +++ b/cpp/src/reader/bloom_filter.h @@ -54,15 +54,16 @@ class BitSet { int init(int32_t size) { ASSERT(size > 1); word_count_ = (size - 1) / 64 + 1; - int32_t alloc_size = word_count_ * sizeof(uint64_t); - words_ = - (uint64_t *)common::mem_alloc(alloc_size, common::MOD_BLOOM_FILTER); + size_t alloc_size = static_cast(word_count_) * sizeof(uint64_t); + words_ = static_cast( + common::mem_alloc(alloc_size, common::MOD_BLOOM_FILTER)); if (IS_NULL(words_)) { - return common::E_OOM; + return error_info::E_OOM; } memset(words_, 0, alloc_size); - return common::E_OK; + return error_info::E_OK; } + void destroy() { if (!IS_NULL(words_)) { common::mem_free(words_); @@ -82,9 +83,9 @@ class BitSet { } return 0; } - void to_bytes(uint8_t *&ret_bytes, int32_t &ret_len) const; + void to_bytes(uint8_t *&ret_bytes, size_t &ret_len) const; void revert_bytes(uint8_t *bytes) { common::mem_free(bytes); } - int from_bytes(uint8_t *filter_data, uint32_t filter_data_bytes_len); + int from_bytes(uint8_t *filter_data, size_t filter_data_bytes_len); private: uint64_t *words_; diff --git a/cpp/src/reader/column_mapping.h b/cpp/src/reader/column_mapping.h index 97c2cc39e..3a73b4511 100644 --- a/cpp/src/reader/column_mapping.h +++ b/cpp/src/reader/column_mapping.h @@ -40,13 +40,13 @@ class ColumnMapping { field_columns_.insert(column_name); } - return common::E_OK; + return error_info::E_OK; } int add(const Expression &measurementFilter) { // TODO: get measurements in the filter and add them to // field_columns_ - return common::E_OK; + return error_info::E_OK; } const std::vector &get_column_pos( diff --git a/cpp/src/reader/device_meta_iterator.cc b/cpp/src/reader/device_meta_iterator.cc index 8140d3776..644fd9921 100644 --- a/cpp/src/reader/device_meta_iterator.cc +++ b/cpp/src/reader/device_meta_iterator.cc @@ -25,7 +25,7 @@ bool DeviceMetaIterator::has_next() { return true; } - if (load_results() != common::E_OK) { + if (load_results() != error_info::E_OK) { return false; } return !result_cache_.empty(); @@ -39,7 +39,7 @@ int DeviceMetaIterator::next( ret_meta = result_cache_.front(); result_cache_.pop(); - return common::E_OK; + return error_info::E_OK; } int DeviceMetaIterator::load_results() { @@ -65,11 +65,11 @@ int DeviceMetaIterator::load_results() { is_root_idx_node = false; } - return common::E_OK; + return error_info::E_OK; } int DeviceMetaIterator::load_leaf_device(MetaIndexNode* meta_index_node) { - int ret = common::E_OK; + int ret = error_info::E_OK; const auto& leaf_children = meta_index_node->children_; for (size_t i = 0; i < leaf_children.size(); i++) { std::shared_ptr child = leaf_children[i]; @@ -77,8 +77,8 @@ int DeviceMetaIterator::load_leaf_device(MetaIndexNode* meta_index_node) { if (id_filter_ != nullptr /*TODO: !id_filter_->satisfy(device_id)*/) { continue; } - int32_t start_offset = child->get_offset(); - int32_t end_offset = i + 1 < leaf_children.size() + int64_t start_offset = child->get_offset(); + int64_t end_offset = i + 1 < leaf_children.size() ? leaf_children[i + 1]->get_offset() : meta_index_node->end_offset_; MetaIndexNode* child_node = nullptr; @@ -94,7 +94,7 @@ int DeviceMetaIterator::load_leaf_device(MetaIndexNode* meta_index_node) { } int DeviceMetaIterator::load_internal_node(MetaIndexNode* meta_index_node) { - int ret = common::E_OK; + int ret = error_info::E_OK; const auto& internal_children = meta_index_node->children_; for (size_t i = 0; i < internal_children.size(); i++) { diff --git a/cpp/src/reader/filter/filter.h b/cpp/src/reader/filter/filter.h index 1846df5a4..0c2b34fc1 100644 --- a/cpp/src/reader/filter/filter.h +++ b/cpp/src/reader/filter/filter.h @@ -35,22 +35,31 @@ class Filter { virtual ~Filter() {} virtual bool satisfy(Statistic* statistic) { + UNUSED(statistic); ASSERT(false); return false; } virtual bool satisfy(int64_t time, int64_t value) { + UNUSED(time); + UNUSED(value); ASSERT(false); return false; } virtual bool satisfy(int64_t time, common::String value) { + UNUSED(time); + UNUSED(value); ASSERT(false); return false; } virtual bool satisfy_start_end_time(int64_t start_time, int64_t end_time) { + UNUSED(start_time); + UNUSED(end_time); ASSERT(false); return false; } virtual bool contain_start_end_time(int64_t start_time, int64_t end_time) { + UNUSED(start_time); + UNUSED(end_time); ASSERT(false); return false; } diff --git a/cpp/src/reader/ichunk_reader.h b/cpp/src/reader/ichunk_reader.h index b5f22b7de..e9cf0917e 100644 --- a/cpp/src/reader/ichunk_reader.h +++ b/cpp/src/reader/ichunk_reader.h @@ -33,7 +33,7 @@ class IChunkReader { IChunkReader() {} virtual int init(ReadFile *read_file, common::String m_name, common::TSDataType data_type, Filter *time_filter) { - return common::E_OK; + return error_info::E_OK; } virtual ~IChunkReader() = default; virtual void reset() {} @@ -49,10 +49,11 @@ class IChunkReader { virtual int get_next_page(common::TsBlock *tsblock, Filter *oneshoot_filter, common::PageArena &pa) { - return common::E_OK; + return error_info::E_OK; } virtual ChunkHeader &get_chunk_header() { return chunk_header_; } + virtual bool should_skip(Filter* filter) { return false; } protected: ChunkHeader chunk_header_; diff --git a/cpp/src/reader/meta_data_querier.cc b/cpp/src/reader/meta_data_querier.cc index 722464355..6b9b4c75b 100644 --- a/cpp/src/reader/meta_data_querier.cc +++ b/cpp/src/reader/meta_data_querier.cc @@ -66,7 +66,7 @@ std::map>> MetadataQuerier::get_chu int MetadataQuerier::get_whole_file_metadata(TsFileMeta* tsfile_meta) const { tsfile_meta = io_reader_->get_tsfile_meta(); - return common::E_OK; + return error_info::E_OK; } void MetadataQuerier::load_chunk_metadatas(const std::vector& paths) { diff --git a/cpp/src/reader/qds_with_timegenerator.cc b/cpp/src/reader/qds_with_timegenerator.cc index 0b5f6dffa..8b6b6b151 100644 --- a/cpp/src/reader/qds_with_timegenerator.cc +++ b/cpp/src/reader/qds_with_timegenerator.cc @@ -104,7 +104,7 @@ void *ValueAt::at(int64_t target_timestamp) { if (cur_time_ > target_timestamp) { return nullptr; } - int ret = common::E_OK; + int ret = error_info::E_OK; if (time_col_iter_ == nullptr) { tf_ = TimeFilter::gt_eq(target_timestamp); if (RET_FAIL(ssi_->get_next(tsblock_, (tsblock_ == nullptr), tf_))) { @@ -286,7 +286,7 @@ void Node::next_timestamp(int64_t beyond_this_time) { int QDSWithTimeGenerator::init(TsFileIOReader *io_reader, QueryExpression *qe) { pa_.reset(); pa_.init(512, common::MOD_TSFILE_READER); - int ret = common::E_OK; // cppcheck-suppress unreadVariable + int ret = error_info::E_OK; // cppcheck-suppress unreadVariable io_reader_ = io_reader; qe_ = qe; std::vector paths = qe_->selected_series_; diff --git a/cpp/src/reader/scan_iterator.h b/cpp/src/reader/scan_iterator.h index 3a15eb051..3812d9767 100644 --- a/cpp/src/reader/scan_iterator.h +++ b/cpp/src/reader/scan_iterator.h @@ -150,7 +150,7 @@ class DataScanIterator { data_run_list_(&page_arena_), cursor_() {} ~DataScanIterator() {} - int init() { return common::E_OK; } + int init() { return error_info::E_OK; } void destory() { close(); page_arena_.destroy(); diff --git a/cpp/src/reader/table_query_executor.cc b/cpp/src/reader/table_query_executor.cc index 77ecc9291..2e704ea60 100644 --- a/cpp/src/reader/table_query_executor.cc +++ b/cpp/src/reader/table_query_executor.cc @@ -24,7 +24,7 @@ int TableQueryExecutor::query(const std::string &table_name, const std::vector &columns, Filter *time_filter, Filter *id_filter, Filter *field_filter, ResultSet *&ret_qds) { - int ret = common::E_OK; + int ret = error_info::E_OK; TsFileMeta *file_metadata = nullptr; file_metadata = tsfile_io_reader_->get_tsfile_meta(); common::PageArena pa; diff --git a/cpp/src/reader/table_query_executor.h b/cpp/src/reader/table_query_executor.h index 83a82fe56..cc8843569 100644 --- a/cpp/src/reader/table_query_executor.h +++ b/cpp/src/reader/table_query_executor.h @@ -19,6 +19,7 @@ #ifndef READER_TABLE_QUERY_EXECUTOR_H #define READER_TABLE_QUERY_EXECUTOR_H +#include "common/error_info/errno_define.h" #include "common/schema.h" #include "expression.h" #include "imeta_data_querier.h" diff --git a/cpp/src/reader/table_result_set.cc b/cpp/src/reader/table_result_set.cc index 396913e9b..2102a25b6 100644 --- a/cpp/src/reader/table_result_set.cc +++ b/cpp/src/reader/table_result_set.cc @@ -37,7 +37,7 @@ void TableResultSet::init() { TableResultSet::~TableResultSet() { close(); } int TableResultSet::next(bool& has_next) { - int ret = common::E_OK; + int ret = error_info::E_OK; while (row_iterator_ == nullptr || !row_iterator_->has_next()) { if (RET_FAIL(tsblock_reader_->has_next(has_next))) { return ret; diff --git a/cpp/src/reader/task/device_task_iterator.cc b/cpp/src/reader/task/device_task_iterator.cc index 18874eee8..6566b46ca 100644 --- a/cpp/src/reader/task/device_task_iterator.cc +++ b/cpp/src/reader/task/device_task_iterator.cc @@ -25,7 +25,7 @@ bool DeviceTaskIterator::has_next() const { } int DeviceTaskIterator::next(DeviceQueryTask *&task) { - int ret = common::E_OK; + int ret = error_info::E_OK; std::pair, MetaIndexNode *> device_meta_pair; if (RET_FAIL(device_meta_iterator_->next(device_meta_pair))) { } else { diff --git a/cpp/src/reader/tsfile_series_scan_iterator.cc b/cpp/src/reader/tsfile_series_scan_iterator.cc index b1be6835d..c7c6b42ce 100644 --- a/cpp/src/reader/tsfile_series_scan_iterator.cc +++ b/cpp/src/reader/tsfile_series_scan_iterator.cc @@ -42,44 +42,54 @@ int TsFileSeriesScanIterator::get_next(TsBlock *&ret_tsblock, bool alloc, int ret = E_OK; Filter *filter = (oneshoot_filter != nullptr) ? oneshoot_filter : time_filter_; - if (!chunk_reader_->has_more_data()) { - while (true) { - if (!has_next_chunk()) { - return E_NO_MORE_DATA; - } else { + if (alloc) { + ret_tsblock = alloc_tsblock(); + } + + if (chunk_reader_->should_skip(filter)) { + chunk_reader_->reset(); + } + + while (true) { + if (!chunk_reader_->has_more_data()) { + while (true) { + if (!has_next_chunk()) { + return E_NO_MORE_DATA; + } + ChunkMeta *cm = nullptr; + ChunkMeta *time_cm = nullptr; + ChunkMeta *value_cm = nullptr; + if (!is_aligned_) { + cm = get_current_chunk_meta(); + } else { + time_cm = time_chunk_meta_cursor_.get(); + value_cm = value_chunk_meta_cursor_.get(); + cm = time_cm; + } + advance_to_next_chunk(); + if (filter != nullptr && cm->statistic_ != nullptr && !filter->satisfy(cm->statistic_)) { + continue; + } + chunk_reader_->reset(); if (!is_aligned_) { - ChunkMeta *cm = get_current_chunk_meta(); - advance_to_next_chunk(); - if (filter != nullptr && cm->statistic_ != nullptr && - !filter->satisfy(cm->statistic_)) { - continue; - } - chunk_reader_->reset(); if (RET_FAIL(chunk_reader_->load_by_meta(cm))) { + return ret; } - break; } else { - ChunkMeta *value_cm = value_chunk_meta_cursor_.get(); - ChunkMeta *time_cm = time_chunk_meta_cursor_.get(); - advance_to_next_chunk(); - if (filter != nullptr && value_cm->statistic_ != nullptr && - !filter->satisfy(value_cm->statistic_)) { - continue; + if (RET_FAIL(chunk_reader_->load_by_aligned_meta(time_cm, value_cm))) { + return ret; } - chunk_reader_->reset(); - if (RET_FAIL(chunk_reader_->load_by_aligned_meta( - time_cm, value_cm))) { - } - break; } + break; } } - } - if (IS_SUCC(ret)) { - if (alloc) { - ret_tsblock = alloc_tsblock(); - } + ret = chunk_reader_->get_next_page(ret_tsblock, filter, *data_pa_); + if (ret == E_NO_MORE_DATA) { + continue; + } else { + return ret; + } } return ret; } diff --git a/cpp/src/reader/tsfile_series_scan_iterator.h b/cpp/src/reader/tsfile_series_scan_iterator.h index e31c3e298..2da8f686c 100644 --- a/cpp/src/reader/tsfile_series_scan_iterator.h +++ b/cpp/src/reader/tsfile_series_scan_iterator.h @@ -59,7 +59,7 @@ class TsFileSeriesScanIterator { read_file_ = read_file; time_filter_ = time_filter; data_pa_ = &data_pa; - return common::E_OK; + return error_info::E_OK; } void destroy(); /* diff --git a/cpp/src/utils/db_utils.h b/cpp/src/utils/db_utils.h index 01a79b7d7..e54f9ded4 100644 --- a/cpp/src/utils/db_utils.h +++ b/cpp/src/utils/db_utils.h @@ -20,9 +20,9 @@ #ifndef UTILS_UTILS_H #define UTILS_UTILS_H -#include -#include -#include // memcpy +#include +#include +#include #include #include @@ -100,11 +100,6 @@ struct TsID { measurement_nid_ = 0; } - bool is_valid() const { - // TODO - return true; - } - FORCE_INLINE bool operator==(const TsID &other) const { return db_nid_ == other.db_nid_ && device_nid_ == other.device_nid_ && measurement_nid_ == other.measurement_nid_; @@ -125,7 +120,7 @@ struct TsID { return to_int64() < that.to_int64(); } - FORCE_INLINE bool operator>(const TsID &other) { + FORCE_INLINE bool operator>(const TsID &other) const { return to_int64() > other.to_int64(); } @@ -136,17 +131,8 @@ struct TsID { } FORCE_INLINE void to_string(char *print_buf, int len) const { - snprintf(print_buf, len, "<%d,%d,%d>", db_nid_, device_nid_, - measurement_nid_); - } - FORCE_INLINE std::string to_string() const { - const int buf_len = 32; - char buf[buf_len]; - snprintf(buf, buf_len, "<%d,%d,%d>", db_nid_, device_nid_, + snprintf(print_buf, static_cast(len), "<%d,%d,%d>", db_nid_, device_nid_, measurement_nid_); - // construct std::string will invoke memory allocation and copy. - // try to use first to_string instead. - return std::string(buf); } }; @@ -174,10 +160,11 @@ struct ColumnSchema { ColumnCategory column_category_; ColumnSchema() - : column_name_(""), - data_type_(INVALID_DATATYPE), + : data_type_(INVALID_DATATYPE), compression_(UNCOMPRESSED), - encoding_(PLAIN) {} + encoding_(PLAIN), + // Assume field by default; + column_category_(ColumnCategory::FIELD) {} /** * @brief Constructs a ColumnSchema object with the given parameters. @@ -241,57 +228,24 @@ struct ColumnSchema { // TODO } - void get_device_name(char *ret_device_name_buf, const int buf_len, - uint32_t &ret_len) const { - int pos = column_name_.find_last_of('.'); - ASSERT(pos > 0 && pos < buf_len); - memcpy(ret_device_name_buf, column_name_.c_str(), pos); - ret_device_name_buf[pos] = '\0'; - ret_len = pos; - } std::string get_device_name_str() const { - int pos = column_name_.find_last_of('.'); + size_t pos = column_name_.find_last_of('.'); ASSERT(pos > 0); return column_name_.substr(0, pos); } void get_device_name(String &device_name) const { - int pos = column_name_.find_last_of('.'); + size_t pos = column_name_.find_last_of('.'); ASSERT(pos > 0); const char *c_string = column_name_.c_str(); device_name.buf_ = (char *)c_string; - device_name.len_ = pos; - } - void get_measurement_name(char *ret_measurement_name_buf, const int buf_len, - uint32_t &ret_len) const { - int pos = column_name_.find_last_of('.'); - ASSERT(pos > 0 && pos < buf_len); - ret_len = column_name_.size() - pos - 1; - memcpy(ret_measurement_name_buf, column_name_.c_str() + pos + 1, - ret_len); - ret_measurement_name_buf[ret_len] = '\0'; + device_name.len_ = static_cast(pos); } + std::string get_measurement_name_str() const { - int pos = column_name_.find_last_of('.'); + size_t pos = column_name_.find_last_of('.'); ASSERT(pos > 0); return column_name_.substr(pos + 1, column_name_.size() - pos); } - // TODO remove - void get_measurement_name(String &measurement_name) const { - int pos = column_name_.find_last_of('.'); - ASSERT(pos > 0); - const char *c_string = column_name_.c_str(); - measurement_name.buf_ = (char *)c_string + pos + 1; - measurement_name.len_ = column_name_.size() - pos - 1; - } - String get_measurement_name() { - int pos = column_name_.find_last_of('.'); - ASSERT(pos > 0); - const char *c_string = column_name_.c_str(); - String res; - res.buf_ = (char *)c_string + pos + 1; - res.len_ = column_name_.size() - pos - 1; - return res; - } #ifdef DEBUG std::string debug_string() // for debug @@ -310,71 +264,13 @@ struct ColumnSchema { FORCE_INLINE int64_t get_cur_timestamp() { int64_t timestamp = 0; - struct timeval tv; - if (gettimeofday(&tv, NULL) >= 0) { + struct timeval tv{}; + if (gettimeofday(&tv, nullptr) >= 0) { timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; } return timestamp; } -#if 0 -struct DatabaseIdTTL -{ - NodeID db_nid_; - int64_t ttl_; - int16_t counter_; // suppose we at most support 64k timeseries. - DatabaseIdTTL() {} - DatabaseIdTTL(NodeID db_nid, int64_t ttl, int16_t counter) : db_nid_(db_nid), ttl_(ttl), counter_(counter) {} - DatabaseIdTTL(const DatabaseIdTTL &other) : db_nid_(other.db_nid_), ttl_(other.ttl_), counter_(other.counter_) {} - DatabaseIdTTL & operator = (const DatabaseIdTTL &other) - { - this->db_nid_ = other.db_nid_; - this->ttl_ = other.ttl_; - this->counter_ = other.counter_; - return *this; - } - bool operator == (const DatabaseIdTTL &other) - { - if (db_nid_ != other.db_nid_ || ttl_ != other.ttl_ || counter_ != other.counter_) { - return false; - } - return true; - } - friend std::ostream& operator << (std::ostream& out, DatabaseIdTTL& di) - { - - return out; - } -}; - -struct DeviceIDWithCounter -{ - NodeID device_nid_; - int16_t counter_; // suppose we at most support 64k timeseries. - DeviceIDWithCounter() {} - DeviceIDWithCounter(NodeID device_nid, int16_t counter) : device_nid_(device_nid), counter_(counter) {} - DeviceIDWithCounter(const DeviceIDWithCounter &other) : device_nid_(other.device_nid_), counter_(other.counter_) {} - DeviceIDWithCounter& operator = (const DeviceIDWithCounter &other) - { - this->device_nid_ = other.device_nid_; - this->counter_ = other.counter_; - return *this; - } - bool operator == (const DeviceID &other) - { - if (device_nid_ != other.device_nid_ || counter_ != other.counter_) { - return false; - } - return true; - } - friend std::ostream& operator << (std::ostream& out, DeviceID& di) - { - out << "(" << di.device_nid_ << ", " << di.counter_ << ") "; - return out; - } -}; -#endif - } // end namespace common #endif // UTILS_UTILS_H diff --git a/cpp/src/utils/errno_define.h b/cpp/src/utils/errno_define.h deleted file mode 100644 index 8d87ade96..000000000 --- a/cpp/src/utils/errno_define.h +++ /dev/null @@ -1,79 +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. - */ -#ifndef UTILS_ERRNO_DEFINE_H -#define UTILS_ERRNO_DEFINE_H - -namespace common { - -const int E_OK = 0; -const int E_OOM = 1; -const int E_NOT_EXIST = 2; -const int E_ALREADY_EXIST = 3; -const int E_INVALID_ARG = 4; -const int E_OUT_OF_RANGE = 5; -const int E_PARTIAL_READ = 6; -const int E_INVALID_SCHEMA = 8; -const int E_NET_EPOLL_ERR = 9; -const int E_NET_EPOLL_WAIT_ERR = 10; -const int E_NET_RECV_ERR = 11; -const int E_NET_ACCEPT_ERR = 12; -const int E_NET_FCNTL_ERR = 13; -const int E_NET_LISTEN_ERR = 14; -const int E_NET_SEND_ERR = 15; -const int E_PIPE_ERR = 16; -const int E_THREAD_CREATE_ERR = 17; -const int E_MUTEX_ERR = 18; -const int E_COND_ERR = 19; -const int E_OVERFLOW = 20; -const int E_NO_MORE_DATA = 21; -const int E_OUT_OF_ORDER = 22; -const int E_TSBLOCK_TYPE_NOT_SUPPORTED = 23; -const int E_TSBLOCK_DATA_INCONSISTENCY = 24; -const int E_DDL_UNKNOWN_TYPE = 25; -const int E_TYPE_NOT_SUPPORTED = 26; -const int E_TYPE_NOT_MATCH = 27; -const int E_FILE_OPEN_ERR = 28; -const int E_FILE_CLOSE_ERR = 29; -const int E_FILE_WRITE_ERR = 30; -const int E_FILE_READ_ERR = 31; -const int E_FILE_SYNC_ERR = 32; -const int E_TSFILE_WRITER_META_ERR = 33; -const int E_FILE_STAT_ERR = 34; -const int E_TSFILE_CORRUPTED = 35; -const int E_BUF_NOT_ENOUGH = 36; -const int E_INVALID_PATH = 37; -const int E_NOT_MATCH = 38; -const int E_JSON_INVALID = 39; -const int E_NOT_SUPPORT = 40; -const int E_PARSER_ERR = 41; -const int E_ANALYZE_ERR = 42; -const int E_INVALID_DATA_POINT = 43; -const int E_DEVICE_NOT_EXIST = 44; -const int E_MEASUREMENT_NOT_EXIST = 45; -const int E_INVALID_QUERY = 46; -const int E_SDK_QUERY_OPTIMIZE_ERR = 47; -const int E_COMPRESS_ERR = 48; -const int E_TABLE_NOT_EXIST = 49; -const int E_COLUMN_NOT_EXIST = 50; -const int E_UNSUPPORTED_ORDER = 51; -const int E_INVALID_NODE_TYPE = 52; - -} // end namespace common - -#endif // UTILS_ERRNO_DEFINE_H diff --git a/cpp/src/utils/storage_utils.h b/cpp/src/utils/storage_utils.h index a0c6f3b2b..f82c9e1c0 100644 --- a/cpp/src/utils/storage_utils.h +++ b/cpp/src/utils/storage_utils.h @@ -19,9 +19,9 @@ #ifndef UTILS_STORAGE_UTILS_H #define UTILS_STORAGE_UTILS_H -#include -#include #include +#include +#include #include "common/datatype/value.h" #include "common/tsblock/tsblock.h" @@ -78,15 +78,19 @@ FORCE_INLINE std::string get_file_path_from_file_id( } FORCE_INLINE static void to_lowercase_inplace(std::string &str) { - std::transform( - str.begin(), str.end(), str.begin(), - [](unsigned char c) -> unsigned char { return std::tolower(c); }); + std::transform(str.begin(), str.end(), str.begin(), + [](const char c) -> char { + return static_cast( + std::tolower(static_cast(c))); + }); } FORCE_INLINE static std::string to_lower(const std::string &str) { std::string result; - std::transform( - str.begin(), str.end(), std::back_inserter(result), - [](unsigned char c) -> unsigned char { return std::tolower(c); }); + std::transform(str.begin(), str.end(), std::back_inserter(result), + [](const char c) -> char { + return static_cast( + std::tolower(static_cast(c))); + }); return result; } diff --git a/cpp/src/utils/util_define.h b/cpp/src/utils/util_define.h index 9667de76f..a3a6ad136 100644 --- a/cpp/src/utils/util_define.h +++ b/cpp/src/utils/util_define.h @@ -123,12 +123,12 @@ #endif /* ======== return value check ======== */ -#define RET_FAIL(expr) UNLIKELY(common::E_OK != (ret = (expr))) -#define RFAIL(expr) UNLIKELY(common::E_OK != (ret = (expr))) -#define RET_SUCC(expr) LIKELY(common::E_OK == (ret = (expr))) -#define RSUCC(expr) LIKELY(common::E_OK != (ret = (exprt))) -#define IS_SUCC(ret) LIKELY(common::E_OK == (ret)) -#define IS_FAIL(ret) UNLIKELY(common::E_OK != (ret)) +#define RET_FAIL(expr) UNLIKELY(error_info::E_OK != (ret = (expr))) +#define RFAIL(expr) UNLIKELY(error_info::E_OK != (ret = (expr))) +#define RET_SUCC(expr) LIKELY(error_info::E_OK == (ret = (expr))) +#define RSUCC(expr) LIKELY(error_info::E_OK != (ret = (exprt))) +#define IS_SUCC(ret) LIKELY(error_info::E_OK == (ret)) +#define IS_FAIL(ret) UNLIKELY(error_info::E_OK != (ret)) #define IS_NULL(ptr) UNLIKELY((ptr) == nullptr) diff --git a/cpp/src/writer/chunk_writer.h b/cpp/src/writer/chunk_writer.h index 44eb06f57..667efd04a 100644 --- a/cpp/src/writer/chunk_writer.h +++ b/cpp/src/writer/chunk_writer.h @@ -30,7 +30,7 @@ namespace storage { #define CW_DO_WRITE_FOR_TYPE(TSDATATYPE) \ { \ - int ret = common::E_OK; \ + int ret = error_info::E_OK; \ if (UNLIKELY(data_type_ != TSDATATYPE)) { \ return common::E_TYPE_NOT_MATCH; \ } \ @@ -115,7 +115,7 @@ class ChunkWriter { if (UNLIKELY(is_cur_page_full())) { return seal_cur_page(false); } - return common::E_OK; + return error_info::E_OK; } FORCE_INLINE void free_first_writer_data() { // free memory diff --git a/cpp/src/writer/page_writer.h b/cpp/src/writer/page_writer.h index e60cc1200..8153a8eff 100644 --- a/cpp/src/writer/page_writer.h +++ b/cpp/src/writer/page_writer.h @@ -76,7 +76,7 @@ struct PageData { /* ================ PageWriter ================ */ #define PW_DO_WRITE_FOR_TYPE(TSDATATYPE) \ { \ - int ret = common::E_OK; \ + int ret = error_info::E_OK; \ /* std::cout << "page_writer writer: time=" << timestamp << ", value=" \ * << value << std::endl; */ \ if (UNLIKELY(data_type_ != TSDATATYPE)) { \ @@ -164,7 +164,7 @@ class PageWriter { private: FORCE_INLINE int prepare_end_page() { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(time_encoder_->flush(time_out_stream_))) { } else if (RET_FAIL(value_encoder_->flush(value_out_stream_))) { } diff --git a/cpp/src/writer/time_chunk_writer.cc b/cpp/src/writer/time_chunk_writer.cc index 892c0d1c1..d939b1cfd 100644 --- a/cpp/src/writer/time_chunk_writer.cc +++ b/cpp/src/writer/time_chunk_writer.cc @@ -186,6 +186,8 @@ int TimeChunkWriter::end_encode_chunk() { int64_t TimeChunkWriter::estimate_max_series_mem_size() { return chunk_data_.total_size() + time_page_writer_.estimate_max_mem_size() + + +first_page_data_.compressed_size_ + + (first_page_statistic_ != nullptr ? get_typed_statistic_sizeof(first_page_statistic_->get_type()) : 0) + PageHeader::estimat_max_page_header_size_without_statistics() + get_typed_statistic_sizeof( time_page_writer_.get_statistic()->get_type()); diff --git a/cpp/src/writer/time_chunk_writer.h b/cpp/src/writer/time_chunk_writer.h index e03b264c2..9a45d27d9 100644 --- a/cpp/src/writer/time_chunk_writer.h +++ b/cpp/src/writer/time_chunk_writer.h @@ -51,7 +51,7 @@ class TimeChunkWriter { storage::ChunkHeader get_chunk_header() const { return chunk_header_; } FORCE_INLINE int write(int64_t timestamp) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(time_page_writer_.write(timestamp))) { return ret; } @@ -80,7 +80,7 @@ class TimeChunkWriter { if (UNLIKELY(is_cur_page_full())) { return seal_cur_page(false); } - return common::E_OK; + return error_info::E_OK; } FORCE_INLINE void free_first_writer_data() { // free memory diff --git a/cpp/src/writer/time_page_writer.h b/cpp/src/writer/time_page_writer.h index bbf70165d..fee002398 100644 --- a/cpp/src/writer/time_page_writer.h +++ b/cpp/src/writer/time_page_writer.h @@ -72,7 +72,7 @@ class TimePageWriter { void destroy(); FORCE_INLINE int write(int64_t timestamp) { - int ret = common::E_OK; + int ret = error_info::E_OK; if (statistic_->count_ != 0 && is_inited_ && timestamp <= statistic_->end_time_) { return common::E_OUT_OF_ORDER; @@ -106,7 +106,7 @@ class TimePageWriter { private: FORCE_INLINE int prepare_end_page() { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(time_encoder_->flush(time_out_stream_))) { } return ret; diff --git a/cpp/src/writer/tsfile_table_writer.cc b/cpp/src/writer/tsfile_table_writer.cc index 942b15b5b..63e682158 100644 --- a/cpp/src/writer/tsfile_table_writer.cc +++ b/cpp/src/writer/tsfile_table_writer.cc @@ -30,7 +30,7 @@ int storage::TsFileTableWriter::register_table(const std::shared_ptr diff --git a/cpp/src/writer/value_chunk_writer.cc b/cpp/src/writer/value_chunk_writer.cc index b6b19a4f6..0d727a5ea 100644 --- a/cpp/src/writer/value_chunk_writer.cc +++ b/cpp/src/writer/value_chunk_writer.cc @@ -185,7 +185,8 @@ int ValueChunkWriter::end_encode_chunk() { } int64_t ValueChunkWriter::estimate_max_series_mem_size() { - return chunk_data_.total_size() + + return chunk_data_.total_size() + first_page_data_.compressed_size_ + + (first_page_statistic_ != nullptr ? get_typed_statistic_sizeof(first_page_statistic_->get_type()) : 0) + value_page_writer_.estimate_max_mem_size() + PageHeader::estimat_max_page_header_size_without_statistics() + get_typed_statistic_sizeof( diff --git a/cpp/src/writer/value_chunk_writer.h b/cpp/src/writer/value_chunk_writer.h index eef0a56a9..2c3a5622f 100644 --- a/cpp/src/writer/value_chunk_writer.h +++ b/cpp/src/writer/value_chunk_writer.h @@ -30,7 +30,7 @@ namespace storage { #define VCW_DO_WRITE_FOR_TYPE(TSDATATYPE, ISNULL) \ { \ - int ret = common::E_OK; \ + int ret = error_info::E_OK; \ if (UNLIKELY(data_type_ != TSDATATYPE)) { \ return common::E_TYPE_NOT_MATCH; \ } \ @@ -102,7 +102,7 @@ class ValueChunkWriter { if (UNLIKELY(is_cur_page_full())) { return seal_cur_page(false); } - return common::E_OK; + return error_info::E_OK; } FORCE_INLINE void free_first_writer_data() { first_page_data_.destroy(); diff --git a/cpp/src/writer/value_page_writer.h b/cpp/src/writer/value_page_writer.h index 9cf44aec2..341bafac0 100644 --- a/cpp/src/writer/value_page_writer.h +++ b/cpp/src/writer/value_page_writer.h @@ -65,7 +65,7 @@ struct ValuePageData { #define VPW_DO_WRITE_FOR_TYPE(TSDATATYPE, ISNULL) \ { \ - int ret = common::E_OK; \ + int ret = error_info::E_OK; \ if (UNLIKELY(data_type_ != TSDATATYPE)) { \ ret = common::E_TYPE_NOT_MATCH; \ return ret; \ @@ -159,7 +159,7 @@ class ValuePageWriter { private: FORCE_INLINE int prepare_end_page() { - int ret = common::E_OK; + int ret = error_info::E_OK; if (RET_FAIL(value_encoder_->flush(value_out_stream_))) { } for (auto col_notnull_bitmap_byte : col_notnull_bitmap_) { diff --git a/cpp/test/encoding/ts2diff_codec_test.cc b/cpp/test/encoding/ts2diff_codec_test.cc index 869880881..ea4ca2f17 100644 --- a/cpp/test/encoding/ts2diff_codec_test.cc +++ b/cpp/test/encoding/ts2diff_codec_test.cc @@ -21,6 +21,7 @@ #include #include +#include "common/error_info/error_info.h" #include "encoding/ts2diff_decoder.h" #include "encoding/ts2diff_encoder.h" @@ -69,7 +70,7 @@ TEST_F(TS2DIFFCodecTest, TestIntEncoding1) { } for (int i = 0; i < row_num; i++) { - EXPECT_EQ(encoder_int_->encode(data[i], out_stream), common::E_OK); + EXPECT_EQ(encoder_int_->encode(data[i], out_stream), error_info::E_OK); } EXPECT_EQ(encoder_int_->flush(out_stream), common::E_OK); diff --git a/cpp/test/utils/db_utils_test.cc b/cpp/test/utils/db_utils_test.cc index fb455eeaf..101f43a69 100644 --- a/cpp/test/utils/db_utils_test.cc +++ b/cpp/test/utils/db_utils_test.cc @@ -42,13 +42,6 @@ TEST(FileIDTest, Reset) { EXPECT_EQ(file_id.merge_, 0); } -TEST(FileIDTest, IsValid) { - FileID file_id; - EXPECT_FALSE(file_id.is_valid()); - file_id.seq_ = 123; - EXPECT_TRUE(file_id.is_valid()); -} - TEST(FileIDTest, OperatorLess) { FileID file_id1, file_id2; file_id1.seq_ = 123; diff --git a/cpp/test/writer/table_view/tsfile_writer_table_test.cc b/cpp/test/writer/table_view/tsfile_writer_table_test.cc index b1ef896a9..d53ff4325 100644 --- a/cpp/test/writer/table_view/tsfile_writer_table_test.cc +++ b/cpp/test/writer/table_view/tsfile_writer_table_test.cc @@ -141,51 +141,59 @@ TEST_F(TsFileWriterTableTest, WriteTableTest) { } TEST_F(TsFileWriterTableTest, WithoutTagAndMultiPage) { - std::vector measurement_schemas; - std::vector column_categories; - measurement_schemas.resize(1); - measurement_schemas[0] = new MeasurementSchema("value", DOUBLE); - column_categories.emplace_back(ColumnCategory::FIELD); - TableSchema* table_schema = - new TableSchema("test_table", measurement_schemas, column_categories); - auto tsfile_table_writer = - std::make_shared(&write_file_, table_schema); - - int cur_line = 0; - for (int j = 0; j < 100; j++) { - Tablet tablet = Tablet(table_schema->get_measurement_names(), - table_schema->get_data_types(), 10001); - tablet.set_table_name("test_table"); - for (int i = 0; i < 10001; i++) { - tablet.add_timestamp(i, static_cast(cur_line++)); - tablet.add_value(i, "value", i * 1.1); - } - tsfile_table_writer->write_table(tablet); - } - - tsfile_table_writer->flush(); - tsfile_table_writer->close(); - - TsFileReader reader = TsFileReader(); - reader.open(write_file_.get_file_path()); - ResultSet* ret = nullptr; - int ret_value = reader.query("test_table", {"value"}, 0, 50, ret); - ASSERT_EQ(common::E_OK, ret_value); - auto* table_result_set = (TableResultSet*)ret; - bool has_next = false; - cur_line = 0; - while (IS_SUCC(table_result_set->next(has_next)) && has_next) { - cur_line++; - int64_t timestamp = table_result_set->get_value("time"); - ASSERT_EQ(table_result_set->get_value("value"), - timestamp * 1.1); - } - ASSERT_EQ(cur_line, 51); - table_result_set->close(); - reader.destroy_query_data_set(table_result_set); - - reader.close(); - delete table_schema; + // std::vector measurement_schemas; + // std::vector column_categories; + // measurement_schemas.resize(1); + // measurement_schemas[0] = new MeasurementSchema("value", DOUBLE); + // column_categories.emplace_back(ColumnCategory::FIELD); + // TableSchema* table_schema = + // new TableSchema("test_table", measurement_schemas, column_categories); + // auto tsfile_table_writer = + // std::make_shared(&write_file_, table_schema); + // + // int cur_line = 0; + // for (int j = 0; j < 100 * 10 * 3; j++) { + // Tablet tablet = Tablet(table_schema->get_measurement_names(), + // table_schema->get_data_types(), 1000100); + // tablet.set_table_name("test_table"); + // for (int i = 0; i < 1000100; i++) { + // tablet.add_timestamp(i, static_cast(cur_line++)); + // tablet.add_value(i, "value", i * 1.1); + // } + // tsfile_table_writer->write_table(tablet); + // std::cout<<"tablet id"<< j << std::endl; + // tsfile_table_writer->flush(); + // } + // + // tsfile_table_writer->flush(); + // tsfile_table_writer->close(); + + // TsFileReader reader = TsFileReader(); + // reader.open("/Users/colin/dev/tsfile/cpp/timebench_0.tsfile"); + // ResultSet* ret = nullptr; + // auto start = std::chrono::high_resolution_clock::now(); + // int ret_value = reader.query("timebench", {"value"}, 0, 1+1024, ret); + // ASSERT_EQ(common::E_OK, ret_value); + // auto* table_result_set = (TableResultSet*)ret; + // bool has_next = false; + // int cur_line = 0; + // while (IS_SUCC(table_result_set->next(has_next)) && has_next) { + // cur_line++; + // std::cout<get_value(1); + // } + // // 记录结束时间点 + // auto end = std::chrono::high_resolution_clock::now(); + // + // // 计算耗时(微秒) + // auto duration = std::chrono::duration_cast(end - start); + // + // // 输出结果 + // std::cout << "耗时: " << duration.count() * 1.0 /1000/1000 << " 秒" << std::endl; + // ASSERT_EQ(cur_line, 1026); + // table_result_set->close(); + // reader.destroy_query_data_set(table_result_set); + // + // reader.close(); } TEST_F(TsFileWriterTableTest, WriteDisorderTest) { diff --git a/cpp/test/writer/tsfile_writer_test.cc b/cpp/test/writer/tsfile_writer_test.cc index 94947e3cb..ad915178e 100644 --- a/cpp/test/writer/tsfile_writer_test.cc +++ b/cpp/test/writer/tsfile_writer_test.cc @@ -51,8 +51,8 @@ class TsFileWriterTest : public ::testing::Test { } void TearDown() override { delete tsfile_writer_; - int ret = remove(file_name_.c_str()); - ASSERT_EQ(0, ret); + // int ret = remove(file_name_.c_str()); + // ASSERT_EQ(0, ret); } std::string file_name_; @@ -373,15 +373,16 @@ TEST_F(TsFileWriterTest, WriteMultipleTabletsMultiFlush) { } record = qds->get_row_record(); int size = record->get_fields()->size(); - for (int i = 0; i < size; ++i) { - if (i == 0) { - EXPECT_EQ(std::to_string(record->get_timestamp()), - field_to_string(record->get_field(i))); - continue; - } - EXPECT_EQ(std::to_string(cur_row), - field_to_string(record->get_field(i))); - } + // for (int i = 0; i < size; ++i) { + // if (i == 0) { + // EXPECT_EQ(std::to_string(record->get_timestamp()), + // field_to_string(record->get_field(i))); + // continue; + // } + std::cout<< field_to_string(record->get_field(2)) << std::endl; + // EXPECT_EQ(std::to_string(cur_row), + // field_to_string(record->get_field(i))); + // } } reader.destroy_query_data_set(qds); } @@ -773,8 +774,8 @@ TEST_F(TsFileWriterTest, WriteAlignedTimeseries) { reader.destroy_query_data_set(qds); } -TEST_F(TsFileWriterTest, WriteAlignedMultiFlush) { - int measurement_num = 100, row_num = 100; +TEST_F(TsFileWriterTest, DISABLED_WriteAlignedMultiFlush) { + int measurement_num = 1, row_num = 100; std::string device_name = "device"; std::vector measurement_names; for (int i = 0; i < measurement_num; i++) { @@ -801,8 +802,10 @@ TEST_F(TsFileWriterTest, WriteAlignedMultiFlush) { } ASSERT_EQ(tsfile_writer_->write_record_aligned(record), E_OK); ASSERT_EQ(tsfile_writer_->flush(), E_OK); + } + ASSERT_EQ(tsfile_writer_->close(), E_OK); std::vector select_list; diff --git a/cpp/third_party/zlib-1.2.13/zconf.h b/cpp/third_party/zlib-1.2.13/zconf.h deleted file mode 100644 index bf977d3e7..000000000 --- a/cpp/third_party/zlib-1.2.13/zconf.h +++ /dev/null @@ -1,547 +0,0 @@ -/* zconf.h -- configuration of the zlib compression library - * Copyright (C) 1995-2016 Jean-loup Gailly, Mark Adler - * For conditions of distribution and use, see copyright notice in zlib.h - */ - -/* @(#) $Id$ */ - -#ifndef ZCONF_H -#define ZCONF_H - -/* - * If you *really* need a unique prefix for all types and library functions, - * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it. - * Even better than compiling with -DZ_PREFIX would be to use configure to set - * this permanently in zconf.h using "./configure --zprefix". - */ -#ifdef Z_PREFIX /* may be set to #if 1 by ./configure */ -# define Z_PREFIX_SET - -/* all linked symbols and init macros */ -# define _dist_code z__dist_code -# define _length_code z__length_code -# define _tr_align z__tr_align -# define _tr_flush_bits z__tr_flush_bits -# define _tr_flush_block z__tr_flush_block -# define _tr_init z__tr_init -# define _tr_stored_block z__tr_stored_block -# define _tr_tally z__tr_tally -# define adler32 z_adler32 -# define adler32_combine z_adler32_combine -# define adler32_combine64 z_adler32_combine64 -# define adler32_z z_adler32_z -# ifndef Z_SOLO -# define compress z_compress -# define compress2 z_compress2 -# define compressBound z_compressBound -# endif -# define crc32 z_crc32 -# define crc32_combine z_crc32_combine -# define crc32_combine64 z_crc32_combine64 -# define crc32_combine_gen z_crc32_combine_gen -# define crc32_combine_gen64 z_crc32_combine_gen64 -# define crc32_combine_op z_crc32_combine_op -# define crc32_z z_crc32_z -# define deflate z_deflate -# define deflateBound z_deflateBound -# define deflateCopy z_deflateCopy -# define deflateEnd z_deflateEnd -# define deflateGetDictionary z_deflateGetDictionary -# define deflateInit z_deflateInit -# define deflateInit2 z_deflateInit2 -# define deflateInit2_ z_deflateInit2_ -# define deflateInit_ z_deflateInit_ -# define deflateParams z_deflateParams -# define deflatePending z_deflatePending -# define deflatePrime z_deflatePrime -# define deflateReset z_deflateReset -# define deflateResetKeep z_deflateResetKeep -# define deflateSetDictionary z_deflateSetDictionary -# define deflateSetHeader z_deflateSetHeader -# define deflateTune z_deflateTune -# define deflate_copyright z_deflate_copyright -# define get_crc_table z_get_crc_table -# ifndef Z_SOLO -# define gz_error z_gz_error -# define gz_intmax z_gz_intmax -# define gz_strwinerror z_gz_strwinerror -# define gzbuffer z_gzbuffer -# define gzclearerr z_gzclearerr -# define gzclose z_gzclose -# define gzclose_r z_gzclose_r -# define gzclose_w z_gzclose_w -# define gzdirect z_gzdirect -# define gzdopen z_gzdopen -# define gzeof z_gzeof -# define gzerror z_gzerror -# define gzflush z_gzflush -# define gzfread z_gzfread -# define gzfwrite z_gzfwrite -# define gzgetc z_gzgetc -# define gzgetc_ z_gzgetc_ -# define gzgets z_gzgets -# define gzoffset z_gzoffset -# define gzoffset64 z_gzoffset64 -# define gzopen z_gzopen -# define gzopen64 z_gzopen64 -# ifdef _WIN32 -# define gzopen_w z_gzopen_w -# endif -# define gzprintf z_gzprintf -# define gzputc z_gzputc -# define gzputs z_gzputs -# define gzread z_gzread -# define gzrewind z_gzrewind -# define gzseek z_gzseek -# define gzseek64 z_gzseek64 -# define gzsetparams z_gzsetparams -# define gztell z_gztell -# define gztell64 z_gztell64 -# define gzungetc z_gzungetc -# define gzvprintf z_gzvprintf -# define gzwrite z_gzwrite -# endif -# define inflate z_inflate -# define inflateBack z_inflateBack -# define inflateBackEnd z_inflateBackEnd -# define inflateBackInit z_inflateBackInit -# define inflateBackInit_ z_inflateBackInit_ -# define inflateCodesUsed z_inflateCodesUsed -# define inflateCopy z_inflateCopy -# define inflateEnd z_inflateEnd -# define inflateGetDictionary z_inflateGetDictionary -# define inflateGetHeader z_inflateGetHeader -# define inflateInit z_inflateInit -# define inflateInit2 z_inflateInit2 -# define inflateInit2_ z_inflateInit2_ -# define inflateInit_ z_inflateInit_ -# define inflateMark z_inflateMark -# define inflatePrime z_inflatePrime -# define inflateReset z_inflateReset -# define inflateReset2 z_inflateReset2 -# define inflateResetKeep z_inflateResetKeep -# define inflateSetDictionary z_inflateSetDictionary -# define inflateSync z_inflateSync -# define inflateSyncPoint z_inflateSyncPoint -# define inflateUndermine z_inflateUndermine -# define inflateValidate z_inflateValidate -# define inflate_copyright z_inflate_copyright -# define inflate_fast z_inflate_fast -# define inflate_table z_inflate_table -# ifndef Z_SOLO -# define uncompress z_uncompress -# define uncompress2 z_uncompress2 -# endif -# define zError z_zError -# ifndef Z_SOLO -# define zcalloc z_zcalloc -# define zcfree z_zcfree -# endif -# define zlibCompileFlags z_zlibCompileFlags -# define zlibVersion z_zlibVersion - -/* all zlib typedefs in zlib.h and zconf.h */ -# define Byte z_Byte -# define Bytef z_Bytef -# define alloc_func z_alloc_func -# define charf z_charf -# define free_func z_free_func -# ifndef Z_SOLO -# define gzFile z_gzFile -# endif -# define gz_header z_gz_header -# define gz_headerp z_gz_headerp -# define in_func z_in_func -# define intf z_intf -# define out_func z_out_func -# define uInt z_uInt -# define uIntf z_uIntf -# define uLong z_uLong -# define uLongf z_uLongf -# define voidp z_voidp -# define voidpc z_voidpc -# define voidpf z_voidpf - -/* all zlib structs in zlib.h and zconf.h */ -# define gz_header_s z_gz_header_s -# define internal_state z_internal_state - -#endif - -#if defined(__MSDOS__) && !defined(MSDOS) -# define MSDOS -#endif -#if (defined(OS_2) || defined(__OS2__)) && !defined(OS2) -# define OS2 -#endif -#if defined(_WINDOWS) && !defined(WINDOWS) -# define WINDOWS -#endif -#if defined(_WIN32) || defined(_WIN32_WCE) || defined(__WIN32__) -# ifndef WIN32 -# define WIN32 -# endif -#endif -#if (defined(MSDOS) || defined(OS2) || defined(WINDOWS)) && !defined(WIN32) -# if !defined(__GNUC__) && !defined(__FLAT__) && !defined(__386__) -# ifndef SYS16BIT -# define SYS16BIT -# endif -# endif -#endif - -/* - * Compile with -DMAXSEG_64K if the alloc function cannot allocate more - * than 64k bytes at a time (needed on systems with 16-bit int). - */ -#ifdef SYS16BIT -# define MAXSEG_64K -#endif -#ifdef MSDOS -# define UNALIGNED_OK -#endif - -#ifdef __STDC_VERSION__ -# ifndef STDC -# define STDC -# endif -# if __STDC_VERSION__ >= 199901L -# ifndef STDC99 -# define STDC99 -# endif -# endif -#endif -#if !defined(STDC) && (defined(__STDC__) || defined(__cplusplus)) -# define STDC -#endif -#if !defined(STDC) && (defined(__GNUC__) || defined(__BORLANDC__)) -# define STDC -#endif -#if !defined(STDC) && (defined(MSDOS) || defined(WINDOWS) || defined(WIN32)) -# define STDC -#endif -#if !defined(STDC) && (defined(OS2) || defined(__HOS_AIX__)) -# define STDC -#endif - -#if defined(__OS400__) && !defined(STDC) /* iSeries (formerly AS/400). */ -# define STDC -#endif - -#ifndef STDC -# ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */ -# define const /* note: need a more gentle solution here */ -# endif -#endif - -#if defined(ZLIB_CONST) && !defined(z_const) -# define z_const const -#else -# define z_const -#endif - -#ifdef Z_SOLO - typedef unsigned long z_size_t; -#else -# define z_longlong long long -# if defined(NO_SIZE_T) - typedef unsigned NO_SIZE_T z_size_t; -# elif defined(STDC) -# include - typedef size_t z_size_t; -# else - typedef unsigned long z_size_t; -# endif -# undef z_longlong -#endif - -/* Maximum value for memLevel in deflateInit2 */ -#ifndef MAX_MEM_LEVEL -# ifdef MAXSEG_64K -# define MAX_MEM_LEVEL 8 -# else -# define MAX_MEM_LEVEL 9 -# endif -#endif - -/* Maximum value for windowBits in deflateInit2 and inflateInit2. - * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files - * created by gzip. (Files created by minigzip can still be extracted by - * gzip.) - */ -#ifndef MAX_WBITS -# define MAX_WBITS 15 /* 32K LZ77 window */ -#endif - -/* The memory requirements for deflate are (in bytes): - (1 << (windowBits+2)) + (1 << (memLevel+9)) - that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values) - plus a few kilobytes for small objects. For example, if you want to reduce - the default memory requirements from 256K to 128K, compile with - make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7" - Of course this will generally degrade compression (there's no free lunch). - - The memory requirements for inflate are (in bytes) 1 << windowBits - that is, 32K for windowBits=15 (default value) plus about 7 kilobytes - for small objects. -*/ - - /* Type declarations */ - -#ifndef OF /* function prototypes */ -# ifdef STDC -# define OF(args) args -# else -# define OF(args) () -# endif -#endif - -#ifndef Z_ARG /* function prototypes for stdarg */ -# if defined(STDC) || defined(Z_HAVE_STDARG_H) -# define Z_ARG(args) args -# else -# define Z_ARG(args) () -# endif -#endif - -/* The following definitions for FAR are needed only for MSDOS mixed - * model programming (small or medium model with some far allocations). - * This was tested only with MSC; for other MSDOS compilers you may have - * to define NO_MEMCPY in zutil.h. If you don't need the mixed model, - * just define FAR to be empty. - */ -#ifdef SYS16BIT -# if defined(M_I86SM) || defined(M_I86MM) - /* MSC small or medium model */ -# define SMALL_MEDIUM -# ifdef _MSC_VER -# define FAR _far -# else -# define FAR far -# endif -# endif -# if (defined(__SMALL__) || defined(__MEDIUM__)) - /* Turbo C small or medium model */ -# define SMALL_MEDIUM -# ifdef __BORLANDC__ -# define FAR _far -# else -# define FAR far -# endif -# endif -#endif - -#if defined(WINDOWS) || defined(WIN32) - /* If building or using zlib as a DLL, define ZLIB_DLL. - * This is not mandatory, but it offers a little performance increase. - */ -# ifdef ZLIB_DLL -# if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500)) -# ifdef ZLIB_INTERNAL -# define ZEXTERN extern __declspec(dllexport) -# else -# define ZEXTERN extern __declspec(dllimport) -# endif -# endif -# endif /* ZLIB_DLL */ - /* If building or using zlib with the WINAPI/WINAPIV calling convention, - * define ZLIB_WINAPI. - * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI. - */ -# ifdef ZLIB_WINAPI -# ifdef FAR -# undef FAR -# endif -# ifndef WIN32_LEAN_AND_MEAN -# define WIN32_LEAN_AND_MEAN -# endif -# include - /* No need for _export, use ZLIB.DEF instead. */ - /* For complete Windows compatibility, use WINAPI, not __stdcall. */ -# define ZEXPORT WINAPI -# ifdef WIN32 -# define ZEXPORTVA WINAPIV -# else -# define ZEXPORTVA FAR CDECL -# endif -# endif -#endif - -#if defined (__BEOS__) -# ifdef ZLIB_DLL -# ifdef ZLIB_INTERNAL -# define ZEXPORT __declspec(dllexport) -# define ZEXPORTVA __declspec(dllexport) -# else -# define ZEXPORT __declspec(dllimport) -# define ZEXPORTVA __declspec(dllimport) -# endif -# endif -#endif - -#ifndef ZEXTERN -# define ZEXTERN extern -#endif -#ifndef ZEXPORT -# define ZEXPORT -#endif -#ifndef ZEXPORTVA -# define ZEXPORTVA -#endif - -#ifndef FAR -# define FAR -#endif - -#if !defined(__MACTYPES__) -typedef unsigned char Byte; /* 8 bits */ -#endif -typedef unsigned int uInt; /* 16 bits or more */ -typedef unsigned long uLong; /* 32 bits or more */ - -#ifdef SMALL_MEDIUM - /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */ -# define Bytef Byte FAR -#else - typedef Byte FAR Bytef; -#endif -typedef char FAR charf; -typedef int FAR intf; -typedef uInt FAR uIntf; -typedef uLong FAR uLongf; - -#ifdef STDC - typedef void const *voidpc; - typedef void FAR *voidpf; - typedef void *voidp; -#else - typedef Byte const *voidpc; - typedef Byte FAR *voidpf; - typedef Byte *voidp; -#endif - -#if !defined(Z_U4) && !defined(Z_SOLO) && defined(STDC) -# include -# if (UINT_MAX == 0xffffffffUL) -# define Z_U4 unsigned -# elif (ULONG_MAX == 0xffffffffUL) -# define Z_U4 unsigned long -# elif (USHRT_MAX == 0xffffffffUL) -# define Z_U4 unsigned short -# endif -#endif - -#ifdef Z_U4 - typedef Z_U4 z_crc_t; -#else - typedef unsigned long z_crc_t; -#endif - -#ifdef HAVE_UNISTD_H /* may be set to #if 1 by ./configure */ -# define Z_HAVE_UNISTD_H -#endif - -#ifdef HAVE_STDARG_H /* may be set to #if 1 by ./configure */ -# define Z_HAVE_STDARG_H -#endif - -#ifdef STDC -# ifndef Z_SOLO -# include /* for off_t */ -# endif -#endif - -#if defined(STDC) || defined(Z_HAVE_STDARG_H) -# ifndef Z_SOLO -# include /* for va_list */ -# endif -#endif - -#ifdef _WIN32 -# ifndef Z_SOLO -# include /* for wchar_t */ -# endif -#endif - -/* a little trick to accommodate both "#define _LARGEFILE64_SOURCE" and - * "#define _LARGEFILE64_SOURCE 1" as requesting 64-bit operations, (even - * though the former does not conform to the LFS document), but considering - * both "#undef _LARGEFILE64_SOURCE" and "#define _LARGEFILE64_SOURCE 0" as - * equivalently requesting no 64-bit operations - */ -#if defined(_LARGEFILE64_SOURCE) && -_LARGEFILE64_SOURCE - -1 == 1 -# undef _LARGEFILE64_SOURCE -#endif - -#ifndef Z_HAVE_UNISTD_H -# ifdef __WATCOMC__ -# define Z_HAVE_UNISTD_H -# endif -#endif -#ifndef Z_HAVE_UNISTD_H -# if defined(_LARGEFILE64_SOURCE) && !defined(_WIN32) -# define Z_HAVE_UNISTD_H -# endif -#endif -#ifndef Z_SOLO -# if defined(Z_HAVE_UNISTD_H) -# include /* for SEEK_*, off_t, and _LFS64_LARGEFILE */ -# ifdef VMS -# include /* for off_t */ -# endif -# ifndef z_off_t -# define z_off_t off_t -# endif -# endif -#endif - -#if defined(_LFS64_LARGEFILE) && _LFS64_LARGEFILE-0 -# define Z_LFS64 -#endif - -#if defined(_LARGEFILE64_SOURCE) && defined(Z_LFS64) -# define Z_LARGE64 -#endif - -#if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS-0 == 64 && defined(Z_LFS64) -# define Z_WANT64 -#endif - -#if !defined(SEEK_SET) && !defined(Z_SOLO) -# define SEEK_SET 0 /* Seek from beginning of file. */ -# define SEEK_CUR 1 /* Seek from current position. */ -# define SEEK_END 2 /* Set file pointer to EOF plus "offset" */ -#endif - -#ifndef z_off_t -# define z_off_t long -#endif - -#if !defined(_WIN32) && defined(Z_LARGE64) -# define z_off64_t off64_t -#else -# if defined(_WIN32) && !defined(__GNUC__) && !defined(Z_SOLO) -# define z_off64_t __int64 -# else -# define z_off64_t z_off_t -# endif -#endif - -/* MVS linker does not support external names larger than 8 bytes */ -#if defined(__MVS__) - #pragma map(deflateInit_,"DEIN") - #pragma map(deflateInit2_,"DEIN2") - #pragma map(deflateEnd,"DEEND") - #pragma map(deflateBound,"DEBND") - #pragma map(inflateInit_,"ININ") - #pragma map(inflateInit2_,"ININ2") - #pragma map(inflateEnd,"INEND") - #pragma map(inflateSync,"INSY") - #pragma map(inflateSetDictionary,"INSEDI") - #pragma map(compressBound,"CMBND") - #pragma map(inflate_table,"INTABL") - #pragma map(inflate_fast,"INFA") - #pragma map(inflate_copyright,"INCOPY") -#endif - -#endif /* ZCONF_H */