Skip to content

Commit de3c1fa

Browse files
authored
Merge pull request #184 from protegrity/av_sequncer_decryption_017
Refactor to move Parquet and AttributeMap logic out of encryption_sequencer
2 parents b53b5b9 + a8426c3 commit de3c1fa

12 files changed

Lines changed: 343 additions & 298 deletions

CMakeLists.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ target_link_libraries(dbps_common_lib PUBLIC tcb_span)
139139
add_library(dbps_server_lib STATIC
140140
src/server/encryption_sequencer.cpp
141141
src/server/auth_utils.cpp
142-
src/server/decoding_utils.cpp
142+
src/server/parquet_utils.cpp
143143
src/server/compression_utils.cpp
144144
src/server/encryptors/basic_encryptor.cpp
145145
)
@@ -278,14 +278,14 @@ if(BUILD_TESTS)
278278
gtest_main
279279
)
280280

281-
# Decoding utils tests
282-
add_executable(decoding_utils_test src/server/decoding_utils_test.cpp)
283-
target_link_libraries(decoding_utils_test
281+
# Parquet utils tests
282+
add_executable(parquet_utils_test src/server/parquet_utils_test.cpp)
283+
target_link_libraries(parquet_utils_test
284284
dbps_server_lib
285285
dbps_common_lib
286286
gtest_main
287287
)
288-
target_include_directories(decoding_utils_test PRIVATE src/server)
288+
target_include_directories(parquet_utils_test PRIVATE src/server)
289289

290290
# Bytes utils tests
291291
add_executable(bytes_utils_test src/common/bytes_utils_test.cpp)
@@ -484,7 +484,7 @@ if(BUILD_TESTS)
484484
value_encryption_utils_test
485485
typed_list_values_test
486486
encryption_sequencer_test
487-
decoding_utils_test
487+
parquet_utils_test
488488
bytes_utils_test
489489
compression_utils_test
490490
basic_encryptor_test
@@ -505,7 +505,7 @@ if(BUILD_TESTS)
505505
gtest_discover_tests(value_encryption_utils_test)
506506
gtest_discover_tests(typed_list_values_test)
507507
gtest_discover_tests(encryption_sequencer_test)
508-
gtest_discover_tests(decoding_utils_test)
508+
gtest_discover_tests(parquet_utils_test)
509509
gtest_discover_tests(bytes_utils_test)
510510
gtest_discover_tests(compression_utils_test)
511511
gtest_discover_tests(basic_encryptor_test)

src/common/bytes_utils.h

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@
2121
#include <cstdint>
2222
#include <limits>
2323
#include <cstring>
24+
#include <map>
25+
#include <string>
26+
#include <variant>
27+
#include <cassert>
2428
#include "exceptions.h"
2529

26-
// Little-endian helpers reused across modules
30+
// Utility functions for little-endian number reading and writing.
31+
2732
inline void append_u32_le(std::vector<uint8_t>& out, uint32_t v) {
2833
out.push_back(static_cast<uint8_t>(v & 0xFF));
2934
out.push_back(static_cast<uint8_t>((v >> 8) & 0xFF));
@@ -69,6 +74,8 @@ inline uint32_t read_u32_le(const std::vector<uint8_t>& in, size_t offset) {
6974
(static_cast<uint32_t>(in[offset + 3]) << 24);
7075
}
7176

77+
// Utility functions for splitting and joining byte vectors.
78+
7279
struct SplitBytesPair {
7380
std::vector<uint8_t> leading;
7481
std::vector<uint8_t> trailing;
@@ -171,3 +178,61 @@ inline SplitBytesPair SplitWithLengthPrefix(const std::vector<uint8_t>& bytes) {
171178

172179
return result;
173180
}
181+
182+
// Utility functions for creating an AttributesMap
183+
184+
// Common alias for converted encoding attributes used across modules.
185+
// Numeric values are captured as int32_t.
186+
using AttributesMap = std::map<std::string, std::variant<int32_t, bool, std::string>>;
187+
188+
inline const std::string& GetRequiredAttribute(
189+
const std::map<std::string, std::string>& attributes,
190+
const std::string& key) {
191+
auto it = attributes.find(key);
192+
if (it == attributes.end()) {
193+
throw InvalidInputException("Required encoding attribute [" + key + "] is missing");
194+
}
195+
return it->second;
196+
}
197+
198+
inline int32_t AddIntAttribute(
199+
AttributesMap& out,
200+
const std::map<std::string, std::string>& attributes,
201+
const std::string& key) {
202+
const std::string& value = GetRequiredAttribute(attributes, key);
203+
try {
204+
int32_t value_int = static_cast<int32_t>(std::stol(value));
205+
assert(value_int >= 0);
206+
out[key] = value_int;
207+
return value_int;
208+
} catch (const std::exception& e) {
209+
throw InvalidInputException(
210+
"Failed to convert [" + key + "] with value [" + value + "] to int: " + e.what());
211+
}
212+
}
213+
214+
inline bool AddBoolAttribute(
215+
AttributesMap& out,
216+
const std::map<std::string, std::string>& attributes,
217+
const std::string& key) {
218+
const std::string& value = GetRequiredAttribute(attributes, key);
219+
if (value == "true") {
220+
out[key] = true;
221+
return true;
222+
} else if (value == "false") {
223+
out[key] = false;
224+
return false;
225+
} else {
226+
throw InvalidInputException(
227+
"Failed to convert [" + key + "] with value [" + value + "] to bool");
228+
}
229+
}
230+
231+
inline std::string AddStringAttribute(
232+
AttributesMap& out,
233+
const std::map<std::string, std::string>& attributes,
234+
const std::string& key) {
235+
const std::string& value = GetRequiredAttribute(attributes, key);
236+
out[key] = value;
237+
return value;
238+
}

src/common/bytes_utils_test.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "exceptions.h"
2020

2121
#include <vector>
22+
#include <variant>
2223
#include <gtest/gtest.h>
2324

2425
TEST(BytesUtils, Split_Normal) {
@@ -199,4 +200,43 @@ TEST(BytesUtils, JoinWithLengthPrefixAndSplit_RoundTrip) {
199200

200201
EXPECT_EQ(leading, parsed.leading);
201202
EXPECT_EQ(trailing, parsed.trailing);
203+
}
204+
205+
TEST(BytesUtils, AttributesMap_AddString) {
206+
std::map<std::string, std::string> attrs{{"page_type", "DATA_PAGE_V1"}};
207+
AttributesMap out;
208+
209+
auto v = AddStringAttribute(out, attrs, "page_type");
210+
EXPECT_EQ("DATA_PAGE_V1", v);
211+
EXPECT_EQ("DATA_PAGE_V1", std::get<std::string>(out.at("page_type")));
212+
213+
EXPECT_THROW(AddStringAttribute(out, attrs, "missing_key"), InvalidInputException);
214+
}
215+
216+
TEST(BytesUtils, AttributesMap_AddInt) {
217+
std::map<std::string, std::string> attrs{{"data_page_num_values", "10"}};
218+
AttributesMap out;
219+
220+
auto v = AddIntAttribute(out, attrs, "data_page_num_values");
221+
EXPECT_EQ(10, v);
222+
EXPECT_EQ(10, std::get<int32_t>(out.at("data_page_num_values")));
223+
224+
std::map<std::string, std::string> bad_attrs{{"data_page_num_values", "abc"}};
225+
EXPECT_THROW(AddIntAttribute(out, bad_attrs, "data_page_num_values"), InvalidInputException);
226+
}
227+
228+
TEST(BytesUtils, AttributesMap_AddBool) {
229+
std::map<std::string, std::string> attrs_true{{"page_v2_is_compressed", "true"}};
230+
AttributesMap out;
231+
auto v_true = AddBoolAttribute(out, attrs_true, "page_v2_is_compressed");
232+
EXPECT_TRUE(v_true);
233+
EXPECT_TRUE(std::get<bool>(out.at("page_v2_is_compressed")));
234+
235+
std::map<std::string, std::string> attrs_false{{"page_v2_is_compressed", "false"}};
236+
auto v_false = AddBoolAttribute(out, attrs_false, "page_v2_is_compressed");
237+
EXPECT_FALSE(v_false);
238+
EXPECT_FALSE(std::get<bool>(out.at("page_v2_is_compressed")));
239+
240+
std::map<std::string, std::string> bad_attrs{{"page_v2_is_compressed", "maybe"}};
241+
EXPECT_THROW(AddBoolAttribute(out, bad_attrs, "page_v2_is_compressed"), InvalidInputException);
202242
}

src/common/value_encryption_utils_test.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include <stdexcept>
2222
#include <vector>
2323
#include <cstdint>
24-
#include <variant>
2524
#include <string>
2625
#include <array>
2726
#include <limits>

0 commit comments

Comments
 (0)