-
Notifications
You must be signed in to change notification settings - Fork 1
/
tap.js
172 lines (148 loc) · 4.75 KB
/
tap.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
// Example usage:
//
// node tap.js -s 127.0.0.1:11210
// node tap.js -s 127.0.0.1:11210 -e "dump(item)"
// node tap.js -s 127.0.0.1:11210 -e "dumpAll(item)"
// node tap.js -s 127.0.0.1:11210 -e 'if (item.key == "hi") { sys.puts("world " + item.data); }'
// node tap.js -s 127.0.0.1:11210 -e 'if (item.key.indexOf("cust:") == 0) { emit(item.key, "1"); }'
//
var sys = require('sys'),
net = require('net'),
repl = require('repl'),
http = require('http'),
Buffer = require('buffer').Buffer;
var n = require('./ntoh'),
mc = require('./mc');
var query = require('./query');
// ------------------------------------
function dumpAll(r) {
sys.puts("----");
sys.puts("magic: " + r.magic);
sys.puts("opcode: " + r.opcode);
sys.puts("keylen: " + r.keylen);
sys.puts("extlen: " + r.extlen);
sys.puts("status: " + r.statusOrReserved);
sys.puts("bodylen: " + r.bodylen);
sys.puts("datalen: " + r.datalen);
sys.puts("ext: " + r.ext);
dump(r);
}
function dump(r) {
sys.puts("--");
sys.puts("key: " + r.key);
sys.puts("data: " + r.data);
}
function emit(key, value) {
value = value || '';
sys.print("set ");
sys.print(key);
sys.print(" 0 0 " + value.length);
sys.print("\r\n");
sys.print(value.toString());
sys.print("\r\n");
}
// ------------------------------------
var executeBefore = null;
var executeDuring = null;
var executeAfter = null;
var servers = [];
var verbose = 0;
for (var i = 2; i < process.argv.length; i++) {
var arg = process.argv[i];
if (arg == '-s') {
var hp = process.argv[++i].split(':');
servers.push({ host: hp[0],
port: hp[1] || 11211,
port_proxy: hp[1] || 11211,
port_direct: hp[2] || ((hp[1] || 11211) - 1) });
}
if (arg == '-b') {
executeBefore = process.argv[++i];
}
if (arg == '-e') {
executeDuring = process.argv[++i];
}
if (arg == '-a') {
executeAfter = process.argv[++i];
}
if (arg == '-v') {
verbose++;
}
if (arg == '-h' || arg == '-?' || arg == '--help') {
usage();
}
}
function usage() {
sys.puts("tap.js - run javascript code against the items in a membase node\n");
sys.puts(" usage: " + process.argv[0] + " tap.js" +
" [-b 'before javascript code']" +
" [-e 'during javascript code']" +
" [-a 'after javascript code']" +
" [-v [-v [-v]]]\n" +
" -s mchost[:11211[:11210]]" +
" [-s mchost2[:11211[:11210]]] ...\n");
sys.puts(" -s specifies another membase host:port target to hit,");
sys.puts(" using the format hostname[:proxy_port[:direct_port]]");
sys.puts(" -v specifies more verbosity.\n");
process.exit(-1);
}
if (servers.length <= 0) {
sys.puts("tap.js needs at least one server: " +
process.argv[0] + " tap.js " + "-s <host>");
process.exit(-1);
}
// ------------------------------------
var actionBefore = eval('(function(ctx) {' +
executeBefore + '})');
var actionDuring = eval('(function(item, ctx) {' +
(executeDuring || "dump(item)") + '})');
var actionAfter = eval('(function(ctx) {' +
(executeAfter || "sys.puts('----')") + '})');
function makeResponseProcessor(stream, actionCtx) {
var leftOver = null;
return function(msg) {
if (leftOver) {
msg = leftOver + msg;
leftOver = null;
}
var cur = 0;
while (cur < msg.length) {
var res = mc.unpackMsgStr(msg, cur);
if (res == -1) {
leftOver = (leftOver || '') + msg.slice(cur);
return; // Need to read more bytes.
}
if (res != null &&
res.opcode == mc.CMD_TAP_MUTATION) {
actionDuring(res, actionCtx);
}
cur = cur + mc.SIZEOF_HEADER + res.bodylen;
}
}
}
for (var i = 0; i < servers.length; i++) {
var stream = net.createConnection(servers[i].port_direct,
servers[i].host);
tapStream(stream);
}
function tapStream(stream) {
var bufExt = new Buffer(4);
n.htonl(bufExt, 0, mc.TAP_CONNECT_FLAG_DUMP);
var actionCtx = {};
stream.setEncoding('binary');
stream.addListener('connect',
function() {
actionBefore(actionCtx);
var req = mc.packRequest(mc.CMD_TAP_CONNECT,
null,
bufExt,
0, 0,
null);
stream.write(req);
});
stream.addListener('data',
makeResponseProcessor(stream,
actionCtx));
stream.addListener('end',
function() { actionAfter(actionCtx); });
}