Bug 933050 - DataStore.add should receive an optional key value., r=ehsan
authorAndrea Marchesini <amarchesini@mozilla.com>
Fri, 08 Nov 2013 23:10:29 +0000
changeset 154252 b90de16dac0454a7c923a74ca7918d5e473ffd6c
parent 154251 593f1ea908dcaca6fabc4bae6c14b7934a100eb2
child 154253 8255ebcdfe7e1b92d9f0a883598825dc3914151d
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersehsan
bugs933050
milestone28.0a1
Bug 933050 - DataStore.add should receive an optional key value., r=ehsan
dom/datastore/DataStore.jsm
dom/datastore/DataStoreDB.jsm
dom/datastore/tests/file_arrays.html
dom/datastore/tests/file_basic.html
dom/datastore/tests/file_changes.html
dom/datastore/tests/file_readonly.html
dom/datastore/tests/file_sync.html
dom/webidl/DataStore.webidl
--- a/dom/datastore/DataStore.jsm
+++ b/dom/datastore/DataStore.jsm
@@ -172,39 +172,39 @@ this.DataStore.prototype = {
       request.onsuccess = function(aEvent) {
         getInternalSuccess(aEvent, currentPos);
       }
     }
 
     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);
         }
       );
     };
   },
 
-  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,
         function() {
           debug("AddInternal - revisionId increased");
           // No wrap here because the result is always a int.
           aResolve(aEvent.target.result);
         }
@@ -379,47 +379,54 @@ this.DataStore.prototype = {
                                 Array.isArray(aId) ?  aId : [ aId ],
                                 function(aResults) {
           aResolve(Array.isArray(aId) ? aResults : aResults[0]);
         });
       }
     );
   },
 
