Bug 963817 - Wrap HomeStorage data insertion loop in a transaction (r=margaret)
authorLucas Rocha <lucasr@mozilla.com>
Wed, 12 Mar 2014 17:02:50 +0000
changeset 190480 b07f0ec10d226e32b8c89b465b71cf4600f61539
parent 190479 b2b8ba0478032822c1f43136be18e3ea0ed73fc0
child 190481 20e22a0b38f6c688d6bb6068ced12b6db527b6b5
push id3503
push userraliiev@mozilla.com
push dateMon, 28 Apr 2014 18:51:11 +0000
treeherdermozilla-beta@c95ac01e332e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret
bugs963817
milestone30.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 963817 - Wrap HomeStorage data insertion loop in a transaction (r=margaret)
mobile/android/base/tests/testHomeProvider.js
mobile/android/modules/HomeProvider.jsm
--- a/mobile/android/base/tests/testHomeProvider.js
+++ b/mobile/android/base/tests/testHomeProvider.js
@@ -97,9 +97,36 @@ add_task(function test_row_validation() 
 
   // Make sure no data has been saved.
   let result = yield db.execute("SELECT * FROM items");
   do_check_eq(result.length, 0);
 
   db.close();
 });
 
+add_task(function test_save_transaction() {
+  // Use the HomeProvider API to save some data.
+  let storage = HomeProvider.getStorage(TEST_DATASET_ID);
+
+  // One valid, one invalid
+  let rows = [
+    { title: TEST_TITLE, url: TEST_URL },
+    { image_url: "image_url" }
+  ];
+
+  // Try to save all the rows at once
+  try {
+    yield storage.save(rows);
+  } catch (e if e instanceof HomeProvider.ValidationError) {
+    // Just catch and ignore validation errors
+  }
+
+  // Peek in the DB to make sure we have the right data.
+  let db = yield Sqlite.openConnection({ path: DB_PATH });
+
+  // Make sure no data has been saved.
+  let result = yield db.execute("SELECT * FROM items");
+  do_check_eq(result.length, 0);
+
+  db.close();
+});
+
 run_next_test();
--- a/mobile/android/modules/HomeProvider.jsm
+++ b/mobile/android/modules/HomeProvider.jsm
@@ -289,32 +289,34 @@ HomeStorage.prototype = {
    *
    * @return Promise
    * @resolves When the operation has completed.
    */
   save: function(data) {
     return Task.spawn(function save_task() {
       let db = yield getDatabaseConnection();
       try {
-        // Insert data into DB.
-        for (let item of data) {
-          validateItem(this.datasetId, item);
+        yield db.executeTransaction(function save_transaction() {
+          // Insert data into DB.
+          for (let item of data) {
+            validateItem(this.datasetId, item);
 
-          // XXX: Directly pass item as params? More validation for item? Batch insert?
-          let params = {
-            dataset_id: this.datasetId,
-            url: item.url,
-            title: item.title,
-            description: item.description,
-            image_url: item.image_url,
-            filter: item.filter,
-            created: Date.now()
-          };
-          yield db.executeCached(SQL.insertItem, params);
-        }
+            // XXX: Directly pass item as params? More validation for item?
+            let params = {
+              dataset_id: this.datasetId,
+              url: item.url,
+              title: item.title,
+              description: item.description,
+              image_url: item.image_url,
+              filter: item.filter,
+              created: Date.now()
+            };
+            yield db.executeCached(SQL.insertItem, params);
+          }
+        }.bind(this));
       } finally {
         yield db.close();
       }
     }.bind(this));
   },
 
   /**
    * Deletes all rows associated with this storage.