{"id":560,"date":"2016-10-15T15:44:55","date_gmt":"2016-10-15T22:44:55","guid":{"rendered":"http:\/\/www.cchsu.com\/arthur\/?p=560"},"modified":"2016-10-15T15:44:55","modified_gmt":"2016-10-15T22:44:55","slug":"%e8%99%8e%e7%80%be%e9%9b%86-%e7%b6%b2%e9%a0%81%e8%88%87%e8%b3%87%e6%96%99%e5%ba%ab-5","status":"publish","type":"post","link":"https:\/\/www.cchsu.com\/arthur\/2016\/10\/15\/560\/","title":{"rendered":"\u864e\u703e\u96c6: \u7db2\u9801\u8207\u8cc7\u6599\u5eab (5)"},"content":{"rendered":"<p>\u8aaa\u5230 IndexedDB \u9019\u500b\u898f\u683c\u7684\u5947\u8469\u9ede\uff0c\u90a3\u5c31\u6709\u7684\u8b1b\u4e86\uff0c\u53cd\u6b63\u655d\u793e\u505a IndexedDB \u7684\u5168\u90e8\u90fd\u77e5\u9053\u6211\u9019\u865f\u611b\u62b1\u6028\u7684\u4eba\u7269\uff0c\u6240\u4ee5\u5c31\u7528\u4e2d\u6587\u518d\u958b\u4e00\u6b21\u5730\u5716\u70ae\u5427\u54c8\u54c8\u54c8 &#8230; IndexedDB \u5728 Mozilla \u6709\u4e00\u4efd<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/IndexedDB_API\/Using_IndexedDB\" target=\"_blank\">\u975e\u5e38\u5b8c\u6574\u7684\u8a73\u7d30\u6559\u7a0b<\/a>\uff0c\u6211\u5011\u76f4\u63a5\u6293\u5176\u4e2d\u7684\u4e00\u90e8\u4efd\u51fa\u4f86\u770b\uff0c\u5c31\u770b\u7528\u6700\u591a\u7684 data retrieval \u597d\u4e86<\/p>\n<pre lang=\"javascript\">\r\ndb.transaction(\"customers\").objectStore(\"customers\").get(\"444-44-4444\").onsuccess = function(event) {\r\n  alert(\"Name for SSN 444-44-4444 is \" + event.target.result.name);\r\n};\r\n<\/pre>\n<p>\u770b\u8d77\u4f86\u5f88\u6b63\u5e38\u554a\uff0c\u6709\u4ec0\u9ebc\u554f\u984c\u55ce? \u6c92\u4ec0\u9ebc\u554f\u984c\uff0c\u5c31\u662f\u5b83\u5929\u751f\u6703\u8dd1\u7684\u6162\u4e00\u9ede\u3002\u9996\u5148\uff0c\u9019\u4e00\u5c0f\u6bb5\u7a0b\u5f0f\u5176\u5be6\u6703 fire \u5169\u500b event: get success \u548c transaction complete\u3002\u6240\u4ee5\u82e5\u4f60\u7528\u9019\u7a2e\u65b9\u6cd5\u6488 1000 \u884c\u8cc7\u6599\uff0c\u5c31\u6703\u6709 1001 \u500b event (1000 \u500b get success \u52a0\u4e00\u500b transaction complete)\u3002\u7576\u5e74\u6211\u7528 HP Z620 \u91cf\u7684\u6642\u5019\u6bcf\u500b event \u5dee\u4e0d\u591a 16\u03bcs\uff0c\u6240\u4ee5\u6488 1000 \u884c\u7684\u57fa\u672c\u7a05\u7387\u5c31\u662f\u5728\u4f60\u7684 JS main thread \u4e0a\u6d6a\u8cbb 16ms\u3002\u90a3\u5982\u679c\u7528 cursor \u5462? \u5c0d\u4e0d\u8d77\uff0c\u4e00\u6a23\u3002\u8981\u8a18\u5f97\u9019\u662f\u5728 HP Z620 \u6709\u5169\u9846 Xeon \u7684\u5de5\u4f5c\u7ad9\u8dd1\u7684\u6578\u5b57\uff0c\u771f\u7684\u5230\u7528\u6236\u7684\u96fb\u8166\u4e0a\uff0c\u90a3\u7a05\u5c31\u4e0d\u53ea\u9019\u9ebc\u4e00\u9ede\u4e86\u3002\u53e6\u5916\u6211\u662f\u5f88\u5ba2\u6c23\u7684\u7528 1000 \u884c\u9019\u7a2e\u5fae\u4e0d\u8db3\u9053\u7684\u6578\u5b57\u4f86\u7b97\u7684\uff0c\u5982\u679c\u662f\u505a\u7167\u7247\u7ba1\u7406\u6216\u8a18\u5e33\u7684\u82e6\u4e3b\uff0c1000 \u7b46\u9019\u7a2e\u6578\u5b57\u585e\u7259\u7e2b\u90fd\u4e0d\u5920\u3002\u9019\u9084\u53ea\u7b97 eventing \u7684\u6642\u9593\uff0cindexing\/data transfer \u90a3\u500b\u6211\u5011\u901a\u901a\u90fd\u6c92\u7b97\u7684\u3002<\/p>\n<p>\u63a5\u4e0b\u4f86\u5c31\u662f\u898b\u8b49\u5947\u8e5f\u5f0f\u7684\u5947\u8469\u7684\u6642\u523b\u3002\u5927\u90e8\u4efd JS developer \u662f\u4e0d\u53ef\u80fd\u7528 raw API \u53bb\u786c\u5e79\u7684\uff0c\u6240\u4ee5\u4e00\u822c\u6703\u7528\u5305\u7684\u597d\u597d\u7684 promise \u5f0f\u7684 API\uff0c\u4f8b\u5982 <a href=\"https:\/\/github.com\/jurca\/indexed-db.es6\" target=\"_blank\">indexed-db.es6<\/a> \u3002\u6bd4\u65b9\u8aaa\uff0c\u5beb\u5e7e\u884c\u8cc7\u6599\u9032\u8cc7\u6599\u5eab:<\/p>\n<pre lang=\"javascript\">\r\ndb.runTransaction(['foo', 'bar'], (foo, bar, abort) => {\r\n  return Promise.all([foo.add('some row'), bar.add('some row')]);\r\n}).then(() => {\r\n  console.log('rows inserted');\r\n});\r\n<\/pre>\n<p>\u5f88\u7c21\u55ae\u561b\uff0c\u6c92\u4ec0\u9ebc\u554f\u984c\u3002\u597d\uff0c\u82e5\u662f\u4f60\u628a\u4f60\u7684\u7a0b\u5f0f\u9019\u6a23\u6539\u7684\u8a71\uff0c\u90a3\u4f60\u5c31\u5b8c\u86cb\u4e86:<\/p>\n<pre lang=\"javascript\">\r\ndb.runTransaction(['foo', 'bar'], (foo, bar, abort) => {\r\n  pingServer();  \/\/ Fire a heartbeat XHR to server, result don't care\r\n  return Promise.all([foo.add('some row'), bar.add('some row')]);\r\n}).then(() => {\r\n  console.log('rows inserted');\r\n});\r\n<\/pre>\n<p>\u4f60\u5f88\u5feb\u6703\u767c\u73fe\u4f60\u7684\u7a0b\u5f0f\u6c38\u9060\u90fd\u7121\u6cd5\u5beb\u5165\u8cc7\u6599\uff0c\u56e0\u70ba transaction auto-commit &#8230; \u9019\u4e00\u95dc\u4e0d\u77e5\u5361\u6b7b\u591a\u5c11\u4eba\uff0c\u56e0\u70ba\u5f88\u591a\u6642\u5019\u6211\u5011\u8981\u548c server \u540c\u6b65\u7684\u65b9\u5f0f\u5c31\u662f\u5982\u4ee5\u4e0b\u7684 pseudo code:<\/p>\n<pre lang=\"sql\">\r\nBEGIN TRANSACTION\r\n  communicate to server end point 1\r\n  process data\r\n  write to database\r\n  communicate to server end point 2\r\n  process data\r\n  write to database\r\nEND TRANSACTION\r\n<\/pre>\n<p>\u76ee\u524d IndexedDB \u7684\u8a2d\u8a08\u6703\u8b93\u4e0a\u8ff0\u7684 pseudo code \u6975\u96e3\u5b8c\u6210\uff0c\u4f60\u8981\u561b\u5c31\u662f\u6539\u8a2d\u8a08\uff0c\u8981\u561b\u5c31\u662f\u7528\u6211\u5beb\u7684 Lovefield\uff0c\u4e0d\u7136\u5c31\u662f\u81ea\u5df1\u505a caching \u548c transaction management \u6700\u5f8c\u518d\u4e00\u6b21\u5beb\u9032\u53bb\u8cc7\u6599\u5eab\uff0c\u6c92\u6709\u592a\u591a\u7684\u9078\u64c7\u3002<\/p>\n<p>\u6211\u597d\u50cf\u5fd8\u8a18\u8b1b\u4e00\u4ef6\u5f88\u53b2\u5bb3\u7684\u4e8b\uff0c\u5982\u679c\u4f60\u7684\u7a0b\u5f0f\u5e38\u5e38\u8981\u505a\u985e\u4f3c\u4e0b\u9762\u7684\u4e8b\u60c5<\/p>\n<pre lang=\"sql\">\r\nSELECT a.foo, b.bar\r\n  FROM a, b\r\n  WHERE a.id = b.id AND\r\n        a.timestamp < $1 AND\r\n        b.volume < $2\r\n  ORDER BY a.timestamp DESC\r\n  LIMIT $3\r\n  SKIP $4\r\n<\/pre>\n<p>\u606d\u559c\u4f60\uff0c\u8acb\u60f3\u60f3\u770b\u4f60\u662f\u5426\u8981\u91cd\u65b0\u8a2d\u8a08\u4e00\u4e0b\u4f60\u7684\u8cc7\u6599\u7d50\u69cb\u3002\u4f60\u770b\u5230\u7684\u6240\u6709 WHERE, ORDER BY, LIMIT, SKIP \u5168\u90e8\u90fd\u8981\u4f60\u81ea\u5df1\u7528 JavaScript \u505a\u51fa\u4f86\uff0cIndexedDB \u5e6b\u4e0d\u4e86\u4f60\u3002\u770b\u5230\u9019\u88e1\u662f\u4e0d\u662f\u89ba\u5f97\u76f4\u63a5\u56de\u982d\u7528 PHP+MySQL \u6216 ASP.Net \u6c92\u6709\u90a3\u9ebc\u611a\u8822\u4e86? \u9019\u5c31\u662f NoSQL hype \u53ef\u6015\u7684\u5730\u65b9\u3002\u5b83\u628a\u6240\u6709\u9700\u8981 relational functionality \u7684\u4eba\u90fd\u903c\u56de\u53bb\u7528 server side solution\uff0c\u518d\u5927\u524c\u524c\u7684\u8aaa client side \u6c92\u6709 relational \u7684\u9700\u6c42\uff0c\u8981 NoSQL \u624d\u5920 webby \u624d\u5920\u6f6e\u5920\u5a01\uff0c\u7136\u5f8c\u5c31\u7e7c\u7e8c\u597d\u68d2\u68d2\u4e0b\u53bb\u4e86\u3002<\/p>\n<p>\u6240\u4ee5\u4f60\u8981\u662f\u554f\u6211 IndexedDB \u5230\u5e95\u6709\u4ec0\u9ebc\u7528\u5462? \u6211\u53ea\u80fd\u544a\u8a34\u4f60\u5982\u679c\u4f60\u7684 server \u7aef\u662f document-based \u7684\u8cc7\u6599\u5eab\uff0c\u7136\u5f8c\u4f60\u4e5f\u4e0d\u592a care \u591a\u91cd\u689d\u4ef6\u67e5\u8a62\u7684\u8a71\uff0cIndexedDB \u9084\u883b\u597d\u7528\u3002\u9664\u6b64\u4e4b\u5916\u7684\u6240\u6709\u72c0\u6cc1\uff0c\u4f60\u5927\u6982\u9084\u662f\u4fdd\u6301\u4f60\u73fe\u6709\u7684 server-side solution \u591a\u8cb7\u9ede AWS \u6216 GCE\uff0c\u8981\u4e0d\u7136\u5c31\u5b78 G-Mail \u4f86\u7528\u6211\u5beb\u7684 <a href=\"https:\/\/github.com\/google\/lovefield\" target=\"_blank\">Lovefield<\/a> \u5427\u3002<\/p>\n<p>\u8b0e\u4e4b\u8072: \u770b\uff0c\u9019\u7bc7\u5f88\u50cf\u5ee3\u544a\u6587\u8036\u3002<br \/>\n\u6211: \u770b\uff0c\u6211\u81ea\u5df1\u7684 blog \u8981\u600e\u9ebc\u5ee3\u544a\u5c31\u600e\u9ebc\u5ee3\u544a\uff0c\u4e0d\u884c\u9006?<br \/>\n\u8b0e\u4e4b\u8072: \u770b\uff0cG-Mail \u7528\u4f60\u7684\u6771\u897f\uff0c\u5439\uff0c\u6211\u8b93\u4f60\u5439 ...<br \/>\n\u6211: inbox.google.com \u52a0\u5165\u5f8c\uff0crefresh \u4e00\u6b21\uff0c\u81ea\u5df1\u53bb JavaScript console \u770b\u770b\u6253 lf \u6703\u51fa\u4f86\u4ec0\u9ebc\u6771\u897f\u5427 ...<\/p>\n","protected":false},"excerpt":{"rendered":"<p>IndexedDB \u5230\u5e95\u5947\u8469\u5728\u54ea\u88e1\u5462? \u6709\u7df4\u904e\u7684\u9ecd\u9ecd\u4f86\u544a\u8a34\u4f60 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,2],"tags":[],"_links":{"self":[{"href":"https:\/\/www.cchsu.com\/arthur\/wp-json\/wp\/v2\/posts\/560"}],"collection":[{"href":"https:\/\/www.cchsu.com\/arthur\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cchsu.com\/arthur\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cchsu.com\/arthur\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cchsu.com\/arthur\/wp-json\/wp\/v2\/comments?post=560"}],"version-history":[{"count":11,"href":"https:\/\/www.cchsu.com\/arthur\/wp-json\/wp\/v2\/posts\/560\/revisions"}],"predecessor-version":[{"id":571,"href":"https:\/\/www.cchsu.com\/arthur\/wp-json\/wp\/v2\/posts\/560\/revisions\/571"}],"wp:attachment":[{"href":"https:\/\/www.cchsu.com\/arthur\/wp-json\/wp\/v2\/media?parent=560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cchsu.com\/arthur\/wp-json\/wp\/v2\/categories?post=560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cchsu.com\/arthur\/wp-json\/wp\/v2\/tags?post=560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}