-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
162 lines (125 loc) · 4.17 KB
/
example.cpp
File metadata and controls
162 lines (125 loc) · 4.17 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
// Copyright 2021 Your Name <your_email>
#include <algorithm>
#include <iostream>
#include <set>
#include <sstream>
#include <example.hpp>
void Log::Write(std::string_view message) const {
*out_ << message << std::endl;
}
void Log::WriteDebug(std::string_view message) const {
if (level_ > 0) *out_ << message << std::endl;
}
Log::Log(size_t level) : level_(level) { out_ = &std::cout; }
constexpr size_t kMinLines = 10;
UsedMemory::UsedMemory(const Log& log) : log_(&log) {}
void UsedMemory::OnDataLoad(const std::vector<Item>& old_items,
const std::vector<Item>& new_items) {
log_->WriteDebug("UsedMemory::OnDataLoad");
for (const auto& item : old_items) {
used_ -= item.id.capacity();
used_ -= item.name.capacity();
used_ -= sizeof(item.score);
}
for (const auto& item : new_items) {
used_ += item.id.capacity();
used_ += item.name.capacity();
used_ += sizeof(item.score);
}
log_->Write("UsedMemory::OnDataLoad: new size = " + std::to_string(used_));
}
void UsedMemory::OnRawDataLoad(const std::vector<std::string>& old_items,
const std::vector<std::string>& new_items) {
log_->WriteDebug("UsedMemory::OnRawDataLoads");
for (const auto& item : old_items) {
used_ -= item.capacity();
}
for (const auto& item : new_items) {
used_ += item.capacity();
}
log_->Write("UsedMemory::OnDataLoad: new size = " + std::to_string(used_));
}
size_t UsedMemory::used() const { return used_; }
StatSender::StatSender(const Log& log) : log_(&log) {}
void StatSender::OnLoaded(const std::vector<Item>& new_items) {
log_->WriteDebug("StatSender::OnDataLoad");
AsyncSend(new_items, "/items/loaded");
}
void StatSender::Skip(const Item& item) { AsyncSend({item}, "/items/skiped"); }
void StatSender::AsyncSend(
const std::vector<Item>& items,
std::string_view path) {
log_->Write(path);
log_->Write("send stat " + std::to_string(items.size()));
for (const auto& item : items) {
log_->WriteDebug("send: " + item.id);
// ... some code
fstr << item.id << item.name << item.score;
fstr.flush();
}
}
void PageContainer::Load(std::istream& io, float threshold) {
std::vector<std::string> raw_data;
while (!io.eof()) {
std::string line;
std::getline(io, line, '\n');
raw_data.push_back(std::move(line));
}
if (raw_data.size() < kMinLines) {
throw std::runtime_error("too small input stream");
}
memory_counter_->OnRawDataLoad(raw_data_, raw_data);
raw_data_ = std::move(raw_data);
std::vector<Item> data;
std::set<std::string> ids;
for (const auto& line : raw_data_) {
std::stringstream stream(line);
Item item;
stream >> item.id >> item.name >> item.score;
if (auto&& [_, inserted] = ids.insert(item.id); !inserted) {
throw std::runtime_error("already seen");
}
if (item.score > threshold) {
data.push_back(std::move(item));
} else {
stat_sender_.Skip(item);
}
}
if (data.size() < kMinLines) {
throw std::runtime_error("oops");
}
memory_counter_->OnDataLoad(data_, data);
stat_sender_.OnLoaded(data);
data_ = std::move(data);
}
const Item& PageContainer::ByIndex(size_t i) const { return data_[i]; }
const Item& PageContainer::ById(const std::string& id) const {
auto it = std::find_if(std::begin(data_), std::end(data_),
[&id](const auto& i) { return id == i.id; });
return *it;
}
void PageContainer::Reload(float threshold) {
std::vector<Item> data;
std::set<std::string> ids;
for (const auto& line : raw_data_) {
std::stringstream stream(line);
Item item;
stream >> item.id >> item.name >> item.score;
if (auto&& [_, inserted] = ids.insert(item.id); !inserted) {
throw std::runtime_error("already seen");
}
if (item.score > threshold) {
data.push_back(std::move(item));
} else {
stat_sender_.Skip(item);
}
}
if (data.size() < kMinLines) {
throw std::runtime_error("oops");
}
memory_counter_->OnDataLoad(data_, data);
stat_sender_.OnLoaded(data);
data_ = std::move(data);
}
PageContainer::PageContainer(const Log& log, UsedMemory* memory_counter)
: log_(&log), memory_counter_(memory_counter), stat_sender_(*log_) {}