-
Notifications
You must be signed in to change notification settings - Fork 2
/
db_instance.cpp
164 lines (137 loc) · 4.31 KB
/
db_instance.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: db_instance.cpp
* Author: reat
*
* Created on August 12, 2017, 3:38 PM
*/
#include "db_instance.h"
#include <fstream>
#include "rapidjson/document.h"
#include "unzipper.h"
namespace {
const std::string data_name("/tmp/data/data.zip");
std::vector<std::string> FileNames(const std::string& pattern) {
zipper::Unzipper unzipper(data_name);
std::vector<zipper::ZipEntry> entries = unzipper.entries();
std::vector<std::string> names;
for (auto& e : entries) {
if (e.name.find(pattern) == 0) {
names.push_back(e.name);
}
}
unzipper.close();
return std::move(names);
}
std::vector<unsigned char> GetFileContent(const std::string& filename) {
std::vector<unsigned char> unzipped_entry;
zipper::Unzipper unzipper(data_name);
unzipper.extractEntryToMemory(filename, unzipped_entry);
unzipper.close();
return std::move(unzipped_entry);
}
} // namespace
std::unique_ptr<DBInstance> DBInstance::instance_ = nullptr;
// static
DBInstance* DBInstance::GetDbInstance() {
if (!instance_) {
instance_.reset(new DBInstance);
}
return instance_.get();
}
void DBInstance::InitializeUsers() {
std::vector<std::string> file_names = FileNames("users_");
for (const auto& name : file_names) {
std::vector<unsigned char> buffer = GetFileContent(name);
rapidjson::Document d;
d.Parse((char*)buffer.data(), buffer.size());
rapidjson::Value& users = d["users"];
for (auto& u : users.GetArray()) {
User* user = new User({
u["first_name"].GetString(),
u["last_name"].GetString(),
u["birth_date"].GetInt64(),
u["email"].GetString(),
User::GetGender(u["gender"].GetString())
});
uint32_t id = u["id"].GetUint();
user->Serialize(id);
users_storage_.Add(id, user, nullptr);
email_index_.Add(std::string(u["email"].GetString()));
}
}
}
void DBInstance::InitializeLocations() {
std::vector<std::string> file_names = FileNames("locations_");
for (const auto& name : file_names) {
std::vector<unsigned char> buffer = GetFileContent(name);
rapidjson::Document d;
d.Parse((char*)buffer.data(), buffer.size());
rapidjson::Value& locations = d["locations"];
for (auto& l : locations.GetArray()) {
Location* location = new Location({
l["place"].GetString(),
l["country"].GetString(),
l["city"].GetString(),
l["distance"].GetUint()
});
uint32_t id = l["id"].GetUint();
location->Serialize(id);
locations_storage_.Add(id, location, nullptr);
}
}
}
void DBInstance::InitializeVisits() {
std::vector<std::string> file_names = FileNames("visits_");
for (const auto& name : file_names) {
std::vector<unsigned char> buffer = GetFileContent(name);
rapidjson::Document d;
d.Parse((char*)buffer.data(), buffer.size());
rapidjson::Value& visits = d["visits"];
for (auto& v : visits.GetArray()) {
Visit* visit = new Visit({
v["location"].GetUint(),
v["user"].GetUint(),
v["visited_at"].GetInt64(),
(char)v["mark"].GetInt()
});
uint32_t id = v["id"].GetUint();
visit->Serialize(id);
Visit* instance = nullptr;
uint32_t user_id = visit->user_id;
uint32_t location_id = visit->location_id;
visits_storage_.Add(id, visit, &instance);
users_visits_index_.Add(user_id, instance);
locations_visits_index_.Add(location_id, instance);
}
}
}
InMemoryStorage<User>* DBInstance::GetUsers() {
return &users_storage_;
}
InMemoryStorage<Location>* DBInstance::GetLocations() {
return &locations_storage_;
}
InMemoryStorage<Visit>* DBInstance::GetVisits() {
return &visits_storage_;
}
MultiIndex<Visit>* DBInstance::GetUsersVisitsIndex() {
return &users_visits_index_;
}
MultiIndex<Visit>* DBInstance::GetLocationsVisitsIndex() {
return &locations_visits_index_;
}
SimpleIndex<std::string>* DBInstance::GetEmailIndex() {
return &email_index_;
}
DBInstance::DBInstance()
: users_storage_(USERS_SIZE),
locations_storage_(LOCATIONS_SIZE),
visits_storage_(VISITS_SIZE),
users_visits_index_(USERS_SIZE),
locations_visits_index_(LOCATIONS_SIZE) {
}