-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrethinkdb.js
242 lines (225 loc) · 6.67 KB
/
rethinkdb.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
// A fork of the [node.js chat app](https://github.com/eiriksm/chat-test-2k)
// by [@orkj](https://twitter.com/orkj) using socket.io, rethinkdb, passport and bcrypt on an express app.
//
// See the [GitHub README](https://github.com/rethinkdb/rethinkdb-example-nodejs-chat/blob/master/README.md)
// for details of the complete stack, installation, and running the app.
var r = require('rethinkdb')
, util = require('util')
, assert = require('assert')
, logdebug = require('debug')('rdb:debug')
, logerror = require('debug')('rdb:error');
// #### Connection details
// RethinkDB database settings. Defaults can be overridden using environment variables.
var dbConfig = {
host: process.env.RDB_HOST || 'localhost',
port: parseInt(process.env.RDB_PORT) || 28015,
db : process.env.RDB_DB || 'dht_crawler',
tables: {
'crawls': 'id',
'crawl_queue': 'id',
'magnets': 'id'
}
};
module.exports.setup = function() {
r.connect({host: dbConfig.host, port: dbConfig.port }, function (err, connection) {
assert.ok(err === null, err);
r.dbCreate(dbConfig.db).run(connection, function(err, result) {
if(err) {
logdebug("[DEBUG] RethinkDB database '%s' already exists (%s:%s)\n%s", dbConfig.db, err.name, err.msg, err.message);
}
else {
logdebug("[INFO ] RethinkDB database '%s' created", dbConfig.db);
}
for(var tbl in dbConfig.tables) {
(function (tableName) {
r.db(dbConfig.db).tableCreate(tableName, {primaryKey: dbConfig.tables[tbl]}).run(connection, function(err, result) {
if(err) {
logdebug("[DEBUG] RethinkDB table '%s' already exists (%s:%s)\n%s", tableName, err.name, err.msg, err.message);
}
else {
logdebug("[INFO ] RethinkDB table '%s' created", tableName);
}
});
})(tbl);
}
});
});
};
/**
* Get all magnets in the table
*
* @param {Function} callback
* callback invoked after collecting all the results
*
* @returns {Array}
*/
module.exports.getMagnets = function (callback) {
onConnect(function (err, connection) {
r.db(dbConfig['db'])
.table('magnets')
.run(connection, function(err, cursor){
if(err) throw err;
cursor.toArray(function(err, result) {
if(err) throw err;
callback(err, result);
})
});
});
};
/**
* Insert a new magnet to crawl.
*
* RethinkDB will use the primary key index to fetch the result.
*
* @param {String} name
* The description of the torrent. optional
*
* @param {String} infoHash
* The infoHash of the torrent.
*
* @param {Boolean} shouldCrawl
* Whether the torrent should be crawled.
*
* @param {Function} callback
* callback invoked after collecting all the results
*
* @returns {Object} the user if found, `null` otherwise
*/
module.exports.insertMagnet = function (name, infoHash, shouldCrawl, callback) {
onConnect(function (err, connection) {
r.db(dbConfig['db'])
.table('magnets')
.insert(
{
name: name,
infoHash: infoHash,
shouldCrawl: shouldCrawl
}).run(connection, function(err, result){
if(err) throw err;
callback(null, []);
});
});
}
/**
* Insert a new crawl result
*
* @param {String} infoHash
* The infoHash of the torrent.
*
* @param {Number} time
* The time in ms when the crawl was finished
*
* @param {Array} peers
* The peers. includes ip addr and geoloc: ip, country, region, city, ll
*
* @param {Function} callback
* callback invoked after collecting all the results
*
* @returns {Array} if success, if not null.
*/
module.exports.insertCrawl = function (infoHash, time, peers, callback) {
onConnect(function (err, connection) {
r.db(dbConfig['db']).table('crawls').insert([
{
time: time,
infoHash: infoHash,
peers: peers
}
]).run(connection, function(err, result){
if(err) throw err;
callback(null, []);
});
});
};
module.exports.newQueue = function (callback) {
onConnect(function (err, connection) {
r.db(dbConfig['db'])
.table('crawl_queue')
.delete()
.run(connection, function(err, res){
if(err) throw err;
r.db(dbConfig['db'])
.table('crawl_queue')
.insert(
r.db(dbConfig['db'])
.table('magnets')
.filter(r.row('shouldCrawl').eq(true))
)
.run(connection, function(err, res){
//inserted
if(err) throw err;
callback(err, res);
r.db(dbConfig['db']).table("crawl_queue")
.filter(r.row('shouldCrawl').eq(true))
.count()
.run(connection, function(err, res){
if(err) throw err;
//console.log("Beginning crawl queue with "+res+" magnets.")
});
})
});
});
};
module.exports.nextCrawl = function(magnetCount, callback){
onConnect(function (err, connection) {
r.db(dbConfig['db']).table("crawl_queue")
.filter({'shouldCrawl':true})
.limit(magnetCount)
.run(connection, function(err, res){
if(err) throw err;
res.toArray(function(err2, res2){
var ret = res2;
r.db(dbConfig['db']).table("crawl_queue")
.filter({'shouldCrawl':true})
.update({'shouldCrawl':false})
.run(connection, function(err3, res3){
callback(err, ret)
});
});
});
});
};
module.exports.delete = function(infoHash, callback){
onConnect(function (err, connection) {
r.db(dbConfig['db']).table("magnets")
.filter(r.row('infoHash').eq(infoHash))
.delete()
.run(connection, function(err, res){
if(err) throw err;
callback(err, res);
});
});
};
module.exports.export = function(callback){
onConnect(function (err, connection) {
callback(err, connection, r.db(dbConfig['db']));
});
};
// #### Helper functions
/**
* A wrapper function for the RethinkDB API `r.connect`
* to keep the configuration details in a single function
* and fail fast in case of a connection error.
*/
function onConnect(callback) {
r.connect({host: dbConfig.host, port: dbConfig.port }, function(err, connection) {
assert.ok(err === null, err);
connection['_id'] = Math.floor(Math.random()*10001);
callback(err, connection);
});
}
// #### Connection management
//
// This application uses a new connection for each query needed to serve
// a user request. In case generating the response would require multiple
// queries, the same connection should be used for all queries.
//
// Example:
//
// onConnect(function (err, connection)) {
// if(err) { return callback(err); }
//
// query1.run(connection, callback);
// query2.run(connection, callback);
// }
//