Greatly increase import speed (by ~38% in testing) by consolidating collection inserts
This commit is contained in:
parent
24cd01e16f
commit
41f3c3a261
3 changed files with 59 additions and 6 deletions
|
@ -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();
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue