Speed up translator initialization in source installs

updateBundledStyles() already has the contents of a new/updated
translator when it copies it into the data dir, so there's no need for
Zotero.Translators.reinit() to read it again from the just-copied file.
This passes the metadata from updateBundledStyles() to reinit() to avoid
the extra file read.

(Alas, this appears to make essentially zero difference on an OS X
system with an SSD, but maybe it will help elsewhere.)
This commit is contained in:
Dan Stillman 2015-05-24 19:38:52 -04:00
parent bd9c53b29c
commit 2ed04eca26
3 changed files with 48 additions and 23 deletions

View File

@ -663,6 +663,7 @@ Zotero.Schema = new function(){
// //
var sql = "SELECT version FROM version WHERE schema=?"; var sql = "SELECT version FROM version WHERE schema=?";
var lastModTime = yield Zotero.DB.valueQueryAsync(sql, mode); var lastModTime = yield Zotero.DB.valueQueryAsync(sql, mode);
var cache = {};
// XPI installation // XPI installation
if (!isUnpacked) { if (!isUnpacked) {
@ -880,6 +881,9 @@ Zotero.Schema = new function(){
else if (mode == 'translators') { else if (mode == 'translators') {
newObj = yield Zotero.Translators.loadFromFile(entry.path); newObj = yield Zotero.Translators.loadFromFile(entry.path);
} }
else {
throw new Error("Invalid mode '" + mode + "'");
}
let existingObj = Zotero[Mode].get(newObj[modeType + "ID"]); let existingObj = Zotero[Mode].get(newObj[modeType + "ID"]);
if (!existingObj) { if (!existingObj) {
Zotero.debug("Installing " + modeType + " '" + newObj[titleField] + "'"); Zotero.debug("Installing " + modeType + " '" + newObj[titleField] + "'");
@ -902,19 +906,16 @@ Zotero.Schema = new function(){
} }
try { try {
let destFile = OS.Path.join(destDir, fileName); let destFile;
if (!existingObj || !existingObj.hidden) {
destFile = OS.Path.join(destDir, fileName);
}
else {
destFile = OS.Path.join(hiddenDir, fileName)
}
try { try {
if (!existingObj || !existingObj.hidden) { yield OS.File.copy(entry.path, destFile, { noOverwrite: true });
yield OS.File.copy(entry.path, destFile, {
noOverwrite: true
});
}
else {
yield OS.File.copy(entry.path, OS.Path.join(hiddenDir, fileName), {
noOverwrite: true
});
}
} }
catch (e) { catch (e) {
if (e instanceof OS.File.Error && e.becauseExists) { if (e instanceof OS.File.Error && e.becauseExists) {
@ -923,17 +924,16 @@ Zotero.Schema = new function(){
+ "'" + fileName + "'"; + "'" + fileName + "'";
Zotero.debug(msg, 1); Zotero.debug(msg, 1);
Components.utils.reportError(msg); Components.utils.reportError(msg);
if (!existingObj || !existingObj.hidden) { yield OS.File.copy(entry.path, destFile);
yield OS.File.copy(entry.path, destFile);
}
else {
yield OS.File.copy(entry.path, OS.Path.join(hiddenDir, fileName));
}
} }
else { else {
throw e; throw e;
} }
} }
if (mode == 'translators') {
cache[fileName] = newObj.metadata;
}
} }
catch (e) { catch (e) {
Components.utils.reportError("Error copying file " + fileName + ": " + e); Components.utils.reportError("Error copying file " + fileName + ": " + e);
@ -956,7 +956,7 @@ Zotero.Schema = new function(){
} }
}); });
yield Zotero[Mode].reinit(); yield Zotero[Mode].reinit(cache);
return true; return true;
}); });

View File

