-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueryProcessor.cpp
170 lines (120 loc) · 3.91 KB
/
QueryProcessor.cpp
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
//
// Created by Matthieu Rudelle on 27/05/16.
//
#include "QueryProcessor.h"
#include "QueryAggregator.h"
#include <queue>
using namespace std;
struct pointer {
int featureID;
int currPos;
int currFileId;
int end;
bool exhausted;
};
struct LessThanByFileId
{
bool operator()(const pointer& lhs, const pointer& rhs) const
{
if (lhs.exhausted) {
return true;
}
if (rhs.exhausted) {
return false;
}
return lhs.currFileId > rhs.currFileId;
}
};
QueryProcessor::QueryProcessor(FeatureStore *str, FeatureIndex *idx) {
store = str;
index = idx;
}
void QueryProcessor::process(int qLength, int q[], int take, int from, vector<agg_result>* out, int* matchedFeatures, int* matchedFiles) {
std::priority_queue<pointer, std::vector<pointer>, LessThanByFileId> pointers;
max_heap_t bestResults;
int totFeatureCount = 0;
int totFileCount = 0;
// build pointers that will perform the query
for (int i = 0; i < qLength; i++) {
int start;
int end;
index->getRange(q[i], &start, &end);
printf("feature %d has range %d to %d ... %d elements\n", q[i], start, end, end-start);
pointers.push({
q[i],
start,
store->at(start)->file_id,
end,
(start >= end) // pointer migth be exhausted from start
});
}
pointer currPointer;
db_feature* currFeature;
QueryAggregator* aggregator = nullptr;
int readHead;
int currFileID = -1;
while (!pointers.top().exhausted) {
currPointer = pointers.top();
readHead = currPointer.currPos;
currFeature = store->at(readHead);
// When we reach a new file we start a new aggregator
if (currFileID != currPointer.currFileId && aggregator) {
// TODO: finalize and append current aggregator
agg_result* agg = new agg_result();
aggregator->finalize(qLength, agg);
insertMaxHeap(agg, take+from, &bestResults);
delete aggregator;
aggregator = nullptr;
}
currFileID = currPointer.currFileId;
while(currFeature->file_id == currFileID && currFeature->feature_id == currPointer.featureID) {
totFeatureCount+=1;
// do something with store->at(readHead)
if (!aggregator) {
aggregator = new QueryAggregator(currFeature);
// means that we found a new file matching query
totFileCount ++;
} else {
aggregator->addFeature(currFeature);
}
currFeature = store->at(++readHead);
}
pointers.pop();
pointers.push({
currPointer.featureID,
readHead,
store->at(readHead)->file_id,
currPointer.end,
(readHead >= currPointer.end)
});
}
// add the final aggregation result
if (aggregator) {
agg_result* agg = new agg_result();
aggregator->finalize(qLength, agg);
insertMaxHeap(agg, take+from, &bestResults);
delete aggregator;
aggregator = nullptr;
}
// fill the output array with the list of best results
for (int j = 0; j < bestResults.size() - take; ++j) {
bestResults.pop();
}
while (!bestResults.empty()) {
out->emplace_back(*bestResults.top());
bestResults.pop();
}
// return the number of results
*matchedFeatures = totFeatureCount;
*matchedFiles = totFileCount;
}
void QueryProcessor::insertMaxHeap(agg_result *in, int maxSize, max_heap_t *maxHeap) {
if (maxHeap->size() < maxSize) {
maxHeap->push(in);
} else if (maxHeap->top()->score < in->score) {
// unallocate this aggregated result
delete maxHeap->top();
maxHeap->pop();
maxHeap->push(in);
}
}