Repository navigation
Expand file tree
/
Copy pathdatabase.js
More file actions
558 lines (521 loc) · 18.7 KB
/
Copy pathdatabase.js
File metadata and controls
558 lines (521 loc) · 18.7 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
(function(exports) {
'use strict';
// ===========================================================
// SchemaVersion Object
var schemaVersions = new Map();
var schemaVersionNamedRetrieval = new Map();
var addSchemaVersion = function(schemaVersion) {
var versionMap = schemaVersionNamedRetrieval.get(schemaVersion.name);
if (!versionMap) {
versionMap = new Map();
schemaVersionNamedRetrieval.set(schemaVersion.name, versionMap);
}
versionMap.set(schemaVersion.version, schemaVersion);
};
function SchemaVersion(databaseName, version, options) {
/**
* SchemaVersion (constructor)
*
* @param {string} databaseName
* @param {number} version - integer n, where 0 < n < 2^53.
* @param {Object} options - an object containing:
* initializer {function} - initializes a new schema version.
* upgrader {function} - converts this schema version to schema
* version n+1.
* downgrader {function} - converts this schema version to
* schema version n-1.
*
* References to SchemaVersion's are maintained automatically
*/
this.name = databaseName;
this.version = version;
Utils.extend(this, {
initializer: null,
upgrader: null,
downgrader: null
}, options);
addSchemaVersion(this);
schemaVersions.set(this, true);
}
SchemaVersion.noop = function(transaction, callback) {
// A noop upgrader/downgrader
callback(null);
};
SchemaVersion.error = function(transaction, callback) {
// An upgrader/downgrader that reports an error
callback(new Error('erroneous upgrader/downgrader'));
};
SchemaVersion.getSchemaVersions = function(databaseName) {
/**
* getSchemaVersions
*
* return the SchemaVersion objects for a particular databaseName
*/
var ret = [];
var databaseMap = schemaVersionNamedRetrieval.get(databaseName);
if (databaseMap) {
for (var i of databaseMap) {
ret.push(i[1]);
}
}
return ret.sort(Utils.data.keyedCompare('version'));
};
SchemaVersion.getAllSchemaVersions = function() {
/**
* getAllSchemaVersions
*
* @return {Array<SchemaVersion>} all the SchemaVersion objects.
*/
var ret = [];
for (var i of schemaVersions) {
ret.push(i[0]);
}
return ret;
};
SchemaVersion.removeSchemaVersions = function(databaseName) {
if (!databaseName) {
schemaVersions.clear();
schemaVersionNamedRetrieval.clear();
} else {
schemaVersionNamedRetrieval.delete(databaseName);
}
};
SchemaVersion.completenessReport = function(databaseName) {
/**
* completenessReport
*
* @param {string} databaseName - database to check.
* @return {object} a report of the schema completeness,
* containing arrays of version numbers
* missing init, up, or downgraders.
*/
var report = {
missingInitializers: [],
missingUpgraders: [],
missingDowngraders: []
};
var schemas = SchemaVersion.getSchemaVersions(databaseName);
for (var i = 0; i < schemas.length; i++) {
var first = i === 0;
var last = i === schemas.length - 1;
if (typeof schemas[i].initializer !== 'function') {
report.missingInitializers.push(schemas[i].version);
}
if (!last && typeof schemas[i].upgrader !== 'function') {
report.missingUpgraders.push(schemas[i].version);
}
if (!first && typeof schemas[i].downgraders !== 'function') {
report.missingDowngraders.push(schemas[i].version);
}
}
return report;
};
// ===========================================================
// SchemaVersion Prototype
SchemaVersion.prototype = {
register: function sv_register(database) {
if (this.initializer) {
database.addInitializer(this.version, this.initializer);
} else {
throw new Error('Cannot add a Schema without an initializer');
}
if (this.upgrader) {
database.addUpgrader(this.version, this.upgrader);
}
if (this.downgrader) {
database.addDowngrader(this.version, this.downgrader);
}
}
};
// ===========================================================
// Database Object
function Database(options) {
/**
* Database (constructor)
*
* @param {object} options - containing:
* name {string} - database name.
* version {number} - integer n, where 0 < n < 2^53.
* schemas {list<string>} - a list of strings containing
* URLs (typically a db/schema_{version}.js naming scheme)
* that define the database Schemas. These will be lazy
* loaded when the effective version !== the source version.
*/
Utils.extend(this, {
initializers: [],
upgraders: [],
downgraders: []
}, options);
}
// ===========================================================
// Database Singletons
var databaseSingletons = new Map();
Database.singleton = function(options) {
var db = databaseSingletons.get(options.name);
if (!db) {
db = new Database(options);
databaseSingletons.set(options.name, db);
}
return db;
};
// ===========================================================
// Database Object Private Methods
var createAdder = function(listName) {
return function(version, fn) {
var added = {
version: version,
fn: fn
};
Utils.data.sortedRemove(added, this[listName],
Utils.data.keyedCompare('version'));
Utils.data.sortedInsert(added, this[listName],
Utils.data.keyedCompare('version'), true);
};
};
var createRemover = function(listName) {
return function(version) {
var removed = { version: version };
Utils.data.sortedRemove(removed, this[listName],
Utils.data.keyedCompare('version'));
};
};
// ===========================================================
// Database Object Prototype
Database.prototype = {
addInitializer: createAdder('initializers'),
removeInitializer: createRemover('initializers'),
addUpgrader: createAdder('upgraders'),
removeUpgrader: createRemover('upgraders'),
addDowngrader: createAdder('downgraders'),
removeDowngrader: createRemover('downgraders'),
get effectiveVersionName() {
return '__effectiveVersion__';
},
setLatestVersion: function(databaseName, version, callback) {
/**
* setLatestVersion @private
*
* @param {string} [databaseName] - name of database.
* @param {number} version - effective version number to set.
* @param {function} callback - call with (err).
*/
if (arguments.length === 2 &&
typeof arguments[0] === 'number' &&
typeof arguments[1] === 'function') {
// if no databaseName was passed, use `this`
databaseName = this.name;
version = arguments[0];
callback = arguments[1];
}
this.requestMutatorTransaction(function(err, transaction) {
var db = transaction.db;
if (Array.prototype.indexOf.call(db.objectStoreNames,
this.effectiveVersionName) !== -1) {
db.deleteObjectStore(this.effectiveVersionName);
}
var ev = db.createObjectStore(this.effectiveVersionName);
var req = ev.put({ number: version }, 0);
req. {
transaction.db.close();
callback && callback(null);
};
req. {
transaction.db.close();
callback && callback(req.error);
};
}.bind(this));
},
getLatestVersion: function(databaseName, callback) {
/**
* getLatestVersion @private
*
* @param {string} [databaseName] - name of database.
* @param {function} callback - call with result:
* (err, versionNumber, effectiveNumber).
*/
if (arguments.length === 1 && typeof arguments[0] === 'function') {
// if no databaseName was passed, use `this`
databaseName = this.name;
callback = arguments[0];
}
var ignoreError = false;
// Force an `onupgradeneeded` event so that we can query for the
// effective version number. The request will be aborted in order
// to prevent the request's side effects on the database.
var req = indexedDB.open(databaseName, Math.pow(2, 53) - 1);
var getEffective = (function(openEvent, transaction, callback) {
var upgrade = transaction.mode === 'versionchange';
var db = transaction.db;
db. {
db.close();
};
var calloutAndCleanup = function(err, effective) {
try {
callback && callback(err,
upgrade ? openEvent.oldVersion : Math.pow(2, 53) - 1,
err !== null ? 0 : effective);
} finally {
db.close();
ignoreError = true;
transaction.abort();
}
};
if ((!upgrade || openEvent.oldVersion > 0) &&
Array.prototype.indexOf.call(db.objectStoreNames,
this.effectiveVersionName) !== -1) {
var ev = transaction.objectStore(this.effectiveVersionName);
var req = ev.get(0);
req. = function(ev) {
calloutAndCleanup(req.error,
req.error === null ? req.result.number : 0);
};
} else {
calloutAndCleanup(null, 0);
}
}).bind(this);
// This function is invoked ~(1 - 10^-14) percent of the time, whenever
// req.result.version < 2^53 - 1. The transaction will be aborted before
// onsuccess is called, to prevent any changes from occurring.
req. {
getEffective(ev, ev.target.transaction, callback);
};
// Edge case: invoked when the IndexDB version number is maxed out.
// This function is invoked ~(10^-14) percent of the time, whenever
// req.result.version === 2^53 - 1. The transaction will be aborted.
req. {
var trans = req.result.transaction(
this.effectiveVersionName, 'readonly');
getEffective(ev, trans, callback);
}).bind(this);
req. = function(ev) {
ev.preventDefault();
// Every aborted transaction triggers an `onerror` event. Because we
// are intentionally aborting the "open" transaction in order to avoid
// side effects, we can safely ignore this event in the successful
// case.
if (!ignoreError) {
callback && callback(
new Error('Error retrieving indexedDB version #'));
}
};
},
// ===========================================================
// Database initializing and upgrading
loadSchemas: function(callback) {
/**
* loadSchemas @private
*
* @param {function} callback - called after all
* schemas are loaded.
*
* Lazily loads schemas and then calls the callback.
* SchemaVersions that were defined with a different
* databaseName must be loaded manually.
*/
LazyLoader.load(this.schemas, function() {
SchemaVersion.getSchemaVersions(this.name).forEach(
function(el) {
el.register(this);
}.bind(this));
callback && callback();
}.bind(this));
},
initialize: function(newVersion, callback) {
/**
* initialize @private
*
* @param {number} newVersion - integer effective version.
* @param {function} callback - function to be called after
* the database is initialized.
*/
this.requestMutatorTransaction(function(err, transaction) {
var db = transaction.db;
var objectStores = db.objectStoreNames;
for (var i = 0; i < objectStores.length; i++) {
db.deleteObjectStore(objectStores[i]);
}
var init = Utils.data.binarySearch({ 'version': newVersion },
this.initializers,
Utils.data.keyedCompare('version'));
if (init.match) {
var setVersion = (function(err) {
db.close();
this.setLatestVersion(this.name, newVersion, callback);
}).bind(this);
this.initializers[init.index].fn(transaction, setVersion);
} else {
callback && callback(new Error('no initializer for ' + newVersion));
}
}.bind(this));
},
requestMutatorTransaction: function(callback) {
/**
* requestMutatorTransaction @private
*
* @param {function} callback - function to be called with the
* versionchange transaction.
*/
this.getLatestVersion(this.name, function(err, version, effective) {
var req = indexedDB.open(this.name, version + 1);
req. {
req.result. {
req.result.close();
};
callback && callback(null, ev.target.transaction, req);
};
req. {
callback && callback(req.error);
};
}.bind(this));
},
upgrade: function(oldVersion, newVersion, callback) {
/**
* upgrade @private
*
* @param {IDBTransaction} transaction - versionchanged transaction.
* @param {number} oldVersion - integer old effective version.
* @param {number} newVersion - integer effective version.
* @param {function} callback - function to be called after
* the database is upgraded.
*
* Attempt to upgrade the database. If the upgrade fails, or if
* there is no upgrade path defined, we default to destroying the
* existing database and initializing a new one of this.version.
*/
var mutators, direction;
if (newVersion === oldVersion) {
return;
} else if (newVersion > oldVersion) {
mutators = this.upgraders;
direction = 1;
} else {
mutators = this.downgraders;
direction = -1;
}
var first = Utils.data.binarySearch({version: oldVersion}, mutators,
Utils.data.keyedCompare('version'));
var plan = [], last = null;
if (first.match) {
var isApplicableMutator = function(m, dir, version) {
// don't upgrade past the target
if (dir === 1) {
return m.version < version;
} else {
return m.version > version;
}
};
for (var i = first.index;
i >= 0 && i < mutators.length &&
isApplicableMutator(mutators[i], direction, newVersion);
i += direction) {
// If our version numbers have a gap larger than 1, we
// cannot fluidly upgrade or downgrade. Truncate the
// plan and continue (forcing an initialize).
if (last && Math.abs(last - mutators[i].version) > 1) {
plan.length = 0;
break;
}
plan.push(mutators[i]);
last = mutators[i].version;
}
}
if (plan.length === 0) {
this.initialize(newVersion, function(err) {
callback && callback(err);
});
return;
}
var finalizer = (function(err) {
if (!err) {
this.setLatestVersion(this.name, newVersion, function(err) {
callback && callback(err);
});
} else {
console.log('Upgrade error:', err.message, err.fileName,
err.lineNumber);
// We can't do anything useful here, so start from scratch,
// destroying user data
this.initialize(newVersion, function(err) {
callback && callback(err);
});
}
}).bind(this);
var last = finalizer;
for (var i = plan.length - 1; i >= 0; i--) {
last = (function(converter, version, cb) {
// de-duplicate the callbacks
var dedup = Utils.async.namedParallel(['converter'], cb);
return (function(passedErr) {
if (passedErr) {
cb(passedErr);
return;
}
this.requestMutatorTransaction(function(err, transaction) {
try {
converter.call(transaction, transaction, dedup.converter);
} catch (err) {
dedup.converter(err);
} finally {
transaction.db.close();
}
});
}).bind(this);
}.bind(this))(plan[i].fn, plan[i].version, last);
}
last(null);
},
connect: function(callback) {
/**
* connect
*
* @param {function} callback - function to be called after
* the database connection is
* created. Called with (err,
* database).
*
* This is the primary API for the database object. Most
* other methods on Database are not relevant in typical usage.
*/
var opener = (function(actualVersion) {
var req = indexedDB.open(this.name, actualVersion);
req. {
req.result. {
req.result.close();
};
callback && callback(null, req.result);
};
req. {
event.preventDefault();
callback && callback(req.error);
}).bind(this);
req. {
callback && callback(new Error('blocked'));
};
}).bind(this);
this.getLatestVersion(this.name, function(err, version, effective) {
if (version === 0 || effective !== this.version) {
this.loadSchemas(function() {
// We have lazy loaded schema files, now upgrade the database
this.upgrade(effective, this.version, function(err) {
if (err) {
callback && callback(err);
return;
}
// The upgrade function has changed our version number,
// get the latest
this.getLatestVersion(this.name,
function(err, version, effective) {
opener.call(this, version);
}.bind(this));
}.bind(this));
}.bind(this));
} else {
// No upgrade necessary, just open a connection
opener.call(this, version);
}
}.bind(this));
}
};
exports.SchemaVersion = SchemaVersion;
exports.Database = Database;
})(this);