-
Notifications
You must be signed in to change notification settings - Fork 12
/
couch.js
321 lines (245 loc) · 7.83 KB
/
couch.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Couch database
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
var defaultable = require('defaultable');
defaultable(module,
{ 'uuid_batch_size': 100
}, function(module, exports, DEFS, require) {
//
// I know maintenance will eventually have to be performed, like purging old documents.
// This is a place where it can go. Maybe in the future you can have dedicated maintenance
// nodes that don't interfere with the main API.
//
// Also, hopefully this file can be ported to browser Javascript and the same code can
// run client-side.
var lib = require('./lib')
, util = require('util')
, events = require('events')
, assert = require('assert')
, follow = require('follow')
, debug = require('debug')
, querystring = require('querystring')
//
// Constants
//
var KNOWN_COUCHES = {};
var UUIDS = {}; // Map couch URLs to UUID pools.
//
// API
//
function Couch (opts) {
var self = this;
self.url = (typeof opts.url === 'string') ? opts.url : null;
self.userCtx = opts.userCtx || null;
self.time_C = opts.time_C || null;
self.known_dbs = null;
self.log = debug('cqs:couch:' + self.url);
}
Couch.prototype.request = function(opts, callback) {
var self = this;
assert.ok(self.url);
assert.ok(callback);
if(typeof opts === 'string')
opts = {'uri':opts};
opts.uri = self.url + '/' + opts.uri;
opts.time_C = opts.time_C || self.time_C;
self.confirmed(function(er) {
if(er) return callback(er);
var method = opts.method || 'GET';
self.log(method + ' ' + opts.uri);
return lib.req_json(opts, callback);
})
}
Couch.prototype.uuid = function get_uuid(count, callback) {
var self = this;
if(typeof count === 'function') {
callback = count;
count = 1;
}
var uuids = uuids_for(self);
return uuids.get(count, callback);
}
Couch.prototype.confirmed = function confirm_couch(callback) {
var self = this;
assert.ok(callback);
assert.ok(self.url);
if(self.userCtx && self.known_dbs)
return callback();
self.url = self.url.replace(/\/+$/, '')
var confirmer = KNOWN_COUCHES[self.url];
if(!confirmer)
confirmer = KNOWN_COUCHES[self.url] = new lib.Once;
confirmer.on_done(function(er, userCtx, known_dbs) {
if(er)
return callback(er);
self.userCtx = userCtx;
self.known_dbs = known_dbs;
return callback();
})
// Don't use self.request because that calls confirmed().
function req(uri, cb) {
return lib.req_json({'uri':uri, 'time_C':self.time_C}, cb);
}
confirmer.job(function(done) {
self.log('Confirming Couch: ' + self.url);
req(self.url, function(er, resp, body) {
if(er) return done(er);
if(body.couchdb !== 'Welcome')
return done(new Error('Bad CouchDB response from ' + self.url));
self.log('Confirming session');
req(self.url + '/_session', function(er, resp, session) {
if(er) return done(er);
if(!session.userCtx)
return done(new Error('Bad CouchDB response: ' + self.url + '/_session'));
self.log('Couch confirmed: ' + self.url + ': ' + lib.JS(session.userCtx));
var known_dbs = {};
return done(null, session.userCtx, known_dbs);
})
})
})
}
function Database (opts) {
var self = this;
opts = defaultable.merge(opts || {}, DEFS);
if(typeof opts.couch !== 'string')
throw new Error('Required "couch" option with URL of CouchDB');
opts.db = opts.db || "";
if(typeof opts.db !== 'string')
throw new Error('Optional "db" option must be string');
self.name = opts.db;
self.couch = new Couch({'url':opts.couch, time_C:opts.time_C});
self.secObj = null;
self.known_queues = null;
self.log = debug('cqs:db:' + self.name);
}
Database.prototype.request = function(opts, callback) {
var self = this;
if(typeof opts === 'string')
opts = {'uri':opts};
opts.uri = self.name + '/' + opts.uri;
self.confirmed(function(er) {
if(er) return callback(er);
self.couch.request(opts, callback);
})
}
Database.prototype.changes = function(opts) {
var self = this
if(typeof opts != 'object')
opts = {}
opts.db = self.couch.url + '/' + self.name
return new follow.Feed(opts)
}
Database.prototype.confirmed = function(callback) {
var self = this;
assert.ok(callback);
assert.ok(self.couch);
self.couch.confirmed(function() {
var confirmer = self.couch.known_dbs[self.name];
if(!confirmer)
confirmer = self.couch.known_dbs[self.name] = new lib.Once;
confirmer.on_done(function(er, secObj, known_queues) {
if(er)
return callback(er);
self.secObj = secObj;
self.known_queues = known_queues;
callback();
})
confirmer.job(confirm_db);
})
function confirm_db(done) {
self.log('Confirming DB: ' + self.name);
self.couch.request(self.name, function(er, resp, db) {
if(er) return done(er);
if(db.db_name !== self.name)
return done(new Error('Expected DB name "'+self.name+'": ' + db.db_name));
self.log('Checking _security: ' + self.name);
self.couch.request(self.name+'/_security', function(er, resp, secObj) {
if(er) return done(er);
if(!secObj)
return done(new Error('Bad _security response from ' + self.name + ': ' + lib.JS(secObj)));
self.log('Confirmed DB: ' + self.name + ': ' + lib.JS(secObj));
var known_queues = {};
return done(null, secObj, known_queues);
})
})
}
}
module.exports = { "Database" : Database
};
//
// Utilities
//
function uuids_for(couch) {
UUIDS[couch.url] = UUIDS[couch.url] || new UUIDGetter(couch);
return UUIDS[couch.url];
}
function UUIDGetter (couch) {
var self = this;
self.couch = couch;
self.waiting = [];
self.pool = [];
self.fetching = false;
}
UUIDGetter.prototype.get = function(count, callback) {
var self = this;
self.waiting.push({'count':count, 'callback':callback});
self.try_to_send();
}
UUIDGetter.prototype.try_to_send = function() {
var self = this;
if(self.fetching)
return; // When the fetch is done, this will be re-run.
var waiter = self.waiting[0];
if(!waiter)
return; // No more waiters for UUIDs.
// If enough UUIDs are in the pool, just send them now.
if(waiter.count <= self.pool.length) {
self.respond(self.waiting.shift());
return self.try_to_send();
}
// Otherwise, fetch some more.
self.fetch();
}
UUIDGetter.prototype.fetch = function() {
var self = this;
assert.equal(self.fetching, false, 'Fetch called twice in a row: ' + self.couch.url);
self.fetching = true;
self.couch.request('_uuids?count=' + DEFS.uuid_batch_size, function(er, resp, result) {
self.fetching = false;
if(er)
self.waiting.forEach(function(waiter) {
waiter.callback(er)
})
else if(!result.uuids || result.uuids.length !== DEFS.uuid_batch_size) {
er = new Error('Unknown _uuids result: ' + lib.JS(result));
self.waiting.forEach(function(waiter) {
waiter.callback(er)
})
}
else {
self.pool = self.pool.concat(result.uuids);
self.try_to_send();
}
})
}
UUIDGetter.prototype.respond = function(waiter) {
var self = this
, count = waiter.count
, callback = waiter.callback
var response = self.pool.slice(0, count);
self.pool = self.pool.slice(count + 1);
if(response.length === 1)
response = response[0];
callback(null, response);
}
}, require) // defaultable