{"action":"created","comment":{"url":"https://api.github.com/repos/mozilla-b2g/gaia/pulls/comments/40285624","id":40285624,"diff_hunk":"@@ -0,0 +1,285 @@\n+/* This Source Code Form is subject to the terms of the Mozilla Public\n+ * License, v. 2.0. If a copy of the MPL was not distributed with this\n+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */\n+\n+/**\n+  History Data Adapter\n+\n+    The feature of History adapter is to retrieve History records from FxSync\n+  Service and merge to Places DataStore which has the same functionality, and\n+  the adapter is based on SyncEngine's design to handle 'update' and\n+  'handleConflict' request. History Data Adapter provides Read-Only version\n+  currently, and the basic ideas are included:\n+  - Traverse all records from History Collection (HC).\n+  - Add a new or existed place to Places DataStore (PDS).\n+    - Convert the records from HC to PDS format.\n+    - Merge a new record and the existed one then write to PDS.\n+  - Remove an existed place when receiving a \"deleted: true\" record.\n+**/\n+\n+'use strict';\n+\n+/* global\n+  asyncStorage,\n+  DataAdapters,\n+  LazyLoader\n+*/\n+\n+const HISTORY_COLLECTION_MTIME = 'collections::history::mtime';\n+const SYNCTOID_PREFIX = 'SynctoId::';\n+\n+var HistoryHelper = (() => {\n+  var placesStore;\n+  function _ensureStore() {\n+    if (placesStore) {\n+      return Promise.resolve(placesStore);\n+    }\n+    return navigator.getDataStores('places').then(stores => {\n+      placesStore = stores[0];\n+      return placesStore;\n+    });\n+  }\n+\n+  function setSyncedCollectionMtime(mtime) {\n+    return new Promise(resolve => {\n+      asyncStorage.setItem(HISTORY_COLLECTION_MTIME, mtime, resolve);\n+    });\n+  }\n+\n+  function getSyncedCollectionMtime() {\n+    return new Promise(resolve => {\n+      asyncStorage.getItem(HISTORY_COLLECTION_MTIME, resolve);\n+    });\n+  }\n+\n+  function setDataStoreId(synctoId, dataStoreId) {\n+    return new Promise(resolve => {\n+      asyncStorage.setItem(SYNCTOID_PREFIX + synctoId, dataStoreId, resolve);\n+    });\n+  }\n+\n+  function getDataStoreId(synctoId) {\n+    return new Promise(resolve => {\n+      asyncStorage.getItem(SYNCTOID_PREFIX + synctoId, resolve);\n+    });\n+  }\n+\n+  function mergeRecordsToDataStore(localRecord, remoteRecord) {\n+    if (!localRecord || !remoteRecord ||\n+        localRecord.url !== remoteRecord.url) {\n+      // The local record has different url(id) with the new one.\n+      throw new Error('Inconsistent records');\n+    }\n+    if (!localRecord.fxsyncId && typeof remoteRecord.fxsyncId === 'string') {\n+      /* When a localRecord is existed without fxsyncId, assign fxsyncId to it\n+         from a remoteRecord. This case always happens at first synchronization\n+         or merging two records with the same URL. */\n+      localRecord.fxsyncId = remoteRecord.fxsyncId;\n+    } else if(localRecord.fxsyncId !== remoteRecord.fxsyncId) {\n+      // Two records have different fxsyncId but have the same url(id).\n+      throw new Error('Inconsistent records');\n+    }","path":"apps/sync/js/adapters/history.js","position":81,"original_position":81,"commit_id":"694efdbd10af651634d17468b19999d3b15e3b1e","original_commit_id":"694efdbd10af651634d17468b19999d3b15e3b1e","user":{"login":"weilonge","id":872741,"avatar_url":"https://avatars.githubusercontent.com/u/872741?v=3","gravatar_id":"","url":"https://api.github.com/users/weilonge","html_url":"https://github.com/weilonge","followers_url":"https://api.github.com/users/weilonge/followers","following_url":"https://api.github.com/users/weilonge/following{/other_user}","gists_url":"https://api.github.com/users/weilonge/gists{/gist_id}","starred_url":"https://api.github.com/users/weilonge/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/weilonge/subscriptions","organizations_url":"https://api.github.com/users/weilonge/orgs","repos_url":"https://api.github.com/users/weilonge/repos","events_url":"https://api.github.com/users/weilonge/events{/privacy}","received_events_url":"https://api.github.com/users/weilonge/received_events","type":"User","site_admin":false},"body":"From line 68 to 81, the code readability would be not good enough if we merge the three condition into one 'if' statement, and I add more comments here to describe the details.","created_at":"2015-09-24T04:56:25Z","updated_at":"2015-09-24T04:56:25Z","html_url":"https://github.com/mozilla-b2g/gaia/pull/31896#discussion_r40285624","pull_request_url":"https://api.github.com/repos/mozilla-b2g/gaia/pulls/31896","_links":{"self":{"href":"https://api.github.com/repos/mozilla-b2g/gaia/pulls/comments/40285624"},"html":{"href":"https://github.com/mozilla-b2g/gaia/pull/31896#discussion_r40285624"},"pull_request":{"href":"https://api.github.com/repos/mozilla-b2g/gaia/pulls/31896"}}},"pull_request":{"url":"https://api.github.com/repos/mozilla-b2g/gaia/pulls/31896","id":45351702,"html_url":"https://github.com/mozilla-b2g/gaia/pull/31896","diff_url":"https://github.com/mozilla-b2g/gaia/pull/31896.diff","patch_url":"https://github.com/mozilla-b2g/gaia/pull/31896.patch","issue_url":"https://api.github.com/repos/mozilla-b2g/gaia/issues/31896","number":31896,"state":"open","locked":false,"title":"Bug 1191773 - Implement History Adapter for FxSync feature.","user":{"login":"weilonge","id":872741,"avatar_url":"https://avatars.githubusercontent.com/u/872741?v=3","gravatar_id":"","url":"https://api.github.com/users/weilonge","html_url":"https://github.com/weilonge","followers_url":"https://api.github.com/users/weilonge/followers","following_url":"https://api.github.com/users/weilonge/following{/other_user}","gists_url":"https://api.github.com/users/weilonge/gists{/gist_id}","starred_url":"https://api.github.com/users/weilonge/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/weilonge/subscriptions","organizations_url":"https://api.github.com/users/weilonge/orgs","repos_url":"https://api.github.com/users/weilonge/repos","events_url":"https://api.github.com/users/weilonge/events{/privacy}","received_events_url":"https://api.github.com/users/weilonge/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-09-17T22:50:30Z","updated_at":"2015-09-24T04:56:25Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f022d8300268b6ec81131ad28181f2b79c6286cb","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/mozilla-b2g/gaia/pulls/31896/commits","review_comments_url":"https://api.github.com/repos/mozilla-b2g/gaia/pulls/31896/comments","review_comment_url":"https://api.github.com/repos/mozilla-b2g/gaia/pulls/comments{/number}","comments_url":"https://api.github.com/repos/mozilla-b2g/gaia/issues/31896/comments","statuses_url":"https://api.github.com/repos/mozilla-b2g/gaia/statuses/694efdbd10af651634d17468b19999d3b15e3b1e","head":{"label":"weilonge:seanlee/DataSync/master/Bug1191773","ref":"seanlee/DataSync/master/Bug1191773","sha":"694efdbd10af651634d17468b19999d3b15e3b1e","user":{"login":"weilonge","id":872741,"avatar_url":"https://avatars.githubusercontent.com/u/872741?v=3","gravatar_id":"","url":"https://api.github.com/users/weilonge","html_url":"https://github.com/weilonge","followers_url":"https://api.github.com/users/weilonge/followers","following_url":"https://api.github.com/users/weilonge/following{/other_user}","gists_url":"https://api.github.com/users/weilonge/gists{/gist_id}","starred_url":"https://api.github.com/users/weilonge/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/weilonge/subscriptions","organizations_url":"https://api.github.com/users/weilonge/orgs","repos_url":"https://api.github.com/users/weilonge/repos","events_url":"https://api.github.com/users/weilonge/events{/privacy}","received_events_url":"https://api.github.com/users/weilonge/received_events","type":"User","site_admin":false},"repo":{"id":24669384,"name":"gaia","full_name":"weilonge/gaia","owner":{"login":"weilonge","id":872741,"avatar_url":"https://avatars.githubusercontent.com/u/872741?v=3","gravatar_id":"","url":"https://api.github.com/users/weilonge","html_url":"https://github.com/weilonge","followers_url":"https://api.github.com/users/weilonge/followers","following_url":"https://api.github.com/users/weilonge/following{/other_user}","gists_url":"https://api.github.com/users/weilonge/gists{/gist_id}","starred_url":"https://api.github.com/users/weilonge/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/weilonge/subscriptions","organizations_url":"https://api.github.com/users/weilonge/orgs","repos_url":"https://api.github.com/users/weilonge/repos","events_url":"https://api.github.com/users/weilonge/events{/privacy}","received_events_url":"https://api.github.com/users/weilonge/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/weilonge/gaia","description":"Gaia is a HTML5-based Phone UI for the Boot 2 Gecko Project. NOTE: For details of what branches are used for what releases, see","fork":true,"url":"https://api.github.com/repos/weilonge/gaia","forks_url":"https://api.github.com/repos/weilonge/gaia/forks","keys_url":"https://api.github.com/repos/weilonge/gaia/keys{/key_id}","collaborators_url":"https://api.github.com/repos/weilonge/gaia/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/weilonge/gaia/teams","hooks_url":"https://api.github.com/repos/weilonge/gaia/hooks","issue_events_url":"https://api.github.com/repos/weilonge/gaia/issues/events{/number}","events_url":"https://api.github.com/repos/weilonge/gaia/events","assignees_url":"https://api.github.com/repos/weilonge/gaia/assignees{/user}","branches_url":"https://api.github.com/repos/weilonge/gaia/branches{/branch}","tags_url":"https://api.github.com/repos/weilonge/gaia/tags","blobs_url":"https://api.github.com/repos/weilonge/gaia/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/weilonge/gaia/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/weilonge/gaia/git/refs{/sha}","trees_url":"https://api.github.com/repos/weilonge/gaia/git/trees{/sha}","statuses_url":"https://api.github.com/repos/weilonge/gaia/statuses/{sha}","languages_url":"https://api.github.com/repos/weilonge/gaia/languages","stargazers_url":"https://api.github.com/repos/weilonge/gaia/stargazers","contributors_url":"https://api.github.com/repos/weilonge/gaia/contributors","subscribers_url":"https://api.github.com/repos/weilonge/gaia/subscribers","subscription_url":"https://api.github.com/repos/weilonge/gaia/subscription","commits_url":"https://api.github.com/repos/weilonge/gaia/commits{/sha}","git_commits_url":"https://api.github.com/repos/weilonge/gaia/git/commits{/sha}","comments_url":"https://api.github.com/repos/weilonge/gaia/comments{/number}","issue_comment_url":"https://api.github.com/repos/weilonge/gaia/issues/comments{/number}","contents_url":"https://api.github.com/repos/weilonge/gaia/contents/{+path}","compare_url":"https://api.github.com/repos/weilonge/gaia/compare/{base}...{head}","merges_url":"https://api.github.com/repos/weilonge/gaia/merges","archive_url":"https://api.github.com/repos/weilonge/gaia/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/weilonge/gaia/downloads","issues_url":"https://api.github.com/repos/weilonge/gaia/issues{/number}","pulls_url":"https://api.github.com/repos/weilonge/gaia/pulls{/number}","milestones_url":"https://api.github.com/repos/weilonge/gaia/milestones{/number}","notifications_url":"https://api.github.com/repos/weilonge/gaia/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/weilonge/gaia/labels{/name}","releases_url":"https://api.github.com/repos/weilonge/gaia/releases{/id}","created_at":"2014-10-01T07:24:02Z","updated_at":"2015-05-04T03:40:54Z","pushed_at":"2015-09-24T01:01:54Z","git_url":"git://github.com/weilonge/gaia.git","ssh_url":"git@github.com:weilonge/gaia.git","clone_url":"https://github.com/weilonge/gaia.git","svn_url":"https://github.com/weilonge/gaia","homepage":"https://wiki.mozilla.org/B2G","size":957410,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":3,"forks":0,"open_issues":3,"watchers":0,"default_branch":"master"}},"base":{"label":"mozilla-b2g:master","ref":"master","sha":"4c530b0a51a319d8acffd4330e23f6c7f6806e4a","user":{"login":"mozilla-b2g","id":1261292,"avatar_url":"https://avatars.githubusercontent.com/u/1261292?v=3","gravatar_id":"","url":"https://api.github.com/users/mozilla-b2g","html_url":"https://github.com/mozilla-b2g","followers_url":"https://api.github.com/users/mozilla-b2g/followers","following_url":"https://api.github.com/users/mozilla-b2g/following{/other_user}","gists_url":"https://api.github.com/users/mozilla-b2g/gists{/gist_id}","starred_url":"https://api.github.com/users/mozilla-b2g/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mozilla-b2g/subscriptions","organizations_url":"https://api.github.com/users/mozilla-b2g/orgs","repos_url":"https://api.github.com/users/mozilla-b2g/repos","events_url":"https://api.github.com/users/mozilla-b2g/events{/privacy}","received_events_url":"https://api.github.com/users/mozilla-b2g/received_events","type":"Organization","site_admin":false},"repo":{"id":2317369,"name":"gaia","full_name":"mozilla-b2g/gaia","owner":{"login":"mozilla-b2g","id":1261292,"avatar_url":"https://avatars.githubusercontent.com/u/1261292?v=3","gravatar_id":"","url":"https://api.github.com/users/mozilla-b2g","html_url":"https://github.com/mozilla-b2g","followers_url":"https://api.github.com/users/mozilla-b2g/followers","following_url":"https://api.github.com/users/mozilla-b2g/following{/other_user}","gists_url":"https://api.github.com/users/mozilla-b2g/gists{/gist_id}","starred_url":"https://api.github.com/users/mozilla-b2g/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mozilla-b2g/subscriptions","organizations_url":"https://api.github.com/users/mozilla-b2g/orgs","repos_url":"https://api.github.com/users/mozilla-b2g/repos","events_url":"https://api.github.com/users/mozilla-b2g/events{/privacy}","received_events_url":"https://api.github.com/users/mozilla-b2g/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mozilla-b2g/gaia","description":"Gaia is a HTML5-based Phone UI for the Boot 2 Gecko Project. NOTE: For details of what branches are used for what releases, see","fork":false,"url":"https://api.github.com/repos/mozilla-b2g/gaia","forks_url":"https://api.github.com/repos/mozilla-b2g/gaia/forks","keys_url":"https://api.github.com/repos/mozilla-b2g/gaia/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mozilla-b2g/gaia/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mozilla-b2g/gaia/teams","hooks_url":"https://api.github.com/repos/mozilla-b2g/gaia/hooks","issue_events_url":"https://api.github.com/repos/mozilla-b2g/gaia/issues/events{/number}","events_url":"https://api.github.com/repos/mozilla-b2g/gaia/events","assignees_url":"https://api.github.com/repos/mozilla-b2g/gaia/assignees{/user}","branches_url":"https://api.github.com/repos/mozilla-b2g/gaia/branches{/branch}","tags_url":"https://api.github.com/repos/mozilla-b2g/gaia/tags","blobs_url":"https://api.github.com/repos/mozilla-b2g/gaia/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mozilla-b2g/gaia/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mozilla-b2g/gaia/git/refs{/sha}","trees_url":"https://api.github.com/repos/mozilla-b2g/gaia/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mozilla-b2g/gaia/statuses/{sha}","languages_url":"https://api.github.com/repos/mozilla-b2g/gaia/languages","stargazers_url":"https://api.github.com/repos/mozilla-b2g/gaia/stargazers","contributors_url":"https://api.github.com/repos/mozilla-b2g/gaia/contributors","subscribers_url":"https://api.github.com/repos/mozilla-b2g/gaia/subscribers","subscription_url":"https://api.github.com/repos/mozilla-b2g/gaia/subscription","commits_url":"https://api.github.com/repos/mozilla-b2g/gaia/commits{/sha}","git_commits_url":"https://api.github.com/repos/mozilla-b2g/gaia/git/commits{/sha}","comments_url":"https://api.github.com/repos/mozilla-b2g/gaia/comments{/number}","issue_comment_url":"https://api.github.com/repos/mozilla-b2g/gaia/issues/comments{/number}","contents_url":"https://api.github.com/repos/mozilla-b2g/gaia/contents/{+path}","compare_url":"https://api.github.com/repos/mozilla-b2g/gaia/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mozilla-b2g/gaia/merges","archive_url":"https://api.github.com/repos/mozilla-b2g/gaia/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mozilla-b2g/gaia/downloads","issues_url":"https://api.github.com/repos/mozilla-b2g/gaia/issues{/number}","pulls_url":"https://api.github.com/repos/mozilla-b2g/gaia/pulls{/number}","milestones_url":"https://api.github.com/repos/mozilla-b2g/gaia/milestones{/number}","notifications_url":"https://api.github.com/repos/mozilla-b2g/gaia/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mozilla-b2g/gaia/labels{/name}","releases_url":"https://api.github.com/repos/mozilla-b2g/gaia/releases{/id}","created_at":"2011-09-03T01:38:46Z","updated_at":"2015-09-23T07:38:18Z","pushed_at":"2015-09-24T04:54:23Z","git_url":"git://github.com/mozilla-b2g/gaia.git","ssh_url":"git@github.com:mozilla-b2g/gaia.git","clone_url":"https://github.com/mozilla-b2g/gaia.git","svn_url":"https://github.com/mozilla-b2g/gaia","homepage":"https://wiki.mozilla.org/B2G","size":1646470,"stargazers_count":1982,"watchers_count":1982,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":2443,"mirror_url":null,"open_issues_count":1222,"forks":2443,"open_issues":1222,"watchers":1982,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/mozilla-b2g/gaia/pulls/31896"},"html":{"href":"https://github.com/mozilla-b2g/gaia/pull/31896"},"issue":{"href":"https://api.github.com/repos/mozilla-b2g/gaia/issues/31896"},"comments":{"href":"https://api.github.com/repos/mozilla-b2g/gaia/issues/31896/comments"},"review_comments":{"href":"https://api.github.com/repos/mozilla-b2g/gaia/pulls/31896/comments"},"review_comment":{"href":"https://api.github.com/repos/mozilla-b2g/gaia/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/mozilla-b2g/gaia/pulls/31896/commits"},"statuses":{"href":"https://api.github.com/repos/mozilla-b2g/gaia/statuses/694efdbd10af651634d17468b19999d3b15e3b1e"}}}}