-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtyrant.js
More file actions
286 lines (227 loc) · 8.21 KB
/
tyrant.js
File metadata and controls
286 lines (227 loc) · 8.21 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* Title: tyrant - tokyo tyrant to nodejs connector
*
* 2010, Michel Beloshitsky
*
* Licensed under the terms of MIT license. See COPYING file in the
* root of distribution.
*/
var bin = require('./binary'), stream = require('./nstream')
/* Error codes */
var TTESUCCESS = 0 /* success */
var TTEINVALID = 1 /* invalid operation */
var TTENOHOST = 2 /* host not found */
var TTEREFUSED = 3 /* connection refused */
var TTESEND = 4 /* send error */
var TTERECV = 5 /* recv error */
var TTEKEEP = 6 /* existing record */
var TTENOREC = 7 /* no record found */
var TTEMISC = 9999 /* miscellaneous error */
/* From ttutil.h */
var TTDEFPORT = 1978 /* default port of the server */
var TTMAGICNUM = 0xc8 /* magic number of each command */
var TTCMDPUT = 0x10 /* ID of put command */
var TTCMDPUTKEEP = 0x11 /* ID of putkeep command */
var TTCMDPUTCAT = 0x12 /* ID of putcat command */
var TTCMDPUTSHL = 0x13 /* ID of putshl command */
var TTCMDPUTNR = 0x18 /* ID of putnr command */
var TTCMDOUT = 0x20 /* ID of out command */
var TTCMDGET = 0x30 /* ID of get command */
var TTCMDMGET = 0x31 /* ID of mget command */
var TTCMDVSIZ = 0x38 /* ID of vsiz command */
var TTCMDITERINIT = 0x50 /* ID of iterinit command */
var TTCMDITERNEXT = 0x51 /* ID of iternext command */
var TTCMDFWMKEYS = 0x58 /* ID of fwmkeys command */
var TTCMDADDINT = 0x60 /* ID of addint command */
var TTCMDADDDOUBLE = 0x61 /* ID of adddouble command */
var TTCMDEXT = 0x68 /* ID of ext command */
var TTCMDSYNC = 0x70 /* ID of sync command */
var TTCMDOPTIMIZE = 0x71 /* ID of optimize command */
var TTCMDVANISH = 0x72 /* ID of vanish command */
var TTCMDCOPY = 0x73 /* ID of copy command */
var TTCMDRESTORE = 0x74 /* ID of restore command */
var TTCMDSETMST = 0x78 /* ID of setmst command */
var TTCMDRNUM = 0x80 /* ID of rnum command */
var TTCMDSIZE = 0x81 /* ID of size command */
var TTCMDSTAT = 0x88 /* ID of stat command */
var TTCMDMISC = 0x90 /* ID of misc command */
var TTCMDREPL = 0xa0 /* ID of repl command */
/* Function: make
The Maker. Constructs new connector instance.
Parameters:
[optional] host - tokyo tyrant server host. If this value omitted
"localhost" used as default hostname.
[optional] port - tokyo tyrant server port. If this value omitted
1978 used as default port number.
[optional] wrkCount - Number of parallel connections to tokyo
tyrant server instance. If omitted 8 connections will be created.
Returns:
Public interface for interaction with specified tokyo tyrant
instance.
*/
exports.make = function (host, port, wrkCount) {
/* Group: Public interface */
/* Function: put
* Put value into the storage
*/
function put (k, v, mod, cb) {
if (mod && !mod.toLowerCase)
cb = mod, mod = null
walloc(function (err, ts) {
var jk = JSON.stringify(k), jv = JSON.stringify(v)
var cmd = ({
'keep': TTCMDPUTKEEP,
'append': TTCMDPUTCAT
})[mod] || (cb ? TTCMDPUT : TTCMDPUTNR)
if (mod == 'append')
jv = '+' + jv
ts.s.write(
bin.format('bbiiss', TTMAGICNUM, cmd, jk.length, jv.length, jk, jv),
cmd == TTCMDPUTNR ? cb : null
)
if (cmd != TTCMDPUTNR)
bin.read('b', ts.s, function (err, res) {
cb(err || res[0])
ts.free()
})
})
}
/* Function: get
* Retreive value from database
*/
function get (k, cb) {
walloc(function (err, ts) {
var jk = JSON.stringify(k)
ts.s.write(
bin.format('bbis', TTMAGICNUM, TTCMDGET, jk.length, jk)
)
bin.read('b', ts.s, function (err, res) {
if (err || res[0]) {
cb(err || res[0]); ts.free()
} else {
bin.read('S', ts.s, function (err, str) {
cb(err, eval(str[0])); ts.free()
})
}
})
})
}
/* Function:del
* Delete value by key.
*/
function del (k, cb) {
var jk = JSON.stringify(k)
walloc(function (err, ts) {
ts.s.write(
bin.format('bbis', TTMAGICNUM, TTCMDOUT, jk.length, jk)
)
bin.read('b', ts.s, function (err, res) {
cb(err || res[0]); ts.free()
})
})
}
/* Function: iter
Run iteration.
*/
function iter(ks, ke, cb) {
var jks = JSON.stringify(ks)
walloc(function (err, ts) {
function end (err) {
cb(err, 'end')
ts.free()
}
function next () {
ts.s.write(
bin.format('bbiiis', TTMAGICNUM, TTCMDMISC,
'iternext'.length, /* opts */0, /* argCount */0,
'iternext')
)
bin.read('bi', ts.s, function (err, res) {
if (res[0] == 1)
return end()
if (err || res[0])
return end(err || res[0])
bin.read('SS', ts.s, function (err, kv) {
var kj = eval(kv[0])
cb(null, 'k-v', kj, eval(kv[1]))
kj == ke ? end() : next()
})
})
}
ts.s.write(
bin.format('bbiiis' + (jks ? 'is' : ''), TTMAGICNUM, TTCMDMISC,
'iterinit'.length, /* opts */0, /* argCount */jks ? 1 : 0,
'iterinit', jks.length, jks)
)
bin.read('bi', ts.s, function (err, res) {
if (err || res[0])
return cb(err || res[0])
cb(null, 'start')
next()
})
})
}
/* Function:halt
* Close connection pool
*/
function halt() {
halted = true
for (w in workers) {
workers[w].s.close()
}
}
/* Group: Internals */
/*
Topic: Workers management
How operations paralelled. Here we use simple connection pool
pattern. At the begin we make N connections and distribute it
among incoming tasks.
If number of incoming task requests overrates number of free
connections we put task requests in fifo queue - which stored in
wpending array.
*/
var workers = {}, free = [], busy = [], wpending = []
/* Function: walloc
* Allocate free connection or pend task request in case
* of no free connections.
*/
function walloc(cb) {
var found = free.pop()
if (found !== undefined) {
busy.push(found)
cb(null, workers[found])
} else {
wpending.push(cb)
}
}
/* Function: wfree
* Free connection and check pending queue. If queue contain
* incoming task requests - alloc it immeditally. */
function wfree(id) {
busy = busy.reduce(function (res, x) {
if (x != id) res.push(x); return res
}, [])
free.push(id)
/* Check pending */
while (wpending.length && free.length) {
walloc(wpending.shift())
}
}
/* The Maker loop - makes connection pool. */
wrkCount = wrkCount || 8 /* This default came from tyrant */
while(wrkCount--) {
/* Here we need a closure to make worker variable unique for
each connection. */
(function () {
var id = wrkCount, worker = {
s: stream.make(port || TTDEFPORT, host, 'binary'),
free: function () { wfree(id) }
}
worker.s.on('connect', function () { worker.free() })
workers[id]=worker
busy.push(id)
})()
}
/* Return public interface */
return {put: put, get: get, del: del, iter:iter, halt: halt}
}