2007-12-10 21:38:53 -08:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Bookmarks Sync.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2007
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Dan Mills <thunder@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2008-06-03 14:45:53 -07:00
|
|
|
const EXPORTED_SYMBOLS = ['Store',
|
|
|
|
'SnapshotStore'];
|
2007-12-10 21:38:53 -08:00
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2007-12-10 21:38:53 -08:00
|
|
|
Cu.import("resource://weave/log4moz.js");
|
|
|
|
Cu.import("resource://weave/constants.js");
|
|
|
|
Cu.import("resource://weave/util.js");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Data Stores
|
|
|
|
* These can wrap, serialize items and apply commands
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Store() {
|
|
|
|
this._init();
|
|
|
|
}
|
|
|
|
Store.prototype = {
|
2008-12-08 09:53:32 -08:00
|
|
|
_logName: "BaseClass",
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-06-27 20:16:43 -07:00
|
|
|
// set this property in child object's wrap()!
|
|
|
|
_lookup: null,
|
|
|
|
|
2008-12-30 23:52:20 -08:00
|
|
|
get cache() {
|
2009-01-02 13:36:28 -08:00
|
|
|
let cache = new Cache();
|
2008-12-30 23:52:20 -08:00
|
|
|
this.__defineGetter__("cache", function() cache);
|
|
|
|
return cache;
|
|
|
|
},
|
|
|
|
|
2007-12-10 21:38:53 -08:00
|
|
|
_init: function Store__init() {
|
2008-12-08 09:53:32 -08:00
|
|
|
this._log = Log4Moz.repository.getLogger("Store." + this._logName);
|
2009-07-14 18:34:03 -07:00
|
|
|
let level = Svc.Prefs.get("log.logger.engine." + this.name, "Debug");
|
|
|
|
this._log.level = Log4Moz.Level[level];
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2009-06-05 00:36:11 -07:00
|
|
|
applyIncoming: function Store_applyIncoming(record) {
|
|
|
|
if (record.deleted)
|
|
|
|
this.remove(record);
|
|
|
|
else if (!this.itemExists(record.id))
|
|
|
|
this.create(record);
|
|
|
|
else
|
|
|
|
this.update(record);
|
2008-12-12 13:55:26 -08:00
|
|
|
},
|
|
|
|
|
2007-12-19 13:24:31 -08:00
|
|
|
// override these in derived objects
|
2008-06-30 23:25:51 -07:00
|
|
|
|
2009-03-31 23:56:32 -07:00
|
|
|
create: function Store_create(record) {
|
|
|
|
throw "override create in a subclass";
|
|
|
|
},
|
|
|
|
|
|
|
|
remove: function Store_remove(record) {
|
|
|
|
throw "override remove in a subclass";
|
|
|
|
},
|
|
|
|
|
|
|
|
update: function Store_update(record) {
|
|
|
|
throw "override update in a subclass";
|
|
|
|
},
|
|
|
|
|
2008-12-29 23:28:17 -08:00
|
|
|
itemExists: function Store_itemExists(id) {
|
|
|
|
throw "override itemExists in a subclass";
|
2008-06-27 20:16:43 -07:00
|
|
|
},
|
2008-06-30 23:25:51 -07:00
|
|
|
|
2009-01-27 17:23:18 -08:00
|
|
|
createRecord: function Store_createRecord(id) {
|
2008-12-29 23:28:17 -08:00
|
|
|
throw "override createRecord in a subclass";
|
2008-06-27 20:16:43 -07:00
|
|
|
},
|
2008-06-29 07:07:11 -07:00
|
|
|
|
2008-12-09 12:26:14 -08:00
|
|
|
changeItemID: function Store_changeItemID(oldID, newID) {
|
|
|
|
throw "override changeItemID in a subclass";
|
|
|
|
},
|
|
|
|
|
2008-12-08 09:53:32 -08:00
|
|
|
getAllIDs: function Store_getAllIDs() {
|
|
|
|
throw "override getAllIDs in a subclass";
|
2008-06-29 07:07:11 -07:00
|
|
|
},
|
2008-12-08 09:53:32 -08:00
|
|
|
|
|
|
|
wipe: function Store_wipe() {
|
|
|
|
throw "override wipe in a subclass";
|
2008-06-29 07:00:53 -07:00
|
|
|
}
|
2007-12-10 21:38:53 -08:00
|
|
|
};
|
|
|
|
|
2008-12-30 23:52:20 -08:00
|
|
|
function Cache() {
|
|
|
|
this.count = 0;
|
|
|
|
this.maxItems = 100;
|
|
|
|
this.fifo = true;
|
|
|
|
this.enabled = true;
|
|
|
|
this._head = this._tail = null;
|
|
|
|
this._items = {};
|
|
|
|
}
|
|
|
|
Cache.prototype = {
|
2009-01-07 17:45:17 -08:00
|
|
|
remove: function Cache_remove(item) {
|
|
|
|
if (this.count <= 0 || this.count == 1) {
|
2008-12-30 23:52:20 -08:00
|
|
|
this.clear();
|
2009-01-07 17:45:17 -08:00
|
|
|
return;
|
2008-12-30 23:52:20 -08:00
|
|
|
}
|
2009-01-07 17:45:17 -08:00
|
|
|
|
|
|
|
item.next.prev = item.prev;
|
|
|
|
item.prev.next = item.next;
|
|
|
|
|
|
|
|
if (item == this._head)
|
|
|
|
this._head = item.next;
|
|
|
|
if (item == this._tail)
|
|
|
|
this._tail = item.prev;
|
|
|
|
|
|
|
|
item.next = null;
|
|
|
|
item.prev = null;
|
|
|
|
|
|
|
|
delete this._items[item.id];
|
|
|
|
this.count--;
|
|
|
|
},
|
|
|
|
pop: function Cache_pop() {
|
|
|
|
if (this.fifo)
|
|
|
|
this.remove(this._tail);
|
|
|
|
else
|
|
|
|
this.remove(this._head);
|
2008-12-30 23:52:20 -08:00
|
|
|
},
|
|
|
|
put: function Cache_put(id, item) {
|
|
|
|
if (!this.enabled)
|
|
|
|
return;
|
|
|
|
|
2009-01-07 17:45:17 -08:00
|
|
|
let wrapper = {id: id, item: item};
|
|
|
|
|
|
|
|
if (this._head === null) {
|
|
|
|
wrapper.next = wrapper;
|
|
|
|
wrapper.prev = wrapper;
|
2008-12-30 23:52:20 -08:00
|
|
|
this._head = wrapper;
|
2009-01-07 17:45:17 -08:00
|
|
|
this._tail = wrapper;
|
2008-12-30 23:52:20 -08:00
|
|
|
} else {
|
2009-01-07 17:45:17 -08:00
|
|
|
wrapper.next = this._tail;
|
|
|
|
wrapper.prev = this._head;
|
|
|
|
this._head.prev = wrapper;
|
|
|
|
this._tail.next = wrapper;
|
2008-12-30 23:52:20 -08:00
|
|
|
this._tail = wrapper;
|
|
|
|
}
|
2009-01-27 17:23:18 -08:00
|
|
|
|
2009-01-07 17:45:17 -08:00
|
|
|
this._items[wrapper.id] = wrapper;
|
2008-12-30 23:52:20 -08:00
|
|
|
this.count++;
|
2009-01-07 17:45:17 -08:00
|
|
|
|
2008-12-30 23:52:20 -08:00
|
|
|
if (this.count >= this.maxItems)
|
2009-01-07 17:45:17 -08:00
|
|
|
this.pop();
|
2008-12-30 23:52:20 -08:00
|
|
|
},
|
|
|
|
get: function Cache_get(id) {
|
|
|
|
if (id in this._items)
|
|
|
|
return this._items[id].item;
|
|
|
|
return undefined;
|
|
|
|
},
|
|
|
|
clear: function Cache_clear() {
|
|
|
|
this.count = 0;
|
|
|
|
this._head = null;
|
|
|
|
this._tail = null;
|
|
|
|
this._items = {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
function SnapshotStore(name) {
|
|
|
|
this._init(name);
|
2007-12-10 21:38:53 -08:00
|
|
|
}
|
|
|
|
SnapshotStore.prototype = {
|
2007-12-19 13:24:31 -08:00
|
|
|
_logName: "SnapStore",
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
_filename: null,
|
|
|
|
get filename() {
|
|
|
|
if (this._filename === null)
|
|
|
|
throw "filename is null";
|
|
|
|
return this._filename;
|
|
|
|
},
|
|
|
|
set filename(value) {
|
|
|
|
this._filename = value + ".json";
|
|
|
|
},
|
|
|
|
|
2007-12-10 21:38:53 -08:00
|
|
|
// Last synced tree, version, and GUID (to detect if the store has
|
|
|
|
// been completely replaced and invalidate the snapshot)
|
|
|
|
|
|
|
|
_data: {},
|
|
|
|
get data() { return this._data; },
|
|
|
|
set data(value) { this._data = value; },
|
|
|
|
|
|
|
|
_version: 0,
|
|
|
|
get version() { return this._version; },
|
|
|
|
set version(value) { this._version = value; },
|
|
|
|
|
|
|
|
_GUID: null,
|
|
|
|
get GUID() {
|
2008-06-20 12:34:29 -07:00
|
|
|
if (!this._GUID)
|
|
|
|
this._GUID = Utils.makeGUID();
|
2007-12-10 21:38:53 -08:00
|
|
|
return this._GUID;
|
|
|
|
},
|
|
|
|
set GUID(GUID) {
|
|
|
|
this._GUID = GUID;
|
|
|
|
},
|
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
_init: function SStore__init(name) {
|
|
|
|
this.filename = name;
|
2008-11-03 15:00:38 -08:00
|
|
|
this._log = Log4Moz.repository.getLogger("Service." + this._logName);
|
2007-12-14 18:07:25 -08:00
|
|
|
},
|
|
|
|
|
2007-12-19 13:24:31 -08:00
|
|
|
_createCommand: function SStore__createCommand(command) {
|
2008-03-05 00:00:56 -08:00
|
|
|
this._data[command.GUID] = Utils.deepCopy(command.data);
|
2007-12-19 13:24:31 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
_removeCommand: function SStore__removeCommand(command) {
|
|
|
|
delete this._data[command.GUID];
|
|
|
|
},
|
|
|
|
|
|
|
|
_editCommand: function SStore__editCommand(command) {
|
|
|
|
if ("GUID" in command.data) {
|
|
|
|
// special-case guid changes
|
|
|
|
let newGUID = command.data.GUID,
|
2008-08-08 12:34:01 -07:00
|
|
|
oldGUID = command.GUID;
|
2007-12-19 13:24:31 -08:00
|
|
|
|
|
|
|
this._data[newGUID] = this._data[oldGUID];
|
2008-06-18 12:32:20 -07:00
|
|
|
delete this._data[oldGUID];
|
2007-12-19 13:24:31 -08:00
|
|
|
|
|
|
|
for (let GUID in this._data) {
|
2008-12-05 03:36:27 -08:00
|
|
|
if (("parentid" in this._data[GUID]) &&
|
|
|
|
(this._data[GUID].parentid == oldGUID))
|
|
|
|
this._data[GUID].parentid = newGUID;
|
2007-12-19 13:24:31 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let prop in command.data) {
|
|
|
|
if (prop == "GUID")
|
|
|
|
continue;
|
2008-06-30 23:25:51 -07:00
|
|
|
if (command.GUID in this._data)
|
|
|
|
this._data[command.GUID][prop] = command.data[prop];
|
|
|
|
else
|
|
|
|
this._log.warn("Warning! Edit command for unknown item: " + command.GUID);
|
2007-12-19 13:24:31 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2007-12-10 21:38:53 -08:00
|
|
|
save: function SStore_save() {
|
|
|
|
this._log.info("Saving snapshot to disk");
|
|
|
|
|
2008-06-18 14:12:24 -07:00
|
|
|
let file = Utils.getProfileFile(
|
|
|
|
{path: "weave/snapshots/" + this.filename,
|
|
|
|
autoCreate: true}
|
|
|
|
);
|
2007-12-10 21:38:53 -08:00
|
|
|
|
|
|
|
let out = {version: this.version,
|
|
|
|
GUID: this.GUID,
|
|
|
|
snapshot: this.data};
|
2009-03-31 12:09:38 -07:00
|
|
|
out = JSON.stringify(out);
|
2008-03-05 00:00:56 -08:00
|
|
|
|
|
|
|
let [fos] = Utils.open(file, ">");
|
|
|
|
fos.writeString(out);
|
2007-12-10 21:38:53 -08:00
|
|
|
fos.close();
|
|
|
|
},
|
|
|
|
|
|
|
|
load: function SStore_load() {
|
2008-06-18 14:12:24 -07:00
|
|
|
let file = Utils.getProfileFile("weave/snapshots/" + this.filename);
|
2007-12-10 21:38:53 -08:00
|
|
|
if (!file.exists())
|
|
|
|
return;
|
|
|
|
|
2008-03-05 17:11:08 -08:00
|
|
|
try {
|
|
|
|
let [is] = Utils.open(file, "<");
|
|
|
|
let json = Utils.readStream(is);
|
|
|
|
is.close();
|
2009-03-31 12:09:38 -07:00
|
|
|
json = JSON.parse(json);
|
2008-03-05 17:11:08 -08:00
|
|
|
|
|
|
|
if (json && 'snapshot' in json && 'version' in json && 'GUID' in json) {
|
2008-07-11 18:55:42 -07:00
|
|
|
this._log.debug("Read saved snapshot from disk");
|
2008-03-05 17:11:08 -08:00
|
|
|
this.data = json.snapshot;
|
|
|
|
this.version = json.version;
|
|
|
|
this.GUID = json.GUID;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
this._log.warn("Could not parse saved snapshot; reverting to initial-sync state");
|
|
|
|
this._log.debug("Exception: " + e);
|
2007-12-10 21:38:53 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
serialize: function SStore_serialize() {
|
2009-03-31 12:09:38 -07:00
|
|
|
let json = JSON.stringify(this.data);
|
2007-12-10 21:38:53 -08:00
|
|
|
json = json.replace(/:{type/g, ":\n\t{type");
|
|
|
|
json = json.replace(/}, /g, "},\n ");
|
2008-12-05 03:36:27 -08:00
|
|
|
json = json.replace(/, parentid/g, ",\n\t parentid");
|
2007-12-10 21:38:53 -08:00
|
|
|
json = json.replace(/, index/g, ",\n\t index");
|
|
|
|
json = json.replace(/, title/g, ",\n\t title");
|
|
|
|
json = json.replace(/, URI/g, ",\n\t URI");
|
|
|
|
json = json.replace(/, tags/g, ",\n\t tags");
|
|
|
|
json = json.replace(/, keyword/g, ",\n\t keyword");
|
|
|
|
return json;
|
|
|
|
},
|
|
|
|
|
|
|
|
wrap: function SStore_wrap() {
|
2007-12-19 13:24:31 -08:00
|
|
|
return this.data;
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2007-12-19 13:24:31 -08:00
|
|
|
wipe: function SStore_wipe() {
|
|
|
|
this.data = {};
|
|
|
|
this.version = -1;
|
|
|
|
this.GUID = null;
|
|
|
|
this.save();
|
2007-12-10 21:38:53 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
SnapshotStore.prototype.__proto__ = new Store();
|