make RemoteStore a but more Store-like (wrap, wipe methods); fix a missing yield

This commit is contained in:
Dan Mills 2008-06-17 18:03:02 +09:00
parent 5e07cb98d5
commit 0c3a240876
2 changed files with 68 additions and 74 deletions

View File

@ -206,16 +206,7 @@ Engine.prototype = {
_resetServer: function Engine__resetServer() {
let self = yield;
this._log.debug("Resetting server data");
this._remote.status.delete(self.cb);
yield;
this._remote.keys.delete(self.cb);
yield;
this._remote.snapshot.delete(self.cb);
yield;
this._remote.deltas.delete(self.cb);
yield;
this._log.debug("Server files deleted");
yield this._remote.wipe(self.cb);
},
_resetClient: function Engine__resetClient() {
@ -260,7 +251,7 @@ Engine.prototype = {
this._log.info("Beginning sync");
try {
yield this._remote.initSession(self.cb);
yield this._remote.openSession(self.cb);
} catch (e if e.message.status == 404) {
yield this._initialUpload.async(this, self.cb);
return;
@ -279,7 +270,6 @@ Engine.prototype = {
this._log.info("Local snapshot version: " + this._snapshot.version);
this._log.info("Server maxVersion: " + this._remote.status.data.maxVersion);
this._log.debug("Server snapVersion: " + this._remote.status.data.snapVersion);
if ("none" != Utils.prefs.getCharPref("encryption")) {
let symkey = yield this._remote.keys.getKey(self.cb, this.pbeId);
@ -288,7 +278,7 @@ Engine.prototype = {
// 1) Fetch server deltas
let server = {};
let serverSnap = yield this._remote.getLatestFromSnap(self.cb, this._snapshot);
let serverSnap = yield this._remote.wrap(self.cb, this._snapshot);
server.snapshot = serverSnap.data;
this._core.detectUpdates(self.cb, this._snapshot.data, server.snapshot);
server.updates = yield;

View File

@ -340,32 +340,6 @@ Keychain.prototype = {
// FIXME: implement setKey()
};
function Snapshot(remoteStore) {
this._init(remoteStore);
}
Snapshot.prototype = {
__proto__: new Resource(),
_init: function Snapshot__init(remoteStore) {
this._remote = remoteStore;
this.__proto__.__proto__._init.call(this, this._remote.serverPrefix + "snapshot.json");
this.pushFilter(new JsonFilter());
this.pushFilter(new CryptoFilter(remoteStore, "snapshotEncryption"));
}
};
function Deltas(remoteStore) {
this._init(remoteStore);
}
Deltas.prototype = {
__proto__: new Resource(),
_init: function Deltas__init(remoteStore) {
this._remote = remoteStore;
this.__proto__.__proto__._init.call(this, this._remote.serverPrefix + "deltas.json");
this.pushFilter(new JsonFilter());
this.pushFilter(new CryptoFilter(remoteStore, "deltasEncryption"));
}
};
function RemoteStore(engine) {
this._engine = engine;
this._log = Log4Moz.Service.getLogger("Service.RemoteStore");
@ -387,19 +361,23 @@ RemoteStore.prototype = {
return keys;
},
get snapshot() {
let snapshot = new Snapshot(this);
this.__defineGetter__("snapshot", function() snapshot);
get _snapshot() {
let snapshot = new Resource(this.serverPrefix + "snapshot.json");
snapshot.pushFilter(new JsonFilter());
snapshot.pushFilter(new CryptoFilter(this, "snapshotEncryption"));
this.__defineGetter__("_snapshot", function() snapshot);
return snapshot;
},
get deltas() {
let deltas = new Deltas(this);
this.__defineGetter__("deltas", function() deltas);
get _deltas() {
let deltas = new Resource(this.serverPrefix + "deltas.json");
deltas.pushFilter(new JsonFilter());
deltas.pushFilter(new CryptoFilter(this, "deltasEncryption"));
this.__defineGetter__("_deltas", function() deltas);
return deltas;
},
_initSession: function RStore__initSession() {
_openSession: function RStore__openSession() {
let self = yield;
if (!this.serverPrefix || !this.engineId)
@ -407,8 +385,8 @@ RemoteStore.prototype = {
this.status.data = null;
this.keys.data = null;
this.snapshot.data = null;
this.deltas.data = null;
this._snapshot.data = null;
this._deltas.data = null;
DAV.MKCOL(this.serverPrefix, self.cb);
let ret = yield;
@ -428,21 +406,19 @@ RemoteStore.prototype = {
ENGINE_STORAGE_FORMAT_VERSION);
throw "Incompatible remote store format";
}
this._inited = true;
},
initSession: function RStore_initSession(onComplete) {
this._initSession.async(this, onComplete);
openSession: function RStore_openSession(onComplete) {
this._openSession.async(this, onComplete);
},
closeSession: function RStore_closeSession() {
this._inited = false;
this.status.data = null;
this.keys.data = null;
this.snapshot.data = null;
this.deltas.data = null;
this._snapshot.data = null;
this._deltas.data = null;
},
// Does a fresh upload of the given snapshot to a new store
_initialize: function RStore__initialize(snapshot) {
let self = yield;
let symkey;
@ -464,8 +440,8 @@ RemoteStore.prototype = {
keys.ring[this.engineId.username] = symkey;
yield this.keys.put(self.cb, keys);
yield this.snapshot.put(self.cb, snapshot.data);
yield this.deltas.put(self.cb, []);
yield this._snapshot.put(self.cb, snapshot.data);
yield this._deltas.put(self.cb, []);
let c = 0;
for (GUID in snapshot.data)
@ -485,29 +461,31 @@ RemoteStore.prototype = {
this._initialize.async(this, onComplete, snapshot);
},
_appendDelta: function RStore__appendDelta(delta) {
// Removes server files - you may want to run initialize() after this
_wipe: function Engine__wipe() {
let self = yield;
if (this.deltas.data == null) {
yield this.deltas.get(self.cb);
if (this.deltas.data == null)
this.deltas.data = [];
}
this.deltas.data.push(delta);
yield this.deltas.put(self.cb, this.deltas.data);
this._log.debug("Deleting remote store data");
yield this.status.delete(self.cb);
yield this.keys.delete(self.cb);
yield this._snapshot.delete(self.cb);
yield this._deltas.delete(self.cb);
this._log.debug("Server files deleted");
},
appendDelta: function RStore_appendDelta(onComplete, delta) {
this._appendDelta.async(this, onComplete, delta);
wipe: function Engine_wipe(onComplete) {
this._wipe.async(this, onComplete)
},
// Gets the latest server snapshot by downloading all server files
// (snapshot + deltas)
_getLatestFromScratch: function RStore__getLatestFromScratch() {
let self = yield;
this._log.info("Downloading all server data from scratch");
this._log.debug("Downloading server snapshot");
let data = yield this.snapshot.get(self.cb);
let data = yield this._snapshot.get(self.cb);
this._log.debug("Downloading server deltas");
let deltas = yield this.deltas.get(self.cb);
let deltas = yield this._deltas.get(self.cb);
let snap = new SnapshotStore();
snap.version = this.status.data.maxVersion;
@ -520,14 +498,15 @@ RemoteStore.prototype = {
self.done(snap);
},
// Gets the latest server snapshot by downloading only the necessary
// deltas from the given snapshot (but may fall back to a full download)
_getLatestFromSnap: function RStore__getLatestFromSnap(lastSyncSnap) {
let self = yield;
let deltas, snap = new SnapshotStore();
snap.version = this.status.data.maxVersion;
if (lastSyncSnap.version < this.status.data.snapVersion) {
snap = this._getLatestFromScratch.async(this, self.cb);
self.done(snap);
self.done(yield this.getLatestFromScratch(self.cb));
return;
} else if (lastSyncSnap.version >= this.status.data.snapVersion &&
@ -535,7 +514,7 @@ RemoteStore.prototype = {
this._log.debug("Using last sync snapshot as starting point for server snapshot");
snap.data = Utils.deepCopy(lastSyncSnap.data);
this._log.info("Downloading server deltas");
let allDeltas = yield this.deltas.get(self.cb);
let allDeltas = yield this._deltas.get(self.cb);
deltas = allDeltas.slice(lastSyncSnap.version - this.status.data.snapVersion);
} else if (lastSyncSnap.version == this.status.data.maxVersion) {
@ -563,7 +542,32 @@ RemoteStore.prototype = {
self.done(snap);
},
getLatestFromSnap: function RStore_getLatestFromSnap(onComplete, lastSyncSnap) {
this._getLatestFromSnap.async(this, onComplete, lastSyncSnap);
// get the latest server snapshot. If a snapshot is given, try to
// download only the necessary deltas to get to the latest
_wrap: function RStore__wrap(snapshot) {
let self = yield;
if (snapshot)
self.done(yield this._getLatestFromSnap.async(this, self.cb, snapshot));
else
self.done(yield this._getLatestFromScratch.async(this, self.cb));
},
wrap: function RStore_wrap(onComplete, snapshot) {
this._wrap.async(this, onComplete, snapshot);
},
// Adds a new set of changes (a delta) to this store
_appendDelta: function RStore__appendDelta(delta) {
let self = yield;
if (this._deltas.data == null) {
yield this._deltas.get(self.cb);
if (this._deltas.data == null)
this._deltas.data = [];
}
this._deltas.data.push(delta);
yield this._deltas.put(self.cb, this._deltas.data);
},
appendDelta: function RStore_appendDelta(onComplete, delta) {
this._appendDelta.async(this, onComplete, delta);
}
};