1414#include < fbpcf/mpc_std_lib/oram/encoder/IOramEncoder.h>
1515#include < fbpcf/mpc_std_lib/oram/encoder/OramEncoder.h>
1616#include < algorithm>
17+ #include < cstdint>
18+ #include < iterator>
1719#include < set>
1820#include < stdexcept>
21+ #include < string>
1922#include " fbpcs/emp_games/common/Csv.h"
2023#include " folly/String.h"
2124
2225namespace pc_translator {
2326
24- std::string PCTranslator::encode (const std::string& inputDataset ) {
27+ std::string PCTranslator::encode (const std::string& inputDatasetPath ) {
2528 auto validInstructionSetNames =
2629 PCTranslator::retrieveInstructionSetNamesForRun (pcsFeatures_);
2730 auto pcInstructionSets =
2831 PCTranslator::retrieveInstructionSets (validInstructionSetNames);
2932 if (pcInstructionSets.empty ()) {
3033 // No instruction set found. return the input dataset path.
31- return inputDataset ;
34+ return inputDatasetPath ;
3235 }
3336 return PCTranslator::transformDataset (
34- inputDataset , pcInstructionSets.front ());
37+ inputDatasetPath , pcInstructionSets.front ());
3538}
3639
3740std::string PCTranslator::decode (
@@ -79,30 +82,43 @@ std::vector<std::string> PCTranslator::retrieveInstructionSetNamesForRun(
7982}
8083
8184std::string PCTranslator::transformDataset (
82- const std::string& inputData ,
85+ const std::string& inputDatasetPath ,
8386 std::shared_ptr<pc_translator::PCInstructionSet> pcInstructionSet) {
8487 // Parse the input CSV
8588 auto lineNo = 0 ;
8689 std::vector<std::vector<uint32_t >> inputColums;
90+ std::vector<std::string> outputHeader;
91+ std::vector<std::vector<std::string>> outputContent;
8792 private_measurement::csv::readCsv (
88- inputData ,
93+ inputDatasetPath ,
8994 [&](const std::vector<std::string>& header,
9095 const std::vector<std::string>& parts) {
9196 std::vector<uint32_t > inputColumnPerRow;
97+ std::string column;
98+ std::uint32_t value;
99+ bool found = false ;
100+ std::vector<std::string> outputContentPerRow;
92101 for (std::vector<std::string>::size_type i = 0 ; i < header.size ();
93102 ++i) {
94- auto & column = header[i];
95- auto value = std::atoi (parts[i].c_str ());
96- auto iter = std::find (
97- pcInstructionSet->getGroupByIds ().begin (),
98- pcInstructionSet->getGroupByIds ().end (),
99- column);
100- if (iter != pcInstructionSet->getGroupByIds ().end ()) {
103+ column = header[i];
104+ value = std::atoi (parts[i].c_str ());
105+ found =
106+ (std::find (
107+ pcInstructionSet->getGroupByIds ().begin (),
108+ pcInstructionSet->getGroupByIds ().end (),
109+ column) != pcInstructionSet->getGroupByIds ().end ());
110+ if (found) {
101111 inputColumnPerRow.push_back (value);
112+ } else {
113+ if (lineNo == 0 ) {
114+ outputHeader.push_back (header[i]);
115+ }
116+ outputContentPerRow.push_back (parts[i]);
102117 }
103118 }
104119
105120 inputColums.push_back (inputColumnPerRow);
121+ outputContent.push_back (outputContentPerRow);
106122 lineNo++;
107123 });
108124
@@ -114,9 +130,34 @@ std::string PCTranslator::transformDataset(
114130
115131 auto encodedIndexes = encoder->generateORAMIndexes (inputColums);
116132
117- // TODO : Append the enodedIndexes at the end of publisher output and return
118- // output path.
119- return " " ;
133+ auto dir = inputDatasetPath.substr (0 , inputDatasetPath.rfind (" /" ) + 1 );
134+ auto output_dataset_path = dir + " transformed_publisher_input.csv" ;
135+
136+ PCTranslator::putOutputData (
137+ output_dataset_path, outputHeader, outputContent, encodedIndexes);
138+ return output_dataset_path;
139+ }
140+
141+ void PCTranslator::putOutputData (
142+ const std::string& output_dataset_path,
143+ std::vector<std::string>& outputHeader,
144+ std::vector<std::vector<std::string>>& outputContent,
145+ const std::vector<uint32_t >& encodedIndexes) {
146+ outputHeader.push_back (" breakdown_id" );
147+
148+ if (outputContent.size () != encodedIndexes.size ()) {
149+ throw std::runtime_error (
150+ " Encoded index vector size should match the input vector size." );
151+ }
152+
153+ for (std::vector<std::string>::size_type i = 0 ; i < encodedIndexes.size ();
154+ ++i) {
155+ auto indexVec = std::to_string (encodedIndexes[i]);
156+ outputContent[i].push_back (indexVec);
157+ }
158+
159+ private_measurement::csv::writeCsv (
160+ output_dataset_path, outputHeader, outputContent);
120161}
121162
122163std::shared_ptr<PCInstructionSet> PCTranslator::parseInstructionSet (
0 commit comments