Commit c2470aaa authored by yiwenshao's avatar yiwenshao

add random insert

parent 481b74e7
#include <string>
#include <iostream>
#include <functional>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
static
uint64_t cur_usec() {
struct timeval tv;
gettimeofday(&tv, 0);
return ((uint64_t)tv.tv_sec) * 1000000 + tv.tv_usec;
}
using namespace std;
static
std::string
getpRandomName(int out_length = 10){
static const char valids[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
char output[out_length + 1];
// std::function<bool()> wrap_srand =[](){srand(time(NULL)); return true;};
std::function<bool()> wrap_srand =[](){srand(cur_usec()); return true;};
std::function<void(bool)> do_nothing = [] (bool b) {return;};
static bool danger_will_robinson = wrap_srand();
do_nothing(danger_will_robinson);
for (int i = 0; i < out_length; ++i) {
output[i] = valids[rand() % strlen(valids)];
}
output[out_length] = 0;
return std::string(output);
}
int main(int argc,char** argv){
if(argc!=2){
exit(0);
}
int length = stoi(string(argv[1]));
cout<<getpRandomName(length)<<endl;
return 0;
}
function generate_insert_int(){
head=$1
pipe=$2
count=$3
for((i=1;i<$count;i++))do
res=$head
for((j=2;j<$pipe;j++))do
res="${res}($RANDOM),"
done
res="${res}($RANDOM);"
echo $res
done
}
h="INSERT INTO int_table VALUES "
generate_insert_int "$h" 10 10
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
using namespace std; using namespace std;
extern Connect *con; extern Connect *con;
string createSelect(string database,string table,int quote){ string createSelect(string database,string table){
auto dbresult = con->execute(string("SELECT * FROM `")+database+"`.`"+string(table)+"` LIMIT 1;"); auto dbresult = con->execute(string("SELECT * FROM `")+database+"`.`"+string(table)+"` LIMIT 1;");
DBResult * result = dbresult.get(); DBResult * result = dbresult.get();
vector<vector<string>> rows = result->getRows(); vector<vector<string>> rows = result->getRows();
...@@ -18,10 +18,7 @@ string createSelect(string database,string table,int quote){ ...@@ -18,10 +18,7 @@ string createSelect(string database,string table,int quote){
if(IS_NUM(types[i])){ if(IS_NUM(types[i])){
head += fields[i]+","; head += fields[i]+",";
}else{ }else{
if(quote==1) head += fields[i]+",";
head+=string("QUOTE(")+fields[i]+") AS quo"+fields[i]+" ,";
else if(quote==2) head+=string("HEX(")+fields[i]+") AS hex"+fields[i]+" ,";
else if(quote==3) head += fields[i]+",";
} }
} }
head[head.size()-1]=' '; head[head.size()-1]=' ';
...@@ -34,18 +31,22 @@ void backupselect(string query,string prefix){ ...@@ -34,18 +31,22 @@ void backupselect(string query,string prefix){
system((string("mkdir -p ")+prefix).c_str()); system((string("mkdir -p ")+prefix).c_str());
auto dbresult = con->execute(query); auto dbresult = con->execute(query);
DBResult * result = dbresult.get(); DBResult * result = dbresult.get();
vector<vector<string>> rows = result->getRows(); vector<vector<string>> rows = result->getRows();
vector<enum_field_types> types = result->getTypes(); vector<enum_field_types> types = result->getTypes();
vector<string> fields = result->getFields(); vector<string> fields = result->getFields();
vector<vector<int>> lengths = result->getLengths();
int len = fields.size(); int len = fields.size();
vector<FILE*> files(len,NULL); vector<FILE*> files(len,NULL);
for(int i=0;i<fields.size();i++){ for(int i=0;i<fields.size();i++){
files[i] = fopen((prefix+fields[i]).c_str(),"a"); files[i] = fopen((prefix+fields[i]).c_str(),"a");
} }
for(auto row:rows){
for(int i=0;i<row.size();i++){
fwrite(row[i].c_str(),1,row[i].size(),files[i]); for(auto i=0u;i<rows.size();i++){
fprintf(files[i],"\n"); for(int j=0;j<rows[i].size();j++){
fwrite(rows[i][j].c_str(),1,lengths[i][j],files[j]);
fprintf(files[j],"\n");
} }
} }
for(int i=0;i<fields.size();i++){ for(int i=0;i<fields.size();i++){
...@@ -72,16 +73,14 @@ vector<string> getTables(string db){ ...@@ -72,16 +73,14 @@ vector<string> getTables(string db){
int main(int argc,char**argv){ int main(int argc,char**argv){
system("rm -rf allTables"); system("rm -rf allTables");
system("mkdir allTables"); system("mkdir allTables");
if(argc!=3){ if(argc!=2){
cout<<"db, 1quote/2hex/3plain"<<endl; cout<<"db"<<endl;
return 0; return 0;
} }
string option(argv[2]);
string num = string(argv[1]); string num = string(argv[1]);
vector<string> tablesprefix = getTables(string(argv[1])); vector<string> tablesprefix = getTables(string(argv[1]));
for(auto item:tablesprefix){ for(auto item:tablesprefix){
string query = createSelect(string(argv[1]),item,stoi(option)); string query = createSelect(string(argv[1]),item);
backupselect(query,string("allTables/")+item+"/"); backupselect(query,string("allTables/")+item+"/");
} }
return 0; return 0;
......
...@@ -62,6 +62,7 @@ class DBResult { ...@@ -62,6 +62,7 @@ class DBResult {
vector<enum_field_types> getTypes(){return types;} vector<enum_field_types> getTypes(){return types;}
vector<string> getTypesString(){return typesString;} vector<string> getTypesString(){return typesString;}
vector<string> getFields(){return fields;} vector<string> getFields(){return fields;}
vector<vector<int>> getLengths(){return lengths;}
~DBResult(); ~DBResult();
private: private:
const uint64_t affected_rows; const uint64_t affected_rows;
......
str_len=16
function generate_insert_int(){
head=$1
pipe=$2
count=$3
for((i=1;i<$count;i++))do
res=$head
for((j=1;j<$pipe;j++))do
cur=`./mtl/rand_str $str_len`
res="${res}('$cur'),"
done
cur=`./mtl/rand_str $str_len`
res="${res}('$cur');"
echo $res
done
}
h="INSERT INTO str_table VALUES "
generate_insert_int "$h" 3 100
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