Bug 769816 - Send newline-delimited requests instead of JSON; r=rnewman
authorGregory Szorc <gps@mozilla.com>
Mon, 09 Jul 2012 15:24:46 -0700
changeset 99291 f6a5b82eb76bd7c0d4f01cf3bf87cd4f533c2134
parent 99290 4a46b155c36578fe32acc07e0f7eb2c5617e9f43
child 99292 5124e5684be353762035b3fa0c214fdfd9ecadb5
push id23117
push usergszorc@mozilla.com
push dateSat, 14 Jul 2012 20:35:19 +0000
treeherdermozilla-central@8e2f9cc15bd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman
bugs769816
milestone16.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 769816 - Send newline-delimited requests instead of JSON; r=rnewman
services/common/storageservice.js
services/common/tests/unit/test_storageservice_client.js
--- a/services/common/storageservice.js
+++ b/services/common/storageservice.js
@@ -1079,38 +1079,53 @@ StorageCollectionGetRequest.prototype = 
 /**
  * Represents a request that sets data in a collection
  *
  * Instances of this type are returned by StorageServiceClient.setBSOs().
  */
 function StorageCollectionSetRequest() {
   StorageServiceRequest.call(this);
 
-  this._inputBSOs = [];
+  this._lines = [];
+  this._size  = 0;
 
   this.successfulIDs = new Set();
   this.failures      = new Map();
 }
 StorageCollectionSetRequest.prototype = {
   __proto__: StorageServiceRequest.prototype,
 
+  /**
+   * Add a BasicStorageObject to this request.
+   *
+   * Please note that the BSO content is retrieved when the BSO is added to
+   * the request. If the BSO changes after it is added to a request, those
+   * changes will not be reflected in the request.
+   *
+   * @param bso
+   *        (BasicStorageObject) BSO to add to the request.
+   */
   addBSO: function addBSO(bso) {
     if (!bso instanceof BasicStorageObject) {
       throw new Error("argument must be a BasicStorageObject instance.");
     }
 
     if (!bso.id) {
       throw new Error("Passed BSO must have id defined.");
     }
 
-    this._inputBSOs.push(bso);
+    let line = JSON.stringify(bso).replace("\n", "\u000a");
+
+    // This is off by 1 in the larger direction. We don't care.
+    this._size += line.length + "\n".length;
+    this._lines.push(line);
   },
 
   _onDispatch: function _onDispatch() {
-    this._data = JSON.stringify(this._inputBSOs);
+    this._data = this._lines.join("\n");
   },
 
   _completeParser: function _completeParser(response) {
     let result = JSON.parse(response.body);
 
     for (let id of result.success) {
       this.successfulIDs.add(id);
     }
@@ -1607,17 +1622,17 @@ StorageServiceClient.prototype = {
   setBSOs: function setBSOs(collection) {
     if (!collection) {
       throw new Error("collection argument must be defined.");
     }
 
     let uri = this._baseURI + "storage/" + collection;
     let request = this._getRequest(uri, "POST", {
       requestType:       StorageCollectionSetRequest,
-      contentType:       "application/json",
+      contentType:       "application/newlines",
       accept:            "application/json",
       allowIfUnmodified: true,
     });
 
     return request;
   },
 
   /**
--- a/services/common/tests/unit/test_storageservice_client.js
+++ b/services/common/tests/unit/test_storageservice_client.js
@@ -678,16 +678,44 @@ add_test(function test_set_bsos_invalid_
   } finally {
     do_check_true(threw);
     threw = false;
   }
 
   run_next_test();
 });
 
+add_test(function test_set_bsos_newline() {
+  _("Ensure that newlines in BSO payloads are formatted properly.");
+
+  let [server, client, username] = getServerAndClient();
+  let user = server.user(username);
+
+  let request = client.setBSOs("testcoll");
+
+  let bso0 = new BasicStorageObject("bso0");
+  bso0.payload = "hello\nworld";
+  request.addBSO(bso0);
+
+  let bso1 = new BasicStorageObject("bso1");
+  bso1.payload = "foobar";
+  request.addBSO(bso1);
+
+  request.dispatch(function onComplete(error, request) {
+    do_check_null(error);
+    do_check_eq(request.successfulIDs.size(), 2);
+
+    let coll = user.collection("testcoll");
+    do_check_eq(coll.bso("bso0").payload, bso0.payload);
+    do_check_eq(coll.bso("bso1").payload, bso1.payload);
+
+    server.stop(run_next_test);
+  });
+});
+
 add_test(function test_delete_bso_simple() {
   _("Ensure deletion of individual BSOs works.");
 
   let [server, client, username] = getServerAndClient();
 
   let user = server.user(username);
   let coll = user.createCollection("testcoll", {
     foo: new ServerBSO("foo", "payload0", Date.now()),