-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathql_stream.cpp
More file actions
236 lines (213 loc) · 5.12 KB
/
Copy pathql_stream.cpp
File metadata and controls
236 lines (213 loc) · 5.12 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
228
229
230
231
232
233
234
235
#include "ql_stream.h"
#include "ix_indexscan.h"
#include "ql_error.h"
using namespace std;
extern RMManager rmManager;
extern IXManager ixManager;
/*~~~~~~~~~~~~~~~~~~~~~~~~~IdxWrapper~~~~~~~~~~~~~~~~~~~~~~*/
IdxWrapper::IdxWrapper(const char* pathname, const DataAttr &idxattr, Operator op, const void* val,
int nattrs, const DataAttr attrs[], int nconds, const Condition conds[])
: Stream(nattrs, attrs, nconds, conds), pathname_(pathname)
{
rmManager.openFile(pathname_, file_);
ixManager.openIndex(pathname_, idxattr.idxno, index_); /* 打开索引文件 */
scan_.openScan(index_, op, val);
}
RC IdxWrapper::open()
{
/* 预处理好各种条件 */
for (int i = 0; i < conds_.size(); i++) {
DataAttr &attr = attrs_[indicator_[conds_[i].lhsAttr.attrname]];
loffsets_.push_back(attr.offset); /* 需要将偏移量记住,方便查询 */
comps_.push_back(make_comp(attr.type, attr.len));
}
return 0;
}
RC IdxWrapper::next(uint8_t *data)
{
bool passed = false;
RID rid;
RMRecord rcd;
RC errval;
Ptr ptr;
while (!passed) {
errval = scan_.getNextEntry(rid);
if (errval == IX_EOF) return QL_EOF;
file_->getRcd(rid, rcd);
ptr = rcd.rawPtr();
int i = 1;
/* 使用各种条件来过滤数据 */
for ( ; i < comps_.size(); i++) {
if (!comps_[i]->eval(ptr + loffsets_[i], conds_[i].op, conds_[i].rhsValue.data)) break;
}
if (i == comps_.size()) passed = true;
}
memcpy(data, ptr, rcdlen_);
return 0;
}
RC IdxWrapper::rewind()
{
return scan_.rewind();
}
RC IdxWrapper::close()
{
rmManager.closeFile(file_);
ixManager.closeIndex(index_);
}
IdxWrapper::~IdxWrapper()
{
for (int i = 0; i < comps_.size(); i++) {
delete comps_[i];
}
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~RcdWrapper~~~~~~~~~~~~~~~~~~~~~~*/
RC RcdWrapper::open()
{
/* step 1 -> 打开文件和RMFileScan */
rmManager.openFile(pathname_, file_);
if (conds_.size() > 0) {
/* 选择第一个条件 */
DataAttr &attr = attrs_[indicator_[conds_[0].lhsAttr.attrname]];
scan_.openScan(file_, attr.type, attr.len, attr.offset, conds_[0].op, conds_[0].rhsValue.data);
}
else {
/* 直接全表遍历 */
scan_.openScan(file_, INT, sizeof(int), 0, NO_OP, nullptr);
}
/* step2 -> 预处理好其余的条件,这里从0开始只是为了方便而已,不用在意 */
for (int i = 0; i < conds_.size(); i++) {
DataAttr &attr = attrs_[indicator_[conds_[i].lhsAttr.attrname]];
loffsets_.push_back(attr.offset);
comps_.push_back(make_comp(attr.type, attr.len));
}
}
//
// next - 获取下一条记录
//
RC RcdWrapper::next(uint8_t *data)
{
bool passed = false;
RMRecord rcd;
RC errval;
Ptr ptr;
while (!passed) {
errval = scan_.getNextRcd(rcd);
if (errval == RM_EOF) return QL_EOF;
ptr = rcd.rawPtr();
int i = 0;
for ( ; i < comps_.size(); i++) {
if (!comps_[i]->eval(ptr + loffsets_[i], conds_[i].op, conds_[i].rhsValue.data)) break;
}
if (i == conds_.size()) passed = true;
}
memcpy(data, ptr, rcdlen_);
return 0;
}
//
// rewind - 重新回到起点
//
RC RcdWrapper::rewind()
{
return scan_.rewind();
}
RC RcdWrapper::close()
{
scan_.closeScan();
rmManager.closeFile(file_);
}
RcdWrapper::~RcdWrapper()
{
for (int i = 0; i < comps_.size(); i++) {
delete comps_[i];
}
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~CombWrpperWrapper~~~~~~~~~~~~~~~~~~~~~~*/
RC CombWrapper::open()
{
lhs_->open();
rhs_->open();
/* step 1 -> 找到condition中左右操作数对应的attr */
const char* lrel, *rrel, *lattr, *rattr;
int idx;
for (int i = 0; i < conds_.size(); i++) {
lrel = conds_[i].lhsAttr.relname;
rrel = conds_[i].rhsAttr.relname;
lattr = conds_[i].lhsAttr.attrname;
rattr = conds_[i].rhsAttr.attrname;
for (int j = 0; j < attrs_.size(); j++) {
if (strcmp(lattr, attrs_[j].attrname) == 0 &&
strcmp(lrel, attrs_[j].relname) == 0) {
loffsets_.push_back(attrs_[j].offset);
idx = j;
}
else if (strcmp(rattr, attrs_[j].attrname) == 0 &&
strcmp(rrel, attrs_[j].relname) == 0) {
roffsets_.push_back(attrs_[j].offset);
}
}
assert(loffsets_.size() == roffsets_.size());
/* 构建一条过滤器 */
comps_.push_back(make_comp(attrs_[idx].type, attrs_[idx].len));
}
return 0;
}
//
// next - 获取下一条记录
//
RC CombWrapper::next(uint8_t *data)
{
bool passed = false;
RC errval;
uint8_t *rdata = data + lhs_->rcdlen_;
/* 刚开始的时候需要设定一下 */
if (ldata_ == nullptr) {
ldata_ = (uint8_t *)malloc(lhs_->rcdlen_);
errval = lhs_->next(ldata_);
if (errval == QL_EOF) return QL_EOF;
}
while (!passed) {
/* 先处理右侧 */
errval = rhs_->next(rdata);
if (errval == QL_EOF) {
rhs_->rewind();
errval = rhs_->next(rdata);
if (errval == QL_EOF) return QL_EOF;
errval = lhs_->next(ldata_);
if (errval == QL_EOF) return QL_EOF;
}
memcpy(data, ldata_, lhs_->rcdlen_);
int i = 0;
for ( ; i < comps_.size(); i++) {
if (!comps_[i]->eval(data + loffsets_[i], conds_[i].op, data + roffsets_[i]))
break;
}
if (i == comps_.size()) passed = true;
}
return 0;
}
RC CombWrapper::rewind()
{
lhs_->rewind();
rhs_->rewind();
return 0;
}
RC CombWrapper::close()
{
lhs_->close();
rhs_->close();
return 0;
}
CombWrapper::~CombWrapper()
{
for (int i = 0; i < comps_.size(); i++) {
delete comps_[i];
}
if (ldata_) free(ldata_);
}
void CombWrapper::clean()
{
lhs_->clean();
rhs_->clean();
delete lhs_;
delete rhs_;
}