Commit 6cede695 authored by yiwenshao's avatar yiwenshao

new version of load and store

parent 5ce6414e
#pragma once #pragma once
class metadata_file{
/*class metadata_file{
string db,table; string db,table;
int num_of_fields; int num_of_fields;
vector<string> field_types; vector<string> field_types;
vector<int> field_lengths; vector<int> field_lengths;
vector<string> field_names; vector<string> field_names;
vector<int> choosen_onions;/*choosen onion for decryption, only one per field.The value could be 0,1,2,3...*/ vector<int> choosen_onions;
public: public:
void set_db(std::string idb){db=idb;} void set_db(std::string idb){db=idb;}
std::string get_db(){return db;} std::string get_db(){return db;}
...@@ -177,18 +178,21 @@ void metadata_file::show(){ ...@@ -177,18 +178,21 @@ void metadata_file::show(){
} }
cout<<endl; cout<<endl;
} }
*/
/*******************************************************************************************************************/ /*******************************************************************************************************************/
class metadata_files{ class metadata_files{
public:
string db,table; string db,table;
/*selected fields*/ /*selected fields*/
vector<vector<int>> selected_field_types; vector<vector<int>> selected_field_types;
vector<vector<int>> selected_field_lengths; vector<vector<int>> selected_field_lengths;
vector<vector<string>> selected_field_names; vector<vector<string>> selected_field_names;
vector<string> has_salt; vector<string> has_salt;
vector<int> dec_onion_index;/*should be 0,1,2,3...*/ vector<int> dec_onion_index;/*should be 0,1,2,3...*/
std::string serialize_vec_int(std::string s,vector<int> vec_int){ std::string serialize_vec_int(std::string s,vector<int> vec_int){
s+=":"; s+=":";
for(auto item:vec_int){ for(auto item:vec_int){
...@@ -266,7 +270,7 @@ public: ...@@ -266,7 +270,7 @@ public:
void metadata_files::serialize(){ void metadata_files::serialize(){
FILE * localmeta = NULL; FILE * localmeta = NULL;
string prefix = string("adata/")+db+"/"+table; string prefix = string("data/")+db+"/"+table;
make_path(prefix); make_path(prefix);
localmeta = fopen((prefix+"/metadata.data").c_str(),"w"); localmeta = fopen((prefix+"/metadata.data").c_str(),"w");
...@@ -304,7 +308,7 @@ void metadata_files::serialize(){ ...@@ -304,7 +308,7 @@ void metadata_files::serialize(){
} }
void metadata_files::deserialize(std::string filename){ void metadata_files::deserialize(std::string filename){
filename = string("adata/")+db+"/"+table+"/"+filename; filename = string("data/")+db+"/"+table+"/"+filename;
std::ifstream infile(filename); std::ifstream infile(filename);
string line; string line;
while(std::getline(infile,line)){ while(std::getline(infile,line)){
......
...@@ -18,8 +18,8 @@ std::shared_ptr<ReturnMeta> getReturnMeta(std::vector<FieldMeta*> fms, std::vect ...@@ -18,8 +18,8 @@ std::shared_ptr<ReturnMeta> getReturnMeta(std::vector<FieldMeta*> fms, std::vect
return myReturnMeta; return myReturnMeta;
} }
static metadata_file load_meta(string db="tdb", string table="student", string filename="metadata.data"){ static metadata_files load_meta(string db="tdb", string table="student", string filename="metadata.data"){
metadata_file mf; metadata_files mf;
mf.set_db(db); mf.set_db(db);
mf.set_table(table); mf.set_table(table);
mf.deserialize(filename); mf.deserialize(filename);
...@@ -44,23 +44,38 @@ static void load_string(string filename, vector<string> &res,unsigned long lengt ...@@ -44,23 +44,38 @@ static void load_string(string filename, vector<string> &res,unsigned long lengt
close(fd); close(fd);
} }
static vector<vector<string>> load_table_fields(metadata_file & input) { template<class T>
vector<T> flat_vec(vector<vector<T>> &input){
vector<T> res;
for(auto item:input){
for(auto i:item){
res.push_back(i);
}
}
return res;
}
static vector<vector<string>> load_table_fields(metadata_files & input) {
string db = input.get_db(); string db = input.get_db();
string table = input.get_table(); string table = input.get_table();
vector<vector<string>> res; vector<vector<string>> res;
string prefix = string("data/")+db+"/"+table+"/"; string prefix = string("data/")+db+"/"+table+"/";
vector<string> datafiles; vector<string> datafiles;
for(auto item:input.get_field_names()){ auto field_names = flat_vec(input.selected_field_names);
auto field_types = flat_vec(input.selected_field_types);
auto field_lengths = flat_vec(input.selected_field_lengths);
for(auto item:field_names){
datafiles.push_back(prefix+item); datafiles.push_back(prefix+item);
} }
for(unsigned int i=0u;i<input.get_field_names().size();i++){ for(unsigned int i=0u;i<field_names.size();i++){
vector<string> column; vector<string> column;
if(IS_NUM(std::stoi(input.get_field_types()[i]))){ if(IS_NUM(field_types[i])){
load_num(datafiles[i],column); load_num(datafiles[i],column);
}else{ }else{
load_string(datafiles[i],column,input.get_field_lengths()[i]); load_string(datafiles[i],column,field_lengths[i]);
} }
for(unsigned int j=0u; j<column.size(); j++){ for(unsigned int j=0u; j<column.size(); j++){
if(j>=res.size()){ if(j>=res.size()){
...@@ -86,12 +101,12 @@ static ResType load_files(std::string db="tdb", std::string table="student"){ ...@@ -86,12 +101,12 @@ static ResType load_files(std::string db="tdb", std::string table="student"){
if(types.size()==1){ if(types.size()==1){
//to be //to be
} }
metadata_file res_meta = load_meta(db,table); metadata_files res_meta = load_meta(db,table);
for(unsigned int i=0;i<res_meta.get_choosen_onions().size();i++){
//choosen onion for each field
res[i].choosenOnions.push_back(res_meta.get_choosen_onions()[i]);
}
// for(unsigned int i=0;i<res_meta.dec_onion_index.size();i++){
//choosen onion for each field
// res[i].choosenOnions.push_back(res_meta.get_choosen_onions()[i]);
// }
std::shared_ptr<ReturnMeta> rm = getReturnMeta(fms,res); std::shared_ptr<ReturnMeta> rm = getReturnMeta(fms,res);
//why do we need this?? //why do we need this??
...@@ -103,10 +118,14 @@ static ResType load_files(std::string db="tdb", std::string table="student"){ ...@@ -103,10 +118,14 @@ static ResType load_files(std::string db="tdb", std::string table="student"){
//load fields in the stored file //load fields in the stored file
vector<vector<string>> res_field = load_table_fields(res_meta); vector<vector<string>> res_field = load_table_fields(res_meta);
resraw2.rowValues = res_field; resraw2.rowValues = res_field;
resraw2.fieldNames = res_meta.get_field_names(); auto field_names = flat_vec(res_meta.selected_field_names);
resraw2.choosen_onions = res_meta.get_choosen_onions(); auto field_types = flat_vec(res_meta.selected_field_types);
for(unsigned int i=0;i<res_meta.get_field_types().size();++i) { auto field_lengths = flat_vec(res_meta.selected_field_lengths);
resraw2.fieldTypes.push_back(static_cast<enum_field_types>(std::stoi(res_meta.get_field_types()[i])));
resraw2.fieldNames = field_names;
// resraw2.choosen_onions = ;
for(unsigned int i=0;i<field_types.size();++i) {
resraw2.fieldTypes.push_back(static_cast<enum_field_types>(field_types[i]));
} }
ResType rawtorestype = MygetResTypeFromLuaTable(false, &resraw2); ResType rawtorestype = MygetResTypeFromLuaTable(false, &resraw2);
auto finalresults = decryptResults(rawtorestype,*rm); auto finalresults = decryptResults(rawtorestype,*rm);
......
...@@ -259,6 +259,7 @@ static std::vector<transField> getTransField(std::vector<FieldMeta *> pfms){ ...@@ -259,6 +259,7 @@ static std::vector<transField> getTransField(std::vector<FieldMeta *> pfms){
tf.numOfOnions++; tf.numOfOnions++;
tf.fields.push_back((ompair.second)->getAnonOnionName()); tf.fields.push_back((ompair.second)->getAnonOnionName());
tf.onions.push_back(ompair.first->getValue()); tf.onions.push_back(ompair.first->getValue());
tf.originalOm.push_back(ompair.second);
} }
if(pfm->getHasSalt()){ if(pfm->getHasSalt()){
tf.hasSalt=true; tf.hasSalt=true;
......
#include "debug/store.hh" #include "debug/store.hh"
#include "debug/common.hh" #include "debug/common.hh"
static void write_meta(rawMySQLReturnValue& resraw,std::vector<transField> &res,string db,string table){ static void write_meta(rawMySQLReturnValue& resraw,std::vector<transField> &res,string db,string table){
metadata_files mf; metadata_files mf;
mf.set_db_table(db,table); mf.set_db_table(db,table);
...@@ -32,15 +34,6 @@ static void write_meta(rawMySQLReturnValue& resraw,std::vector<transField> &res, ...@@ -32,15 +34,6 @@ static void write_meta(rawMySQLReturnValue& resraw,std::vector<transField> &res,
mf.set_selected_field_names(selected_field_names); mf.set_selected_field_names(selected_field_names);
mf.set_dec_onion_index(dec_onion_index); mf.set_dec_onion_index(dec_onion_index);
mf.set_has_salt(has_salt); mf.set_has_salt(has_salt);
// std::vector<std::string> temp;
// for(auto item:resraw.fieldTypes){
// temp.push_back(std::to_string(static_cast<int>(item)));
// }
// mf.set_field_types(temp);
// mf.set_field_lengths(resraw.lengths);
// mf.set_field_names(resraw.fieldNames);
// mf.set_choosen_onions(resraw.choosen_onions);
// mf.serilize();
mf.serialize(); mf.serialize();
} }
static void write_row_data(rawMySQLReturnValue& resraw,string db, string table){ static void write_row_data(rawMySQLReturnValue& resraw,string db, string table){
...@@ -77,13 +70,13 @@ static void store(std::string db, std::string table){ ...@@ -77,13 +70,13 @@ static void store(std::string db, std::string table){
std::vector<FieldMeta*> fms = getFieldMeta(*schema,db,table); std::vector<FieldMeta*> fms = getFieldMeta(*schema,db,table);
//transform the field so that selected onions can be used //transform the field so that selected onions can be used
std::vector<transField> res = getTransField(fms); std::vector<transField> res = getTransField(fms);
//generate the backup query and then fetch the tuples
std::string backup_query = getTestQuery(*schema,res,db,table);
rawMySQLReturnValue resraw = executeAndGetResultRemote(globalConn,backup_query);
for(auto &item:res){ for(auto &item:res){
(void)item; (void)item;
resraw.choosen_onions.push_back(0); item.choosenOnions.push_back(0);
} }
//generate the backup query and then fetch the tuples
std::string backup_query = getTestQuery(*schema,res,db,table);
rawMySQLReturnValue resraw = executeAndGetResultRemote(globalConn,backup_query);
//write the tuples into files //write the tuples into files
write_raw_data_to_files(resraw,res,db,table); write_raw_data_to_files(resraw,res,db,table);
} }
......
...@@ -53,6 +53,20 @@ private: ...@@ -53,6 +53,20 @@ private:
std::unique_ptr<QueryRewrite> qr; std::unique_ptr<QueryRewrite> qr;
}; };
//representation of one field.
struct transField{
bool hasSalt;
FieldMeta *originalFm;
vector<int> choosenOnions;
//used to construct return meta
int onionIndex = 0;
int numOfOnions=0;
//onions
std::vector<std::string> fields;
std::vector<onion> onions;
};
static std::string embeddedDir="/t/cryt/shadow"; static std::string embeddedDir="/t/cryt/shadow";
//global map, for each client, we have one WrapperState which contains ProxyState. //global map, for each client, we have one WrapperState which contains ProxyState.
static std::map<std::string, WrapperState*> clients; static std::map<std::string, WrapperState*> clients;
...@@ -103,19 +117,7 @@ void rawMySQLReturnValue::show(){ ...@@ -103,19 +117,7 @@ void rawMySQLReturnValue::show(){
cout<<endl; cout<<endl;
} }
//representation of one field.
struct transField{
bool hasSalt;
FieldMeta *originalFm;
vector<int> choosenOnions;
//used to construct return meta
int onionIndex = 0;
int numOfOnions=0;
//onions
std::vector<std::string> fields;
std::vector<onion> onions;
// std::vector<OnionMeta*>originalOm;
};
static void init(){ static void init(){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment