-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtableIO.cpp
More file actions
161 lines (153 loc) · 4.6 KB
/
tableIO.cpp
File metadata and controls
161 lines (153 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "tableManager.h"
#include <memory>
vector<blockData> blockData::allData;
QMutex blockData::mutex;
vector<vector<string>> table::toStrVec(){
int m=(this->allCol[0]->getAllData()).size()+1;
int n=this->allCol.size();
vector<vector<string>> tableframe(m,vector<string>(n,""));
for(int i=0;i<n;i++){
int sort=this->allCol[i]->getType();
tableframe[0][i]=(this->allCol[i]->ID)+":"+to_string(sort);
}
int i=0;
for (col* c : this->allCol)
{ int j=1;
for (Basic* b : c->getAllData())
{
tableframe[j][i]=b->toStr();
j++;
}
i++;
}
return tableframe;
}
QString table::toStr()
{
const vector<vector<string>>& tableframe=this->toStrVec();
string data;
int m=tableframe.size();
int n=tableframe[0].size();
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(j==n-1){
data.append(tableframe[i][j]+"\n");
}
else{
data.append(tableframe[i][j]+",");
}
}
}
return QString::fromStdString(data);
}
vector<string> table::toStr(const int& fileLen){
const vector<vector<string>>& tableframe=this->toStrVec();
int m=tableframe.size();
int n=tableframe[0].size();
string title="";
for(int i=0;i<n;++i){
if(i==n-1){
title.append(tableframe[0][i]+"\n");
continue;
}
title.append(tableframe[0][i]+",");
}
int totalNum=(m-1)/fileLen+((m-1)%fileLen==0? 0:1);
if(totalNum==0){
return {title};
}
vector<string> result(totalNum,"");
for(int i=1;i<m;i++){
int tmpNum=(i-1)/fileLen;
string& data=result[tmpNum];
if((i-1)%fileLen==0){
data.append(title);
}
for(int j=0;j<n;j++){
if(j==n-1){
data.append(tableframe[i][j]+"\n");
}
else{
data.append(tableframe[i][j]+",");
}
}
}
return result;
}
void table:: saveFile(const string& path) //将整个表的内容按约定格式写入空文件
{
const vector<string>& allData=this->toStr(IO::singleFileLen);
int num=1;
for(const string& data:allData){
const string& tmp=IO::path_to_splitpath(path,num);
const vector<vector<int>>& len_data=IO::strdata_to_lendata(data);
multiSave* ms= new multiSave(tmp,data,len_data);
ms->setAutoDelete(true);
QThreadPool::globalInstance()->start(ms);
++num;
}
IO::del_table_blocks(path,num);
this->allRecord.clear();
}
table* table::loadFile(const string& path,int mark) //按约定格式从文件中读取表
{
vector<col*> cols;
vector<int> blocksLen;
const int& num=IO::table_blocks_num(path);
if(num==0){
throw string("No corresponding table was found");
}
for(int i=1;i<=num;++i){
multiRead* mr=new multiRead (IO::path_to_splitpath(path,i),i);
mr->setAutoDelete(true);
QThreadPool::globalInstance()->start(mr);
}
while (true) {
if(blockData::allData.size()==num){
break;
}
}
sort(blockData::allData.begin(),blockData::allData.end());
for(const blockData& tmpData:blockData::allData){
if(tmpData.num==1){
cols=IO::get_empty_table_cols(tmpData.data);
}
const int& dataLen=IO::put_single_block_data(cols,tmpData.data);
blocksLen.push_back(dataLen);
}
blockData::allData=vector<blockData>();
return new table(IO::path_to_name(path),cols,blocksLen);
}
void table::updateFile(const string& path) //根据table.allRecord更新文件内容
{
if(this->allRecord.empty()){
return;
}
map<int,fstream> fileStore;
map<int,vector<vector<int>>> lenStore;
for(record rcd:this->allRecord){
switch (rcd.type) {
case ADD:{
IO::Add(rcd.targetTuple,fileStore,lenStore,path,this->allBlocksLen);
break;
}
case DEL:{
IO::Del(rcd.opSub,fileStore,lenStore,path,this->allBlocksLen);
break;
}
case MOD:{
IO::Del(rcd.opSub,fileStore,lenStore,path,this->allBlocksLen);
IO::Add(rcd.targetTuple,fileStore,lenStore,path,this->allBlocksLen);
break;
}
}
}
for(auto iter=fileStore.begin();iter!=fileStore.end();iter++){
iter->second.flush();
}
for(auto iter=lenStore.begin();iter!=lenStore.end();iter++){
IO::write_to_len_file(IO::path_to_splitpath(path,iter->first),iter->second);
}
//全部写入后清除record记录
this->allRecord.clear();
}