- Fix bug in Zotero.ID that could cause seemingly random SQLite constraint errors, particularly with large syncs and imports

- Closes #1090, Increase Zotero.ID.get() interval, which should speed up large syncs and imports
- MozMill tests
This commit is contained in:
Dan Stillman 2009-08-07 13:05:56 +00:00
parent f058ab8ecf
commit 49cc44c75b

View file

@ -20,16 +20,25 @@
***** END LICENSE BLOCK ***** ***** END LICENSE BLOCK *****
*/ */
Zotero.ID = new function () { Zotero.ID_Tracker = function () {
this.get = get; this.get = get;
this.getKey = getKey; this.getKey = getKey;
this.getBigInt = getBigInt; this.getBigInt = getBigInt;
this.skip = skip; this.skip = skip;
this.getTableName = getTableName; this.getTableName = getTableName;
_available = {}; // Number of ids to compare against at a time
_min = {}; this.__defineGetter__('numIDs', function () 10000);
_skip = {};
// Number of times to try increasing the maxID if first range fails
this.__defineGetter__('maxTries', function () 3);
// Total number of ids to find
this.__defineGetter__('maxToFind', function () 1000);
var _available = {};
var _min = {};
var _skip = {};
/* /*
@ -141,7 +150,8 @@ Zotero.ID = new function () {
/* /*
* Returns the lowest available unused primary key id for table * Returns the lowest available unused primary key id for table,
* or NULL if none could be loaded in _loadAvailable()
*/ */
function _getNextAvailable(table) { function _getNextAvailable(table) {
if (!_available[table]) { if (!_available[table]) {
@ -150,26 +160,30 @@ Zotero.ID = new function () {
var arr = _available[table]; var arr = _available[table];
for (var i in arr) { while (arr[0]) {
var id = arr[i][0]; var id = arr[0][0];
if (_skip[table] && _skip[table][id]) {
continue;
}
// End of range -- remove range // End of range -- remove range
if (id == arr[i][1]) { if (id == arr[0][1]) {
arr.shift(); arr.shift();
}
// Within range -- increment
else {
arr[i][0]++;
}
// Prepare table for refresh if all rows used // Prepare table for refresh if all rows used
if (arr.length == 0) { if (arr.length == 0) {
delete _available[table]; delete _available[table];
} }
}
// Within range -- increment
else {
arr[0][0]++;
}
if (_skip[table] && _skip[table][id]) {
Zotero.debug("Skipping " + table + " id " + id);
if (!_available[table]) {
_loadAvailable(table);
}
continue;
}
_min[table] = id; _min[table] = id;
return id; return id;
@ -212,9 +226,9 @@ Zotero.ID = new function () {
Zotero.debug("Loading available ids for table '" + table + "'"); Zotero.debug("Loading available ids for table '" + table + "'");
var minID = _min[table] ? _min[table] + 1 : 1; var minID = _min[table] ? _min[table] + 1 : 1;
var numIDs = 3; // Number of ids to compare against at a time var numIDs = Zotero.ID.numIDs;
var maxTries = 3; // Number of times to try increasing the maxID var maxTries = Zotero.ID.maxTries;
var maxToFind = 1000; var maxToFind = Zotero.ID.maxToFind;
var column = _getTableColumn(table); var column = _getTableColumn(table);
@ -239,9 +253,9 @@ Zotero.ID = new function () {
var sql = "SELECT " + column + " FROM " + table var sql = "SELECT " + column + " FROM " + table
+ " WHERE " + column + " BETWEEN ? AND ? ORDER BY " + column; + " WHERE " + column + " BETWEEN ? AND ? ORDER BY " + column;
var ids = Zotero.DB.columnQuery(sql, [minID, maxID]); var ids = Zotero.DB.columnQuery(sql, [minID, maxID]);
// If no ids found, we have maxID unused ids // If no ids found, we have numIDs unused ids
if (!ids) { if (!ids) {
maxID = Math.min(maxID, maxToFind); maxID = Math.min(maxID, minID + (maxToFind - 1));
Zotero.debug("Found " + (maxID - minID + 1) + " available ids in table '" + table + "'"); Zotero.debug("Found " + (maxID - minID + 1) + " available ids in table '" + table + "'");
_available[table] = [[minID, maxID]]; _available[table] = [[minID, maxID]];
return; return;
@ -256,9 +270,10 @@ Zotero.ID = new function () {
maxTries--; maxTries--;
} }
// Didn't find any unused ids // Didn't find any unused ids -- _getNextAvailable() will return NULL for
// this table for rest of session
if (ids.length == numIDs) { if (ids.length == numIDs) {
Zotero.debug("Found 0 available ids in table '" + table + "'"); Zotero.debug("Found no available ids in table '" + table + "'");
_available[table] = []; _available[table] = [];
return; return;
} }
@ -353,7 +368,7 @@ Zotero.ID = new function () {
} }
} }
Zotero.ID = new Zotero.ID_Tracker;
/** /**
* Notifier observer to mark saved object ids as used * Notifier observer to mark saved object ids as used