-
Notifications
You must be signed in to change notification settings - Fork 0
Adding Snappy to API server flow #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
e0cd859
1e64a08
6132ac0
2323177
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,6 +82,13 @@ FetchContent_Declare( | |
| GIT_TAG master | ||
| ) | ||
|
|
||
| # Snappy is a fast compression/decompression library | ||
| FetchContent_Declare( | ||
| snappy | ||
| GIT_REPOSITORY https://github.com/google/snappy.git | ||
| GIT_TAG 1.1.10 | ||
| ) | ||
|
|
||
| # Fetch dependencies | ||
|
|
||
| # Disable dependency tests for cppcodec | ||
|
|
@@ -92,6 +99,19 @@ set(BUILD_TESTING OFF CACHE BOOL "" FORCE) | |
| FetchContent_MakeAvailable(cppcodec) | ||
| set(BUILD_TESTING ON CACHE BOOL "" FORCE) | ||
|
|
||
| # Disable Snappy tests to avoid conflicts with our GoogleTest | ||
| # Snappy includes its own GoogleTest which conflicts with our FetchContent GoogleTest | ||
| set(SNAPPY_BUILD_TESTS OFF CACHE BOOL "" FORCE) | ||
| FetchContent_MakeAvailable(snappy) | ||
| set(SNAPPY_BUILD_TESTS ON CACHE BOOL "" FORCE) | ||
|
|
||
| # Suppress warnings from Snappy's source code | ||
| if(TARGET snappy) | ||
| target_compile_options(snappy PRIVATE | ||
| -Wno-sign-compare | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need this?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are a bunch of compilation warnings that make the compilation output too noisy, so added this to suppress the warnings. |
||
| ) | ||
| endif() | ||
|
|
||
| FetchContent_MakeAvailable(crow httplib nlohmann_json cxxopts googletest jwt-cpp) | ||
|
|
||
| # Make GoogleTest helper macros available (gtest_discover_tests) | ||
|
|
@@ -118,13 +138,15 @@ add_library(dbps_server_lib STATIC | |
| src/server/encryption_sequencer.cpp | ||
| src/server/auth_utils.cpp | ||
| src/server/decoding_utils.cpp | ||
| src/server/compression_utils.cpp | ||
| ) | ||
| target_link_libraries(dbps_server_lib PUBLIC dbps_common_lib) | ||
| target_link_libraries(dbps_server_lib PUBLIC dbps_common_lib snappy) | ||
| target_include_directories(dbps_server_lib PUBLIC | ||
| src/server | ||
| ${CMAKE_BINARY_DIR}/_deps/cppcodec-src | ||
| ${CMAKE_BINARY_DIR}/_deps/jwt-cpp-src/include | ||
| ${CMAKE_BINARY_DIR}/_deps/nlohmann_json-src/include | ||
| ${CMAKE_BINARY_DIR}/_deps/snappy-src | ||
| ) | ||
|
|
||
| # Find and link OpenSSL (required by jwt-cpp) | ||
|
|
@@ -244,6 +266,15 @@ if(BUILD_TESTS) | |
| ) | ||
| target_include_directories(decoding_utils_test PRIVATE src/server) | ||
|
|
||
| # Compression utils tests | ||
| add_executable(compression_utils_test src/server/compression_utils_test.cpp) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need this now that we have GTEST?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the same as the other test make targets, no? Following the same pattern. |
||
| target_link_libraries(compression_utils_test | ||
| dbps_server_lib | ||
| dbps_common_lib | ||
| gtest_main | ||
| ) | ||
| target_include_directories(compression_utils_test PRIVATE src/server) | ||
|
|
||
| # Auth utils tests | ||
| add_executable(auth_utils_test src/server/auth_utils_test.cpp) | ||
| target_link_libraries(auth_utils_test | ||
|
|
@@ -413,6 +444,7 @@ if(BUILD_TESTS) | |
| enum_utils_test | ||
| encryption_sequencer_test | ||
| decoding_utils_test | ||
| compression_utils_test | ||
| auth_utils_test | ||
| dbpa_interface_test | ||
| dbpa_utils_test | ||
|
|
@@ -429,6 +461,7 @@ if(BUILD_TESTS) | |
| gtest_discover_tests(enum_utils_test) | ||
| gtest_discover_tests(encryption_sequencer_test) | ||
| gtest_discover_tests(decoding_utils_test) | ||
| gtest_discover_tests(compression_utils_test) | ||
| gtest_discover_tests(auth_utils_test) | ||
| gtest_discover_tests(dbpa_interface_test) | ||
| gtest_discover_tests(dbpa_utils_test) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // 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 "compression_utils.h" | ||
| #include <snappy.h> | ||
|
|
||
| using namespace dbps::external; | ||
| using namespace dbps::enum_utils; | ||
|
|
||
| std::vector<uint8_t> Compress(const std::vector<uint8_t>& bytes, CompressionCodec::type compression) { | ||
| if (compression == CompressionCodec::UNCOMPRESSED) { | ||
| return bytes; | ||
| } | ||
|
|
||
| if (compression == CompressionCodec::SNAPPY) { | ||
| if (bytes.empty()) { | ||
| return bytes; | ||
| } | ||
| std::string compressed; | ||
| snappy::Compress(reinterpret_cast<const char*>(bytes.data()), bytes.size(), &compressed); | ||
| return std::vector<uint8_t>(compressed.begin(), compressed.end()); | ||
| } | ||
|
|
||
| // Note for future implementations: If compression fails because of invalid or corrupt input, | ||
| // then throw an InvalidInputException. | ||
| throw DBPSUnsupportedException( | ||
| "Unsupported compression codec: " + std::string(to_string(compression))); | ||
| } | ||
|
|
||
| std::vector<uint8_t> Decompress(const std::vector<uint8_t>& bytes, CompressionCodec::type compression) { | ||
| if (compression == CompressionCodec::UNCOMPRESSED) { | ||
| return bytes; | ||
| } | ||
|
|
||
| if (compression == CompressionCodec::SNAPPY) { | ||
| if (bytes.empty()) { | ||
| return bytes; | ||
| } | ||
| std::string decompressed; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wait, string? why is this? It seems odd - specially given that just below (before returning) we're convering it to a byte array.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah! This is because
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. got it, makes sense - string being used as an auto-resize buffer. let's add a quick note to the code. Have we checked is there risk for side effects for using strings? (e.g. UTF-8 encoding doing something 'magic' but undesired with the bytes ?) If this is not an issue, using
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added an inline comment for this. Also added (then removed) a unittest to Compress/Decompress special utf-8 chars. It feels still wrong to merge code with special chars, so won't merge that specific test, but it works. |
||
| bool success = snappy::Uncompress(reinterpret_cast<const char*>(bytes.data()), bytes.size(), &decompressed); | ||
| if (!success) { | ||
| throw InvalidInputException("Failed to decompress data: invalid or corrupt Snappy-compressed input"); | ||
| } | ||
| return std::vector<uint8_t>(decompressed.begin(), decompressed.end()); | ||
| } | ||
|
|
||
| // Note for future implementations: If decompression fails because of invalid or corrupt input, | ||
| // then throw an InvalidInputException. | ||
| throw DBPSUnsupportedException( | ||
| "Unsupported compression codec: " + std::string(to_string(compression))); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,15 +34,7 @@ using namespace dbps::enum_utils; | |
| * @return Compressed bytes, or original bytes if UNCOMPRESSED | ||
| * @throws DBPSUnsupportedException if the compression codec is not supported | ||
| */ | ||
| inline std::vector<uint8_t> Compress(const std::vector<uint8_t>& bytes, CompressionCodec::type compression) { | ||
| if (compression == CompressionCodec::UNCOMPRESSED) { | ||
| return bytes; | ||
| } | ||
| // Note for future implementations: If compression fails because of invalid or corrupt input, | ||
| // then throw an InvalidInputException. | ||
| throw DBPSUnsupportedException( | ||
| "Unsupported compression codec: " + std::string(to_string(compression))); | ||
| } | ||
| std::vector<uint8_t> Compress(const std::vector<uint8_t>& bytes, CompressionCodec::type compression); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no namespace? (I'm OK either way.)
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Added a namespace, thanks for the suggestion. |
||
|
|
||
| /** | ||
| * Decompress bytes using the compression codec. | ||
|
|
@@ -52,13 +44,4 @@ inline std::vector<uint8_t> Compress(const std::vector<uint8_t>& bytes, Compress | |
| * @return Decompressed bytes, or original bytes if UNCOMPRESSED | ||
| * @throws DBPSUnsupportedException if the compression codec is not supported | ||
| */ | ||
| inline std::vector<uint8_t> Decompress(const std::vector<uint8_t>& bytes, CompressionCodec::type compression) { | ||
| if (compression == CompressionCodec::UNCOMPRESSED) { | ||
| return bytes; | ||
| } | ||
| // Note for future implementations: If decompression fails because of invalid or corrupt input, | ||
| // then throw an InvalidInputException. | ||
| throw DBPSUnsupportedException( | ||
| "Unsupported compression codec: " + std::string(to_string(compression))); | ||
| } | ||
|
|
||
| std::vector<uint8_t> Decompress(const std::vector<uint8_t>& bytes, CompressionCodec::type compression); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| // 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 "compression_utils.h" | ||
| #include "exceptions.h" | ||
| #include <vector> | ||
| #include <cstdint> | ||
| #include <gtest/gtest.h> | ||
|
|
||
| using namespace dbps::external; | ||
|
|
||
| TEST(CompressionUtils, Compress_Uncompressed) { | ||
| std::vector<uint8_t> input = {0x01, 0x02, 0x03, 0x04, 0x05}; | ||
| std::vector<uint8_t> result = Compress(input, CompressionCodec::UNCOMPRESSED); | ||
| EXPECT_EQ(input, result); | ||
| } | ||
|
|
||
| TEST(CompressionUtils, Decompress_Uncompressed) { | ||
| std::vector<uint8_t> input = {0x01, 0x02, 0x03, 0x04, 0x05}; | ||
| std::vector<uint8_t> result = Decompress(input, CompressionCodec::UNCOMPRESSED); | ||
| EXPECT_EQ(input, result); | ||
| } | ||
|
|
||
| TEST(CompressionUtils, Compress_Empty) { | ||
| std::vector<uint8_t> input; | ||
| std::vector<uint8_t> result = Compress(input, CompressionCodec::SNAPPY); | ||
| EXPECT_EQ(input, result); | ||
| } | ||
|
|
||
| TEST(CompressionUtils, Decompress_Empty) { | ||
| std::vector<uint8_t> input; | ||
| std::vector<uint8_t> result = Decompress(input, CompressionCodec::SNAPPY); | ||
| EXPECT_EQ(input, result); | ||
| } | ||
|
|
||
| TEST(CompressionUtils, CompressDecompress_Snappy_RoundTrip) { | ||
| std::vector<uint8_t> original = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; | ||
| std::vector<uint8_t> compressed = Compress(original, CompressionCodec::SNAPPY); | ||
| std::vector<uint8_t> decompressed = Decompress(compressed, CompressionCodec::SNAPPY); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this test should also validate than
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Added the verification. |
||
| EXPECT_EQ(original, decompressed); | ||
| } | ||
|
|
||
| TEST(CompressionUtils, Decompress_InvalidData) { | ||
| std::vector<uint8_t> invalid_data = {0xFF, 0xFF, 0xFF, 0xFF}; | ||
| EXPECT_THROW(Decompress(invalid_data, CompressionCodec::SNAPPY), InvalidInputException); | ||
| } | ||
|
|
||
| TEST(CompressionUtils, Compress_UnsupportedCodec) { | ||
| std::vector<uint8_t> input = {0x01, 0x02, 0x03}; | ||
| EXPECT_THROW(Compress(input, CompressionCodec::GZIP), DBPSUnsupportedException); | ||
| } | ||
|
|
||
| TEST(CompressionUtils, Decompress_UnsupportedCodec) { | ||
| std::vector<uint8_t> input = {0x01, 0x02, 0x03}; | ||
| EXPECT_THROW(Decompress(input, CompressionCodec::GZIP), DBPSUnsupportedException); | ||
| } | ||
|
|
||
| TEST(CompressionUtils, CompressDecompress_Snappy_SingleByte) { | ||
| std::vector<uint8_t> original = {0x42}; | ||
| std::vector<uint8_t> compressed = Compress(original, CompressionCodec::SNAPPY); | ||
| std::vector<uint8_t> decompressed = Decompress(compressed, CompressionCodec::SNAPPY); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similar to above. let's at least verify that 'original' was transformed (and that 'original' != 'compressed')
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| EXPECT_EQ(original, decompressed); | ||
| } | ||
|
|
||
| TEST(CompressionUtils, CompressDecompress_Snappy_LargeData) { | ||
| std::vector<uint8_t> original; | ||
| original.resize(10000); | ||
| for (size_t i = 0; i < original.size(); ++i) { | ||
| original[i] = static_cast<uint8_t>(i % 256); | ||
| } | ||
| std::vector<uint8_t> compressed = Compress(original, CompressionCodec::SNAPPY); | ||
| std::vector<uint8_t> decompressed = Decompress(compressed, CompressionCodec::SNAPPY); | ||
| EXPECT_EQ(original, decompressed); | ||
| } | ||
|
|
||
| TEST(CompressionUtils, CompressDecompress_Snappy_RepeatingPattern) { | ||
| std::vector<uint8_t> original; | ||
| original.resize(1000, 0xAA); | ||
| std::vector<uint8_t> compressed = Compress(original, CompressionCodec::SNAPPY); | ||
| std::vector<uint8_t> decompressed = Decompress(compressed, CompressionCodec::SNAPPY); | ||
| EXPECT_EQ(original, decompressed); | ||
| EXPECT_LT(compressed.size(), original.size()); | ||
| } | ||
|
|
||
| TEST(CompressionUtils, CompressDecompress_Snappy_TextData) { | ||
| std::string text = "The quick brown fox jumps over the lazy dog. This is a test string."; | ||
| std::vector<uint8_t> original(text.begin(), text.end()); | ||
| std::vector<uint8_t> compressed = Compress(original, CompressionCodec::SNAPPY); | ||
| std::vector<uint8_t> decompressed = Decompress(compressed, CompressionCodec::SNAPPY); | ||
| EXPECT_EQ(original, decompressed); | ||
| } | ||
|
|
||
| TEST(CompressionUtils, CompressDecompress_Snappy_BinaryData) { | ||
| std::vector<uint8_t> original = {0x00, 0xFF, 0xAA, 0x55, 0x11, 0xEE, 0x99, 0x66}; | ||
| std::vector<uint8_t> compressed = Compress(original, CompressionCodec::SNAPPY); | ||
| std::vector<uint8_t> decompressed = Decompress(compressed, CompressionCodec::SNAPPY); | ||
| EXPECT_EQ(original, decompressed); | ||
| } | ||
|
|
||
| TEST(CompressionUtils, CompressDecompress_Snappy_MultipleRoundTrips) { | ||
| std::vector<uint8_t> original = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; | ||
| std::vector<uint8_t> data = original; | ||
| for (int i = 0; i < 5; ++i) { | ||
| data = Compress(data, CompressionCodec::SNAPPY); | ||
| data = Decompress(data, CompressionCodec::SNAPPY); | ||
| } | ||
| EXPECT_EQ(original, data); | ||
| } | ||
|
|
||
| TEST(CompressionUtils, Compress_Snappy_CompressionRatio) { | ||
| std::vector<uint8_t> original; | ||
| original.resize(5000, 0x42); | ||
| std::vector<uint8_t> compressed = Compress(original, CompressionCodec::SNAPPY); | ||
| EXPECT_LE(compressed.size(), original.size()); | ||
| EXPECT_GT(compressed.size(), 0); | ||
| } | ||
|
|
||
| TEST(CompressionUtils, Decompress_InvalidData_Empty) { | ||
| std::vector<uint8_t> empty_data = {}; | ||
| std::vector<uint8_t> result = Decompress(empty_data, CompressionCodec::SNAPPY); | ||
| EXPECT_EQ(empty_data, result); | ||
| } | ||
|
|
||
| TEST(CompressionUtils, Decompress_InvalidData_TooShort) { | ||
| std::vector<uint8_t> invalid_data = {0x01}; | ||
| EXPECT_THROW(Decompress(invalid_data, CompressionCodec::SNAPPY), InvalidInputException); | ||
| } | ||
|
|
||
| TEST(CompressionUtils, Compress_UnsupportedCodec_AllTypes) { | ||
| std::vector<uint8_t> input = {0x01, 0x02, 0x03}; | ||
| EXPECT_THROW(Compress(input, CompressionCodec::BROTLI), DBPSUnsupportedException); | ||
| EXPECT_THROW(Compress(input, CompressionCodec::ZSTD), DBPSUnsupportedException); | ||
| EXPECT_THROW(Compress(input, CompressionCodec::LZ4), DBPSUnsupportedException); | ||
| } | ||
|
|
||
| TEST(CompressionUtils, Decompress_UnsupportedCodec_AllTypes) { | ||
| std::vector<uint8_t> input = {0x01, 0x02, 0x03}; | ||
| EXPECT_THROW(Decompress(input, CompressionCodec::BROTLI), DBPSUnsupportedException); | ||
| EXPECT_THROW(Decompress(input, CompressionCodec::ZSTD), DBPSUnsupportedException); | ||
| EXPECT_THROW(Decompress(input, CompressionCodec::LZ4), DBPSUnsupportedException); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems familiar :)