Asynchronous DB queries for autocomplete -- for Elena, although I'm not sure this actually speeds things up. Needs testing with massive databases on slow machines.
This commit is contained in:
parent
39ab82f9db
commit
4912e9d09c
1 changed files with 127 additions and 110 deletions
|
@ -29,76 +29,10 @@ const Ci = Components.interfaces;
|
||||||
const Cr = Components.results;
|
const Cr = Components.results;
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Implements nsIAutoCompleteResult
|
|
||||||
*/
|
|
||||||
function ZoteroAutoCompleteResult(searchString, searchResult, defaultIndex,
|
|
||||||
errorDescription, results, comments){
|
|
||||||
this._searchString = searchString;
|
|
||||||
this._searchResult = searchResult;
|
|
||||||
this._defaultIndex = defaultIndex;
|
|
||||||
this._errorDescription = errorDescription;
|
|
||||||
this._results = results;
|
|
||||||
this._comments = comments;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ZoteroAutoCompleteResult.prototype = {
|
|
||||||
_searchString: "",
|
|
||||||
_searchResult: 0,
|
|
||||||
_defaultIndex: 0,
|
|
||||||
_errorDescription: "",
|
|
||||||
_results: [],
|
|
||||||
_comments: [],
|
|
||||||
get searchString(){ return this._searchString; },
|
|
||||||
get searchResult(){ return this._searchResult; },
|
|
||||||
get defaultIndex(){ return this._defaultIndex; },
|
|
||||||
get errorDescription(){ return this._errorDescription; },
|
|
||||||
get matchCount(){ return this._results.length; }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ZoteroAutoCompleteResult.prototype.getCommentAt = function(index){
|
|
||||||
return this._comments[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ZoteroAutoCompleteResult.prototype.getImageAt = function(index) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ZoteroAutoCompleteResult.prototype.getStyleAt = function(index){
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ZoteroAutoCompleteResult.prototype.getValueAt = function(index){
|
|
||||||
return this._results[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ZoteroAutoCompleteResult.prototype.removeValueAt = function(index){
|
|
||||||
this._results.splice(index, 1);
|
|
||||||
this._comments.splice(index, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ZoteroAutoCompleteResult.prototype.QueryInterface = function(iid){
|
|
||||||
if (!iid.equals(Ci.nsIAutoCompleteResult) &&
|
|
||||||
!iid.equals(Ci.nsISupports)){
|
|
||||||
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Implements nsIAutoCompleteSearch
|
* Implements nsIAutoCompleteSearch
|
||||||
*/
|
*/
|
||||||
function ZoteroAutoComplete(){
|
function ZoteroAutoComplete() {
|
||||||
// Get the Zotero object
|
// Get the Zotero object
|
||||||
this._zotero = Components.classes["@zotero.org/Zotero;1"]
|
this._zotero = Components.classes["@zotero.org/Zotero;1"]
|
||||||
.getService(Components.interfaces.nsISupports)
|
.getService(Components.interfaces.nsISupports)
|
||||||
|
@ -106,18 +40,22 @@ function ZoteroAutoComplete(){
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ZoteroAutoComplete.prototype.startSearch = function(searchString, searchParam,
|
ZoteroAutoComplete.prototype.startSearch = function(searchString, searchParam, previousResult, listener) {
|
||||||
previousResult, listener){
|
var result = Cc["@mozilla.org/autocomplete/simple-result;1"]
|
||||||
|
.createInstance(Ci.nsIAutoCompleteSimpleResult);
|
||||||
|
result.setSearchString(searchString);
|
||||||
|
|
||||||
|
this._result = result;
|
||||||
|
this._results = [];
|
||||||
|
this._listener = listener;
|
||||||
|
|
||||||
|
this._zotero.debug("Starting autocomplete search of type '"
|
||||||
|
+ searchParam + "'" + " with string '" + searchString + "'");
|
||||||
|
|
||||||
this.stopSearch();
|
this.stopSearch();
|
||||||
|
|
||||||
/*
|
var self = this;
|
||||||
this._zotero.debug("Starting autocomplete search of type '"
|
var statement;
|
||||||
+ searchParam + "'" + " with string '" + searchString + "'");
|
|
||||||
*/
|
|
||||||
|
|
||||||
var results = [];
|
|
||||||
var comments = [];
|
|
||||||
|
|
||||||
// Allow extra parameters to be passed in
|
// Allow extra parameters to be passed in
|
||||||
var pos = searchParam.indexOf('/');
|
var pos = searchParam.indexOf('/');
|
||||||
|
@ -129,12 +67,12 @@ ZoteroAutoComplete.prototype.startSearch = function(searchString, searchParam,
|
||||||
var searchParts = searchParam.split('-');
|
var searchParts = searchParam.split('-');
|
||||||
searchParam = searchParts[0];
|
searchParam = searchParts[0];
|
||||||
|
|
||||||
switch (searchParam){
|
switch (searchParam) {
|
||||||
case '':
|
case '':
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'tag':
|
case 'tag':
|
||||||
var sql = "SELECT DISTINCT name FROM tags WHERE name LIKE ?";
|
var sql = "SELECT DISTINCT name, NULL FROM tags WHERE name LIKE ?";
|
||||||
var sqlParams = [searchString + '%'];
|
var sqlParams = [searchString + '%'];
|
||||||
if (extra){
|
if (extra){
|
||||||
sql += " AND name NOT IN (SELECT name FROM tags WHERE tagID IN ("
|
sql += " AND name NOT IN (SELECT name FROM tags WHERE tagID IN ("
|
||||||
|
@ -142,9 +80,10 @@ ZoteroAutoComplete.prototype.startSearch = function(searchString, searchParam,
|
||||||
sqlParams.push(extra);
|
sqlParams.push(extra);
|
||||||
}
|
}
|
||||||
|
|
||||||
var results = this._zotero.DB.columnQuery(sql, sqlParams);
|
statement = this._zotero.DB.getStatement(sql, sqlParams);
|
||||||
if (results) {
|
|
||||||
var collation = this._zotero.getLocaleCollation();
|
var resultsCallback = function (results) {
|
||||||
|
var collation = self._zotero.getLocaleCollation();
|
||||||
results.sort(function(a, b) {
|
results.sort(function(a, b) {
|
||||||
return collation.compareString(1, a, b);
|
return collation.compareString(1, a, b);
|
||||||
});
|
});
|
||||||
|
@ -161,7 +100,7 @@ ZoteroAutoComplete.prototype.startSearch = function(searchString, searchParam,
|
||||||
if (fieldMode==2)
|
if (fieldMode==2)
|
||||||
{
|
{
|
||||||
var sql = "SELECT DISTINCT CASE fieldMode WHEN 1 THEN lastName "
|
var sql = "SELECT DISTINCT CASE fieldMode WHEN 1 THEN lastName "
|
||||||
+ "WHEN 0 THEN firstName || ' ' || lastName END AS name "
|
+ "WHEN 0 THEN firstName || ' ' || lastName END AS name, NULL "
|
||||||
+ "FROM creators NATURAL JOIN creatorData WHERE CASE fieldMode "
|
+ "FROM creators NATURAL JOIN creatorData WHERE CASE fieldMode "
|
||||||
+ "WHEN 1 THEN lastName "
|
+ "WHEN 1 THEN lastName "
|
||||||
+ "WHEN 0 THEN firstName || ' ' || lastName END "
|
+ "WHEN 0 THEN firstName || ' ' || lastName END "
|
||||||
|
@ -213,37 +152,30 @@ ZoteroAutoComplete.prototype.startSearch = function(searchString, searchParam,
|
||||||
sql += " ORDER BY name";
|
sql += " ORDER BY name";
|
||||||
}
|
}
|
||||||
|
|
||||||
var rows = this._zotero.DB.query(sql, sqlParams);
|
statement = this._zotero.DB.getStatement(sql, sqlParams);
|
||||||
for each(var row in rows){
|
|
||||||
results.push(row['name']);
|
|
||||||
comments.push(row['creatorID'])
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'dateModified':
|
case 'dateModified':
|
||||||
case 'dateAdded':
|
case 'dateAdded':
|
||||||
var sql = "SELECT DISTINCT DATE(" + searchParam + ", 'localtime') FROM items "
|
var sql = "SELECT DISTINCT DATE(" + searchParam + ", 'localtime'), NULL FROM items "
|
||||||
+ "WHERE " + searchParam + " LIKE ? ORDER BY " + searchParam;
|
+ "WHERE " + searchParam + " LIKE ? ORDER BY " + searchParam;
|
||||||
var results = this._zotero.DB.columnQuery(sql, searchString + '%');
|
statement = this._zotero.DB.getStatement(sql, searchString + '%');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'accessDate':
|
case 'accessDate':
|
||||||
var fieldID = this._zotero.ItemFields.getID('accessDate');
|
var fieldID = this._zotero.ItemFields.getID('accessDate');
|
||||||
|
|
||||||
var sql = "SELECT DISTINCT DATE(value, 'localtime') FROM itemData "
|
var sql = "SELECT DISTINCT DATE(value, 'localtime'), NULL FROM itemData "
|
||||||
+ "WHERE fieldID=? AND value LIKE ? ORDER BY value";
|
+ "WHERE fieldID=? AND value LIKE ? ORDER BY value";
|
||||||
var results = this._zotero.DB.columnQuery(sql, [fieldID, searchString + '%']);
|
statement = this._zotero.DB.getStatement(sql, [fieldID, searchString + '%']);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
var sql = "SELECT fieldID FROM fields WHERE fieldName=?";
|
var fieldID = this._zotero.ItemFields.getID(searchParam);
|
||||||
var fieldID = this._zotero.DB.valueQuery(sql, {string:searchParam});
|
if (!fieldID) {
|
||||||
|
|
||||||
if (!fieldID){
|
|
||||||
this._zotero.debug("'" + searchParam + "' is not a valid autocomplete scope", 1);
|
this._zotero.debug("'" + searchParam + "' is not a valid autocomplete scope", 1);
|
||||||
var results = [];
|
this.updateResults([], false, Ci.nsIAutoCompleteResult.RESULT_IGNORED);
|
||||||
var resultCode = Ci.nsIAutoCompleteResult.RESULT_IGNORED;
|
return;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// We don't use date autocomplete anywhere, but if we're not
|
// We don't use date autocomplete anywhere, but if we're not
|
||||||
|
@ -251,8 +183,8 @@ ZoteroAutoComplete.prototype.startSearch = function(searchString, searchParam,
|
||||||
// use the user part of the multipart field
|
// use the user part of the multipart field
|
||||||
var valueField = searchParam=='date' ? 'SUBSTR(value, 12, 100)' : 'value';
|
var valueField = searchParam=='date' ? 'SUBSTR(value, 12, 100)' : 'value';
|
||||||
|
|
||||||
var sql = "SELECT DISTINCT " + valueField
|
var sql = "SELECT DISTINCT " + valueField + ", NULL "
|
||||||
+ " FROM itemData NATURAL JOIN itemDataValues "
|
+ "FROM itemData NATURAL JOIN itemDataValues "
|
||||||
+ "WHERE fieldID=?1 AND " + valueField
|
+ "WHERE fieldID=?1 AND " + valueField
|
||||||
+ " LIKE ?2 "
|
+ " LIKE ?2 "
|
||||||
|
|
||||||
|
@ -263,26 +195,111 @@ ZoteroAutoComplete.prototype.startSearch = function(searchString, searchParam,
|
||||||
sqlParams.push(extra);
|
sqlParams.push(extra);
|
||||||
}
|
}
|
||||||
sql += "ORDER BY value";
|
sql += "ORDER BY value";
|
||||||
var results = this._zotero.DB.columnQuery(sql, sqlParams);
|
statement = this._zotero.DB.getStatement(sql, sqlParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!results || !results.length){
|
this.pendingStatement = statement.executeAsync({
|
||||||
var results = [];
|
handleResult: function (storageResultSet) {
|
||||||
var resultCode = Ci.nsIAutoCompleteResult.RESULT_NOMATCH;
|
self._zotero.debug("Handling autocomplete results");
|
||||||
|
|
||||||
|
var results = [];
|
||||||
|
var comments = [];
|
||||||
|
|
||||||
|
for (let row = storageResultSet.getNextRow();
|
||||||
|
row;
|
||||||
|
row = storageResultSet.getNextRow()) {
|
||||||
|
results.push(row.getResultByIndex(0));
|
||||||
|
let comment = row.getResultByIndex(1);
|
||||||
|
if (comment) {
|
||||||
|
comments.push(comment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (resultsCallback) {
|
||||||
|
if (comments.length) {
|
||||||
|
throw ("Cannot sort results with comments in ZoteroAutoComplete.startSearch()");
|
||||||
|
}
|
||||||
|
resultsCallback(results);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.updateResults(results, comments, true);
|
||||||
|
},
|
||||||
|
|
||||||
|
handleError: function (e) {
|
||||||
|
Components.utils.reportError(e.message);
|
||||||
|
},
|
||||||
|
|
||||||
|
handleCompletion: function (reason) {
|
||||||
|
self.pendingStatement = null;
|
||||||
|
|
||||||
|
if (reason != Ci.mozIStorageStatementCallback.REASON_FINISHED) {
|
||||||
|
var resultCode = Ci.nsIAutoCompleteResult.RESULT_FAILURE;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var resultCode = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.updateResults(null, null, false, resultCode);
|
||||||
|
|
||||||
|
if (resultCode) {
|
||||||
|
self._zotero.debug("Autocomplete query aborted");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
self._zotero.debug("Autocomplete query completed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ZoteroAutoComplete.prototype.updateResults = function (results, comments, ongoing, resultCode) {
|
||||||
|
if (!results) {
|
||||||
|
results = [];
|
||||||
}
|
}
|
||||||
else if (typeof resultCode == 'undefined'){
|
if (!comments) {
|
||||||
var resultCode = Ci.nsIAutoCompleteResult.RESULT_SUCCESS;
|
comments = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = new ZoteroAutoCompleteResult(searchString,
|
for (var i=0; i<results.length; i++) {
|
||||||
resultCode, 0, "", results, comments);
|
let result = results[i];
|
||||||
|
|
||||||
|
if (this._results.indexOf(result) == -1) {
|
||||||
|
comment = comments[i] ? comments[i] : null;
|
||||||
|
this._zotero.debug("Appending autocomplete value '" + result + "'" + (comment ? " (" + comment + ")" : ""));
|
||||||
|
this._result.appendMatch(result, comment, null, null);
|
||||||
|
this._results.push(result);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
//this._zotero.debug("Skipping existing value '" + result + "'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
listener.onSearchResult(this, result);
|
if (!resultCode) {
|
||||||
|
resultCode = "RESULT_";
|
||||||
|
if (!this._results.length) {
|
||||||
|
resultCode += "NOMATCH";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
resultCode += "SUCCESS";
|
||||||
|
}
|
||||||
|
if (ongoing) {
|
||||||
|
resultCode += "_ONGOING";
|
||||||
|
}
|
||||||
|
resultCode = Ci.nsIAutoCompleteResult[resultCode];
|
||||||
|
}
|
||||||
|
|
||||||
|
this._result.setSearchResult(resultCode);
|
||||||
|
this._listener.onSearchResult(this, this._result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ZoteroAutoComplete.prototype.stopSearch = function(){
|
ZoteroAutoComplete.prototype.stopSearch = function(){
|
||||||
//this._zotero.debug('Stopping autocomplete search');
|
if (this.pendingStatement) {
|
||||||
|
// DEBUG: This appears to take as long as letting the query complete
|
||||||
|
this._zotero.debug('Stopping autocomplete search');
|
||||||
|
this.pendingStatement.cancel();
|
||||||
|
this._zotero.debug('Search cancelled');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue