2 // php経由でMySQLデータベースに同期する機能と
5 function MemoryDB(syncPHPURL){
7 this.root = new Array();
9 this.nodeList = new Array();
11 this.syncPHPURL = syncPHPURL;
12 this.isEnabledNetDB = true;
14 this.callback_updatedNode = null; // function(nodeinstance){};
16 MemoryDB.prototype = {
17 UUID_Null: "00000000-0000-0000-0000-000000000000",
18 UUID_NodeType_DecimalNumber: "e3346fd4-ac17-41c3-b3c7-e04972e5c014",
19 UUID_Node_MemoryDBNetworkTimestamp: "a2560a9c-dcf7-4746-ac14-347188518cf2",
20 UUID_Node_MemoryDBNetworkResponseCode: "1eeb6d3d-751f-444f-91c8-ed940e65f8bd",
21 createRequestObject: function(){
25 // XMLHttpRequest オブジェクトを作成
26 rq = new XMLHttpRequest();
30 rq = new ActiveXObject('MSXML2.XMLHTTP.6.0');
33 rq = new ActiveXObject('MSXML2.XMLHTTP.3.0');
36 rq = new ActiveXObject('MSXML2.XMLHTTP');
43 requestObjectDisableCache: function(rq){
44 //call after open request.
46 //http://vird2002.s8.xrea.com/javascript/XMLHttpRequest.html
47 rq.setRequestHeader('Pragma', 'no-cache'); // HTTP/1.0 における汎用のヘッダフィールド
48 rq.setRequestHeader('Cache-Control', 'no-cache'); // HTTP/1.1 におけるキャッシュ制御のヘッダフィールド
49 rq.setRequestHeader('If-Modified-Since', 'Thu, 01 Jun 1970 00:00:00 GMT');
51 sendRequestSync: function(mode, url, data){
53 var q = this.createRequestObject();
54 q.open(mode, url, false);
55 q.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
56 this.requestObjectDisableCache(q);
60 console.log("sendRequestSync:Network Error.\n");
61 this.isEnabledNetDB = false;
65 console.log("ネットワークにアクセスできません。" + q.status + ":" + q.statusText);
66 }else if((200 <= q.status && q.status < 300) || (q.status == 304)){
67 var res = q.responseText;
70 console.log("サーバーがエラーを返しました。" + request.status + ":" + request.statusText);
72 this.isEnabledNetDB = false;
79 if(this.root.length == 0){
81 r = this.sendRequestSync("GET", this.syncPHPURL + "?action=getallnode", null);
84 d = this.getNodeFromUUID(this.UUID_Node_MemoryDBNetworkTimestamp).identifier;
85 r = this.sendRequestSync("GET", this.syncPHPURL + "?action=getnodemod&t=" + d, null);
88 for(var i = 0, iLen = a.length; i < iLen; i++){
92 console.log(i + ": " + e + "\n");
98 this.updateNodeInternal(d[2], d[1], d[0]);
100 //console.log(this.root);
102 updateNode: function(ident, tid, nid){
103 // 該当タグのデータを書き換え、もしくは新規作成する。
105 // nid(nodeid)は省略可能で、省略時は新たなUUIDが自動的に付与される
106 // tid(typeid)も省略可能で、省略時はNullUUIDが付与される。
107 // identはnullもしくは空文字でもかまわない。
108 // 戻り値はMemoryDBNodeTagインスタンス
109 // エラー発生時はundefinedを返す。
110 this.updateNodeInternal(ident, tid, nid, true);
112 updateNodeInternal: function(ident, tid, nid, enableSync){
114 // enableSync == trueでネットワーク同期する
117 tid = this.UUID_Null;
120 nid = this.createUUID();
123 t = this.getNodeFromUUID(nid);
127 t.identifier = ident;
128 if(enableSync && this.isEnabledNetDB){
129 s = this.syncPHPURL + "?action=updatenode";
130 s += "&nid=" + encodeURIComponent(nid);
131 s += "&tid=" + encodeURIComponent(tid);
132 s += "&ident=" + encodeURIComponent(ident);
133 r = this.sendRequestSync("GET", s, null);
138 t = new MemoryDBNodeTag(nid, tid, ident);
140 this.nodeList.push(t);
141 if(enableSync && this.isEnabledNetDB){
142 s = this.syncPHPURL + "?action=addnode";
143 s += "&nid=" + encodeURIComponent(nid);
144 s += "&tid=" + encodeURIComponent(tid);
145 s += "&ident=" + encodeURIComponent(ident);
146 r = this.sendRequestSync("GET", s, null);
150 if(this.callback_updatedNode){
151 this.callback_updatedNode(t);
154 createUUID: function(){
155 var f = this.createUUIDSub;
156 return (f() + f() + "-" + f() + "-" + f() + "-" + f() + "-" + f() + f() + f());
158 createUUIDSub: function(){
159 return (((1 + Math.random()) * 0x10000) | 0).toString(16).toLowerCase().substring(1);
161 getNodeFromUUID: function(nodeid){
162 return this.nodeList.isIncluded(nodeid, function(a, b){ return a.nodeid == b; });
166 function MemoryDBNodeTag(nodeid, typeid, identifier){
167 this.nodeid = nodeid;
168 this.typeid = typeid;
169 this.identifier = identifier;
172 MemoryDBNodeTag.prototype = {
176 function MemoryDBEdgeTag(typeUUIDStr){
179 MemoryDBEdgeTag.prototype = {