Commit 82747d8f authored by yiwenshao's avatar yiwenshao

able to use metadatafile class in load and store

parent 0e66ba42
#pragma once
class metadata_file{
string db,table;
int num_of_fields;
vector<string> field_types;
vector<int> field_lengths;
vector<string> field_names;
vector<int> choosen_onions;
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_num_of_fields(int num){num_of_fields = num;}
int get_num_of_fields(){return num_of_fields;}
void set_field_types(vector<string> input){field_types = input;}
std::vector<std::string> & get_field_types(){return field_types;}
void set_field_lengths(vector<int> input){field_lengths = input;}
std::vector<int> & get_field_lengths(){return field_lengths;}
void set_field_names(vector<string> input){field_names = input;}
std::vector<std::string> & get_field_names(){return field_names;}
void set_choosen_onions(vector<int> input){choosen_onions = input;}
std::vector<int>& get_choosen_onions(){return choosen_onions;}
void serilize();
void deserialize(std::string filename);
void show();
static bool 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_file::serilize(){
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);
s = string("num_of_fields:")+to_string(num_of_fields)+"\n";
fwrite(s.c_str(),1,s.size(),localmeta);
s = string("field_types:");
for(auto item:field_types){
s+=item+=" ";
}
s.back()='\n';
fwrite(s.c_str(),1,s.size(),localmeta);
s = string("field_lengths:");
for(auto item : field_lengths){
s+=to_string(item)+=" ";
}
s.back()='\n';
fwrite(s.c_str(),1,s.size(),localmeta);
s = string("field_names:");
for(auto item : field_names){
s+=item+=" ";
}
s.back()='\n';
fwrite(s.c_str(),1,s.size(),localmeta);
s = string("choosen_onions:");
for(auto item : choosen_onions){
s+=to_string(item)+=" ";
}
s.back()='\n';
fwrite(s.c_str(),1,s.size(),localmeta);
fclose(localmeta);
}
void metadata_file::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=="database"){
set_db(line.substr(index+1));
}else if(head=="table"){
set_table(line.substr(index+1));
}else if(head=="num_of_fields"){
set_num_of_fields(std::stoi(line.substr(index+1)));
}else if(head=="field_types"){
string types = line.substr(index+1);
int start=0,next=0;
std::vector<std::string> tmp;
while((next=types.find(' ',start))!=-1){
string item = types.substr(start,next-start);
tmp.push_back(item);
start = next+1;
}
string item = types.substr(start);
tmp.push_back(item);
set_field_types(tmp);
}else if(head=="field_lengths"){
string lengths = line.substr(index+1);
int start=0,next=0;
std::vector<int> tmp;
while((next=lengths.find(' ',start))!=-1){
string item = lengths.substr(start,next-start);
tmp.push_back(std::stoi(item));
start = next+1;
}
string item = lengths.substr(start);
tmp.push_back(std::stoi(item));
set_field_lengths(tmp);
}else if(head=="field_names"){
std::vector<std::string> tmp;
string names = line.substr(index+1);
int start=0,next=0;
while((next=names.find(' ',start))!=-1){
string item = names.substr(start,next-start);
tmp.push_back(item);
start = next+1;
}
string item = names.substr(start);
tmp.push_back(item);
set_field_names(tmp);
}else if(head=="choosen_onions"){
std::vector<int> tmp;
string c_onions = line.substr(index+1);
int start=0,next=0;
while((next=c_onions.find(' ',start))!=-1){
string item = c_onions.substr(start,next-start);
tmp.push_back(std::stoi(item));
start = next+1;
}
string item = c_onions.substr(start);
tmp.push_back(std::stoi(item));
set_choosen_onions(tmp);
}
}
infile.close();
}
void metadata_file::show(){
cout<<db<<endl;
cout<<table<<endl;
cout<<num_of_fields<<endl;
for(auto item:field_types){
cout<<item<<"\t";
}
cout<<endl;
for(auto item:field_lengths){
cout<<item<<"\t";
}
cout<<endl;
for(auto item:field_names){
cout<<item<<"\t";
}
cout<<endl;
for(auto item:choosen_onions){
cout<<item<<"\t";
}
cout<<endl;
}
#include "debug/load.hh" #include "debug/load.hh"
#include "debug/common.hh"
//get returnMeta //get returnMeta
//for each filed, we have a fieldmeta. we can chosse one onion under that field to construct a return meta. //for each filed, we have a fieldmeta. we can chosse one onion under that field to construct a return meta.
//in fact, a returnmeta can contain many fields. //in fact, a returnmeta can contain many fields.
...@@ -19,9 +19,13 @@ std::shared_ptr<ReturnMeta> getReturnMeta(std::vector<FieldMeta*> fms, std::vect ...@@ -19,9 +19,13 @@ std::shared_ptr<ReturnMeta> getReturnMeta(std::vector<FieldMeta*> fms, std::vect
} }
static meta_file load_meta(string db="tdb", string table="student", string filename="metadata.data"){ static metadata_file load_meta(string db="tdb", string table="student", string filename="metadata.data"){
filename = string("data/")+db+"/"+table+"/"+filename; metadata_file mf;
std::ifstream infile(filename); mf.set_db(db);
mf.set_table(table);
mf.deserialize(filename);
return mf;
/*
string line; string line;
meta_file res; meta_file res;
while(std::getline(infile,line)){ while(std::getline(infile,line)){
...@@ -29,53 +33,72 @@ static meta_file load_meta(string db="tdb", string table="student", string filen ...@@ -29,53 +33,72 @@ static meta_file load_meta(string db="tdb", string table="student", string filen
string head = line.substr(0,index); string head = line.substr(0,index);
if(head=="database"){ if(head=="database"){
res.db = line.substr(index+1); res.db = line.substr(index+1);
// mf.set_db(line.substr(index+1));
}else if(head=="table"){ }else if(head=="table"){
res.table = line.substr(index+1); res.table = line.substr(index+1);
// mf.set_table(line.substr(index+1));
}else if(head=="num_of_fields"){ }else if(head=="num_of_fields"){
res.num_of_fields = std::stoi(line.substr(index+1)); res.num_of_fields = std::stoi(line.substr(index+1));
// mf.set_num_of_fields(std::stoi(line.substr(index+1)));
}else if(head=="field_types"){ }else if(head=="field_types"){
string types = line.substr(index+1); string types = line.substr(index+1);
int start=0,next=0; int start=0,next=0;
// std::vector<std::string> tmp;
while((next=types.find(' ',start))!=-1){ while((next=types.find(' ',start))!=-1){
string item = types.substr(start,next-start); string item = types.substr(start,next-start);
res.field_types.push_back(item); res.field_types.push_back(item);
// tmp.push_back(item);
start = next+1; start = next+1;
} }
string item = types.substr(start); string item = types.substr(start);
// tmp.push_back(item);
res.field_types.push_back(item); res.field_types.push_back(item);
// mf.set_field_types(tmp);
}else if(head=="field_lengths"){ }else if(head=="field_lengths"){
string lengths = line.substr(index+1); string lengths = line.substr(index+1);
int start=0,next=0; int start=0,next=0;
// std::vector<int> tmp;
while((next=lengths.find(' ',start))!=-1){ while((next=lengths.find(' ',start))!=-1){
string item = lengths.substr(start,next-start); string item = lengths.substr(start,next-start);
res.field_lengths.push_back(std::stoi(item)); res.field_lengths.push_back(std::stoi(item));
// tmp.push_back(std::stoi(item));
start = next+1; start = next+1;
} }
string item = lengths.substr(start); string item = lengths.substr(start);
res.field_lengths.push_back(std::stoi(item)); res.field_lengths.push_back(std::stoi(item));
// tmp.push_back(std::stoi(item));
// mf.set_field_lengths(tmp);
}else if(head=="field_names"){ }else if(head=="field_names"){
// std::vector<std::string> tmp;
string names = line.substr(index+1); string names = line.substr(index+1);
int start=0,next=0; int start=0,next=0;
while((next=names.find(' ',start))!=-1){ while((next=names.find(' ',start))!=-1){
string item = names.substr(start,next-start); string item = names.substr(start,next-start);
res.field_names.push_back(item); res.field_names.push_back(item);
// tmp.push_back(item);
start = next+1; start = next+1;
} }
string item = names.substr(start); string item = names.substr(start);
res.field_names.push_back(item); res.field_names.push_back(item);
// tmp.push_back(item);
// mf.set_field_names(tmp);
}else if(head=="choosen_onions"){ }else if(head=="choosen_onions"){
// std::vector<int> tmp;
string c_onions = line.substr(index+1); string c_onions = line.substr(index+1);
int start=0,next=0; int start=0,next=0;
while((next=c_onions.find(' ',start))!=-1){ while((next=c_onions.find(' ',start))!=-1){
string item = c_onions.substr(start,next-start); string item = c_onions.substr(start,next-start);
res.choosen_onions.push_back(std::stoi(item)); res.choosen_onions.push_back(std::stoi(item));
// tmp.push_back(std::stoi(item));
start = next+1; start = next+1;
} }
string item = c_onions.substr(start); string item = c_onions.substr(start);
res.choosen_onions.push_back(std::stoi(item)); res.choosen_onions.push_back(std::stoi(item));
// tmp.push_back(std::stoi(item));
// mf.set_choosen_onions(tmp);
} }
} }
return res; //return res;*/
} }
...@@ -98,23 +121,23 @@ static void load_string(string filename, vector<string> &res,unsigned long lengt ...@@ -98,23 +121,23 @@ static void load_string(string filename, vector<string> &res,unsigned long lengt
close(fd); close(fd);
} }
static vector<vector<string>> load_table_fields(meta_file & input) { static vector<vector<string>> load_table_fields(metadata_file & input) {
string db = input.db; string db = input.get_db();
string table = input.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.field_names){ for(auto item:input.get_field_names()){
datafiles.push_back(prefix+item); datafiles.push_back(prefix+item);
} }
for(unsigned int i=0u;i<input.field_names.size();i++){ for(unsigned int i=0u;i<input.get_field_names().size();i++){
vector<string> column; vector<string> column;
if(IS_NUM(std::stoi(input.field_types[i]))){ if(IS_NUM(std::stoi(input.get_field_types()[i]))){
load_num(datafiles[i],column); load_num(datafiles[i],column);
}else{ }else{
load_string(datafiles[i],column,input.field_lengths[i]); load_string(datafiles[i],column,input.get_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()){
...@@ -141,10 +164,10 @@ static ResType load_files(std::string db="tdb", std::string table="student"){ ...@@ -141,10 +164,10 @@ static ResType load_files(std::string db="tdb", std::string table="student"){
//to be //to be
} }
meta_file res_meta = load_meta(db,table); metadata_file res_meta = load_meta(db,table);
for(unsigned int i=0;i<res_meta.choosen_onions.size();i++){ for(unsigned int i=0;i<res_meta.get_choosen_onions().size();i++){
res[i].choosenOnions.push_back(res_meta.choosen_onions[i]); 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);
...@@ -155,10 +178,10 @@ static ResType load_files(std::string db="tdb", std::string table="student"){ ...@@ -155,10 +178,10 @@ 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.field_names; resraw2.fieldNames = res_meta.get_field_names();
resraw2.choosen_onions = res_meta.choosen_onions; resraw2.choosen_onions = res_meta.get_choosen_onions();
for(unsigned int i=0;i<res_meta.field_types.size();++i) { for(unsigned int i=0;i<res_meta.get_field_types().size();++i) {
resraw2.fieldTypes.push_back(static_cast<enum_field_types>(std::stoi(res_meta.field_types[i]))); resraw2.fieldTypes.push_back(static_cast<enum_field_types>(std::stoi(res_meta.get_field_types()[i])));
} }
ResType rawtorestype = MygetResTypeFromLuaTable(false, &resraw2); ResType rawtorestype = MygetResTypeFromLuaTable(false, &resraw2);
auto finalresults = decryptResults(rawtorestype,*rm); auto finalresults = decryptResults(rawtorestype,*rm);
......
#include "debug/store.hh" #include "debug/store.hh"
#include "debug/common.hh"
static void write_meta(rawMySQLReturnValue& resraw,string db,string table){ static void write_meta(rawMySQLReturnValue& resraw,string db,string table){
//write metadata //write metadata
......
...@@ -294,7 +294,7 @@ std::shared_ptr<ReturnMeta> getReturnMeta(std::vector<FieldMeta*> fms, std::vect ...@@ -294,7 +294,7 @@ std::shared_ptr<ReturnMeta> getReturnMeta(std::vector<FieldMeta*> fms, std::vect
/* /*
only support relative path only support relative path
*/ */
static bool make_path(string directory){ /*static bool make_path(string directory){
struct stat st; struct stat st;
if(directory.size()==0||directory[0]=='/') return false; if(directory.size()==0||directory[0]=='/') return false;
if(directory.back()=='/') directory.pop_back(); if(directory.back()=='/') directory.pop_back();
...@@ -311,102 +311,10 @@ static bool make_path(string directory){ ...@@ -311,102 +311,10 @@ static bool make_path(string directory){
} }
} }
return true; return true;
} }*/
class metadata_file{
string db,table;
int num_of_fields;
vector<string> field_types;
vector<int> field_lengths;
vector<string> field_names;
vector<int> choosen_onions;
public:
void set_db_table(std::string idb,std::string itable){db=idb;table=itable;}
void set_num_of_fields(int num){num_of_fields = num;}
void set_field_types(vector<string> input){field_types = input;}
void set_field_lengths(vector<int> input){field_lengths = input;}
void set_field_names(vector<string> input){field_names = input;}
void set_choosen_onions(vector<int> input){choosen_onions = input;}
void serilize();
void deserilize();
void show();
};
void metadata_file::serilize(){
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);
s = string("num_of_fields:")+to_string(num_of_fields)+"\n";
fwrite(s.c_str(),1,s.size(),localmeta);
s = string("field_types:");
for(auto item:field_types){
s+=item+=" ";
}
s.back()='\n';
fwrite(s.c_str(),1,s.size(),localmeta);
s = string("field_lengths:");
for(auto item : field_lengths){
s+=to_string(item)+=" ";
}
s.back()='\n';
fwrite(s.c_str(),1,s.size(),localmeta);
s = string("field_names:");
for(auto item : field_names){
s+=item+=" ";
}
s.back()='\n';
fwrite(s.c_str(),1,s.size(),localmeta);
s = string("choosen_onions:");
for(auto item : choosen_onions){
s+=to_string(item)+=" ";
}
s.back()='\n';
fwrite(s.c_str(),1,s.size(),localmeta);
fclose(localmeta);
}
void metadata_file::deserilize(){
}
void metadata_file::show(){
cout<<db<<endl;
cout<<table<<endl;
cout<<num_of_fields<<endl;
for(auto item:field_types){
cout<<item<<"\t";
}
cout<<endl;
for(auto item:field_lengths){
cout<<item<<"\t";
}
cout<<endl;
for(auto item:field_names){
cout<<item<<"\t";
}
cout<<endl;
for(auto item:choosen_onions){
cout<<item<<"\t";
}
cout<<endl;
}
/*for each field, convert the format to transField*/ /*for each field, convert the format to transField*/
static std::vector<transField> getTransField(std::vector<FieldMeta *> pfms){ static std::vector<transField> getTransField(std::vector<FieldMeta *> pfms){
std::vector<transField> res; std::vector<transField> res;
......
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