commit 41f3c3a26138dd0e85072790ff6a27ccbfbbe32f
parent 24cd01e16fdb3150ac94108bbe1e9dad97102af3
Author: Dan Stillman <dstillman@zotero.org>
Date: Mon, 27 Jul 2009 09:17:42 +0000
Greatly increase import speed (by ~38% in testing) by consolidating collection inserts
Diffstat:
3 files changed, 59 insertions(+), 6 deletions(-)
diff --git 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
@@ -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; i<itemIDs.length; i++) {
- this.addItem(itemIDs[i]);
+ var itemID = itemIDs[i];
+ if (current && current.indexOf(itemID) != -1) {
+ Zotero.debug("Item " + itemID + " already a child of collection "
+ + this.id + " in Zotero.Collection.addItems()");
+ continue;
+ }
+
+ if (!Zotero.Items.get(itemID)) {
+ Zotero.DB.rollbackTransaction();
+ throw(itemID + ' is not a valid item id');
+ }
+
+ // If we're already above the max, just increment
+ if (nextOrderIndex>max) {
+ 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
@@ -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;
}