-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdbUtils.js
More file actions
190 lines (149 loc) · 4.76 KB
/
Copy pathdbUtils.js
File metadata and controls
190 lines (149 loc) · 4.76 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
var child = require('child_process'),
fs = require('fs'),
http = require('http'),
events = require('events'),
eventEmitter = new events.EventEmitter(),
DB = require('node-cassandra-cql').Client,
os = require('os'),
dbhosts = [];//Database host
//debugger;
var usage = 'Usage: node dbUtils.js [action] <host>. Actions are: create, erase, drop, verify. Host is your Cassandra instance, default is local hostname.'
process.on('SIGINT',exit);
//Verify arguments
if (process.argv.length < 3) {
console.log(usage);
return;
}
if (process.argv[3]) {
var hostname = process.argv[3];
dbhosts.push(hostname);
var db = new DB({hosts:dbhosts,keyspace:'iomapper'});
}
else{
var hostname = os.hostname();
dbhosts.push(hostname);
var db = new DB({hosts:dbhosts,keyspace:'iomapper'});
}
var action = process.argv[2];
switch(action){
case 'erase':
eraseDb();
break;
case 'create':
console.log('Not implemented yet');
break;
case 'drop':
console.log('Not implemented yet');
break;
case 'verify':
console.log('Not implemented yet');
break;
default:
console.log(usage);
return;
}
function exit(){
db.shutdown(function(){console.log("Shutting down database connection");
console.log("about to exit");
process.exit();
})
}
/*
* Delete ranges: Cassandra doesn't support range deletion
* Delete ranges: select the range, and run the following on the result
*
for(var z = 0; z < result.rows.length;z++){
var ts = result.rows[z]['ts'];
var query = "delete from io where date = '3-23-2015' and host in('id_564deac3','id_217a965c','id_ca86045b','id_ec4f352c','id_18348163','id_80d3e154','id_ab2dc88d') and ts = ?";
var props = [ts];
db.execute(query,props,function(err,res){if (err){console.log(err)}});
}
var query = "select html_id,ts from mapper where html_id in('id_564deac3','id_217a965c','id_ca86045b','id_ec4f352c','id_18348163','id_80d3e154','id_ab2dc88d','id_clientContainer_id_cluster_default','id_cluster_default') and ts >= '2015-03-23 18:37:00-0700'";
for(var z = 0; z < result.rows.length;z++){
var ts = result.rows[z]['ts'];
var query = "delete from mapper where html_id in('id_564deac3','id_217a965c','id_ca86045b','id_ec4f352c','id_18348163','id_80d3e154','id_ab2dc88d','id_clientContainer_id_cluster_default','id_cluster_default') and ts = ?";
var props = [ts];
db.execute(query,props,function(err,res){if (err){console.log(err)}});
}
*/
function eraseDb(){
var status = {'map':0,'io':0};
eventEmitter.on('done',function(){
done = 1;
for (var task in status){
if (status[task] == 0) {
done = 0;
}
}
if (done == 1) {
console.log('All tasks done, exiting...');
exit();
}
})
eraseIo();
eraseMapper();
function eraseIo(){
console.log('Erasing IO...')
var query = 'select date,host from io limit 50';
var props = [];
db.execute(query,props,function(err,result){
if (err) {console.log('ERROR: ',err);return}
if (result.rows.length == 0) {
//No more items left, exiting
console.log('No more IO items left, exiting...');
status.io = 1;
eventEmitter.emit('done');
return;
}
debugger;
var date = result.rows[0]['date'];
var hosts = [];
for(var a = 0; a < result.rows.length; a++){
hosts.push(result.rows[a]['host']);
}
hosts = hosts.filter (function (v, i, a) { return a.indexOf (v) == i });
var props = [date].concat(hosts);
var params = [];
for (var a = 0; a<hosts.length; a++){params.push('?')};
var params = params.join();
var query = 'delete from io where date=? and host in (' + params + ')';
db.execute(query,props,function(err,res){
if (err) {console.log('ERROR: ',err);return};
//Rinse and repeat
eraseIo();
})
})
}
function eraseMapper(){
console.log('Erasing Mapper...')
var query = 'select html_id from mapper limit 50';
var props = [];
db.execute(query,props,function(err,result){
if (err) {console.log('ERROR: ',err);return}
if (result.rows.length == 0) {
//No more items left, exiting
console.log('No more MAPPER items left, exiting...');
status.map = 1;
eventEmitter.emit('done');
return;
}
debugger;
//var date = result.rows[0]['date'];
var html_ids = [];
for(var a = 0; a < result.rows.length; a++){
html_ids.push(result.rows[a]['html_id']);
}
html_ids = html_ids.filter (function (v, i, a) { return a.indexOf (v) == i });
var props = html_ids;
var params = [];
for (var a = 0; a<html_ids.length; a++){params.push('?')};
var params = params.join();
var query = 'delete from mapper where html_id in (' + params + ')';
db.execute(query,props,function(err,res){
if (err) {console.log('ERROR: ',err);return};
//Rinse and repeat
eraseMapper();
})
})
}
}