20% speed boost in collection switching

This commit is contained in:
Simon Kornblith 2011-09-06 18:14:23 +00:00
parent 96973016a5
commit 28e7531710
4 changed files with 94 additions and 39 deletions

View file

@ -1767,6 +1767,23 @@ Zotero.CollectionTreeView.prototype.cycleHeader = function(column) { }
/// ///
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Zotero.ItemGroupCache = {
"lastItemGroup":null,
"lastTempTable":null,
"lastSearch":null,
"lastResults":null,
"clear":function() {
this.lastItemGroup = null;
this.lastSearch = null;
this.lastTempTable = null;
if(this.lastTempTable) {
Zotero.DB.query("DROP TABLE "+this.lastTempTable);
}
this.lastResults = null;
}
};
Zotero.ItemGroup = function(type, ref) Zotero.ItemGroup = function(type, ref)
{ {
this.type = type; this.type = type;
@ -1947,24 +1964,8 @@ Zotero.ItemGroup.prototype.getItems = function()
return []; return [];
} }
var s = this.getSearchObject();
// FIXME: Hack to exclude group libraries for now
if (this.isSearch()) {
var currentLibraryID = this.ref.libraryID;
if (currentLibraryID) {
s.addCondition('libraryID', 'is', currentLibraryID);
}
else {
var groups = Zotero.Groups.getAll();
for each(var group in groups) {
s.addCondition('libraryID', 'isNot', group.libraryID);
}
}
}
try { try {
var ids = s.search(); var ids = this.getSearchResults();
} }
catch (e) { catch (e) {
Zotero.DB.rollbackAllTransactions(); Zotero.DB.rollbackAllTransactions();
@ -1975,6 +1976,40 @@ Zotero.ItemGroup.prototype.getItems = function()
return Zotero.Items.get(ids); return Zotero.Items.get(ids);
} }
Zotero.ItemGroup.prototype.getSearchResults = function(asTempTable) {
if(Zotero.ItemGroupCache.lastItemGroup !== this) {
Zotero.ItemGroupCache.clear();
}
if(!Zotero.ItemGroupCache.lastResults) {
var s = this.getSearchObject();
// FIXME: Hack to exclude group libraries for now
if (this.isSearch()) {
var currentLibraryID = this.ref.libraryID;
if (currentLibraryID) {
s.addCondition('libraryID', 'is', currentLibraryID);
}
else {
var groups = Zotero.Groups.getAll();
for each(var group in groups) {
s.addCondition('libraryID', 'isNot', group.libraryID);
}
}
}
Zotero.ItemGroupCache.lastResults = s.search();
Zotero.ItemGroupCache.lastItemGroup = this;
}
if(asTempTable) {
if(!Zotero.ItemGroupCache.lastTempTable) {
Zotero.ItemGroupCache.lastTempTable = Zotero.Search.idsToTempTable(Zotero.ItemGroupCache.lastResults);
}
return Zotero.ItemGroupCache.lastTempTable;
}
return Zotero.ItemGroupCache.lastResults;
}
/* /*
* Returns the search object for the currently display * Returns the search object for the currently display
@ -1982,6 +2017,14 @@ Zotero.ItemGroup.prototype.getItems = function()
* This accounts for the collection, saved search, quicksearch, tags, etc. * This accounts for the collection, saved search, quicksearch, tags, etc.
*/ */
Zotero.ItemGroup.prototype.getSearchObject = function() { Zotero.ItemGroup.prototype.getSearchObject = function() {
if(Zotero.ItemGroupCache.lastItemGroup !== this) {
Zotero.ItemGroupCache.clear();
}
if(Zotero.ItemGroupCache.lastSearch) {
return Zotero.ItemGroupCache.lastSearch;
}
var includeScopeChildren = false; var includeScopeChildren = false;
// Create/load the inner search // Create/load the inner search
@ -2040,6 +2083,8 @@ Zotero.ItemGroup.prototype.getSearchObject = function() {
} }
} }
Zotero.ItemGroupCache.lastItemGroup = this;
Zotero.ItemGroupCache.lastSearch = s2;
return s2; return s2;
} }
@ -2060,18 +2105,22 @@ Zotero.ItemGroup.prototype.getChildTags = function() {
return false; return false;
} }
var s = this.getSearchObject(); return Zotero.Tags.getAllWithinSearch(this.getSearchObject(),
return Zotero.Tags.getAllWithinSearch(s); undefined, this.getSearchResults(true));
} }
Zotero.ItemGroup.prototype.setSearch = function(searchText) Zotero.ItemGroup.prototype.setSearch = function(searchText)
{ {
if(searchText !== this.searchText) {
Zotero.ItemGroupCache.clear();
}
this.searchText = searchText; this.searchText = searchText;
} }
Zotero.ItemGroup.prototype.setTags = function(tags) Zotero.ItemGroup.prototype.setTags = function(tags)
{ {
Zotero.ItemGroupCache.clear();
this.tags = tags; this.tags = tags;
} }

