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/common.hh"
//get returnMeta
//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.
......@@ -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"){
filename = string("data/")+db+"/"+table+"/"+filename;
std::ifstream infile(filename);
static metadata_file load_meta(string db="tdb", string table="student", string filename="metadata.data"){
metadata_file mf;
mf.set_db(db);
mf.set_table(table);
mf.deserialize(filename);
return mf;
/*
string line;
meta_file res;
while(std::getline(infile,line)){
......@@ -29,53 +33,72 @@ static meta_file load_meta(string db="tdb", string table="student", string filen
string head = line.substr(0,index);
if(head=="database"){
res.db = line.substr(index+1);
// mf.set_db(line.substr(index+1));
}else if(head=="table"){
res.table = line.substr(index+1);
// mf.set_table(line.substr(index+1));
}else if(head=="num_of_fields"){
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"){
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);
res.field_types.push_back(item);
// tmp.push_back(item);
start = next+1;
}
string item = types.substr(start);
// tmp.push_back(item);
res.field_types.push_back(item);
// mf.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);
res.field_lengths.push_back(std::stoi(item));
// tmp.push_back(std::stoi(item));
start = next+1;
}
string item = lengths.substr(start);
res.field_lengths.push_back(std::stoi(item));
// tmp.push_back(std::stoi(item));
// mf.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);
res.field_names.push_back(item);
// tmp.push_back(item);
start = next+1;
}
string item = names.substr(start);
res.field_names.push_back(item);
// tmp.push_back(item);
// mf.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);
res.choosen_onions.push_back(std::stoi(item));
// tmp.push_back(std::stoi(item));
start = next+1;
}
string item = c_onions.substr(start);
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
close(fd);
}
static vector<vector<string>> load_table_fields(meta_file & input) {
string db = input.db;
string table = input.table;
static vector<vector<string>> load_table_fields(metadata_file & input) {
string db = input.get_db();
string table = input.get_table();
vector<vector<string>> res;
string prefix = string("data/")+db+"/"+table+"/";
vector<string> datafiles;
for(auto item:input.field_names){
for(auto item:input.get_field_names()){
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;
if(IS_NUM(std::stoi(input.field_types[i]))){
if(IS_NUM(std::stoi(input.get_field_types()[i]))){
load_num(datafiles[i],column);
}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++){
if(j>=res.size()){
......@@ -141,10 +164,10 @@ static ResType load_files(std::string db="tdb", std::string table="student"){
//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++){
res[i].choosenOnions.push_back(res_meta.choosen_onions[i]);
for(unsigned int i=0;i<res_meta.get_choosen_onions().size();i++){
res[i].choosenOnions.push_back(res_meta.get_choosen_onions()[i]);
}
std::shared_ptr<ReturnMeta> rm = getReturnMeta(fms,res);
......@@ -155,10 +178,10 @@ static ResType load_files(std::string db="tdb", std::string table="student"){
//load fields in the stored file
vector<vector<string>> res_field = load_table_fields(res_meta);
resraw2.rowValues = res_field;
resraw2.fieldNames = res_meta.field_names;
resraw2.choosen_onions = res_meta.choosen_onions;
for(unsigned int i=0;i<res_meta.field_types.size();++i) {
resraw2.fieldTypes.push_back(static_cast<enum_field_types>(std::stoi(res_meta.field_types[i])));
resraw2.fieldNames = res_meta.get_field_names();
resraw2.choosen_onions = res_meta.get_choosen_onions();
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.get_field_types()[i])));
}
ResType rawtorestype = MygetResTypeFromLuaTable(false, &resraw2);
auto finalresults = decryptResults(rawtorestype,*rm);
......
#include "debug/store.hh"
#include "debug/common.hh"
static void write_meta(rawMySQLReturnValue& resraw,string db,string table){
//write metadata
......
......@@ -294,7 +294,7 @@ std::shared_ptr<ReturnMeta> getReturnMeta(std::vector<FieldMeta*> fms, std::vect
/*
only support relative path
*/
static bool make_path(string directory){
/*static bool make_path(string directory){
struct stat st;
if(directory.size()==0||directory[0]=='/') return false;
if(directory.back()=='/') directory.pop_back();
......@@ -311,101 +311,9 @@ static bool make_path(string directory){
}
}
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*/
static std::vector<transField> getTransField(std::vector<FieldMeta *> pfms){
......
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