-  update: function(aId, aObj) {
+  put: function(aObj, aId) {
     aId = parseInt(aId);
     if (isNaN(aId) || aId <= 0) {
       return throwInvalidArg(this._window);
     }
 
     if (this._readOnly) {
       return throwReadOnly(this._window);
     }
 
     let self = this;
 
     // 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);
     }
 
     let self = this;
 
     // 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);
       }
     );
   },
 
   remove: function(aId) {
     aId = parseInt(aId);
     if (isNaN(aId) || aId <= 0) {
       return throwInvalidArg(this._window);
--- a/dom/datastore/DataStoreDB.jsm
+++ b/dom/datastore/DataStoreDB.jsm
@@ -78,20 +78,20 @@ DataStoreDB.prototype = {
       aType,
       DATASTOREDB_REVISION,
       aCallback,
       function() {},
       aErrorCb
     );
   },
 
-  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);
     }
   },
 
   getInternalRevisionId: function(aRevisionId, aStore, aSuccessCb) {
     debug('GetInternalRevisionId');
     let request = aStore.index(DATASTOREDB_REVISION_INDEX).getKey(aRevisionId);
--- a/dom/datastore/tests/file_arrays.html
+++ b/dom/datastore/tests/file_arrays.html
@@ -29,17 +29,17 @@
   function testGetDataStores() {
     navigator.getDataStores('foo').then(function(stores) {
       is(stores.length, 1, "getDataStores('foo') returns 1 element");
       is(stores[0].name, 'foo', 'The dataStore.name is foo');
       is(stores[0].readOnly, false, 'The dataStore foo is not in readonly');
 
       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");
 
       gStore = stores[0];
 
       runTest();
     }, cbError);
--- a/dom/datastore/tests/file_basic.html
+++ b/dom/datastore/tests/file_basic.html
@@ -29,17 +29,17 @@
   function testGetDataStores() {
     navigator.getDataStores('foo').then(function(stores) {
       is(stores.length, 1, "getDataStores('foo') returns 1 element");
       is(stores[0].name, 'foo', 'The dataStore.name is foo');
       is(stores[0].readOnly, false, 'The dataStore foo is not in readonly');
 
       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");
       ok("revisionId" in store, "store.revisionId exists");
       ok("getLength" in store, "store.getLength exists");
       ok("sync" in store, "store.sync exists");
 
       gStore = stores[0];
@@ -60,19 +60,19 @@
   function testStoreAdd(value) {
     return gStore.add(value).then(function(what) {
       ok(true, "store.add() is called");
       ok(what > 0, "store.add() returns something");
       return what;
     }, 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);
   }
 
   function testStoreGetLength(number) {
     return gStore.getLength().then(function(n) {
       is(number, n, "store.getLength() returns the right number");
     }, cbError);
   }
@@ -108,18 +108,18 @@
                    gId = id; runTest(); }, cbError); },
     function() { testStoreGet(gId, true); },
 
     // Add + Get - string
     function() { testStoreAdd("hello world").then(function(id) {
                    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"); },
 
     // getLength
     function() { testStoreGetLength(3).then(function() { runTest(); }, cbError); },
 
     // Remove
     function() { testStoreRemove(gId).then(function(what) {
--- a/dom/datastore/tests/file_changes.html
+++ b/dom/datastore/tests/file_changes.html
@@ -44,19 +44,19 @@
   }
 
   function testStoreAdd(value, expectedId) {
     gStore.add(value).then(function(id) {
       is(id, expectedId, "store.add() is called");
     }, 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);
   }
 
   function testStoreRemove(id, expectedSuccess) {
     gStore.remove(id).then(function(success) {
       is(success, expectedSuccess, "store.remove() returns the right value");
     }, cbError);
   }
@@ -79,38 +79,38 @@
       is(gStore.onchange, eventListener, "onChange is set");
       runTest();
     },
 
     // Add
     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';
                  testStoreRemove(1, true); },
 
     // Remove onchange function and replace it with addEventListener
     function() {
       gStore.onchange = null;
       gStore.addEventListener('change', eventListener);
       runTest();
     },
 
     // Add
     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';
                  testStoreRemove(2, true); },
 
     // Remove event listener
     function() {
       gStore.removeEventListener('change', eventListener);
--- a/dom/datastore/tests/file_readonly.html
+++ b/dom/datastore/tests/file_readonly.html
@@ -28,17 +28,17 @@
   function runTest() {
     navigator.getDataStores('bar').then(function(stores) {
       is(stores.length, 1, "getDataStores('bar') returns 1 element");
       is(stores[0].name, 'bar', 'The dataStore.name is bar');
       is(stores[0].readOnly, true, 'The dataStore bar is eadonly');
 
       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");
 
       var f = store.clear();
       f = f.then(cbError, function() {
         ok(true, "store.clear() fails because the db is readonly");
         return store.remove(123);
@@ -46,21 +46,21 @@
 
       f = f.then(cbError, function() {
         ok(true, "store.remove() fails because the db is readonly");
         return store.add(123, true);
       });
 
       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() {
         // All done.
         ok(true, "All done");
         finish();
       });
     }, cbError);
--- a/dom/datastore/tests/file_sync.html
+++ b/dom/datastore/tests/file_sync.html
@@ -128,241 +128,241 @@
                     { operation: 'done' }];
       testCursor(cursor, steps);
     },
 
     // Test add from scratch
     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");
       });
     },
 
     function() {
       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);
     },
 
     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);
     },
 
     function() {
       var cursor = gStore.sync(gRevisions[2]);
       var steps = [ { operation: 'done' }];
       testCursor(cursor, steps);
     },
 
     // Test after an update
     function() {
       gExpectedEvents = true;
-      gStore.update(1, 3).then(function() {
+      gStore.put(3, 2).then(function() {
         gRevisions.push(gStore.revisionId);
       });
     },
 
     function() {
       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);
     },
 
     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);
     },
 
     function() {
       var cursor = gStore.sync(gRevisions[3]);
       var steps = [ { operation: 'done' }];
       testCursor(cursor, steps);
     },
 
     // Test after a remove
     function() {
       gExpectedEvents = true;
-      gStore.remove(2).then(function() {
+      gStore.remove(3).then(function() {
         gRevisions.push(gStore.revisionId);
       });
     },
 
     function() {
       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);
     },
 
     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);
     },
 
     function() {
       var cursor = gStore.sync(gRevisions[4]);
       var steps = [ { operation: 'done' }];
       testCursor(cursor, steps);
     },
 
     // New events when the cursor is active
     function() {
       gCursor = gStore.sync();
       var steps = [ { operation: 'clear', },
-                    { operation: 'add', id: 1, data: 3 } ];
+                    { operation: 'add', id: 2, data: 3 } ];
       testCursor(gCursor, steps);
     },
 
     function() {
       gStore.add(42).then(function(id) {
         ok(true, "Item: " + id + " added");
         gRevisions.push(gStore.revisionId);
         runTest();
       });
     },
 
     // 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);
     },
 
     function() {
       gStore.add(42).then(function(id) {
         ok(true, "Item: " + id + " added");
         gRevisions.push(gStore.revisionId);
         runTest();
       });
     },
 
     function() {
-      var steps = [ { operation: 'add', id: 4, data: 42 } ];
+      var steps = [ { operation: 'add', id: 5, data: 42 } ];
       testCursor(gCursor, steps);
     },
 
     function() {
       gStore.clear().then(function() {
         gRevisions.push(gStore.revisionId);
         runTest();
       });
@@ -373,17 +373,17 @@
         ok(true, "Item: " + id + " added");
         gRevisions.push(gStore.revisionId);
         runTest();
       });
     },
 
     function() {
       var steps = [ { operation: 'clear' },
-                    { operation: 'add', id: 5, data: 42 },
+                    { operation: 'add', id: 6, data: 42 },
                     { operation: 'done' } ];
       testCursor(gCursor, steps);
     },
 
     function() {
       gExpectedEvents = true;
       gStore.add(42).then(function(id) {
       });
--- a/dom/webidl/DataStore.webidl
+++ b/dom/webidl/DataStore.webidl
@@ -19,20 +19,20 @@ interface DataStore : EventTarget {
 
   // Promise<any>
   Promise get(unsigned long id);
 
   // Promise<any>
   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);
 
   // Promise<void>
   Promise clear();
 
   readonly attribute DOMString revisionId;