-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessingTimeSlidingWindow.cpp
62 lines (48 loc) · 2.36 KB
/
ProcessingTimeSlidingWindow.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
#include "ProcessingTimeSlidingWindow.h"
#include <iostream>
#include <sstream>
using namespace Operator;
void _ProcessingTimeSlidingWindow::setInputSchema(std::shared_ptr<_Schema> inputSchema) {
_inputSchema = inputSchema;
_outputSchema = Schema(QueueType(ContainerType(TupleType(_inputSchema->getVariables()))));
}
std::shared_ptr<_Schema> _ProcessingTimeSlidingWindow::getOutputSchema() {
return _outputSchema;
}
std::string _ProcessingTimeSlidingWindow::getCode() {
auto queueIdentifier = _outputSchema->getOnly()->getVariableIdentifier();
std::stringstream stream;
// build tuple
stream << "auto newTuple = std::make_tuple(";
auto& variables = _inputSchema->getVariables();
for (int i = 0; i < variables.size() - 1; i++)
stream << variables[i]->getVariableIdentifier() << ",";
stream << variables.back()->getVariableIdentifier() << ");" << std::endl;
//evaluate if window closes
stream << "std::chrono::time_point<std::chrono::high_resolution_clock> currentTime = std::chrono::high_resolution_clock::now();"
<< std::endl;
stream << "while (std::chrono::time_point(" << _nextWindowClosesAtIdentifier << ") <= currentTime) {{"
<< std::endl
//update next window closing
<< _nextWindowClosesAtIdentifier << " += std::chrono::milliseconds(" << _slideSizeMilliseconds << ");" << std::endl
<< "while (" << queueIdentifier << ".size() > " << _slidesPerWindow << ") {{" << std::endl
<< queueIdentifier << ".pop_front(); " << std::endl
<< "}}" << std::endl
<< "{yield}"
<< queueIdentifier << ".emplace_back();"
<< "}}" << std::endl;
//push to queue
stream << queueIdentifier << ".back().push_back(newTuple);" << std::endl;
return stream.str();
}
std::set<std::string> _ProcessingTimeSlidingWindow::getHeaders() {
return {{"<queue>"}, {"<tuple>"}, {"<chrono>"}};
}
std::string _ProcessingTimeSlidingWindow::getGlobalDeclarations() {
std::stringstream stream;
stream << _outputSchema->getOnly()->getDeclaration() << std::endl;
stream << "std::chrono::time_point<std::chrono::high_resolution_clock> " << _nextWindowClosesAtIdentifier
<< " = std::chrono::high_resolution_clock::now() + "
<< "std::chrono::milliseconds(" << _slideSizeMilliseconds << ");" << std::endl;
return stream.str();
}