Bug 933050 - DataStore.add should receive an optional key value., r=ehsan

This commit is contained in:
Andrea Marchesini 2013-11-08 23:10:29 +00:00
Родитель 4abbe765f1
Коммит dc77f637fc
8 изменённых файлов: 74 добавлений и 67 удалений

Просмотреть файл

@ -177,17 +177,17 @@ this.DataStore.prototype = {
getInternalRequest();
},
updateInternal: function(aResolve, aStore, aRevisionStore, aId, aObj) {
debug("UpdateInternal " + aId);
putInternal: function(aResolve, aStore, aRevisionStore, aObj, aId) {
debug("putInternal " + aId);
let self = this;
let request = aStore.put(aObj, aId);
request.onsuccess = function(aEvent) {
debug("UpdateInternal success");
debug("putInternal success");
self.addRevision(aRevisionStore, aId, REVISION_UPDATED,
function() {
debug("UpdateInternal - revisionId increased");
debug("putInternal - revisionId increased");
// No wrap here because the result is always a int.
aResolve(aEvent.target.result);
}
@ -195,11 +195,11 @@ this.DataStore.prototype = {
};
},
addInternal: function(aResolve, aStore, aRevisionStore, aObj) {
addInternal: function(aResolve, aStore, aRevisionStore, aObj, aId) {
debug("AddInternal");
let self = this;
let request = aStore.put(aObj);
let request = aStore.add(aObj, aId);
request.onsuccess = function(aEvent) {
debug("Request successful. Id: " + aEvent.target.result);
self.addRevision(aRevisionStore, aEvent.target.result, REVISION_ADDED,
@ -384,7 +384,7 @@ this.DataStore.prototype = {
);
},
update: function(aId, aObj) {
put: function(aObj, aId) {
aId = parseInt(aId);
if (isNaN(aId) || aId <= 0) {
return throwInvalidArg(this._window);
@ -399,12 +399,19 @@ this.DataStore.prototype = {
// Promise<void>
return this.newDBPromise("readwrite",
function(aResolve, aReject, aTxn, aStore, aRevisionStore) {
self.updateInternal(aResolve, aStore, aRevisionStore, aId, aObj);
self.putInternal(aResolve, aStore, aRevisionStore, aObj, aId);
}
);
},
add: function(aObj) {
add: function(aObj, aId) {
if (aId) {
aId = parseInt(aId);
if (isNaN(aId) || aId <= 0) {
return throwInvalidArg(this._window);
}
}
if (this._readOnly) {
return throwReadOnly(this._window);
}
@ -414,7 +421,7 @@ this.DataStore.prototype = {
// Promise<int>
return this.newDBPromise("readwrite",
function(aResolve, aReject, aTxn, aStore, aRevisionStore) {
self.addInternal(aResolve, aStore, aRevisionStore, aObj);
self.addInternal(aResolve, aStore, aRevisionStore, aObj, aId);
}
);
},

Просмотреть файл

@ -83,10 +83,10 @@ DataStoreDB.prototype = {
);
},
addRevision: function(aStore, aId, aType, aSuccessCb) {
debug("AddRevision: " + aId + " - " + aType);
addRevision: function(aStore, aKey, aType, aSuccessCb) {
debug("AddRevision: " + aKey + " - " + aType);
let revisionId = uuidgen.generateUUID().toString();
let request = aStore.put({ revisionId: revisionId, objectId: aId, operation: aType });
let request = aStore.put({ revisionId: revisionId, objectId: aKey, operation: aType });
request.onsuccess = function() {
aSuccessCb(revisionId);
}

Просмотреть файл

@ -34,7 +34,7 @@
var store = stores[0];
ok("get" in store, "store.get exists");
ok("update" in store, "store.update exists");
ok("put" in store, "store.put exists");
ok("add" in store, "store.add exists");
ok("remove" in store, "store.remove exists");
ok("clear" in store, "store.clear exists");

Просмотреть файл

@ -34,7 +34,7 @@
var store = stores[0];
ok("get" in store, "store.get exists");
ok("update" in store, "store.update exists");
ok("put" in store, "store.put exists");
ok("add" in store, "store.add exists");
ok("remove" in store, "store.remove exists");
ok("clear" in store, "store.clear exists");
@ -65,9 +65,9 @@
}, cbError);
}
function testStoreUpdate(id, value) {
return gStore.update(id, value).then(function() {
ok(true, "store.update() is called");
function testStorePut(value, id) {
return gStore.put(value, id).then(function() {
ok(true, "store.put() is called");
}, cbError);
}
@ -113,8 +113,8 @@
gId = id; runTest(); }, cbError); },
function() { testStoreGet(gId, "hello world"); },
// Update + Get - string
function() { testStoreUpdate(gId, "hello world 2").then(function() {
// Put + Get - string
function() { testStorePut("hello world 2", gId).then(function() {
runTest(); }, cbError); },
function() { testStoreGet(gId, "hello world 2"); },

Просмотреть файл

@ -49,9 +49,9 @@
}, cbError);
}
function testStoreUpdate(id, value) {
gStore.update(id, value).then(function(retId) {
is(id, retId, "store.update() is called with the right id");
function testStorePut(value, id) {
gStore.put(value, id).then(function(retId) {
is(id, retId, "store.put() is called with the right id");
}, cbError);
}
@ -84,9 +84,9 @@
function() { gChangeId = 1; gChangeOperation = 'added';
testStoreAdd({ number: 42 }, 1); },
// Update
// Put
function() { gChangeId = 1; gChangeOperation = 'updated';
testStoreUpdate(1, { number: 43 }); },
testStorePut({ number: 43 }, 1); },
// Remove
function() { gChangeId = 1; gChangeOperation = 'removed';
@ -103,9 +103,9 @@
function() { gChangeId = 2; gChangeOperation = 'added';
testStoreAdd({ number: 42 }, 2); },
// Update
// Put
function() { gChangeId = 2; gChangeOperation = 'updated';
testStoreUpdate(2, { number: 43 }); },
testStorePut({ number: 43 }, 2); },
// Remove
function() { gChangeId = 2; gChangeOperation = 'removed';

Просмотреть файл

@ -33,7 +33,7 @@
var store = stores[0];
ok("get" in store, "store.get exists");
ok("update" in store, "store.update exists");
ok("put" in store, "store.put exists");
ok("add" in store, "store.add exists");
ok("remove" in store, "store.remove exists");
ok("clear" in store, "store.clear exists");
@ -51,11 +51,11 @@
f = f.then(cbError, function() {
ok(true, "store.add() fails because the db is readonly");
return store.update(123, {});
return store.put({}, 123);
})
f = f.then(cbError, function() {
ok(true, "store.update() fails because the db is readonly");
ok(true, "store.put() fails because the db is readonly");
})
f.then(function() {

Просмотреть файл

@ -133,14 +133,14 @@
function() {
gExpectedEvents = true;
gStore.add(1).then(function(id) {
gStore.add(1,2).then(function(id) {
gRevisions.push(gStore.revisionId);
ok(true, "Iteme: " + id + " added");
});
},
function() {
gStore.add(2).then(function(id) {
gStore.add(2,3).then(function(id) {
gRevisions.push(gStore.revisionId);
ok(true, "Iteme: " + id + " added");
});
@ -150,8 +150,8 @@
gExpectedEvents = false;
var cursor = gStore.sync();
var steps = [ { operation: 'clear', },
{ operation: 'add', id: 1, data: 1 },
{ operation: 'add', id: 2, data: 2 },
{ operation: 'add', id: 2, data: 1 },
{ operation: 'add', id: 3, data: 2 },
{ operation: 'done' }];
testCursor(cursor, steps);
},
@ -159,23 +159,23 @@
function() {
var cursor = gStore.sync('wrong revision ID');
var steps = [ { operation: 'clear', },
{ operation: 'add', id: 1, data: 1 },
{ operation: 'add', id: 2, data: 2 },
{ operation: 'add', id: 2, data: 1 },
{ operation: 'add', id: 3, data: 2 },
{ operation: 'done' }];
testCursor(cursor, steps);
},
function() {
var cursor = gStore.sync(gRevisions[0]);
var steps = [ { operation: 'add', id: 1, data: 1 },
{ operation: 'add', id: 2, data: 2 },
var steps = [ { operation: 'add', id: 2, data: 1 },
{ operation: 'add', id: 3, data: 2 },
{ operation: 'done' }];
testCursor(cursor, steps);
},
function() {
var cursor = gStore.sync(gRevisions[1]);
var steps = [ { operation: 'add', id: 2, data: 2 },
var steps = [ { operation: 'add', id: 3, data: 2 },
{ operation: 'done' }];
testCursor(cursor, steps);
},
@ -189,7 +189,7 @@
// Test after an update
function() {
gExpectedEvents = true;
gStore.update(1, 3).then(function() {
gStore.put(3, 2).then(function() {
gRevisions.push(gStore.revisionId);
});
},
@ -198,8 +198,8 @@
gExpectedEvents = false;
var cursor = gStore.sync();
var steps = [ { operation: 'clear', },
{ operation: 'add', id: 1, data: 3 },
{ operation: 'add', id: 2, data: 2 },
{ operation: 'add', id: 2, data: 3 },
{ operation: 'add', id: 3, data: 2 },
{ operation: 'done' }];
testCursor(cursor, steps);
},
@ -207,31 +207,31 @@
function() {
var cursor = gStore.sync('wrong revision ID');
var steps = [ { operation: 'clear', },
{ operation: 'add', id: 1, data: 3 },
{ operation: 'add', id: 2, data: 2 },
{ operation: 'add', id: 2, data: 3 },
{ operation: 'add', id: 3, data: 2 },
{ operation: 'done' }];
testCursor(cursor, steps);
},
function() {
var cursor = gStore.sync(gRevisions[0]);
var steps = [ { operation: 'add', id: 1, data: 3 },
{ operation: 'add', id: 2, data: 2 },
var steps = [ { operation: 'add', id: 2, data: 3 },
{ operation: 'add', id: 3, data: 2 },
{ operation: 'done' }];
testCursor(cursor, steps);
},
function() {
var cursor = gStore.sync(gRevisions[1]);
var steps = [ { operation: 'add', id: 2, data: 2 },
{ operation: 'update', id: 1, data: 3 },
var steps = [ { operation: 'add', id: 3, data: 2 },
{ operation: 'update', id: 2, data: 3 },
{ operation: 'done' }];
testCursor(cursor, steps);
},
function() {
var cursor = gStore.sync(gRevisions[2]);
var steps = [ { operation: 'update', id: 1, data: 3 },
var steps = [ { operation: 'update', id: 2, data: 3 },
{ operation: 'done' }];
testCursor(cursor, steps);
},
@ -245,7 +245,7 @@
// Test after a remove
function() {
gExpectedEvents = true;
gStore.remove(2).then(function() {
gStore.remove(3).then(function() {
gRevisions.push(gStore.revisionId);
});
},
@ -254,7 +254,7 @@
gExpectedEvents = false;
var cursor = gStore.sync();
var steps = [ { operation: 'clear', },
{ operation: 'add', id: 1, data: 3 },
{ operation: 'add', id: 2, data: 3 },
{ operation: 'done' }];
testCursor(cursor, steps);
},
@ -262,36 +262,36 @@
function() {
var cursor = gStore.sync('wrong revision ID');
var steps = [ { operation: 'clear', },
{ operation: 'add', id: 1, data: 3 },
{ operation: 'add', id: 2, data: 3 },
{ operation: 'done' }];
testCursor(cursor, steps);
},
function() {
var cursor = gStore.sync(gRevisions[0]);
var steps = [ { operation: 'add', id: 1, data: 3 },
var steps = [ { operation: 'add', id: 2, data: 3 },
{ operation: 'done' }];
testCursor(cursor, steps);
},
function() {
var cursor = gStore.sync(gRevisions[1]);
var steps = [ { operation: 'update', id: 1, data: 3 },
var steps = [ { operation: 'update', id: 2, data: 3 },
{ operation: 'done' }];
testCursor(cursor, steps);
},
function() {
var cursor = gStore.sync(gRevisions[2]);
var steps = [ { operation: 'update', id: 1, data: 3 },
{ operation: 'remove', id: 2 },
var steps = [ { operation: 'update', id: 2, data: 3 },
{ operation: 'remove', id: 3 },
{ operation: 'done' }];
testCursor(cursor, steps);
},
function() {
var cursor = gStore.sync(gRevisions[3]);
var steps = [ { operation: 'remove', id: 2 },
var steps = [ { operation: 'remove', id: 3 },
{ operation: 'done' }];
testCursor(cursor, steps);
},
@ -306,7 +306,7 @@
function() {
gCursor = gStore.sync();
var steps = [ { operation: 'clear', },
{ operation: 'add', id: 1, data: 3 } ];
{ operation: 'add', id: 2, data: 3 } ];
testCursor(gCursor, steps);
},
@ -320,31 +320,31 @@
// New events when the cursor is active
function() {
var steps = [ { operation: 'add', id: 3, data: 42 } ];
var steps = [ { operation: 'add', id: 4, data: 42 } ];
testCursor(gCursor, steps);
},
function() {
gStore.update(1, 42).then(function(id) {
gStore.put(42, 2).then(function(id) {
gRevisions.push(gStore.revisionId);
runTest();
});
},
function() {
var steps = [ { operation: 'update', id: 1, data: 42 } ];
var steps = [ { operation: 'update', id: 2, data: 42 } ];
testCursor(gCursor, steps);
},
function() {
gStore.remove(1).then(function(id) {
gStore.remove(2).then(function(id) {
gRevisions.push(gStore.revisionId);
runTest();
});
},
function() {
var steps = [ { operation: 'remove', id: 1 } ];
var steps = [ { operation: 'remove', id: 2 } ];
testCursor(gCursor, steps);
},
@ -357,7 +357,7 @@
},
function() {
var steps = [ { operation: 'add', id: 4, data: 42 } ];
var steps = [ { operation: 'add', id: 5, data: 42 } ];
testCursor(gCursor, steps);
},
@ -378,7 +378,7 @@
function() {
var steps = [ { operation: 'clear' },
{ operation: 'add', id: 5, data: 42 },
{ operation: 'add', id: 6, data: 42 },
{ operation: 'done' } ];
testCursor(gCursor, steps);
},

Просмотреть файл

@ -24,10 +24,10 @@ interface DataStore : EventTarget {
Promise get(sequence<unsigned long> id);
// Promise<void>
Promise update(unsigned long id, any obj);
Promise put(any obj, unsigned long id);
// Promise<unsigned long>
Promise add(any obj);
Promise add(any obj, optional unsigned long id);
// Promise<boolean>
Promise remove(unsigned long id);