forked from adamjodlowski/tweet-perf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase-mysql.js
255 lines (176 loc) · 5.25 KB
/
database-mysql.js
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* Database module based on 'mysql' https://github.com/felixge/node-mysql
*/
var MySQLPool = require("mysql-pool").MySQLPool;
var config = require('./config');
var clients = [];
var USERS = 'users';
var STATUSES = 'statuses';
var FOLLOWERS = 'followers';
var LIMIT = 20;
/*
* Database schema:
* users: id, name, screen_name
* statuses: id, user_id, text, created_at
* followers: user_id, follower_id
*/
// tweets are partitioned into databases
var DATABASE0 = 'twitter1';
var DATABASE1 = 'twitter2';
var DATABASE2 = 'twitter3';
var DATABASE3 = 'twitter4';
clients.push(new MySQLPool({
poolSize: 20,
user: config.user,
password: config.password,
host: config.host,
port: config.port,
database: DATABASE0
}));
clients.push(new MySQLPool({
poolSize: 20,
user: config.user,
password: config.password,
host: config.host,
port: config.port,
database: DATABASE1
}));
clients.push(new MySQLPool({
poolSize: 20,
user: config.user,
password: config.password,
host: config.host,
port: config.port,
database: DATABASE2
}));
clients.push(new MySQLPool({
poolSize: 20,
user: config.user,
password: config.password,
host: config.host,
port: config.port,
database: DATABASE3
}));
//------------------------------------------------------------------------------
function Database() {
};
exports.Database = Database;
Database.prototype.selectTweets = function (username, callback) {
var counter = 0;
var full_results = [];
var joinTweets = function(results) {
full_results = full_results.concat(results);
counter++;
if (counter == 4) {
callback (full_results);
}
}
for (i = 0; i < 4; i++) {
clients[i].query(
'SELECT * FROM ' + STATUSES + ' s INNER JOIN ' + USERS + ' u ON s.user_id = u.id WHERE screen_name LIKE ? ORDER BY s.created_at LIMIT ' + LIMIT,
[username],
function(err, results, fields) {
joinTweets(results);
}
);
}
}
Database.prototype.insertTweet = function (username, status, callback) {
var now = new Date();
clients[0].query(
'INSERT INTO ' + STATUSES + ' SET id = ?, user_id = ?, text = ?, created_at = ?',
[now.toString(), username, status, now],
function(err, result) {
callback ({'created_at': now.toString(), 'id': result.insertId});
});
};
Database.prototype.findUsers = function (callback) {
var counter = 0;
var full_results = [];
var joinUsers = function(results) {
full_results = full_results.concat(results);
counter++;
if (counter >= 4) {
callback (full_results);
}
}
for (var i = 0; i < 4; i++) {
clients[i].query(
'SELECT * FROM ' + USERS,
function(err, results, fields) {
joinUsers(results);
}
);
}
};
Database.prototype.selectTimeline = function (username, callback) {
var counter = 0;
var followers_size = 0;
var full_results = [];
var joinTweets = function(results) {
full_results = full_results.concat(results);
counter++;
if (counter >= 4*followers_size) {
callback (full_results);
}
}
var counter = 0;
var full_followers = [];
var joinFollowers = function(followers) {
full_followers = full_followers.concat(followers);
counter++;
if (counter == 4) {
followers_size = full_followers.length;
for (k = 0; k < followers_size; k++) {
for (l = 0; l < 4; l++) {
clients[l].query(
'SELECT * FROM ' + STATUSES + ' WHERE user_id = ? ORDER BY created_at LIMIT ' + LIMIT,
[full_followers[k]],
function(err, results, fields) {
joinTweets(results);
}
);
}
}
}
}
for (i = 0; i < 4; i++) {
clients[i].query(
'SELECT * FROM ' + FOLLOWERS + ' f INNER JOIN ' + USERS + ' u ON f.follower_id = u.id WHERE screen_name LIKE ?',
[username],
function(err, results, fields) {
var temp_followers = [];
for (j = 0; j < results.length; j++) {
temp_followers.push(results[j].user_id);
}
joinFollowers(temp_followers);
}
);
}
};
Database.prototype.findFollowers = function (username, callback) {
var counter = 0;
var followers_size = 0;
var counter = 0;
var full_followers = [];
var joinFollowers = function(followers) {
full_followers = full_followers.concat(followers);
counter++;
if (counter == 4) {
callback (full_followers);
}
}
for (i = 0; i < 4; i++) {
clients[i].query(
'SELECT * FROM ' + FOLLOWERS + ' f INNER JOIN ' + USERS + ' u ON f.follower_id = u.id WHERE screen_name LIKE ?',
[username],
function(err, results, fields) {
var temp_followers = [];
for (j = 0; j < results.length; j++) {
temp_followers.push(results[j].user_id);
}
joinFollowers(temp_followers);
}
);
}
};