-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIO.h
More file actions
227 lines (183 loc) · 5.16 KB
/
IO.h
File metadata and controls
227 lines (183 loc) · 5.16 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#ifndef CONJUNCTIVEQ_IO_H
#define CONJUNCTIVEQ_IO_H
#include <iomanip>
#include "DB.h"
#include <algorithm>
#include "random"
extern double scale_fac;
extern unsigned sliding_window_size;
extern unsigned sliding_window_factor;
extern unsigned sliding_window_time_range;
extern string dynamic_stream_fn;
extern string schema_file;
extern string query_file;
extern string static_stream_fn;
int start_period_length = 10000000;
string biggest_table;
class Buffer
{
public:
map<string, vector<Tuple>> table_name_2_tuples;
map<int, string> end_offset_2_table_name;
vector<Operation> operations_without_delete;
map<string, int> table_name_2_next_tuple_id;
int idx = 0;
vector<Operation> operations;
Operation empty_oper{};
const Operation &GetNextOper();
int GetOperNum()
{
return operations.size();
}
unsigned ReadDimTableTuples(const set<string> &dimension_table_names)
{
int num_tuples = 0;
for (const string &table_name : dimension_table_names)
{
for (auto &tup : table_name_2_tuples[table_name])
{
table_name_2_next_tuple_id[table_name]++;
Operation oper(table_name, tup, Operation::insert);
operations_without_delete.emplace_back(oper);
num_tuples++;
}
assert(table_name_2_next_tuple_id[table_name] == table_name_2_tuples[table_name].size());
}
return num_tuples;
}
};
const Operation &Buffer::GetNextOper()
{
if (idx >= operations.size())
return empty_oper;
const auto &ans = operations[idx];
idx++;
return ans;
}
void PrepareQuery(Query &query)
{
map<string, AttributeType> attribute_2_type;
map<string, vector<string>> table_2_attributes;
char buf[100000];
stringstream ss;
ifstream query_input;
ifstream schema_input;
schema_input.open(schema_file);
while (schema_input.getline(buf, 99999))
{
ss.clear();
ss << buf;
int tmp1;
string table_name, tmp;
ss >> table_name >> tmp1;
vector<string> attributes;
while (ss >> tmp)
{
attributes.emplace_back(tmp);
}
table_2_attributes.insert({table_name, attributes});
}
query_input.open(query_file);
string t_name;
set<string> used_table_name;
map<string, vector<string>> used_table_2_attributes;
int table_num;
query_input >> table_num;
for (int i = 0; i < table_num; i++)
{
query_input >> t_name;
used_table_name.insert(t_name);
used_table_2_attributes.insert({t_name, table_2_attributes.at(t_name)});
}
query.Init(attribute_2_type, used_table_2_attributes);
return;
}
pair<bool, Operation> ReadTupleFromStreamFile(ifstream &file, bool with_type)
{
char cbuf[100000];
if (!file.getline(cbuf, 99999))
return {false, {}};
// cout<<cbuf<<endl;
stringstream ss;
Tuple tuple;
ss.clear();
ss << cbuf;
bool is_insert = true;
if (with_type)
ss >> is_insert;
string table_name;
ss >> table_name;
ss.get();
auto columns = GetCSVColumns(ss);
for (auto &col : columns)
{
// cout<<col<<endl;
tuple.AddContent(col.c_str());
}
// exit(0);
return {true, {table_name, tuple, is_insert ? Operation::insert : Operation::delete_}};
}
/**
* @note: diff with ReadTupleFromStreamFile: here, the file split attributes with '|', and do not use "" to wrap contents containing ','.
*/
pair<bool, Operation> ReadTPCTupleFromStreamFile(ifstream &file, bool with_type)
{
char cbuf[100000];
if (!file.getline(cbuf, 99999))
return {false, {}};
char attr_buf[100000];
// cout<<cbuf<<endl;
stringstream ss;
Tuple tuple;
ss.clear();
ss << cbuf;
bool is_insert = true;
if (with_type)
ss >> is_insert;
string table_name;
ss >> table_name;
ss.get();
while (ss.getline(attr_buf, 99999, '|'))
{
// cout<<attr_buf<<endl;
tuple.AddContent(attr_buf);
}
return {true, {table_name, tuple, is_insert ? Operation::insert : Operation::delete_}};
}
void PrepareData(Buffer &input_buf, const Query &query)
{
for (auto &[table_name, _] : query.table_name_2_index_info)
{
Operation::table_id_2_name.emplace_back(table_name);
}
ifstream static_opers, dynamic_opers;
static_opers.open(static_stream_fn);
Tuple tmp;
int valid_static_tuple_num = 0;
while (true)
{
auto [not_eof, oper] = ReadTupleFromStreamFile(static_opers, false);
if (!not_eof)
{
break;
}
input_buf.operations.emplace_back(oper);
}
static_opers.close();
dynamic_opers.open(dynamic_stream_fn);
assert(dynamic_opers.is_open());
while (true)
{
auto [not_eof, oper] = ReadTupleFromStreamFile(dynamic_opers, true);
if (!not_eof)
break;
input_buf.operations.emplace_back(oper);
}
dynamic_opers.close();
return;
}
pair<unsigned, unsigned> DB::GetNumAnswersAndPeak()
{
return {answer_num, answer_set_peak_size};
}
#endif // CONJUNCTIVEQ_IO_H