@ -59,6 +59,7 @@ var TRANSLATOR_SAVE_PROPERTIES = TRANSLATOR_REQUIRED_PROPERTIES.concat(["browser
* @property {Object} hiddenPrefs Hidden preferences configurable through about:config * @property {Object} hiddenPrefs Hidden preferences configurable through about:config
* @property {Boolean} inRepository Whether the translator may be found in the repository * @property {Boolean} inRepository Whether the translator may be found in the repository
* @property {String} lastUpdated SQL-style date and time of translator's last update * @property {String} lastUpdated SQL-style date and time of translator's last update
* @property {Object} metadata - Metadata block as object
* @property {String} code The executable JavaScript for the translator * @property {String} code The executable JavaScript for the translator
* @property {Boolean} cacheCode Whether to cache code for this session (non-connector only) * @property {Boolean} cacheCode Whether to cache code for this session (non-connector only)
* @property {String} [path] File path corresponding to this translator (non-connector only) * @property {String} [path] File path corresponding to this translator (non-connector only)
@ -129,6 +130,11 @@ Zotero.Translator.prototype.init = function(info) {
} else if(this.hasOwnProperty("code")) { } else if(this.hasOwnProperty("code")) {
delete this.code; delete this.code;
} }
// Save a copy of the metadata block
delete info.path;
delete info.code;
this.metadata = info;
} }
/** /**
@ -192,6 +198,7 @@ Zotero.Translator.prototype.logError = function(message, type, line, lineNumber,
var ios = Components.classes["@mozilla.org/network/io-service;1"]. var ios = Components.classes["@mozilla.org/network/io-service;1"].
getService(Components.interfaces.nsIIOService); getService(Components.interfaces.nsIIOService);
Zotero.log(message, type ? type : "error", ios.newFileURI(file).spec); Zotero.log(message, type ? type : "error", ios.newFileURI(file).spec);
Zotero.debug(message, 1);
} else { } else {
Zotero.logError(message); Zotero.logError(message);
} }

View File

@ -23,6 +23,8 @@
***** END LICENSE BLOCK ***** ***** END LICENSE BLOCK *****
*/ */
"use strict";
// Enumeration of types of translators // Enumeration of types of translators
const TRANSLATOR_TYPES = {"import":1, "export":2, "web":4, "search":8}; const TRANSLATOR_TYPES = {"import":1, "export":2, "web":4, "search":8};
@ -36,8 +38,11 @@ Zotero.Translators = new function() {
/** /**
* Initializes translator cache, loading all translator metadata into memory * Initializes translator cache, loading all translator metadata into memory
*
* @param {Object} [memCache] - Translator metadata keyed by filename, if already available
* (e.g., in updateBundledFiles()), to avoid unnecesary file reads
*/ */
this.reinit = Zotero.Promise.coroutine(function* () { this.reinit = Zotero.Promise.coroutine(function* (memCache) {
Zotero.debug("Initializing translators"); Zotero.debug("Initializing translators");
var start = new Date; var start = new Date;
_initialized = true; _initialized = true;
@ -76,7 +81,14 @@ Zotero.Translators = new function() {
lastModifiedTime = (yield OS.File.stat(path)).lastModificationDate.getTime(); lastModifiedTime = (yield OS.File.stat(path)).lastModificationDate.getTime();
} }
var dbCacheEntry = false; // Check passed cache for metadata
let memCacheJSON = false;
if (memCache && memCache[fileName]) {
memCacheJSON = memCache[fileName];
}
// Check DB cache
let dbCacheEntry = false;
if (dbCache[fileName]) { if (dbCache[fileName]) {
filesInCache[fileName] = true; filesInCache[fileName] = true;
if (dbCache[fileName].lastModifiedTime == lastModifiedTime) { if (dbCache[fileName].lastModifiedTime == lastModifiedTime) {
@ -85,8 +97,10 @@ Zotero.Translators = new function() {
} }
// Get JSON from cache if possible // Get JSON from cache if possible
if(dbCacheEntry) { if (memCacheJSON || dbCacheEntry) {
var translator = Zotero.Translators.load(dbCacheEntry.metadataJSON, path); var translator = Zotero.Translators.load(
memCacheJSON || dbCacheEntry.metadataJSON, path
);
} }
// Otherwise, load from file // Otherwise, load from file
else { else {
@ -175,9 +189,13 @@ Zotero.Translators = new function() {
/** /**
* Loads a translator from JSON, with optional code * Loads a translator from JSON, with optional code
*
* @param {String|Object} json - Metadata JSON
* @param {String} path
* @param {String} [code]
*/ */
this.load = function (json, path, code) { this.load = function (json, path, code) {
var info = JSON.parse(json); var info = typeof json == 'string' ? JSON.parse(json) : json;
info.path = path; info.path = path;
info.code = code; info.code = code;
return new Zotero.Translator(info); return new Zotero.Translator(info);