-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikidata_db.cpp
198 lines (172 loc) · 5.51 KB
/
wikidata_db.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
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
#include "main.h"
#include <unistd.h>
TPlatform *root_platform ;
std::mutex g_root_platform_mutex;
//________________________________________________________________________________________________
#define DB_ESCAPE_BUFFER_SIZE 1000000
#define USE_DB_CONCURRENCY_LIMIT 0
#define MAX_CONCURRENT_DB 9
uint32_t concurrent_db = 0 ;
bool curl_initialized = false ;
std::mutex concurrent_db_mutex;
TWikidataDB::TWikidataDB ( string wiki , TPlatform *_platform ) {
while ( USE_DB_CONCURRENCY_LIMIT && concurrent_db >= MAX_CONCURRENT_DB ) {
cout << "DB concurrency limit reached" << endl ;
usleep ( 1000 ) ;
}
if ( 1 ) {
std::lock_guard<std::mutex> guard(concurrent_db_mutex) ;
concurrent_db++ ;
}
//cout << "Concurrent DBs: " << concurrent_db << endl ;
platform = _platform ;
setHostDBFromWiki ( wiki ) ;
if ( !curl_initialized ) {
curl_global_init(CURL_GLOBAL_ALL);
curl_initialized = true ;
}
doConnect(true) ;
}
bool TWikidataDB::setHostDBFromWiki ( string wiki ) {
_wiki = wiki ;
vector <string> parts = { "quote" , "source" , "books" , "source" , "news" , "versity" , "voyage" } ;
for ( auto p: parts ) {
stringReplace ( wiki , "wiki"+p+"wiki" , "wiki"+p ) ;
}
stringReplace ( wiki , "-" , "_" ) ;
stringReplace ( wiki , "wikispecieswiki" , "specieswiki" ) ;
stringReplace ( wiki , "specieswikimedia" , "specieswiki" ) ;
stringReplace ( wiki , "wiktionarywiki" , "wiktionary" ) ;
stringReplace ( wiki , "be_taraskwiki" , "be_x_oldwiki" ) ;
if ( 0 ) { // Future special cases
} else {
// _host = wiki+".web.db.svc.eqiad.wmflabs" ; // Fast ones
_host = wiki+".analytics.db.svc.eqiad.wmflabs" ; // New, long-query server, use this if possible
// _host = wiki+".labsdb" ; // Old host, try not to use
_database = wiki+"_p" ;
}
}
bool TWikidataDB::isConnected() {
return did_connect ;
}
void TWikidataDB::doConnect ( bool first ) {
if ( !first ) {
int i = mysql_ping ( &mysql ) ;
if ( i == 0 ) return ; // Connection is already up
if ( string("MySQL server has gone away") == string(mysql_error(&mysql)) ) return doConnect(true) ;
// if ( i != CR_SERVER_GONE_ERROR ) {
finishWithError() ;
// cerr << "MySQL PING says: " << i << endl ;
// exit ( 0 ) ;
// }
}
std::lock_guard<std::mutex> guard(g_root_platform_mutex);
if ( root_platform == NULL ) {
cout << "Root platform not set\n" ;
exit ( 1 ) ;
}
string user = root_platform->config["user"] ;
string password = root_platform->config["password"] ;
mysql_init(&mysql);
if ( _force_utf8 ) {
mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "utf8");
mysql_options(&mysql, MYSQL_INIT_COMMAND, "SET NAMES utf8");
}
if ( mysql_real_connect(&mysql,_host.c_str(),user.c_str(),password.c_str(),_database.c_str(),0,NULL,0) ) {
did_connect = true ;
} else {
/*
string error = mysql_error(&mysql) ;
if ( error.find("max_user_connections") != string::npos ) {
//cout << "WAITING FOR USER CONNECTION" << endl ;
usleep ( 100 ) ;
continue;
}
did_connect = false ;
*/
finishWithError() ;
}
// printf("MySQL Server Version is %s\n",mysql_get_server_info(&mysql));
}
TWikidataDB::~TWikidataDB () {
mysql_close(&mysql);
// curl_global_cleanup();
concurrent_db-- ;
}
string TWikidataDB::escape ( string s ) {
unsigned long len = s.length() * 2 + 1 ;
if ( len >= DB_ESCAPE_BUFFER_SIZE ) {
cerr << "DB:Escape for\n" << s << "\nrequires " << len << " bytes, but limit is " << DB_ESCAPE_BUFFER_SIZE << endl ;
return "" ;
}
char tmp[DB_ESCAPE_BUFFER_SIZE] ; // Would prefer "new" below, but that dies for some reason on "delete"
// char *tmp = new char ( len ) ;
long unsigned int end = mysql_real_escape_string ( &mysql , tmp , s.c_str() , s.length() ) ;
string ret ( tmp ) ;
// delete [] tmp ;
return ret ;
}
void TWikidataDB::runQuery ( string sql ) {
doConnect() ;
int result ;
try {
result = mysql_query ( &mysql , sql.c_str() ) ;
} catch ( ... ) {
result = 1 ;
}
if ( result ) {
if ( string("MySQL server has gone away") == string(mysql_error(&mysql)) ) {
doConnect(true) ;
return runQuery ( sql ) ;
}
finishWithError() ;
}
}
MYSQL_RES *TWikidataDB::getQueryResults ( string sql ) {
try {
runQuery ( sql ) ;
} catch ( ... ) {
fprintf(stderr, "!! %s\n", mysql_error(&mysql));
finishWithError("SQL problem [1]:",sql) ;
return NULL ;
}
MYSQL_RES *result ;
try {
result = mysql_store_result(&mysql);
} catch ( ... ) {
fprintf(stderr, "!! %s\n", mysql_error(&mysql));
finishWithError("SQL problem [2]:",sql) ;
return NULL ;
}
if ( result == NULL ) {
if ( string("MySQL server has gone away") == string(mysql_error(&mysql)) ) {
usleep ( 100 ) ;
doConnect(true) ;
return getQueryResults ( sql ) ;
}
if ( string("Lost connection to MySQL server during query") == string(mysql_error(&mysql)) ) {
usleep ( 100 ) ;
doConnect(true) ;
return getQueryResults ( sql ) ;
}
fprintf(stderr, "!! %s\n", mysql_error(&mysql));
string error_message = mysql_error(&mysql) ;
finishWithError("SQL problem [3]: "+error_message,sql) ;
}
return result ;
}
void TWikidataDB::finishWithError ( string msg , string sql ) {
// if ( platform ) platform->error ( _wiki+": "+msg+"\n"+string(mysql_error(&mysql)) ) ;
if ( msg.empty() ) fprintf(stderr, "%s\n", mysql_error(&mysql));
else fprintf(stderr, "%s:%s\n", _wiki.c_str(),msg.c_str());
// if ( !sql.empty() ) cout << sql << endl ;
mysql_close(&mysql);
}
void TWikidataDB::setHostDB ( string host , string db , bool force_utf8 ) {
_host = host ;
_database = db ;
_force_utf8 = force_utf8 ;
}
uint32_t TWikidataDB::lastInsertID () {
return mysql_insert_id ( &mysql ) ;
}