diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index e6e66caaf..e45cd87f3 100755 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -487,6 +487,13 @@ if (BUILD_BENCHMARK) target_link_libraries(alp_vs_gorilla_benchmark PRIVATE tsfile Threads::Threads) + add_executable(gorilla_v1_batch_decode_benchmark + bench_mark/gorilla_v1_batch_decode_benchmark.cc) + target_include_directories(gorilla_v1_batch_decode_benchmark PRIVATE + ${PROJECT_SRC_DIR}) + target_link_libraries(gorilla_v1_batch_decode_benchmark PRIVATE + tsfile Threads::Threads) + add_executable(sprintz_batch_decode_benchmark bench_mark/sprintz_batch_decode_benchmark.cc) target_include_directories(sprintz_batch_decode_benchmark PRIVATE diff --git a/cpp/bench_mark/README.md b/cpp/bench_mark/README.md index 14950e14b..e7851641f 100644 --- a/cpp/bench_mark/README.md +++ b/cpp/bench_mark/README.md @@ -54,3 +54,21 @@ meaningful results, repeat both forms and record filesystem, storage device, file sizes, compiler flags, and whether the OS page cache was warm. The checksum makes accidental short reads, metadata-load failures, or optimizer removal visible; it is not a TsFile-content checksum. + +`gorilla_v1_batch_decode_benchmark` compares the per-value GORILLA_V1 decode +loop with the raw-pointer batch decoder on the same encoded stream for FLOAT +and DOUBLE columns. It reports elapsed time, values per second and the speedup +for a smooth (time-series-like) series, a high-entropy series and a constant +series, and verifies that both paths decode identical bit patterns. Build it +through the main CMake project: + +```bash +cmake -S cpp -B cpp/target/gv1-benchmark \ + -DCMAKE_BUILD_TYPE=Release \ + -DBUILD_BENCHMARK=ON -DBUILD_TEST=OFF -DBUILD_TOOLS=OFF +cmake --build cpp/target/gv1-benchmark \ + --target gorilla_v1_batch_decode_benchmark --config Release +./cpp/target/gv1-benchmark/gorilla_v1_batch_decode_benchmark [values] [runs] +``` + +The defaults are 4 Mi values and a best-of-5 timing per case. diff --git a/cpp/bench_mark/gorilla_v1_batch_decode_benchmark.cc b/cpp/bench_mark/gorilla_v1_batch_decode_benchmark.cc new file mode 100644 index 000000000..db37f744a --- /dev/null +++ b/cpp/bench_mark/gorilla_v1_batch_decode_benchmark.cc @@ -0,0 +1,260 @@ +/* + * 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. + */ + +// Compares the per-value GORILLA_V1 decode loop (the pre-batch behavior, which +// Decoder::read_batch_double/int32 still performs) with the raw-pointer batch +// decoder added in gorilla_v1_decoder.h, on the same encoded stream. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "common/allocator/byte_stream.h" +#include "encoding/gorilla_v1_decoder.h" +#include "encoding/gorilla_v1_encoder.h" +#include "utils/errno_define.h" + +namespace { + +using Clock = std::chrono::high_resolution_clock; + +template +struct BenchCodec; + +template <> +struct BenchCodec { + typedef storage::FloatGorillaV1Encoder Encoder; + typedef storage::FloatGorillaV1Decoder Decoder; + static int read_batch(Decoder& decoder, float* out, int capacity, + int& actual, common::ByteStream& in) { + return decoder.read_batch_float(out, capacity, actual, in); + } + // The pre-batch implementation: qualified call, no virtual dispatch to the + // override, so Decoder::read_batch_float's per-value loop runs. + static int read_batch_per_value(Decoder& decoder, float* out, int capacity, + int& actual, common::ByteStream& in) { + return decoder.Decoder::read_batch_float(out, capacity, actual, in); + } + static uint64_t bits(float value) { + return static_cast( + static_cast(common::float_to_int(value))); + } + static uint64_t nan_bits() { return 0x7FC00000ULL; } +}; + +template <> +struct BenchCodec { + typedef storage::DoubleGorillaV1Encoder Encoder; + typedef storage::DoubleGorillaV1Decoder Decoder; + static int read_batch(Decoder& decoder, double* out, int capacity, + int& actual, common::ByteStream& in) { + return decoder.read_batch_double(out, capacity, actual, in); + } + static int read_batch_per_value(Decoder& decoder, double* out, int capacity, + int& actual, common::ByteStream& in) { + return decoder.Decoder::read_batch_double(out, capacity, actual, in); + } + static uint64_t bits(double value) { + return static_cast(common::double_to_long(value)); + } + static uint64_t nan_bits() { return 0x7FF8000000000000ULL; } +}; + +template +std::vector EncodeV1(const std::vector& values) { + typename BenchCodec::Encoder encoder; + common::ByteStream stream(65536, common::MOD_DEFAULT); + for (size_t i = 0; i < values.size(); i++) { + encoder.encode(values[i], stream); + } + encoder.flush(stream); + std::vector bytes(static_cast(stream.total_size())); + uint32_t read_len = 0; + stream.read_buf(bytes.data(), static_cast(bytes.size()), + read_len); + bytes.resize(read_len); + return bytes; +} + +template +bool DecodeAll(int mode, const std::vector& bytes, uint64_t count, + std::vector& out, double& elapsed_ms) { + typename BenchCodec::Decoder decoder; + common::ByteStream in(common::MOD_DEFAULT); + in.wrap_from(reinterpret_cast(bytes.data()), + static_cast(bytes.size())); + out.assign(static_cast(count), static_cast(0)); + const Clock::time_point start = Clock::now(); + int ret = common::E_OK; + int decoded = 0; + while (decoded < static_cast(count)) { + int actual = 0; + ret = mode == 0 ? BenchCodec::read_batch_per_value( + decoder, out.data() + decoded, + static_cast(count) - decoded, actual, in) + : BenchCodec::read_batch( + decoder, out.data() + decoded, + static_cast(count) - decoded, actual, in); + if (ret != common::E_OK) { + break; + } + if (actual == 0) { + break; + } + decoded += actual; + } + const Clock::time_point end = Clock::now(); + elapsed_ms = + std::chrono::duration_cast>( + end - start) + .count(); + return ret == common::E_OK && decoded == static_cast(count); +} + +template +std::vector MakeSmoothSeries(uint64_t count) { + std::vector values(static_cast(count)); + double level = 100.0; + uint64_t noise = 0x9E3779B97F4A7C15ULL; + for (uint64_t i = 0; i < count; i++) { + noise = noise * 6364136223846793005ULL + 1442695040888963407ULL; + const double jitter = + (static_cast(noise >> 40) / 16777216.0 - 0.5) * 0.01; + level += std::sin(static_cast(i) / 500.0) * 0.05 + jitter; + values[static_cast(i)] = static_cast(level); + } + return values; +} + +template +std::vector MakeHighEntropy(uint64_t count) { + std::vector values(static_cast(count)); + uint64_t state = 0x243F6A8885A308D3ULL; + for (uint64_t i = 0; i < count; i++) { + state = state * 6364136223846793005ULL + 1442695040888963407ULL; + const uint64_t bits = state ^ (state >> 31); + if (sizeof(T) == sizeof(float)) { + const uint32_t narrow = static_cast(bits); + float value = 0.0f; + std::memcpy(&value, &narrow, sizeof(value)); + if (std::isnan(value)) { + value = static_cast(i % 1024) * 0.001f; + } + values[static_cast(i)] = value; + } else { + double value = 0.0; + std::memcpy(&value, &bits, sizeof(value)); + if (std::isnan(value)) { + value = static_cast(i % 1024) * 0.001; + } + values[static_cast(i)] = value; + } + } + return values; +} + +template +void RunCase(const char* type_name, const std::string& pattern, + const std::vector& values, int repetitions) { + const std::vector bytes = EncodeV1(values); + const uint64_t count = values.size(); + + std::vector old_out; + std::vector new_out; + double best_old_ms = std::numeric_limits::max(); + double best_new_ms = std::numeric_limits::max(); + bool correct = true; + for (int rep = 0; rep < repetitions; rep++) { + double ms = 0.0; + if (!DecodeAll(0, bytes, count, old_out, ms)) { + correct = false; + } + best_old_ms = std::min(best_old_ms, ms); + if (!DecodeAll(1, bytes, count, new_out, ms)) { + correct = false; + } + best_new_ms = std::min(best_new_ms, ms); + } + for (uint64_t i = 0; i < count; i++) { + if (BenchCodec::bits(old_out[static_cast(i)]) != + BenchCodec::bits(new_out[static_cast(i)])) { + correct = false; + break; + } + } + + const double values_per_sec_old = + best_old_ms > 0.0 ? static_cast(count) / (best_old_ms / 1000.0) + : 0.0; + const double values_per_sec_new = + best_new_ms > 0.0 ? static_cast(count) / (best_new_ms / 1000.0) + : 0.0; + const double speedup = best_new_ms > 0.0 ? best_old_ms / best_new_ms : 0.0; + std::cout << std::left << std::setw(8) << type_name << std::setw(14) + << pattern << std::right << std::setw(12) << count + << std::setw(14) << bytes.size() << std::setw(12) << std::fixed + << std::setprecision(3) << best_old_ms << std::setw(12) + << best_new_ms << std::setw(16) << std::setprecision(0) + << values_per_sec_old << std::setw(16) << values_per_sec_new + << std::setw(10) << std::setprecision(2) << speedup + << std::setw(8) << (correct ? "yes" : "no") << std::endl; +} + +template +void RunAll(const char* type_name, uint64_t count, int repetitions) { + std::cout << "==== " << type_name << " ====" << std::endl; + std::cout << std::left << std::setw(8) << "type" << std::setw(14) + << "pattern" << std::right << std::setw(12) << "values" + << std::setw(14) << "encoded(B)" << std::setw(12) << "old(ms)" + << std::setw(12) << "new(ms)" << std::setw(16) << "old val/s" + << std::setw(16) << "new val/s" << std::setw(10) << "speedup" + << std::setw(8) << "ok" << std::endl; + RunCase(type_name, "smooth", MakeSmoothSeries(count), repetitions); + RunCase(type_name, "high-entropy", MakeHighEntropy(count), + repetitions); + RunCase(type_name, "constant", + std::vector(static_cast(count), static_cast(3.5)), + repetitions); +} + +} // namespace + +int main(int argc, char** argv) { + uint64_t count = 4 * 1024 * 1024; + int repetitions = 5; + if (argc > 1) { + count = static_cast(std::max(1, atoi(argv[1]))); + } + if (argc > 2) { + repetitions = std::max(1, atoi(argv[2])); + } + std::cout << "GORILLA_V1 per-value vs batch decode, " << count + << " values, best of " << repetitions << " runs" << std::endl; + RunAll("DOUBLE", count, repetitions); + RunAll("FLOAT", count, repetitions); + return 0; +} diff --git a/cpp/src/encoding/gorilla_v1_decoder.h b/cpp/src/encoding/gorilla_v1_decoder.h index 950856daa..2f0e61906 100644 --- a/cpp/src/encoding/gorilla_v1_decoder.h +++ b/cpp/src/encoding/gorilla_v1_decoder.h @@ -20,15 +20,135 @@ #ifndef ENCODING_GORILLA_V1_DECODER_H #define ENCODING_GORILLA_V1_DECODER_H +#include #include #include #include "common/allocator/byte_stream.h" #include "decoder.h" #include "encoding/encode_utils.h" +#include "gorilla_decoder.h" // GorillaBitReader namespace storage { +namespace gorilla_v1_detail { + +// Type-dependent parts of the GORILLA_V1 bit layout. Stored is the raw +// little-endian bit pattern of a value (uint32_t for FLOAT, uint64_t for +// DOUBLE), matching the types used by the per-value decoders below. +template +struct GorillaV1RawTraits; + +template <> +struct GorillaV1RawTraits { + typedef float Value; + static constexpr int VALUE_BITS = 32; + static constexpr int LEADING_BITS = 5; + static constexpr int LEN_BITS = 6; + + static FORCE_INLINE float to_value(uint32_t bits) { + return common::int_to_float(static_cast(bits)); + } + static FORCE_INLINE int leading_zeros(uint32_t bits) { + return number_of_leading_zeros(static_cast(bits)); + } + static FORCE_INLINE int trailing_zeros(uint32_t bits) { + return number_of_trailing_zeros(static_cast(bits)); + } +}; + +template <> +struct GorillaV1RawTraits { + typedef double Value; + static constexpr int VALUE_BITS = 64; + static constexpr int LEADING_BITS = 6; + static constexpr int LEN_BITS = 7; + + static FORCE_INLINE double to_value(uint64_t bits) { + return common::long_to_double(static_cast(bits)); + } + static FORCE_INLINE int leading_zeros(uint64_t bits) { + return number_of_leading_zeros(static_cast(bits)); + } + static FORCE_INLINE int trailing_zeros(uint64_t bits) { + return number_of_trailing_zeros(static_cast(bits)); + } +}; + +// Raw-pointer equivalent of the per-value prefetch(): decodes the encoding of +// the value that follows the one just returned and stores it in @pre_value. +// A NaN marks the encoder's end-of-stream sentinel and is never returned as a +// value. +template +FORCE_INLINE void prefetch_raw(GorillaBitReader& r, Stored& pre_value, + int& leading_zero_num, int& trailing_zero_num, + bool& is_end, int& read_error) { + typedef GorillaV1RawTraits traits; + const bool next_flag1 = r.read_bit(); + if (UNLIKELY(r.exhausted)) { + read_error = common::E_BUF_NOT_ENOUGH; + return; + } + if (!next_flag1) { + return; + } + const bool next_flag2 = r.read_bit(); + if (UNLIKELY(r.exhausted)) { + read_error = common::E_BUF_NOT_ENOUGH; + return; + } + if (!next_flag2) { + const int len = + traits::VALUE_BITS - leading_zero_num - trailing_zero_num; + if (UNLIKELY(len < 0 || len > traits::VALUE_BITS)) { + read_error = common::E_DECODE_ERR; + return; + } + uint64_t xor_value = r.read_long(len); + if (UNLIKELY(r.exhausted)) { + read_error = common::E_BUF_NOT_ENOUGH; + return; + } + xor_value <<= trailing_zero_num; + pre_value ^= static_cast(xor_value); + } else { + const int leading = static_cast(r.read_long(traits::LEADING_BITS)); + if (UNLIKELY(r.exhausted)) { + read_error = common::E_BUF_NOT_ENOUGH; + return; + } + const int len = static_cast(r.read_long(traits::LEN_BITS)); + if (UNLIKELY(r.exhausted)) { + read_error = common::E_BUF_NOT_ENOUGH; + return; + } + if (UNLIKELY(len < 0 || leading < 0 || + leading + len > traits::VALUE_BITS)) { + read_error = common::E_DECODE_ERR; + return; + } + uint64_t xor_value = r.read_long(len); + if (UNLIKELY(r.exhausted)) { + read_error = common::E_BUF_NOT_ENOUGH; + return; + } + // len == 0 carries no xor bits, so the value is zero; skipping the + // shift also avoids shifting by the full type width on a corrupt + // stream whose leading/len fields both decode to zero. + if (len > 0) { + xor_value <<= (traits::VALUE_BITS - leading - len); + } + pre_value ^= static_cast(xor_value); + } + leading_zero_num = traits::leading_zeros(pre_value); + trailing_zero_num = traits::trailing_zeros(pre_value); + if (std::isnan(traits::to_value(pre_value))) { + is_end = true; + } +} + +} // namespace gorilla_v1_detail + // Decoder for the original Gorilla (GORILLA_V1) layout written by Java's // SinglePrecisionEncoderV1/DoublePrecisionEncoderV1. Values are decoded one // step ahead, exactly like the Java decoder, because the NaN sentinel that @@ -82,6 +202,127 @@ class GorillaV1DecoderBase : public Decoder { return num; } + // Batch decode over a wrapped ByteStream. Walks the same read sequence as + // the per-value read_float/read_double (lookahead included, so a batch must + // never return the NaN sentinel), but reads bits from a 64-bit reservoir + // over the wrapped buffer instead of calling ByteStream::read_buf per byte. + // All decoder state is preserved so a batch may stop mid-stream and the + // next read_batch_* / read_float / read_double resumes correctly. + template + int batch_decode_raw( + typename gorilla_v1_detail::GorillaV1RawTraits::Value* out, + int capacity, int& actual, Stored& pre_value, common::ByteStream& in) { + typedef gorilla_v1_detail::GorillaV1RawTraits traits; + actual = 0; + if (capacity <= 0) { + return common::E_OK; + } + + const uint64_t cursor = in.read_pos(); + const uint64_t end_pos = cursor + in.remaining_size(); + // Anchor at the byte holding unread bits (if any) so the batch resumes + // exactly where a per-value read stopped: those bits are the low + // bits_left_ bits of buffer_, a byte already consumed by the stream. + const bool has_partial = bits_left_ > 0; + const uint64_t base_off = has_partial ? cursor - 1 : cursor; + const uint64_t data_len64 = end_pos - base_off; + // Pages are far smaller than 4 GiB, so saturating keeps the 32-bit + // cursor of GorillaBitReader safe. + const uint32_t data_len = data_len64 > UINT32_MAX + ? UINT32_MAX + : static_cast(data_len64); + + GorillaBitReader r; + r.data = + reinterpret_cast(in.get_wrapped_buf()) + base_off; + r.pos = has_partial ? 1 : 0; + r.data_len = data_len; + r.bits = has_partial ? bits_left_ : 0; + r.buffer = has_partial + ? static_cast(static_cast(buffer_)) + : 0; + + int ret = common::E_OK; + const uint32_t value_bytes = traits::VALUE_BITS / 8; + while (actual < capacity) { + // The lookahead that returns the last value also decodes the NaN + // sentinel and latches is_end_; the sentinel is never a value. In + // a well-formed page that lookahead also exhausts the bytes, so + // the guard below would stop here anyway; checking is_end_ first + // keeps a batch from decoding past an embedded NaN (the per-value + // decoder stops fetching bytes once is_end_ is set). + if (UNLIKELY(is_end_)) { + break; + } + // Mirror the per-value batch guard (has_remaining()): stop once + // every byte of the stream has been fetched, even if some low bits + // of the last byte are still buffered. + const uint64_t consumed_bits = static_cast(r.pos) * 8 - + static_cast(r.bits); + if (UNLIKELY(data_len == 0 || + consumed_bits > + static_cast(data_len) * 8 - 8)) { + break; + } + if (UNLIKELY(read_error_ != common::E_OK)) { + ret = read_error_; + break; + } + + typename traits::Value value; + if (UNLIKELY(!flag_)) { + flag_ = true; + if (UNLIKELY(r.bits != 0 || r.pos + value_bytes > r.data_len)) { + // Truncated before the first value finished. read_buf + // consumes the remaining bytes before reporting the error. + r.pos = r.data_len; + ret = common::E_DECODE_ERR; + break; + } + Stored raw = 0; + for (uint32_t i = 0; i < value_bytes; i++) { + raw |= static_cast(r.data[r.pos + i]) << (i * 8); + } + r.pos += value_bytes; + pre_value = raw; + leading_zero_num_ = traits::leading_zeros(raw); + trailing_zero_num_ = traits::trailing_zeros(raw); + value = traits::to_value(raw); + } else { + value = traits::to_value(pre_value); + } + gorilla_v1_detail::prefetch_raw( + r, pre_value, leading_zero_num_, trailing_zero_num_, is_end_, + read_error_); + if (UNLIKELY(read_error_ != common::E_OK)) { + // The per-value read returns the error together with the value + // whose lookahead failed; the batch loop discards that value. + ret = read_error_; + break; + } + out[actual++] = value; + } + + // Sync the raw cursor back. The unread bits are the low `bits` bits of + // the reservoir, so the byte cursor follows from the consumed bit + // count; restore the byte-oriented scalar state from that. + const uint64_t consumed_bits = + static_cast(r.pos) * 8 - static_cast(r.bits); + const uint64_t consumed_bytes = consumed_bits / 8; + const int partial_bits = static_cast(consumed_bits % 8); + if (partial_bits == 0) { + buffer_ = 0; + bits_left_ = 0; + in.wrapped_buf_advance_read_pos(base_off + consumed_bytes - cursor); + } else { + buffer_ = r.data[consumed_bytes]; + bits_left_ = 8 - partial_bits; + in.wrapped_buf_advance_read_pos(base_off + consumed_bytes + 1 - + cursor); + } + return ret; + } + bool flag_ = false; int leading_zero_num_ = 0; int trailing_zero_num_ = 0; @@ -134,6 +375,38 @@ class FloatGorillaV1Decoder : public GorillaV1DecoderBase { common::ByteStream& /*in*/) override { return common::E_TYPE_NOT_MATCH; } + int read_batch_float(float* out, int capacity, int& actual, + common::ByteStream& in) override { + // The raw path needs a contiguous wrapped buffer, and it anchors on the + // byte holding unread bits; with no consumed byte to anchor on (a + // caller re-wrapped the stream without resetting) only the per-value + // loop can make sense of the buffered bits. + if (UNLIKELY(!in.is_wrapped() || + (bits_left_ > 0 && in.read_pos() == 0))) { + return Decoder::read_batch_float(out, capacity, actual, in); + } + return batch_decode_raw(out, capacity, actual, pre_value_, + in); + } + int skip_float(int count, int& skipped, common::ByteStream& in) override { + skipped = 0; + // No cheaper way to skip exists: the encoding is stateful, so decode + // through the batch path and discard. + float scratch[64]; + while (skipped < count) { + const int chunk = std::min(count - skipped, 64); + int got = 0; + const int ret = read_batch_float(scratch, chunk, got, in); + if (ret != common::E_OK) { + return ret; + } + skipped += got; + if (got < chunk) { + break; + } + } + return common::E_OK; + } int read_double(double& /*ret_value*/, common::ByteStream& /*in*/) override { return common::E_TYPE_NOT_MATCH; @@ -173,7 +446,12 @@ class FloatGorillaV1Decoder : public GorillaV1DecoderBase { for (int i = 0; i < len; i++) { xor_value = (xor_value << 1) | (read_bit(in) ? 1u : 0u); } - xor_value <<= (32 - leading - len); + // len == 0 carries no xor bits, so the value is zero; skipping the + // shift also avoids shifting by the full type width on a corrupt + // stream whose leading/len fields both decode to zero. + if (len > 0) { + xor_value <<= (32 - leading - len); + } pre_value_ = xor_value ^ pre_value_; } leading_zero_num_ = @@ -236,6 +514,36 @@ class DoubleGorillaV1Decoder : public GorillaV1DecoderBase { int read_float(float& /*ret_value*/, common::ByteStream& /*in*/) override { return common::E_TYPE_NOT_MATCH; } + int read_batch_double(double* out, int capacity, int& actual, + common::ByteStream& in) override { + // See read_batch_float: the raw path needs a wrapped buffer and a + // consumed byte to anchor on. + if (UNLIKELY(!in.is_wrapped() || + (bits_left_ > 0 && in.read_pos() == 0))) { + return Decoder::read_batch_double(out, capacity, actual, in); + } + return batch_decode_raw(out, capacity, actual, pre_value_, + in); + } + int skip_double(int count, int& skipped, common::ByteStream& in) override { + skipped = 0; + // No cheaper way to skip exists: the encoding is stateful, so decode + // through the batch path and discard. + double scratch[64]; + while (skipped < count) { + const int chunk = std::min(count - skipped, 64); + int got = 0; + const int ret = read_batch_double(scratch, chunk, got, in); + if (ret != common::E_OK) { + return ret; + } + skipped += got; + if (got < chunk) { + break; + } + } + return common::E_OK; + } int read_String(common::String& /*ret_value*/, common::PageArena& /*pa*/, common::ByteStream& /*in*/) override { return common::E_TYPE_NOT_MATCH; @@ -271,7 +579,12 @@ class DoubleGorillaV1Decoder : public GorillaV1DecoderBase { for (int i = 0; i < len; i++) { xor_value = (xor_value << 1) | (read_bit(in) ? 1ULL : 0ULL); } - xor_value <<= (64 - leading - len); + // len == 0 carries no xor bits, so the value is zero; skipping the + // shift also avoids shifting by the full type width on a corrupt + // stream whose leading/len fields both decode to zero. + if (len > 0) { + xor_value <<= (64 - leading - len); + } pre_value_ = xor_value ^ pre_value_; } leading_zero_num_ = diff --git a/cpp/test/encoding/gorilla_v1_codec_test.cc b/cpp/test/encoding/gorilla_v1_codec_test.cc new file mode 100644 index 000000000..e065d34f3 --- /dev/null +++ b/cpp/test/encoding/gorilla_v1_codec_test.cc @@ -0,0 +1,948 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include + +#include +#include +#include +#include +#include +#include + +#include "common/allocator/byte_stream.h" +#include "encoding/gorilla_v1_decoder.h" +#include "encoding/gorilla_v1_encoder.h" + +namespace storage { + +namespace { + +// Type-dispatched access to the GORILLA_V1 entry points, so the same test body +// covers FLOAT (single precision) and DOUBLE. +template +struct V1Codec; + +template <> +struct V1Codec { + typedef FloatGorillaV1Encoder Encoder; + typedef FloatGorillaV1Decoder Decoder; + + static int read_one(Decoder& d, float& value, common::ByteStream& in) { + return d.read_float(value, in); + } + static int read_batch(Decoder& d, float* out, int capacity, int& actual, + common::ByteStream& in) { + return d.read_batch_float(out, capacity, actual, in); + } + static int read_exact(Decoder& d, float* out, int count, + common::ByteStream& in) { + return d.read_exact_float(out, count, in); + } + static int skip(Decoder& d, int count, int& skipped, + common::ByteStream& in) { + return d.skip_float(count, skipped, in); + } + static uint64_t bits(float value) { + return static_cast( + static_cast(common::float_to_int(value))); + } + static float from_bits(uint64_t bits) { + uint32_t narrow = static_cast(bits); + float value = 0.0f; + std::memcpy(&value, &narrow, sizeof(value)); + return value; + } +}; + +template <> +struct V1Codec { + typedef DoubleGorillaV1Encoder Encoder; + typedef DoubleGorillaV1Decoder Decoder; + + static int read_one(Decoder& d, double& value, common::ByteStream& in) { + return d.read_double(value, in); + } + static int read_batch(Decoder& d, double* out, int capacity, int& actual, + common::ByteStream& in) { + return d.read_batch_double(out, capacity, actual, in); + } + static int read_exact(Decoder& d, double* out, int count, + common::ByteStream& in) { + return d.read_exact_double(out, count, in); + } + static int skip(Decoder& d, int count, int& skipped, + common::ByteStream& in) { + return d.skip_double(count, skipped, in); + } + static uint64_t bits(double value) { + return static_cast(common::double_to_long(value)); + } + static double from_bits(uint64_t bits) { + int64_t narrow = static_cast(bits); + double value = 0.0; + std::memcpy(&value, &narrow, sizeof(value)); + return value; + } +}; + +template +std::vector EncodeV1(const std::vector& values) { + typename V1Codec::Encoder encoder; + common::ByteStream stream(1024, common::MOD_DEFAULT); + for (size_t i = 0; i < values.size(); i++) { + if (encoder.encode(values[i], stream) != common::E_OK) { + return std::vector(); + } + } + if (encoder.flush(stream) != common::E_OK) { + return std::vector(); + } + std::vector bytes(static_cast(stream.total_size())); + if (!bytes.empty()) { + uint32_t read_len = 0; + stream.read_buf(bytes.data(), static_cast(bytes.size()), + read_len); + if (read_len != bytes.size()) { + return std::vector(); + } + } + return bytes; +} + +std::vector Slice(const std::vector& bytes, size_t len) { + return std::vector(bytes.begin(), bytes.begin() + len); +} + +void WrapStream(const std::vector& bytes, common::ByteStream& in) { + in.wrap_from( + reinterpret_cast(bytes.empty() ? nullptr : bytes.data()), + static_cast(bytes.size())); +} + +template +struct Decoded { + Decoded() : ret(common::E_OK) {} + std::vector values; + int ret; +}; + +// Reference decoder: the per-value loop that Decoder::read_batch_* performs. +template +Decoded DecodePerValue(const std::vector& bytes, int limit) { + typename V1Codec::Decoder decoder; + common::ByteStream in(common::MOD_DEFAULT); + WrapStream(bytes, in); + Decoded out; + while (static_cast(out.values.size()) < limit && + decoder.has_remaining(in)) { + T value; + const int ret = V1Codec::read_one(decoder, value, in); + if (ret != common::E_OK) { + out.ret = ret; + return out; + } + out.values.push_back(value); + } + return out; +} + +// Batch decoder: repeatedly calls read_batch_* with the given capacity cycle +// until the decoder reports that no further value can be produced. +template +Decoded DecodeBatched(const std::vector& bytes, int limit, + const std::vector& capacity_cycle) { + typename V1Codec::Decoder decoder; + common::ByteStream in(common::MOD_DEFAULT); + WrapStream(bytes, in); + Decoded out; + std::vector buffer; + size_t cycle_pos = 0; + while (static_cast(out.values.size()) < limit) { + int capacity = capacity_cycle[cycle_pos % capacity_cycle.size()]; + cycle_pos++; + if (capacity <= 0) { + continue; + } + if (capacity > limit - static_cast(out.values.size())) { + capacity = limit - static_cast(out.values.size()); + } + buffer.resize(static_cast(capacity)); + int actual = 0; + const int ret = V1Codec::read_batch(decoder, buffer.data(), capacity, + actual, in); + // A failing call still reports the values it decoded before the error, + // exactly like Decoder::read_batch_*'s per-value loop. + out.values.insert(out.values.end(), buffer.begin(), + buffer.begin() + actual); + if (ret != common::E_OK) { + out.ret = ret; + return out; + } + if (actual < capacity) { + break; + } + } + return out; +} + +// Reference skip: the per-value loop that Decoder::skip_* performs. +template +int SkipPerValue(typename V1Codec::Decoder& decoder, int count, int& skipped, + common::ByteStream& in) { + skipped = 0; + while (skipped < count && decoder.has_remaining(in)) { + T value; + const int ret = V1Codec::read_one(decoder, value, in); + if (ret != common::E_OK) { + return ret; + } + ++skipped; + } + return common::E_OK; +} + +template +void ExpectSameValues(const std::vector& expected, + const std::vector& actual, + const std::string& context) { + ASSERT_EQ(expected.size(), actual.size()) << context; + for (size_t i = 0; i < expected.size(); i++) { + EXPECT_EQ(V1Codec::bits(expected[i]), V1Codec::bits(actual[i])) + << context << " index=" << i; + } +} + +template +std::vector MakeRandomValues(int count, uint64_t seed) { + std::vector values; + values.reserve(static_cast(count)); + uint64_t state = seed; + for (int i = 0; i < count; i++) { + state = state * UINT64_C(6364136223846793005) + + UINT64_C(1442695040888963407); + T value = V1Codec::from_bits(state ^ (state >> 29)); + if (std::isnan(value)) { + value = static_cast(i % 977) * static_cast(0.25); + } + values.push_back(value); + } + return values; +} + +template +std::vector MakeSmoothValues(int count) { + std::vector values; + values.reserve(static_cast(count)); + for (int i = 0; i < count; i++) { + const double x = static_cast(i) / 100.0; + values.push_back(static_cast(std::sin(x) * 1000.0 + + std::cos(x * 0.37) * 0.125 + + static_cast(i) * 0.001)); + } + return values; +} + +template +std::vector MakeRepeatedValues() { + std::vector values; + for (int i = 0; i < 517; i++) { + values.push_back(static_cast(17.25)); + } + for (int i = 0; i < 600; i++) { + values.push_back(static_cast(-3.5)); + } + for (int i = 0; i < 300; i++) { + values.push_back(static_cast(i) * static_cast(0.125)); + } + return values; +} + +template +std::vector MakeSpecialValues() { + const T inf = std::numeric_limits::infinity(); + std::vector values; + values.push_back(static_cast(0)); + values.push_back(-static_cast(0)); + values.push_back(static_cast(1)); + values.push_back(static_cast(-1)); + values.push_back(inf); + values.push_back(-inf); + values.push_back(std::numeric_limits::denorm_min()); + values.push_back(std::numeric_limits::max()); + values.push_back(-std::numeric_limits::max()); + values.push_back(std::numeric_limits::min()); + values.push_back(std::numeric_limits::epsilon()); + values.push_back(static_cast(1e-30)); + values.push_back(static_cast(1e30)); + values.push_back(static_cast(-1e-30)); + // Largest subnormal: adjacent bit pattern to the smallest normal. + values.push_back(V1Codec::from_bits( + V1Codec::bits(std::numeric_limits::min()) - 1)); + values.push_back(static_cast(0)); + values.push_back(inf); + values.push_back(std::numeric_limits::denorm_min()); + return values; +} + +// Encodes @values, then checks that the per-value decoder, the batch decoder +// under several capacity cycles, and the input values all agree bit for bit. +template +void RunParityCase(const std::vector& values, const std::string& context) { + ASSERT_FALSE(values.empty()) << context; + const std::vector bytes = EncodeV1(values); + ASSERT_FALSE(bytes.empty()) << context; + const int count = static_cast(values.size()); + + const Decoded reference = DecodePerValue(bytes, count + 8); + ASSERT_EQ(reference.ret, common::E_OK) << context; + ExpectSameValues(values, reference.values, context + " per-value"); + + const std::vector> cycles = { + std::vector(1, 1), std::vector(1, 2), + std::vector(1, 3), std::vector(1, 7), + std::vector(1, 64), std::vector(1, 129), + std::vector(1, 1000000), std::vector{5, 1, 9, 2, 17, 33}, + std::vector{257, 1}, + }; + for (size_t c = 0; c < cycles.size(); c++) { + const Decoded batched = + DecodeBatched(bytes, count + 8, cycles[c]); + ASSERT_EQ(batched.ret, common::E_OK) << context << " cycle=" << c; + ExpectSameValues(values, batched.values, + context + " batch cycle=" + std::to_string(c)); + } +} + +// Truncates @values' encoding at every prefix length and checks that the batch +// decoder reports the same outcome as the per-value loop. +template +void RunTruncationParity(const std::vector& values, + const std::string& context) { + const std::vector bytes = EncodeV1(values); + ASSERT_FALSE(bytes.empty()) << context; + const int limit = static_cast(values.size()) + 8; + for (size_t len = 0; len <= bytes.size(); len++) { + const std::vector prefix = Slice(bytes, len); + const std::string label = context + " len=" + std::to_string(len); + const Decoded reference = DecodePerValue(prefix, limit); + const std::vector cycle{3, 1, 5}; + const Decoded batched = DecodeBatched(prefix, limit, cycle); + EXPECT_EQ(batched.ret, reference.ret) << label; + ExpectSameValues(reference.values, batched.values, label); + } +} + +// Decodes an unwrapped (owning) ByteStream: the batch override must fall back +// to the per-value loop there. +template +void RunUnwrappedFallback(const std::vector& values, + const std::string& context) { + typename V1Codec::Encoder encoder; + common::ByteStream stream(1024, common::MOD_DEFAULT); + for (size_t i = 0; i < values.size(); i++) { + ASSERT_EQ(encoder.encode(values[i], stream), common::E_OK) << context; + } + ASSERT_EQ(encoder.flush(stream), common::E_OK) << context; + ASSERT_FALSE(stream.is_wrapped()) << context; + + typename V1Codec::Decoder decoder; + std::vector decoded; + std::vector buffer(7); + while (static_cast(decoded.size()) < static_cast(values.size())) { + int actual = 0; + ASSERT_EQ(V1Codec::read_batch(decoder, buffer.data(), + static_cast(buffer.size()), + actual, stream), + common::E_OK) + << context; + ASSERT_GT(actual, 0) << context; + decoded.insert(decoded.end(), buffer.begin(), buffer.begin() + actual); + } + ExpectSameValues(values, decoded, context); +} + +// Switches between batch and per-value reads mid-stream, in both orders. +template +void RunHandoff(const std::vector& values, int handoff, + const std::string& context) { + const std::vector bytes = EncodeV1(values); + ASSERT_FALSE(bytes.empty()) << context; + const int count = static_cast(values.size()); + ASSERT_LE(handoff, count) << context; + + { + typename V1Codec::Decoder decoder; + common::ByteStream in(common::MOD_DEFAULT); + WrapStream(bytes, in); + std::vector decoded(static_cast(handoff)); + int actual = 0; + ASSERT_EQ(V1Codec::read_batch(decoder, decoded.data(), handoff, + actual, in), + common::E_OK) + << context; + ASSERT_EQ(actual, handoff) << context; + while (static_cast(decoded.size()) < count && + decoder.has_remaining(in)) { + T value; + ASSERT_EQ(V1Codec::read_one(decoder, value, in), common::E_OK) + << context; + decoded.push_back(value); + } + ExpectSameValues(values, decoded, context + " batch->per-value"); + } + { + typename V1Codec::Decoder decoder; + common::ByteStream in(common::MOD_DEFAULT); + WrapStream(bytes, in); + std::vector decoded; + while (static_cast(decoded.size()) < handoff) { + T value; + ASSERT_EQ(V1Codec::read_one(decoder, value, in), common::E_OK) + << context; + decoded.push_back(value); + } + std::vector buffer(static_cast(count - handoff)); + int actual = 0; + ASSERT_EQ( + V1Codec::read_batch(decoder, buffer.data(), + static_cast(buffer.size()), actual, in), + common::E_OK) + << context; + decoded.insert(decoded.end(), buffer.begin(), buffer.begin() + actual); + ExpectSameValues(values, decoded, context + " per-value->batch"); + } +} + +template +void RunSkipCase(const std::vector& values, int skip_count, + const std::string& context) { + const std::vector bytes = EncodeV1(values); + ASSERT_FALSE(bytes.empty()) << context; + const int count = static_cast(values.size()); + ASSERT_GE(skip_count, 0) << context; + ASSERT_LE(skip_count, count) << context; + + const int expected_skipped = skip_count; + typename V1Codec::Decoder decoder; + common::ByteStream in(common::MOD_DEFAULT); + WrapStream(bytes, in); + int skipped = -1; + ASSERT_EQ(V1Codec::skip(decoder, skip_count, skipped, in), common::E_OK) + << context; + ASSERT_EQ(skipped, expected_skipped) << context; + + std::vector rest(static_cast(count - skip_count)); + if (!rest.empty()) { + ASSERT_EQ(V1Codec::read_exact(decoder, rest.data(), + static_cast(rest.size()), in), + common::E_OK) + << context; + std::vector expected(values.begin() + skip_count, values.end()); + ExpectSameValues(expected, rest, context); + } +} + +// Batches @values and checks skipping more values than the stream holds stops +// at the end marker without error. +template +void RunSkipPastEnd(const std::vector& values, const std::string& context) { + const std::vector bytes = EncodeV1(values); + typename V1Codec::Decoder decoder; + common::ByteStream in(common::MOD_DEFAULT); + WrapStream(bytes, in); + int skipped = 0; + ASSERT_EQ(V1Codec::skip(decoder, static_cast(values.size()) + 10, + skipped, in), + common::E_OK) + << context; + EXPECT_EQ(skipped, static_cast(values.size())) << context; + EXPECT_FALSE(decoder.has_remaining(in)) << context; +} + +class GorillaV1CodecTest : public ::testing::Test {}; + +} // namespace + +// ── Java wire-format golden streams ──────────────────────────────────────── +// Byte streams generated with the Java implementation's +// SinglePrecisionEncoderV1 / DoublePrecisionEncoderV1 so the C++ encoder and +// both C++ decode paths are checked against the Java wire format. + +TEST_F(GorillaV1CodecTest, JavaGoldenFloatStreams) { + const std::vector one_two = {0x00, 0x00, 0x80, 0x3F, 0xC2, + 0x47, 0xFE, 0x22, 0x3F, 0xC0}; + const std::vector one_two_values = {1.0f, 2.0f}; + EXPECT_EQ(EncodeV1(one_two_values), one_two); + EXPECT_EQ(DecodePerValue(one_two, 10).ret, common::E_OK); + ExpectSameValues(one_two_values, DecodePerValue(one_two, 10).values, + "java [1.0, 2.0] per-value"); + ExpectSameValues( + one_two_values, + DecodeBatched(one_two, 10, std::vector(1, 2)).values, + "java [1.0, 2.0] batch"); + + const std::vector one_two_three = {0x00, 0x00, 0x80, 0x3F, 0xC2, + 0x47, 0xFE, 0x90, 0x73, 0xF8}; + const std::vector three_values = {1.0f, 2.0f, 3.0f}; + EXPECT_EQ(EncodeV1(three_values), one_two_three); + ExpectSameValues( + three_values, + DecodeBatched(one_two_three, 10, std::vector(1, 1)).values, + "java [1.0, 2.0, 3.0] batch"); + + const std::vector single = {0x00, 0x00, 0xF0, 0x40, 0x9F, 0x98}; + const std::vector single_value = {7.5f}; + EXPECT_EQ(EncodeV1(single_value), single); + const Decoded decoded = + DecodeBatched(single, 10, std::vector(1, 8)); + ASSERT_EQ(decoded.ret, common::E_OK); + ExpectSameValues(single_value, decoded.values, "java [7.5] batch"); + + const std::vector empty = {0x00, 0x00, 0xC0, 0x7F}; + EXPECT_EQ(EncodeV1(std::vector()), empty); + // The encoder writes only the raw NaN sentinel; both paths must report + // E_BUF_NOT_ENOUGH and produce no value. + EXPECT_EQ(DecodePerValue(empty, 10).ret, common::E_BUF_NOT_ENOUGH); + const Decoded empty_batched = + DecodeBatched(empty, 10, std::vector(1, 3)); + EXPECT_EQ(empty_batched.ret, common::E_BUF_NOT_ENOUGH); + EXPECT_TRUE(empty_batched.values.empty()); +} + +TEST_F(GorillaV1CodecTest, JavaGoldenDoubleStreams) { + const std::vector one_two = {0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0xF0, 0x3F, 0xC1, 0x17, + 0xFF, 0xF0, 0x85, 0xFF, 0xF0}; + const std::vector one_two_values = {1.0, 2.0}; + EXPECT_EQ(EncodeV1(one_two_values), one_two); + ExpectSameValues(one_two_values, DecodePerValue(one_two, 10).values, + "java double [1.0, 2.0] per-value"); + ExpectSameValues( + one_two_values, + DecodeBatched(one_two, 10, std::vector(1, 2)).values, + "java double [1.0, 2.0] batch"); + + const std::vector mixed = { + 0x18, 0x2D, 0x44, 0x54, 0xFB, 0x21, 0x09, 0x40, 0xC0, 0x7B, 0x80, 0x12, + 0x43, 0xF6, 0xA8, 0x88, 0x5A, 0x3C, 0x07, 0xDF, 0xC6, 0xFC, 0x87, 0x91, + 0x00, 0x0E, 0xB3, 0xC0, 0x73, 0xF9, 0x0F, 0x22, 0x00, 0x1D, 0x67}; + const std::vector mixed_values = {3.141592653589793, -0.0, 1e300}; + EXPECT_EQ(EncodeV1(mixed_values), mixed); + ExpectSameValues( + mixed_values, + DecodeBatched(mixed, 10, std::vector(1, 1)).values, + "java double pi/-0/1e300 batch"); + + const std::vector extremes = { + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEF, 0x7F, 0xBF, + 0xF7, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x60, 0xBF, + 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}; + const std::vector extreme_values = { + std::numeric_limits::max(), + std::numeric_limits::denorm_min()}; + EXPECT_EQ(EncodeV1(extreme_values), extremes); + ExpectSameValues( + extreme_values, + DecodeBatched(extremes, 10, std::vector(1, 5)).values, + "java double max/denorm_min batch"); + + const std::vector neg_zero = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x80, 0xC0, 0x1B, 0xFF, 0xF0}; + const std::vector neg_zero_values = {-0.0}; + EXPECT_EQ(EncodeV1(neg_zero_values), neg_zero); + ExpectSameValues( + neg_zero_values, + DecodeBatched(neg_zero, 10, std::vector(1, 4)).values, + "java double -0.0 batch"); + + const std::vector empty = {0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0xF8, 0x7F}; + EXPECT_EQ(EncodeV1(std::vector()), empty); + EXPECT_EQ(DecodePerValue(empty, 10).ret, common::E_BUF_NOT_ENOUGH); + EXPECT_EQ(DecodeBatched(empty, 10, std::vector(1, 2)).ret, + common::E_BUF_NOT_ENOUGH); +} + +TEST_F(GorillaV1CodecTest, JavaGoldenFirstValueNaN) { + // Java encodes a leading NaN as the raw first value; the following value + // is still decodable because only the lookahead treats NaN as the marker. + const std::vector bytes = {0x00, 0x00, 0xC0, 0x7F, + 0xA0, 0x18, 0x41, 0x80}; + const std::vector values = {std::numeric_limits::quiet_NaN(), + 1.5f}; + EXPECT_EQ(EncodeV1(values), bytes); + const Decoded reference = DecodePerValue(bytes, 10); + ASSERT_EQ(reference.ret, common::E_OK); + ASSERT_EQ(reference.values.size(), 2u); + ExpectSameValues(values, reference.values, "java [NaN, 1.5] per-value"); + const Decoded batched = + DecodeBatched(bytes, 10, std::vector(1, 1)); + ASSERT_EQ(batched.ret, common::E_OK); + ExpectSameValues(values, batched.values, "java [NaN, 1.5] batch"); +} + +// ── Batch/per-value parity ───────────────────────────────────────────────── + +TEST_F(GorillaV1CodecTest, FloatParityRandomValues) { + RunParityCase(MakeRandomValues(5000, UINT64_C(0x9e3779b97f4a7c15)), + "float random"); +} + +TEST_F(GorillaV1CodecTest, DoubleParityRandomValues) { + RunParityCase(MakeRandomValues(5000, UINT64_C(0x2545F4914F6CDD1D)), + "double random"); +} + +TEST_F(GorillaV1CodecTest, FloatParitySmoothValues) { + RunParityCase(MakeSmoothValues(4000), "float smooth"); +} + +TEST_F(GorillaV1CodecTest, DoubleParitySmoothValues) { + RunParityCase(MakeSmoothValues(4000), "double smooth"); +} + +TEST_F(GorillaV1CodecTest, FloatParityRepeatedRuns) { + RunParityCase(MakeRepeatedValues(), "float repeated"); +} + +TEST_F(GorillaV1CodecTest, DoubleParityRepeatedRuns) { + RunParityCase(MakeRepeatedValues(), "double repeated"); +} + +TEST_F(GorillaV1CodecTest, FloatParityConstant) { + RunParityCase(std::vector(10000, 3.141592653589793f), + "float constant"); + RunParityCase(std::vector(513, 0.0f), "float zeros"); +} + +TEST_F(GorillaV1CodecTest, DoubleParityConstant) { + RunParityCase(std::vector(10000, 3.141592653589793), + "double constant"); + RunParityCase(std::vector(513, -0.0), "double neg zeros"); +} + +TEST_F(GorillaV1CodecTest, FloatParitySpecialValues) { + RunParityCase(MakeSpecialValues(), "float special"); +} + +TEST_F(GorillaV1CodecTest, DoubleParitySpecialValues) { + RunParityCase(MakeSpecialValues(), "double special"); +} + +TEST_F(GorillaV1CodecTest, FloatParitySingleValue) { + RunParityCase(std::vector(1, 42.25f), "float single"); + RunParityCase(std::vector(1, -0.0f), "float single -0"); + RunParityCase(std::vector(1, std::numeric_limits::infinity()), + "float single inf"); +} + +TEST_F(GorillaV1CodecTest, DoubleParitySingleValue) { + RunParityCase(std::vector(1, -1234.5), "double single"); + RunParityCase(std::vector(1, std::numeric_limits::min()), + "double single min normal"); +} + +TEST_F(GorillaV1CodecTest, ParityTwoValuesAndBoundaryLengths) { + for (int count = 2; count <= 40; count++) { + std::vector values; + for (int i = 0; i < count; i++) { + values.push_back(static_cast(i) * 0.75f - 5.0f); + } + RunParityCase(values, "float len=" + std::to_string(count)); + } +} + +TEST_F(GorillaV1CodecTest, FloatParityLargeMagnitudeJumps) { + std::vector values; + for (int i = 0; i < 2000; i++) { + switch (i % 4) { + case 0: + values.push_back(static_cast(i) * 1e30f); + break; + case 1: + values.push_back(static_cast(i) * 1e-30f); + break; + case 2: + values.push_back(-static_cast(i) * 1.0f); + break; + default: + values.push_back(static_cast(i % 7)); + break; + } + } + RunParityCase(values, "float magnitude jumps"); +} + +// ── Truncated streams ────────────────────────────────────────────────────── + +TEST_F(GorillaV1CodecTest, FloatTruncationParity) { + RunTruncationParity(std::vector{1.5f, 2.5f, 3.25f, -8.0f}, + "float truncation"); + RunTruncationParity(MakeRepeatedValues(), "float truncation long"); +} + +TEST_F(GorillaV1CodecTest, DoubleTruncationParity) { + RunTruncationParity(std::vector{1.5, 2.5, 3.25, -8.0}, + "double truncation"); + RunTruncationParity(MakeSmoothValues(200), + "double truncation long"); +} + +// ── Capacity and stream-boundary edges ───────────────────────────────────── + +TEST_F(GorillaV1CodecTest, ZeroCapacityLeavesStateUntouched) { + const std::vector values = MakeSmoothValues(50); + const std::vector bytes = EncodeV1(values); + FloatGorillaV1Decoder decoder; + common::ByteStream in(common::MOD_DEFAULT); + WrapStream(bytes, in); + int actual = -1; + EXPECT_EQ(decoder.read_batch_float(nullptr, 0, actual, in), common::E_OK); + EXPECT_EQ(actual, 0); + EXPECT_EQ(in.read_pos(), 0u); + EXPECT_TRUE(decoder.has_remaining(in)); + + std::vector decoded(values.size()); + int got = 0; + ASSERT_EQ(decoder.read_batch_float( + decoded.data(), static_cast(decoded.size()), got, in), + common::E_OK); + ExpectSameValues(values, decoded, "float after zero capacity"); + + const std::vector dvalues = MakeSmoothValues(50); + const std::vector dbytes = EncodeV1(dvalues); + DoubleGorillaV1Decoder ddecoder; + common::ByteStream din(common::MOD_DEFAULT); + WrapStream(dbytes, din); + EXPECT_EQ(ddecoder.read_batch_double(nullptr, 0, actual, din), + common::E_OK); + EXPECT_EQ(actual, 0); +} + +TEST_F(GorillaV1CodecTest, CapacityBeyondRemainingValues) { + const std::vector values = MakeSmoothValues(37); + const std::vector bytes = EncodeV1(values); + FloatGorillaV1Decoder decoder; + common::ByteStream in(common::MOD_DEFAULT); + WrapStream(bytes, in); + + std::vector decoded(values.size() + 5); + int actual = 0; + ASSERT_EQ(decoder.read_batch_float( + decoded.data(), static_cast(decoded.size()), actual, in), + common::E_OK); + ASSERT_EQ(actual, static_cast(values.size())); + decoded.resize(static_cast(actual)); + ExpectSameValues(values, decoded, "float capacity beyond"); + + // Further calls must report no values and keep returning E_OK. + for (int i = 0; i < 3; i++) { + actual = -1; + EXPECT_EQ(decoder.read_batch_float(decoded.data(), 8, actual, in), + common::E_OK); + EXPECT_EQ(actual, 0); + } +} + +TEST_F(GorillaV1CodecTest, ReadExactAcrossMultipleCalls) { + const std::vector values = MakeSmoothValues(1000); + const std::vector bytes = EncodeV1(values); + DoubleGorillaV1Decoder decoder; + common::ByteStream in(common::MOD_DEFAULT); + WrapStream(bytes, in); + std::vector decoded(values.size()); + ASSERT_EQ(decoder.read_exact_double(decoded.data(), 300, in), common::E_OK); + ASSERT_EQ( + decoder.read_exact_double(decoded.data() + 300, + static_cast(decoded.size()) - 300, in), + common::E_OK); + ExpectSameValues(values, decoded, "double read_exact chunks"); +} + +// The readers decode each page with a reset decoder over a freshly wrapped +// page buffer; make sure a decoder can be reused that way after the NaN +// sentinel of the previous page has been consumed. +TEST_F(GorillaV1CodecTest, ResetBetweenPages) { + const std::vector page1 = MakeSmoothValues(300); + const std::vector page2 = MakeRepeatedValues(); + const std::vector bytes1 = EncodeV1(page1); + const std::vector bytes2 = EncodeV1(page2); + DoubleGorillaV1Decoder decoder; + + std::vector decoded1(page1.size()); + { + common::ByteStream in(common::MOD_DEFAULT); + WrapStream(bytes1, in); + ASSERT_EQ(decoder.read_exact_double( + decoded1.data(), static_cast(decoded1.size()), in), + common::E_OK); + ExpectSameValues(page1, decoded1, "page 1"); + } + + decoder.reset(); + std::vector decoded2(page2.size()); + { + common::ByteStream in(common::MOD_DEFAULT); + WrapStream(bytes2, in); + ASSERT_EQ(decoder.read_exact_double( + decoded2.data(), static_cast(decoded2.size()), in), + common::E_OK); + ExpectSameValues(page2, decoded2, "page 2"); + } + + const std::vector fpage1 = MakeSmoothValues(300); + const std::vector fpage2 = MakeRepeatedValues(); + const std::vector fbytes1 = EncodeV1(fpage1); + const std::vector fbytes2 = EncodeV1(fpage2); + FloatGorillaV1Decoder fdecoder; + std::vector fdecoded1(fpage1.size()); + { + common::ByteStream in(common::MOD_DEFAULT); + WrapStream(fbytes1, in); + ASSERT_EQ(fdecoder.read_exact_float( + fdecoded1.data(), static_cast(fdecoded1.size()), in), + common::E_OK); + ExpectSameValues(fpage1, fdecoded1, "float page 1"); + } + fdecoder.reset(); + std::vector fdecoded2(fpage2.size()); + { + common::ByteStream in(common::MOD_DEFAULT); + WrapStream(fbytes2, in); + ASSERT_EQ(fdecoder.read_exact_float( + fdecoded2.data(), static_cast(fdecoded2.size()), in), + common::E_OK); + ExpectSameValues(fpage2, fdecoded2, "float page 2"); + } +} + +// ── Unwrapped streams fall back to the per-value loop ────────────────────── + +TEST_F(GorillaV1CodecTest, FloatUnwrappedFallback) { + RunUnwrappedFallback(MakeSmoothValues(300), "float unwrapped"); +} + +TEST_F(GorillaV1CodecTest, DoubleUnwrappedFallback) { + RunUnwrappedFallback(MakeRepeatedValues(), "double unwrapped"); +} + +// ── Mixed per-value / batch reads ────────────────────────────────────────── + +TEST_F(GorillaV1CodecTest, BatchPerValueHandoff) { + const std::vector values = MakeSmoothValues(777); + RunHandoff(values, 1, "float handoff 1"); + RunHandoff(values, 256, "float handoff 256"); + RunHandoff(values, 776, "float handoff 776"); + + const std::vector dvalues = MakeSmoothValues(777); + RunHandoff(dvalues, 1, "double handoff 1"); + RunHandoff(dvalues, 256, "double handoff 256"); + RunHandoff(dvalues, 776, "double handoff 776"); +} + +// ── Skip ────────────────────────────────────────────────────────────────── + +TEST_F(GorillaV1CodecTest, FloatSkipParity) { + const std::vector values = MakeSmoothValues(1500); + RunSkipCase(values, 0, "float skip 0"); + RunSkipCase(values, 1, "float skip 1"); + RunSkipCase(values, 129, "float skip 129"); + RunSkipCase(values, 1499, "float skip last"); + RunSkipCase(values, 1500, "float skip all"); + RunSkipPastEnd(values, "float skip past end"); +} + +TEST_F(GorillaV1CodecTest, DoubleSkipParity) { + const std::vector values = MakeRepeatedValues(); + RunSkipCase(values, 0, "double skip 0"); + RunSkipCase(values, 517, "double skip 517"); + RunSkipCase(values, 777, "double skip 777"); + RunSkipCase(values, 1416, "double skip last"); + RunSkipPastEnd(values, "double skip past end"); + + // Skip must consume the same values as the per-value skip loop. + const std::vector bytes = EncodeV1(values); + DoubleGorillaV1Decoder decoder; + common::ByteStream in(common::MOD_DEFAULT); + WrapStream(bytes, in); + int skipped = 0; + ASSERT_EQ(decoder.skip_double(600, skipped, in), common::E_OK); + ASSERT_EQ(skipped, 600); + + // Same skip through the per-value reference loop: both must stop at the + // same stream position. + const std::vector bytes2 = EncodeV1(values); + DoubleGorillaV1Decoder ref_decoder; + common::ByteStream ref_in(common::MOD_DEFAULT); + WrapStream(bytes2, ref_in); + int ref_skipped = 0; + ASSERT_EQ(SkipPerValue(ref_decoder, 600, ref_skipped, ref_in), + common::E_OK); + EXPECT_EQ(ref_skipped, skipped); + EXPECT_EQ(ref_in.read_pos(), in.read_pos()); + + std::vector rest(values.size() - 600); + ASSERT_EQ(decoder.read_exact_double(rest.data(), + static_cast(rest.size()), in), + common::E_OK); + std::vector expected(values.begin() + 600, values.end()); + ExpectSameValues(expected, rest, "double skip then read"); +} + +// ── NaN handling ────────────────────────────────────────────────────────── + +TEST_F(GorillaV1CodecTest, BatchStopsAtEmbeddedNaN) { + // GORILLA_V1 cannot carry a mid-stream NaN: the encoder writes it with the + // same canonical bit pattern as the flush sentinel, so the decoder treats + // it as the end of the stream. The per-value loop then stops fetching + // bytes; a batch must stop at the marker instead of emitting it or + // decoding whatever follows. + std::vector values = {1.5f, std::numeric_limits::quiet_NaN(), + 2.5f, 3.5f}; + const std::vector bytes = EncodeV1(values); + FloatGorillaV1Decoder decoder; + common::ByteStream in(common::MOD_DEFAULT); + WrapStream(bytes, in); + + std::vector decoded(16); + int actual = 0; + ASSERT_EQ(decoder.read_batch_float(decoded.data(), 16, actual, in), + common::E_OK); + ASSERT_EQ(actual, 1); + EXPECT_EQ(V1Codec::bits(decoded[0]), V1Codec::bits(1.5f)); + actual = -1; + EXPECT_EQ(decoder.read_batch_float(decoded.data(), 16, actual, in), + common::E_OK); + EXPECT_EQ(actual, 0); + + std::vector dvalues = { + 1.5, std::numeric_limits::quiet_NaN(), 2.5}; + const std::vector dbytes = EncodeV1(dvalues); + DoubleGorillaV1Decoder ddecoder; + common::ByteStream din(common::MOD_DEFAULT); + WrapStream(dbytes, din); + std::vector ddecoded(16); + ASSERT_EQ(ddecoder.read_batch_double(ddecoded.data(), 16, actual, din), + common::E_OK); + ASSERT_EQ(actual, 1); + EXPECT_EQ(V1Codec::bits(ddecoded[0]), V1Codec::bits(1.5)); +} + +} // namespace storage