-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.cpp
More file actions
231 lines (204 loc) · 5.47 KB
/
db.cpp
File metadata and controls
231 lines (204 loc) · 5.47 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "fs.h"
#include "db.hpp"
#include <sstream>
using namespace std;
using namespace pas;
const int BSIZE = 1024;
string query_columns;
string parameter_columns;
void InitPreparedStatement()
{
query_columns = "(";
parameter_columns = " values (";
size_t n = sizeof(track_column_names) / sizeof(string);
for (size_t i = 0; i < n; i++)
{
if (i > 0)
{
query_columns += ", ";
parameter_columns += ", ";
}
query_columns += track_column_names[i];
parameter_columns += "?";
}
query_columns += ") ";
parameter_columns += ") ";
}
bool TruncateTables(sql::Connection * connection, Logger & _log_)
{
bool rv = false;
sql::Statement * stmt = connection->createStatement();
try
{
if (stmt)
{
stmt->execute("truncate paths;");
stmt->execute("truncate tracks;");
delete stmt;
rv = true;
}
}
catch (sql::SQLException & e)
{
stringstream ss;
ss << "# ERR: SQLException in " << __FILE__;
ss << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
ss << "# ERR: " << e.what();
ss << " (MySQL error code: " << e.getErrorCode();
ss << ", SQLState: " << e.getSQLState() << " )" << endl;
throw LOG2(_log_, ss.str(), LogLevel::FATAL);
}
return rv;
}
string SanitizeString(string & s)
{
string rv;
rv.reserve(s.size() * 2);
for (size_t i = 0; i < s.size(); i++)
{
switch (s[i])
{
case '\'':
rv += "''";
break;
default:
rv += s[i];
break;
}
}
return rv;
}
bool InsertDirectory(sql::Connection * connection, DIRENT * d, string & nspace, Logger & _log_)
{
bool rv = false;
sql::Statement * stmt = connection->createStatement();
try
{
if (stmt)
{
string s;
s = "insert into paths (me, up, name, namespace) values(";
s += to_string(d->me) + ", ";
s += to_string(d->up) + ", ";
s += "\'" + SanitizeString(d->name) + "\', ";
s += "\'" + SanitizeString(nspace) + "\')";
s += ";";
stmt->execute(s.c_str());
rv = true;
}
else {
throw LOG2(_log_, "createStatement failed", LogLevel::FATAL);
}
}
catch (sql::SQLException & e)
{
stringstream ss;
ss << "# ERR: SQLException in " << __FILE__;
ss << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
ss << "# ERR: " << e.what();
ss << " (MySQL error code: " << e.getErrorCode();
ss << ", SQLState: " << e.getSQLState() << " )" << endl;
throw LOG2(_log_, ss.str(), LogLevel::FATAL);
}
return rv;
}
bool InsertTrack(sql::Connection * connection, DIRENT * d, string & nspace, Logger & _log_)
{
bool rv = false;
FILE * p;
if (access(d->fullpath.c_str(), R_OK) < 0)
{
cerr << "cannot access: " + d->fullpath << endl;
}
string cmdline = string("ffprobe -loglevel quiet -show_entries stream_tags:format_tags -show_entries format=duration -sexagesimal \"") + d->fullpath + string("\"");
if ((p = popen(cmdline.c_str(), "r")) == nullptr)
{
perror("pipe to ffprobe failed");
return rv;
}
char buffer[BSIZE] = { 0 };
Track track;
while (fgets(buffer, BSIZE, p) != nullptr)
{
string b(buffer);
track.SetTag(b);
memset(buffer, 0, BSIZE);
}
pclose(p);
size_t n = sizeof(track_column_names) / sizeof(string);
sql::PreparedStatement * stmt = nullptr;
string s = "insert into tracks " + query_columns + parameter_columns + ";";
try
{
stmt = connection->prepareStatement(s.c_str());
if (stmt == nullptr) {
throw LOG2(_log_, "prepareStatement() failed", LogLevel::FATAL);
}
// These three fields of the insert are data generated here in this
// program - they are the folder id of where this file lives, its
// file name within that folder and the namespace in which it is
// found.
stmt->setString(1, to_string(d->up));
stmt->setString(2, d->name);
stmt->setString(3, nspace);
// These fields come from ffprobe and are media related. The + 1
// to i is significant as mysql counts from 1, not zero.
for (size_t i = 3; i < n; i++) {
stmt->setString(i + 1, track.GetTag(track_column_names[i]));
}
stmt->execute();
delete stmt;
rv = true;
}
catch (sql::SQLException & e)
{
stringstream ss;
ss << "# ERR: SQLException in " << __FILE__;
ss << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
ss << "# ERR: " << e.what();
ss << " (MySQL error code: " << e.getErrorCode();
ss << ", SQLState: " << e.getSQLState() << " )" << endl;
throw LOG2(_log_, ss.str(), LogLevel::FATAL);
}
return rv;
}
void DoTheDB(string & nspace, vector<DIRENT> & t, string & dbhost, Logger & _log_)
{
InitPreparedStatement();
omp_set_num_threads(omp_get_max_threads());
sql::Driver * driver = nullptr;
sql::Connection * connections[(const int) omp_get_max_threads()] = { nullptr };
string dbh = "tcp://" + dbhost + ":3306";
if ((driver = get_driver_instance()))
{
cerr << "have driver instance" << endl;
for (int i = 0; i < omp_get_max_threads(); i++)
{
// NOTE:
// NOTE: Must catch SQL exceptions here but do not. This is a bug.
// NOTE:
connections[i] = driver->connect(dbh.c_str(), "pas", "pas");
connections[i]->setSchema("pas2");
LOG(_log_, "have connection and schema");
}
TruncateTables(connections[0], _log_);
#pragma omp parallel for
for (size_t i = 0; i < t.size(); i++)
{
if (t[i].type == DT_DIR)
InsertDirectory(connections[omp_get_thread_num()], &t[i], nspace, _log_);
else
InsertTrack(connections[omp_get_thread_num()], &t[i], nspace, _log_);
}
for (int i = 0; i < omp_get_max_threads(); i++)
{
connections[i]->close();
delete connections[i];
LOG(_log_, "deleted connection");
}
}
else
{
throw LOG2(_log_, "no driver instance", LogLevel::FATAL);
}
}