View file

@ -199,23 +199,25 @@ Zotero.Tags = new function() {
* *
* _types_ is an optional array of tag types to fetch * _types_ is an optional array of tag types to fetch
*/ */
function getAllWithinSearch(search, types) { function getAllWithinSearch(search, types, tmpTable) {
// Save search results to temporary table // Save search results to temporary table
try { if(!tmpTable) {
var tmpTable = search.search(true); try {
} var tmpTable = search.search(true);
catch (e) {
if (typeof e == 'string'
&& e.match(/Saved search [0-9]+ does not exist/)) {
Zotero.DB.rollbackTransaction();
Zotero.debug(e, 2);
} }
else { catch (e) {
throw (e); if (typeof e == 'string'
&& e.match(/Saved search [0-9]+ does not exist/)) {
Zotero.DB.rollbackTransaction();
Zotero.debug(e, 2);
}
else {
throw (e);
}
}
if (!tmpTable) {
return {};
} }
}
if (!tmpTable) {
return {};
} }
var sql = "SELECT DISTINCT tagID, name, type FROM itemTags " var sql = "SELECT DISTINCT tagID, name, type FROM itemTags "
@ -226,7 +228,9 @@ Zotero.Tags = new function() {
} }
var tags = Zotero.DB.query(sql); var tags = Zotero.DB.query(sql);
Zotero.DB.query("DROP TABLE " + tmpTable); if(!tmpTable) {
Zotero.DB.query("DROP TABLE " + tmpTable);
}
if (!tags) { if (!tags) {
return {}; return {};

View file

@ -63,6 +63,7 @@ Zotero.ItemTreeView.prototype.addCallback = function(callback) {
Zotero.ItemTreeView.prototype._runCallbacks = function() { Zotero.ItemTreeView.prototype._runCallbacks = function() {
for each(var cb in this._callbacks) { for each(var cb in this._callbacks) {
this.showLoadingMessageIfNecessary();
cb(); cb();
} }
} }

View file

@ -651,7 +651,7 @@ Zotero.Search.prototype.search = function(asTempTable){
return false; return false;
} }
var tmpTable = this._idsToTempTable(ids); var tmpTable = Zotero.Search.idsToTempTable(ids);
} }
// Otherwise, just copy to temp table directly // Otherwise, just copy to temp table directly
else { else {
@ -720,7 +720,7 @@ Zotero.Search.prototype.search = function(asTempTable){
if (joinMode == 'any') { if (joinMode == 'any') {
if (!tmpTable) { if (!tmpTable) {
Zotero.DB.beginTransaction(); Zotero.DB.beginTransaction();
var tmpTable = this._idsToTempTable(ids); var tmpTable = Zotero.Search.idsToTempTable(ids);
} }
var sql = "SELECT itemID FROM items WHERE " var sql = "SELECT itemID FROM items WHERE "
@ -839,7 +839,7 @@ Zotero.Search.prototype.search = function(asTempTable){
if (this.hasPostSearchFilter() && if (this.hasPostSearchFilter() &&
(includeParentsAndChildren || includeParents || includeChildren)) { (includeParentsAndChildren || includeParents || includeChildren)) {
Zotero.DB.beginTransaction(); Zotero.DB.beginTransaction();
var tmpTable = this._idsToTempTable(ids); var tmpTable = Zotero.Search.idsToTempTable(ids);
if (includeParentsAndChildren || includeParents) { if (includeParentsAndChildren || includeParents) {
//Zotero.debug("Adding parent items to result set"); //Zotero.debug("Adding parent items to result set");
@ -890,7 +890,8 @@ Zotero.Search.prototype.search = function(asTempTable){
} }
if (asTempTable) { if (asTempTable) {
return this._idsToTempTable(ids); var table = Zotero.Search.idsToTempTable(ids);
return table;
} }
return ids; return ids;
@ -951,7 +952,7 @@ Zotero.Search.prototype._prepFieldChange = function (field) {
/* /*
* Batch insert * Batch insert
*/ */
Zotero.Search.prototype._idsToTempTable = function (ids) { Zotero.Search.idsToTempTable = function (ids) {
var tmpTable = "tmpSearchResults_" + Zotero.randomString(8); var tmpTable = "tmpSearchResults_" + Zotero.randomString(8);
Zotero.DB.beginTransaction(); Zotero.DB.beginTransaction();