From 41f3c3a26138dd0e85072790ff6a27ccbfbbe32f Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Mon, 27 Jul 2009 09:17:42 +0000 Subject: [PATCH] Greatly increase import speed (by ~38% in testing) by consolidating collection inserts --- chrome/content/zotero/fileInterface.js | 4 +- .../content/zotero/xpcom/data/collection.js | 51 ++++++++++++++++++- chrome/content/zotero/xpcom/translate.js | 10 +++- 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/chrome/content/zotero/fileInterface.js b/chrome/content/zotero/fileInterface.js index e1dfabee58..7fce01c010 100644 --- a/chrome/content/zotero/fileInterface.js +++ b/chrome/content/zotero/fileInterface.js @@ -298,9 +298,7 @@ var Zotero_File_Interface = new function() { */ function _importDone(obj, worked) { // add items to import collection - for each(var itemID in obj.newItems) { - _importCollection.addItem(itemID); - } + _importCollection.addItems(obj.newItems); Zotero.DB.commitTransaction(); diff --git a/chrome/content/zotero/xpcom/data/collection.js b/chrome/content/zotero/xpcom/data/collection.js index 492082a6f1..5c9a0eb0ad 100644 --- a/chrome/content/zotero/xpcom/data/collection.js +++ b/chrome/content/zotero/xpcom/data/collection.js @@ -648,10 +648,59 @@ Zotero.Collection.prototype.addItems = function(itemIDs) { return; } + var current = this.getChildItems(true); + Zotero.DB.beginTransaction(); + + var sql = "SELECT IFNULL(MAX(orderIndex), 0) FROM collectionItems WHERE collectionID=?"; + var max = Zotero.DB.valueQuery(sql, this.id); + var nextOrderIndex = 0; + + sql = "SELECT IFNULL(MAX(orderIndex)+1, 0) FROM collectionItems WHERE collectionID=?"; + var selectStatement = Zotero.DB.getStatement(sql); + + sql = "INSERT OR IGNORE INTO collectionItems VALUES (?,?,?)"; + var insertStatement = Zotero.DB.getStatement(sql); + for (var i=0; imax) { + nextOrderIndex++; + } + else { + selectStatement.bindInt32Parameter(0, this.id); + selectStatement.executeStep(); + nextOrderIndex = selectStatement.getInt32(0); + selectStatement.reset(); + } + + insertStatement.bindInt32Parameter(0, this.id); + insertStatement.bindInt32Parameter(1, itemID); + insertStatement.bindInt32Parameter(2, nextOrderIndex); + + try { + insertStatement.execute(); + } + catch(e) { + throw (e + ' [ERROR: ' + Zotero.DB.getLastErrorString() + ']'); + } } + + sql = "UPDATE collections SET dateModified=?, clientDateModified=? WHERE collectionID=?"; + Zotero.DB.query(sql, [Zotero.DB.transactionDateTime, Zotero.DB.transactionDateTime, this.id]); + Zotero.DB.commitTransaction(); } diff --git a/chrome/content/zotero/xpcom/translate.js b/chrome/content/zotero/xpcom/translate.js index d363a7a2ce..48e01fd90f 100644 --- a/chrome/content/zotero/xpcom/translate.js +++ b/chrome/content/zotero/xpcom/translate.js @@ -1700,6 +1700,8 @@ Zotero.Translate.prototype._processCollection = function(collection, parentID) { this.newCollections.push(myID); + var toAdd = []; + for each(child in collection.children) { if(child.type == "collection") { // do recursive processing of collections @@ -1707,14 +1709,18 @@ Zotero.Translate.prototype._processCollection = function(collection, parentID) { } else { // add mapped items to collection if(this._IDMap[child.id]) { - Zotero.debug("Translate: Adding "+this._IDMap[child.id], 5); - newCollection.addItem(this._IDMap[child.id]); + toAdd.push(this._IDMap[child.id]); } else { Zotero.debug("Translate: Could not map "+child.id+" to an imported item", 2); } } } + if (toAdd.length) { + Zotero.debug("Translate: Adding " + toAdd, 5); + newCollection.addItems(toAdd); + } + return newCollection; }