1 | | // Generated by CoffeeScript 1.4.0 |
2 | 1 | var EventEmitter, Stream, each, split, thrift, transport, |
3 | | __slice = [].slice; |
4 | | |
5 | 1 | Stream = require('stream'); |
6 | | |
7 | 1 | each = require('each'); |
8 | | |
9 | 1 | thrift = require('thrift'); |
10 | | |
11 | 1 | transport = require('thrift/lib/thrift/transport'); |
12 | | |
13 | 1 | EventEmitter = require('events').EventEmitter; |
14 | | |
15 | 1 | split = module.exports.split = function(hqls) { |
16 | 5 | var commented; |
17 | 5 | if (Array.isArray(hqls)) { |
18 | 0 | return hqls; |
19 | | } |
20 | 5 | commented = false; |
21 | 5 | hqls = hqls.split('\n').filter(function(line) { |
22 | 54 | var skip; |
23 | 54 | line = line.trim(); |
24 | 54 | skip = false; |
25 | 54 | if (!commented && line.indexOf('/*') !== -1) { |
26 | 1 | commented = '/*'; |
27 | 1 | skip = true; |
28 | 53 | } else if (!commented && line === '--') { |
29 | 1 | commented = '--'; |
30 | 1 | skip = true; |
31 | 52 | } else if (commented === '/*' && line.lastIndexOf('*/') !== -1 && line.lastIndexOf('*/') === (line.length - 2)) { |
32 | 1 | commented = false; |
33 | 1 | skip = true; |
34 | 51 | } else if (commented === '--' && line === '--') { |
35 | 1 | commented = false; |
36 | 1 | skip = true; |
37 | | } |
38 | 54 | if (line.indexOf('--') === 0) { |
39 | 16 | skip = true; |
40 | | } |
41 | 54 | return !commented && !skip; |
42 | | }).join('\n'); |
43 | 5 | hqls = hqls.split(';'); |
44 | 5 | hqls = hqls.map(function(query) { |
45 | 18 | return query.trim(); |
46 | | }); |
47 | 5 | return hqls = hqls.filter(function(query) { |
48 | 18 | return query.indexOf('--') !== 0 && query !== ''; |
49 | | }); |
50 | | }; |
51 | | |
52 | 1 | module.exports.createClient = function(options) { |
53 | 4 | var client, connection, _ref, _ref1, _ref2, _ref3, _ref4; |
54 | 4 | if (options == null) { |
55 | 0 | options = {}; |
56 | | } |
57 | 4 | if ((_ref = options.version) == null) { |
58 | 0 | options.version = '0.7.1-cdh3u3'; |
59 | | } |
60 | 4 | if ((_ref1 = options.server) == null) { |
61 | 0 | options.server = '127.0.0.1'; |
62 | | } |
63 | 4 | if ((_ref2 = options.port) == null) { |
64 | 0 | options.port = 10000; |
65 | | } |
66 | 4 | if ((_ref3 = options.timeout) == null) { |
67 | 0 | options.timeout = 1000; |
68 | | } |
69 | 4 | if ((_ref4 = options.transport) == null) { |
70 | 1 | options.transport = transport.TBufferedTransport; |
71 | | } |
72 | 4 | connection = thrift.createConnection(options.server, options.port, options); |
73 | 4 | client = thrift.createClient(require("./" + options.version + "/ThriftHive"), connection); |
74 | 4 | return { |
75 | | connection: connection, |
76 | | client: client, |
77 | | end: connection.end.bind(connection), |
78 | | execute: function(query, callback) { |
79 | 14 | var emitter; |
80 | 14 | emitter = new EventEmitter; |
81 | 14 | process.nextTick(function() { |
82 | 14 | emitter.emit('before', query); |
83 | 14 | return client.execute(query, function(err) { |
84 | 14 | var emitError, lboth, lerror; |
85 | 14 | if (err) { |
86 | 2 | emitter.readable = false; |
87 | 2 | lerror = emitter.listeners('error').length; |
88 | 2 | lboth = emitter.listeners('both').length; |
89 | 2 | emitError = lerror || (!lerror && !lboth && !callback); |
90 | 2 | if (emitError) { |
91 | 1 | emitter.emit('error', err); |
92 | | } |
93 | | } else { |
94 | 12 | emitter.emit('end', null, query); |
95 | | } |
96 | 14 | emitter.emit('both', err, query); |
97 | 14 | if (callback) { |
98 | 9 | return callback(err, callback); |
99 | | } |
100 | | }); |
101 | | }); |
102 | 14 | return emitter; |
103 | | }, |
104 | | query: function(query, size) { |
105 | 8 | var buffer, callback, count, emitter, exec, fetch, handle, |
106 | | _this = this; |
107 | 8 | if (arguments.length === 2 && typeof size === 'function') { |
108 | 0 | callback = size; |
109 | 0 | size = null; |
110 | | } |
111 | 8 | exec = function() { |
112 | 7 | emitter.emit('before', query); |
113 | 7 | return client.execute(query, function(err) { |
114 | 7 | var emitError, lboth, lerror; |
115 | 7 | if (err) { |
116 | 1 | emitter.readable = false; |
117 | 1 | lerror = emitter.listeners('error').length; |
118 | 1 | lboth = emitter.listeners('both').length; |
119 | 1 | emitError = lerror || (!lerror && !lboth); |
120 | 1 | if (emitError) { |
121 | 1 | emitter.emit('error', err); |
122 | | } |
123 | 1 | emitter.emit('both', err, query); |
124 | 1 | return; |
125 | | } |
126 | 6 | return fetch(); |
127 | | }); |
128 | | }; |
129 | 8 | if (query) { |
130 | 5 | process.nextTick(exec); |
131 | | } |
132 | 8 | buffer = []; |
133 | 8 | count = 0; |
134 | 8 | emitter = new Stream; |
135 | 8 | emitter.size = size; |
136 | 8 | emitter.readable = true; |
137 | 8 | emitter.paused = 0; |
138 | 8 | emitter.query = function(q) { |
139 | 2 | if (query) { |
140 | 0 | throw new Error('Query already defined'); |
141 | | } |
142 | 2 | query = q; |
143 | 2 | exec(); |
144 | 2 | return this; |
145 | | }; |
146 | 8 | emitter.pause = function() { |
147 | 54 | return this.paused = 1; |
148 | | }; |
149 | 8 | emitter.resume = function() { |
150 | 54 | this.was = this.paused; |
151 | 54 | this.paused = 0; |
152 | 54 | if (this.was) { |
153 | 6 | return fetch(); |
154 | | } |
155 | | }; |
156 | 8 | handle = function(err, rows) { |
157 | 21 | var emitError, lboth, lerror, row, _i, _len; |
158 | 21 | if (err) { |
159 | 0 | emitter.readable = false; |
160 | 0 | lerror = emitter.listeners('error').length; |
161 | 0 | lboth = emitter.listeners('both').length; |
162 | 0 | emitError = lerror || (!lerror && !lboth); |
163 | 0 | if (emitError) { |
164 | 0 | emitter.emit('error', err); |
165 | | } |
166 | 0 | emitter.emit('both', err, query); |
167 | 0 | return; |
168 | | } |
169 | 21 | rows = rows.map(function(row) { |
170 | 273 | return row.split('\t'); |
171 | | }); |
172 | 21 | for (_i = 0, _len = rows.length; _i < _len; _i++) { |
173 | 273 | row = rows[_i]; |
174 | 273 | if (count === 0) { |
175 | 6 | emitter.emit('row-first', row, 0); |
176 | | } |
177 | 273 | emitter.emit('row', row, count++); |
178 | | } |
179 | 21 | if (rows.length === emitter.size) { |
180 | 15 | if (!emitter.paused) { |
181 | 10 | return fetch(); |
182 | | } |
183 | | } else { |
184 | 6 | emitter.emit('row-last', row, count - 1); |
185 | 6 | emitter.readable = false; |
186 | 6 | emitter.emit('end', query); |
187 | 6 | return emitter.emit('both', null, query); |
188 | | } |
189 | | }; |
190 | 8 | fetch = function() { |
191 | 22 | if (emitter.paused || !emitter.readable) { |
192 | 1 | return; |
193 | | } |
194 | 21 | if (emitter.size) { |
195 | 18 | return client.fetchN(emitter.size, handle); |
196 | | } else { |
197 | 3 | return client.fetchAll(handle); |
198 | | } |
199 | | }; |
200 | 8 | return emitter; |
201 | | }, |
202 | | multi_execute: function(hqls, callback) { |
203 | 2 | var emitter, |
204 | | _this = this; |
205 | 2 | emitter = new EventEmitter; |
206 | 2 | hqls = split(hqls); |
207 | 2 | each(hqls).on('item', function(next, query) { |
208 | 4 | var exec; |
209 | 4 | exec = _this.execute(query, next); |
210 | 4 | return exec.on('before', function() { |
211 | 4 | var _ref5; |
212 | 4 | return (_ref5 = emitter.emit).call.apply(_ref5, [emitter, 'before'].concat(__slice.call(arguments))); |
213 | | }); |
214 | | }).on('both', function(err) { |
215 | 2 | var _ref5, _ref6, _ref7; |
216 | 2 | if (err) { |
217 | 1 | (_ref5 = emitter.emit).call.apply(_ref5, [emitter, 'error'].concat(__slice.call(arguments))); |
218 | | } else { |
219 | 1 | (_ref6 = emitter.emit).call.apply(_ref6, [emitter, 'end'].concat(__slice.call(arguments))); |
220 | | } |
221 | 2 | (_ref7 = emitter.emit).call.apply(_ref7, [emitter, 'both'].concat(__slice.call(arguments))); |
222 | 2 | if (callback) { |
223 | 2 | return callback(err); |
224 | | } |
225 | | }); |
226 | 2 | return emitter; |
227 | | }, |
228 | | multi_query: function(hqls, size) { |
229 | 3 | var query, |
230 | | _this = this; |
231 | 3 | hqls = split(hqls); |
232 | 3 | query = this.query(); |
233 | 3 | each(hqls).on('item', function(next, hql, i) { |
234 | 7 | var exec; |
235 | 7 | if (hqls.length !== i + 1) { |
236 | 5 | exec = _this.execute(hql); |
237 | 5 | exec.on('before', function() { |
238 | 5 | var _ref5; |
239 | 5 | return (_ref5 = query.emit).call.apply(_ref5, [query, 'before'].concat(__slice.call(arguments))); |
240 | | }); |
241 | 5 | exec.on('error', function(err) { |
242 | 1 | var emitError, lboth, lerror; |
243 | 1 | query.readable = false; |
244 | 1 | lerror = query.listeners('error').length; |
245 | 1 | lboth = query.listeners('both').length; |
246 | 1 | emitError = lerror || (!lerror && !lboth); |
247 | 1 | if (emitError) { |
248 | 1 | query.emit('error', err); |
249 | | } |
250 | 1 | return query.emit('both', err, query); |
251 | | }); |
252 | 5 | return exec.on('end', function() { |
253 | 4 | return next(); |
254 | | }); |
255 | | } else { |
256 | 2 | return query.query(hql, size); |
257 | | } |
258 | | }); |
259 | 3 | return query; |
260 | | } |
261 | | }; |
262 | | }; |