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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ target_link_libraries(dbps_common_lib PUBLIC tcb_span)
add_library(dbps_server_lib STATIC
src/server/encryption_sequencer.cpp
src/server/auth_utils.cpp
src/server/decoding_utils.cpp
src/server/parquet_utils.cpp
src/server/compression_utils.cpp
src/server/encryptors/basic_encryptor.cpp
)
Expand Down Expand Up @@ -278,14 +278,14 @@ if(BUILD_TESTS)
gtest_main
)

# Decoding utils tests
add_executable(decoding_utils_test src/server/decoding_utils_test.cpp)
target_link_libraries(decoding_utils_test
# Parquet utils tests
add_executable(parquet_utils_test src/server/parquet_utils_test.cpp)
target_link_libraries(parquet_utils_test
dbps_server_lib
dbps_common_lib
gtest_main
)
target_include_directories(decoding_utils_test PRIVATE src/server)
target_include_directories(parquet_utils_test PRIVATE src/server)

# Bytes utils tests
add_executable(bytes_utils_test src/common/bytes_utils_test.cpp)
Expand Down Expand Up @@ -484,7 +484,7 @@ if(BUILD_TESTS)
value_encryption_utils_test
typed_list_values_test
encryption_sequencer_test
decoding_utils_test
parquet_utils_test
bytes_utils_test
compression_utils_test
basic_encryptor_test
Expand All @@ -505,7 +505,7 @@ if(BUILD_TESTS)
gtest_discover_tests(value_encryption_utils_test)
gtest_discover_tests(typed_list_values_test)
gtest_discover_tests(encryption_sequencer_test)
gtest_discover_tests(decoding_utils_test)
gtest_discover_tests(parquet_utils_test)
gtest_discover_tests(bytes_utils_test)
gtest_discover_tests(compression_utils_test)
gtest_discover_tests(basic_encryptor_test)
Expand Down
67 changes: 66 additions & 1 deletion src/common/bytes_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@
#include <cstdint>
#include <limits>
#include <cstring>
#include <map>
#include <string>
#include <variant>
#include <cassert>
#include "exceptions.h"

// Little-endian helpers reused across modules
// Utility functions for little-endian number reading and writing.

inline void append_u32_le(std::vector<uint8_t>& out, uint32_t v) {
out.push_back(static_cast<uint8_t>(v & 0xFF));
out.push_back(static_cast<uint8_t>((v >> 8) & 0xFF));
Expand Down Expand Up @@ -69,6 +74,8 @@ inline uint32_t read_u32_le(const std::vector<uint8_t>& in, size_t offset) {
(static_cast<uint32_t>(in[offset + 3]) << 24);
}

// Utility functions for splitting and joining byte vectors.

struct SplitBytesPair {
std::vector<uint8_t> leading;
std::vector<uint8_t> trailing;
Expand Down Expand Up @@ -171,3 +178,61 @@ inline SplitBytesPair SplitWithLengthPrefix(const std::vector<uint8_t>& bytes) {

return result;
}

// Utility functions for creating an AttributesMap

// Common alias for converted encoding attributes used across modules.
// Numeric values are captured as int32_t.
using AttributesMap = std::map<std::string, std::variant<int32_t, bool, std::string>>;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why only int32_t (as oppossed to other int types also?) - let's add a comment.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Added comment.


inline const std::string& GetRequiredAttribute(
const std::map<std::string, std::string>& attributes,
const std::string& key) {
auto it = attributes.find(key);
if (it == attributes.end()) {
throw InvalidInputException("Required encoding attribute [" + key + "] is missing");
}
return it->second;
}

inline int32_t AddIntAttribute(
AttributesMap& out,
const std::map<std::string, std::string>& attributes,
const std::string& key) {
const std::string& value = GetRequiredAttribute(attributes, key);
try {
int32_t value_int = static_cast<int32_t>(std::stol(value));
assert(value_int >= 0);
out[key] = value_int;
return value_int;
} catch (const std::exception& e) {
throw InvalidInputException(
"Failed to convert [" + key + "] with value [" + value + "] to int: " + e.what());
}
}

inline bool AddBoolAttribute(
AttributesMap& out,
const std::map<std::string, std::string>& attributes,
const std::string& key) {
const std::string& value = GetRequiredAttribute(attributes, key);
if (value == "true") {
out[key] = true;
return true;
} else if (value == "false") {
out[key] = false;
return false;
} else {
throw InvalidInputException(
"Failed to convert [" + key + "] with value [" + value + "] to bool");
}
}

inline std::string AddStringAttribute(
AttributesMap& out,
const std::map<std::string, std::string>& attributes,
const std::string& key) {
const std::string& value = GetRequiredAttribute(attributes, key);
out[key] = value;
return value;
}
40 changes: 40 additions & 0 deletions src/common/bytes_utils_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "exceptions.h"

#include <vector>
#include <variant>
#include <gtest/gtest.h>

TEST(BytesUtils, Split_Normal) {
Expand Down Expand Up @@ -199,4 +200,43 @@ TEST(BytesUtils, JoinWithLengthPrefixAndSplit_RoundTrip) {

EXPECT_EQ(leading, parsed.leading);
EXPECT_EQ(trailing, parsed.trailing);
}

TEST(BytesUtils, AttributesMap_AddString) {
std::map<std::string, std::string> attrs{{"page_type", "DATA_PAGE_V1"}};
AttributesMap out;

auto v = AddStringAttribute(out, attrs, "page_type");
EXPECT_EQ("DATA_PAGE_V1", v);
EXPECT_EQ("DATA_PAGE_V1", std::get<std::string>(out.at("page_type")));

EXPECT_THROW(AddStringAttribute(out, attrs, "missing_key"), InvalidInputException);
}

TEST(BytesUtils, AttributesMap_AddInt) {
std::map<std::string, std::string> attrs{{"data_page_num_values", "10"}};
AttributesMap out;

auto v = AddIntAttribute(out, attrs, "data_page_num_values");
EXPECT_EQ(10, v);
EXPECT_EQ(10, std::get<int32_t>(out.at("data_page_num_values")));

std::map<std::string, std::string> bad_attrs{{"data_page_num_values", "abc"}};
EXPECT_THROW(AddIntAttribute(out, bad_attrs, "data_page_num_values"), InvalidInputException);
}

TEST(BytesUtils, AttributesMap_AddBool) {
std::map<std::string, std::string> attrs_true{{"page_v2_is_compressed", "true"}};
AttributesMap out;
auto v_true = AddBoolAttribute(out, attrs_true, "page_v2_is_compressed");
EXPECT_TRUE(v_true);
EXPECT_TRUE(std::get<bool>(out.at("page_v2_is_compressed")));

std::map<std::string, std::string> attrs_false{{"page_v2_is_compressed", "false"}};
auto v_false = AddBoolAttribute(out, attrs_false, "page_v2_is_compressed");
EXPECT_FALSE(v_false);
EXPECT_FALSE(std::get<bool>(out.at("page_v2_is_compressed")));

std::map<std::string, std::string> bad_attrs{{"page_v2_is_compressed", "maybe"}};
EXPECT_THROW(AddBoolAttribute(out, bad_attrs, "page_v2_is_compressed"), InvalidInputException);
}
1 change: 0 additions & 1 deletion src/common/value_encryption_utils_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <stdexcept>
#include <vector>
#include <cstdint>
#include <variant>
#include <string>
#include <array>
#include <limits>
Expand Down
Loading