-
Notifications
You must be signed in to change notification settings - Fork 2
/
database-mysql.js
182 lines (126 loc) · 3.33 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
/*
* Database module based on 'mysql' https://github.com/felixge/node-mysql
*/
var mysql = require('mysql');
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';
var clients = [];
clients.push(mysql.createClient({
user: 'devcamp',
password: 'devcamp',
host: 'localhost',
port: 3306,
database: DATABASE0,
}));
clients.push(mysql.createClient({
user: 'devcamp',
password: 'devcamp',
host: 'localhost',
port: 3306,
database: DATABASE1,
}));
clients.push(mysql.createClient({
user: 'devcamp',
password: 'devcamp',
host: 'localhost',
port: 3306,
database: DATABASE2,
}));
clients.push(mysql.createClient({
user: 'devcamp',
password: 'devcamp',
host: 'localhost',
port: 3306,
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.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);
}
);
}
};