-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulkmt.h
104 lines (83 loc) · 2.56 KB
/
bulkmt.h
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
#include <mutex>
#include <iostream>
#include <thread>
#include <condition_variable>
#include <fstream>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <cctype>
#include <vector>
#include <deque>
#include "commands_handler.h"
#include "commands_queue.h"
namespace bulkmt {
struct InfoStruct
{
uint32_t lines_counter = 0;
uint32_t commands_counter = 0;
uint32_t blocks_counter = 0;
};
std::condition_variable cv_logger;
std::condition_variable cv_file;
std::atomic_bool quit = false;
std::string parse_command(std::vector<std::string> command)
{
std::ostringstream commands_line;
if (!command.empty())
{
std::copy(command.begin(), command.end() - 1,
std::ostream_iterator<std::string>(commands_line, " "));
commands_line << command.back();
}
return commands_line.str();
}
void logger(std::queue<CommandsQueue> &queue, std::mutex &cv_m, std::promise<InfoStruct> &&info)
{
InfoStruct info_struct;
bool commands_queue_empty = false;
while (!quit.load() || !commands_queue_empty)
{
std::unique_lock<std::mutex> lk(cv_m);
cv_logger.wait(lk, [&]() { return (!queue.empty() || quit.load()); });
if(queue.empty()) {
commands_queue_empty = true;
continue;
}
auto com = queue.front().get_command();
std::string commands_string = parse_command(com);
queue.pop();
lk.unlock();
std::cout << "bulk: " << commands_string << std::endl;
info_struct.commands_counter += com.size();
info_struct.blocks_counter += 1;
}
info.set_value(info_struct);
}
void file_writer(std::queue<CommandsQueue> &queue, std::mutex &cv_m, std::promise<InfoStruct> &&info)
{
InfoStruct info_struct;
bool commands_queue_empty = false;
while (!quit.load() || !commands_queue_empty)
{
std::unique_lock<std::mutex> lk(cv_m);
cv_file.wait(lk, [&]() { return (!queue.empty() || quit.load()); });
if(queue.empty()) {
commands_queue_empty = true;
continue;
}
auto com = queue.front().get_command();
std::string commands_string = parse_command(com);
std::string file_name(queue.front().get_first_command_timestamp() + "_" + std::to_string(queue.front().get_unique_file_id()) + ".log");
queue.pop();
lk.unlock();
std::ofstream bulk_file;
bulk_file.open(file_name);
bulk_file << commands_string << std::endl;
bulk_file.close();
info_struct.commands_counter += com.size();
info_struct.blocks_counter += 1;
}
info.set_value(info_struct);
}
} // namespace bulkmt