aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMihaela Ion <mion@google.com>2021-07-21 19:00:40 +0200
committerMihaela Ion <mion@google.com>2021-07-21 19:00:40 +0200
commit247802334ab523f9dc611fed872833f43d0c9f5a (patch)
treeaab20963d6dbb9beab817a761af51be50699288d
parent11cfedad51513688c421e86a77fe0e57399340f3 (diff)
downloadprivate-join-and-compute-247802334ab523f9dc611fed872833f43d0c9f5a.tar.gz
Adds a helper function for writing multiple protos to file
-rw-r--r--util/proto_util.h14
-rw-r--r--util/proto_util_test.cc4
2 files changed, 17 insertions, 1 deletions
diff --git a/util/proto_util.h b/util/proto_util.h
index 5c79a5f..5749863 100644
--- a/util/proto_util.h
+++ b/util/proto_util.h
@@ -40,6 +40,9 @@ class ProtoUtils {
static Status WriteProtoToFile(const google::protobuf::MessageLite& record,
absl::string_view filename);
+ template <typename ProtoType>
+ static Status WriteRecordsToFile(absl::string_view file,
+ const std::vector<ProtoType>& records);
};
template <typename ProtoType>
@@ -74,6 +77,17 @@ inline Status ProtoUtils::WriteProtoToFile(
RETURN_IF_ERROR(writer->Write(ProtoUtils::ToString(record)));
return writer->Close();
}
+
+template <typename ProtoType>
+inline Status ProtoUtils::WriteRecordsToFile(
+ absl::string_view file, const std::vector<ProtoType>& records) {
+ std::unique_ptr<RecordWriter> writer(RecordWriter::Get());
+ RETURN_IF_ERROR(writer->Open(file));
+ for (const auto& record : records) {
+ RETURN_IF_ERROR(writer->Write(ProtoUtils::ToString(record)));
+ }
+ return writer->Close();
+}
} // namespace private_join_and_compute
#endif // INTERNAL_UTIL_PROTO_UTIL_H_
diff --git a/util/proto_util_test.cc b/util/proto_util_test.cc
index 43626d3..2234357 100644
--- a/util/proto_util_test.cc
+++ b/util/proto_util_test.cc
@@ -18,6 +18,7 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
+#include <filesystem>
#include <string>
#include "util/file_test.pb.h"
@@ -39,7 +40,8 @@ TEST(ProtoUtilsTest, ConvertsToAndFrom) {
}
TEST(ProtoUtilsTest, ReadWriteToFile) {
- std::string filename = (::testing::TempDir() + "proto_file");
+ std::filesystem::path temp_dir(::testing::TempDir());
+ std::string filename = (temp_dir / "proto_file").string();
TestProto expected_test_proto;
expected_test_proto.set_record("data");