Commit 98606d82 authored by yiwenshao's avatar yiwenshao

replace FieldMeta_Wrapper with FieldMetaTrans in debug/store.cc

parent 5c19f0c7
#pragma once
class metadata_files{
public:
string db,table;
/*selected fields*/
vector<vector<int>> selected_field_types;
vector<vector<int>> selected_field_lengths;
vector<vector<string>> selected_field_names;
vector<string> has_salt;
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_str(std::string s,vector<string> vec_str);
vector<string> string_to_vec_str(string line);
vector<int> string_to_vec_int(string line);
static bool make_path(std::string directory);
public:
void set_db(std::string idb){db=idb;}
std::string get_db(){return db;}
void set_table(std::string itable){table=itable;}
std::string get_table(){return table;}
void set_db_table(std::string idb,std::string itable){db=idb;table=itable;}
void set_selected_field_types(vector<vector<int>> input){selected_field_types = input;}
vector<vector<int>> &get_selected_field_types(){return selected_field_types;};
void set_selected_field_lengths(vector<vector<int>> input){selected_field_lengths = input;}
vector<vector<int>> &get_selected_field_lengths(){return selected_field_lengths;}
void set_selected_field_names(vector<vector<string>> input){selected_field_names = input;}
vector<vector<string>> &get_selected_field_names(){return selected_field_names;}
void set_dec_onion_index(vector<int> input){dec_onion_index = input;}
vector<int> &get_dec_onion_index(){return dec_onion_index;}
void set_has_salt(vector<string> input){has_salt = input;}
vector<string> &get_has_salt(){return has_salt;}
void serialize();
void deserialize(std::string filename);
};
std::string metadata_files::serialize_vec_int(std::string s,vector<int> vec_int){
s+=":";
for(auto item:vec_int){
s+=to_string(item)+=" ";
}
s.back()='\n';
return s;
}
std::string metadata_files::serialize_vec_str(std::string s,vector<string> vec_str){
s+=":";
for(auto item:vec_str){
s+=item+=" ";
}
s.back()='\n';
return s;
}
vector<string> metadata_files::string_to_vec_str(string line){
int start=0,next=0;
std::vector<std::string> tmp;
while((next=line.find(' ',start))!=-1){
string item = line.substr(start,next-start);
tmp.push_back(item);
start = next+1;
}
string item = line.substr(start);
tmp.push_back(item);
return tmp;
}
vector<int> metadata_files::string_to_vec_int(string line){
int start=0,next=0;
std::vector<int> tmp;
while((next=line.find(' ',start))!=-1){
string item = line.substr(start,next-start);
tmp.push_back(std::stoi(item));
start = next+1;
}
string item = line.substr(start);
tmp.push_back(std::stoi(item));
return tmp;
}
bool metadata_files::make_path(std::string directory){
struct stat st;
if(directory.size()==0||directory[0]=='/') return false;
if(directory.back()=='/') directory.pop_back();
int start = 0,next=0;
while(stat(directory.c_str(),&st)==-1&&next!=-1){
next = directory.find('/',start);
if(next!=-1){
string sub = directory.substr(0,next);
if(stat(sub.c_str(),&st)==-1)
mkdir(sub.c_str(),0700);
start = next + 1;
}else{
mkdir(directory.c_str(),0700);
}
}
return true;
}
void metadata_files::serialize(){
FILE * localmeta = NULL;
string prefix = string("data/")+db+"/"+table;
make_path(prefix);
localmeta = fopen((prefix+"/metadata.data").c_str(),"w");
string s = string("database:")+db;
s+="\n";
fwrite(s.c_str(),1,s.size(),localmeta);
s = string("table:")+table;
s+="\n";
fwrite(s.c_str(),1,s.size(),localmeta);
for(unsigned int i=0;i<dec_onion_index.size();i++){
s=string("INDEX:")+to_string(i)+"\n";
fwrite(s.c_str(),1,s.size(),localmeta);
//then for each index, that is, for each plain field
auto field_types = selected_field_types[i];
s = serialize_vec_int("field_types",field_types);
fwrite(s.c_str(),1,s.size(),localmeta);
auto field_lengths = selected_field_lengths[i];
s = serialize_vec_int("field_lengths",field_lengths);
fwrite(s.c_str(),1,s.size(),localmeta);
auto field_names = selected_field_names[i];
s = serialize_vec_str("field_names",field_names);
fwrite(s.c_str(),1,s.size(),localmeta);
auto get_has_salt = has_salt[i];
s = string("has_salt:")+get_has_salt+string("\n");
fwrite(s.c_str(),1,s.size(),localmeta);
auto onion_index = to_string(dec_onion_index[i]);
s = string("onion_index:")+onion_index+string("\n");
fwrite(s.c_str(),1,s.size(),localmeta);
}
fclose(localmeta);
}
void metadata_files::deserialize(std::string filename){
filename = string("data/")+db+"/"+table+"/"+filename;
std::ifstream infile(filename);
string line;
while(std::getline(infile,line)){
int index = line.find(":");
string head = line.substr(0,index);
if(head == "INDEX"){
}else if(head=="database"){
set_db(line.substr(index+1));
}else if(head=="table"){
set_table(line.substr(index+1));
}else if(head=="field_types"){
string types = line.substr(index+1);
auto res = string_to_vec_int(types);
selected_field_types.push_back(res);
}else if(head=="field_lengths"){
string lengths = line.substr(index+1);
auto res = string_to_vec_int(lengths);
selected_field_lengths.push_back(res);
}else if(head=="field_names"){
string names = line.substr(index+1);
auto res = string_to_vec_str(names);
selected_field_names.push_back(res);
}else if(head=="has_salt"){
has_salt.push_back(line.substr(index+1));
}else if(head=="onion_index"){
dec_onion_index.push_back(stoi(line.substr(index+1)));
}else{
exit(-1);
}
}
infile.close();
}
......@@ -242,7 +242,7 @@ static ResType load_files(std::string db="tdb", std::string table="student"){
for(unsigned int i=0;i<field_types.size();++i){
resraw.fieldTypes.push_back(static_cast<enum_field_types>(field_types[i]));
}
ResType rawtorestype = MygetResTypeFromLuaTable(false, &resraw);
ResType rawtorestype = rawMySQLReturnValue_to_ResType(false, &resraw);
auto finalresults = decryptResults(rawtorestype,*rm);
return finalresults;
}
......
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <map>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <main/rewrite_main.hh>
#include <main/rewrite_util.hh>
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::string;
using std::to_string;
#include "debug/common.hh"
int main(){
metadata_files mf;
mf.set_db_table("tdb","student");
mf.deserialize("metadata.data");
return 0;
}
......@@ -36,7 +36,7 @@ static void init(){
//query for testing purposes
static
std::string getTestQuery(SchemaInfo &schema, std::vector<FieldMeta_Wrapper> &tfds,
std::string getTestQuery(SchemaInfo &schema, std::vector<FieldMetaTrans> &tfds,
std::string db="tdb",std::string table="student1"){
std::string res = "SELECT ";
const std::unique_ptr<IdentityMetaKey> dbmeta_key(new IdentityMetaKey(db));
......@@ -46,13 +46,13 @@ std::string getTestQuery(SchemaInfo &schema, std::vector<FieldMeta_Wrapper> &tfd
std::string annotablename = tbm.getAnonTableName();
//then a list of onion names
for(auto item:tfds){
for(auto index:item.choosenOnions){
res += item.fields[index];
for(auto tf:tfds){
for(auto item : tf.getChoosenOnionName()){
res += item;
res += " , ";
}
if(item.hasSalt){
res += item.originalFm->getSaltName()+" , ";
if(tf.getHasSalt()){
res += tf.getSaltName() + " , ";
}
}
res = res.substr(0,res.size()-2);
......@@ -60,7 +60,7 @@ std::string getTestQuery(SchemaInfo &schema, std::vector<FieldMeta_Wrapper> &tfd
return res;
}
static void write_meta(rawMySQLReturnValue& resraw,std::vector<FieldMeta_Wrapper> &res,string db,string table){
static void write_meta(rawMySQLReturnValue& resraw,std::vector<FieldMetaTrans> &res,string db,string table){
metadata_files mf;
mf.set_db_table(db,table);
vector<vector<int>> selected_field_types;
......@@ -72,31 +72,31 @@ static void write_meta(rawMySQLReturnValue& resraw,std::vector<FieldMeta_Wrapper
unsigned int type_index=0u,length_index=0u;
for(auto item:res){
for(auto ft:res){
vector<int> field_types;
vector<int> field_lengths;
vector<string> field_names;
//only choosen fields
for(auto i:item.choosenOnions){
field_names.push_back(item.fields[i]);
for(auto item:ft.getChoosenOnionName()){
field_names.push_back(item);
}
if(item.hasSalt){
field_names.push_back(item.fields.back());
if(ft.getHasSalt()){
field_names.push_back(ft.getSaltName());
}
int onion_index = item.onionIndex;
int onion_index = 0;
for(unsigned int i=0u;i<field_names.size();i++){
field_types.push_back(static_cast<int>(resraw.fieldTypes[type_index]));
type_index++;
}
// field_lengths = resraw.lengths;
for(unsigned int i=0u;i<field_names.size();i++){
field_lengths.push_back(resraw.lengths[length_index]);
length_index++;
}
if(item.hasSalt){
if(ft.getHasSalt()){
has_salt.push_back("true");
}else has_salt.push_back("false");
......@@ -113,30 +113,8 @@ static void write_meta(rawMySQLReturnValue& resraw,std::vector<FieldMeta_Wrapper
mf.serialize();
}
static void write_row_data(rawMySQLReturnValue& resraw,string db, string table){
vector<FILE*> data_files;
string prefix = string("data/")+db+"/"+table+"/";
for(auto item:resraw.fieldNames){
item=prefix+item;
FILE * data = fopen(item.c_str(),"w");
data_files.push_back(data);
}
const string token = "\n";
for(auto &item : resraw.rowValues){
for(unsigned int i=0u;i<item.size();i++){
fwrite(item[i].c_str(),1,item[i].size(),data_files[i]);
if(IS_NUM(resraw.fieldTypes[i])){
fwrite(token.c_str(),1,token.size(),data_files[i]);
}
}
}
for(auto item:data_files){
fclose(item);
}
}
static
void write_raw_data_to_files(rawMySQLReturnValue& resraw,std::vector<FieldMeta_Wrapper> &res ,string db,string table){
void write_raw_data_to_files(rawMySQLReturnValue& resraw,std::vector<FieldMetaTrans> &res ,string db,string table){
//write metafiles
write_meta(resraw,res,db,table);
//write datafiles
......@@ -148,11 +126,14 @@ static void store(std::string db, std::string table){
//get all the fields in the tables
std::vector<FieldMeta*> fms = getFieldMeta(*schema,db,table);
//transform the field so that selected onions can be used
std::vector<FieldMeta_Wrapper> res = FieldMeta_to_Wrapper(fms);
for(auto &item:res){
(void)item;
item.choosenOnions.push_back(0);
std::vector<FieldMetaTrans> res;
for(auto i=0u;i<fms.size();i++){
FieldMetaTrans ft;
res.push_back(ft);
res.back().trans(fms[i]);
std::vector<int> in{0};
//this is our strategy !!!!!
res.back().choose(in);
}
//generate the backup query and then fetch the tuples
std::string backup_query = getTestQuery(*schema,res,db,table);
......
......@@ -22,7 +22,6 @@ struct help_select{
std::string query;
};
SharedProxyState *shared_ps;
Connect *globalConn;
ProxyState *ps;
......@@ -47,7 +46,13 @@ static void sp_next_first(const help_select &hs){
ps->getSchemaCache().updateStaleness(ps->getEConn(),false);
const std::string next_query = hs.query;
rawMySQLReturnValue resRemote = executeAndGetResultRemote(globalConn,next_query);
const auto &againGet = MygetResTypeFromLuaTable(false,&resRemote);
const auto &againGet = rawMySQLReturnValue_to_ResType(false,&resRemote);
rawMySQLReturnValue str;
transform_to_rawMySQLReturnValue(str,const_cast<ResType &>(againGet));
str.fieldNames = againGet.names;
write_row_data(str,"tdb","student","tdata/");
//AbstractQueryExecutor::ResultType::RESULTS
const auto &res = decryptResults(againGet,hs.rmeta);
parseResType(res);
......
......@@ -25,6 +25,7 @@ public:
vector<vector<string>> selected_field_names;
vector<string> has_salt;
vector<int> dec_onion_index;/*should be 0,1,2,3...*/
string serialize_vec_int(string s,vector<int> vec_int);
string serialize_vec_str(string s,vector<string> vec_str);
vector<string> string_to_vec_str(string line);
......@@ -57,12 +58,3 @@ public:
void serialize();
void deserialize(string filename);
};
......@@ -40,8 +40,42 @@ void FieldMeta_Wrapper::show(){
if(hasSalt){
cout<<"has salt"<<endl;
}else cout<<"do not have salt"<<endl;
}
void FieldMetaTrans::trans(FieldMeta *fm) {
originalFm=fm;
if(fm->getHasSalt()){
hasSalt = true;
saltName = fm->getSaltName();
}else{
hasSalt = false;
}
for(std::pair<const OnionMetaKey *, OnionMeta *> &omkv:fm->orderedOnionMetas()){
onion o = omkv.first->getValue();
OnionMeta* om = omkv.second;
onionsO.push_back(o);
// onionsStr.push_back(TypeText<onion>::toText(o));
onionsOm.push_back(om);
onionsName.push_back(om->getAnonOnionName());
}
}
void FieldMetaTrans::choose(std::vector<onion> onionSet){
choosenOnionO = onionSet;
for(auto &o:onionSet){
choosenOnionName.push_back(originalFm->getOnionMeta(o)->getAnonOnionName()
);
}
}
void FieldMetaTrans::choose(std::vector<int> onionIndexSet){
choosenIndex = onionIndexSet;
for(auto index:onionIndexSet){
choosenOnionO.push_back(onionsO[index]);
choosenOnionName.push_back(onionsName[index]);
}
}
......@@ -65,7 +99,7 @@ itemNullVector(unsigned int count)
return out;
}
ResType MygetResTypeFromLuaTable(bool isNULL,rawMySQLReturnValue *inRow,int in_last_insert_id){
ResType rawMySQLReturnValue_to_ResType(bool isNULL,rawMySQLReturnValue *inRow,int in_last_insert_id){
std::vector<std::string> names;
std::vector<enum_field_types> types;
std::vector<std::vector<Item *>> rows;
......@@ -253,7 +287,8 @@ std::vector<FieldMeta_Wrapper> FieldMeta_to_Wrapper(std::vector<FieldMeta *> pfm
for(auto pfm:pfms){
FieldMeta_Wrapper tf;
tf.originalFm = pfm;
for(std::pair<const OnionMetaKey *, OnionMeta *> &ompair:pfm->orderedOnionMetas()){
for(std::pair<const OnionMetaKey *, OnionMeta *> &ompair:
pfm->orderedOnionMetas()){
tf.numOfOnions++;
tf.fields.push_back((ompair.second)->getAnonOnionName());
tf.onions.push_back(ompair.first->getValue());
......@@ -323,5 +358,25 @@ executeAndGetResultRemote(Connect * curConn,std::string query){
return myRaw;
}
void
write_row_data(rawMySQLReturnValue& resraw,std::string db,std::string table,std::string prefix){
std::vector<FILE*> data_files;
prefix = prefix+db+"/"+table+"/";
for(auto item:resraw.fieldNames){
item=prefix+item;
FILE * data = fopen(item.c_str(),"w");
data_files.push_back(data);
}
const std::string token = "\n";
for(auto &item : resraw.rowValues){
for(unsigned int i=0u;i<item.size();i++){
fwrite(item[i].c_str(),1,item[i].size(),data_files[i]);
if(IS_NUM(resraw.fieldTypes[i])){
fwrite(token.c_str(),1,token.size(),data_files[i]);
}
}
}
for(auto item:data_files){
fclose(item);
}
}
......@@ -45,24 +45,50 @@ struct FieldMeta_Wrapper{
bool hasSalt;
FieldMeta *originalFm;
std::vector<int> choosenOnions;
std::vector<onion> choosen_onions_o;
std::vector<std::string> choosen_onions_str;
//used to construct return meta
int onionIndex = 0;
int numOfOnions=0;
//onions
std::vector<std::string> fields;
std::vector<onion> onions;
std::vector<std::string> onion_str;
std::vector<OnionMeta*>originalOm;
void show();
};
/*Transformed version of FieldMeta*/
class FieldMetaTrans{
FieldMeta *originalFm;
bool hasSalt;
std::string saltName;
std::vector<OnionMeta*> onionsOm;
std::vector<onion> onionsO;
std::vector<std::string> onionsName;
std::vector<int> choosenIndex;
std::vector<onion> choosenOnionO;
std::vector<std::string> choosenOnionName;
public:
void trans(FieldMeta *fm);
void choose(std::vector<onion> onionSet);
void choose(std::vector<int> onionIndexSet);
const std::vector<std::string> getChoosenOnionName(){return choosenOnionName;}
bool getHasSalt(){return hasSalt;}
std::string getSaltName(){return saltName;}
};
/*Functions*/
Item_null*
make_null(const std::string &name="");
std::vector<Item *>
itemNullVector(unsigned int count);
ResType MygetResTypeFromLuaTable(bool isNULL,rawMySQLReturnValue *inRow = NULL,int in_last_insert_id = 0);
ResType rawMySQLReturnValue_to_ResType(bool isNULL,rawMySQLReturnValue *inRow = NULL,int in_last_insert_id = 0);
void
......@@ -88,3 +114,8 @@ void transform_to_rawMySQLReturnValue(rawMySQLReturnValue & str,ResType & item);
rawMySQLReturnValue
executeAndGetResultRemote(Connect * curConn,std::string query);
void
write_row_data(rawMySQLReturnValue& resraw,std::string db,std::string table,std::string prefix="data/");
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