2 // php経由でMySQLデータベースに同期する機能と
5 function MemoryDB(syncPHPURL){
7 this.root = new Array();
9 this.nodeList = new Array();
10 this.edgeList = new Array();
12 this.syncPHPURL = syncPHPURL;
13 this.isEnabledNetDB = true;
15 this.callback_updatedNode = null; // function(nodeInstance){};
16 this.callback_updatedEdge = null; // function(edgeInstance){};
18 MemoryDB.prototype = {
19 UUID_Null: "00000000-0000-0000-0000-000000000000",
20 UUID_NodeType_DecimalNumber: "e3346fd4-ac17-41c3-b3c7-e04972e5c014",
21 UUID_Node_MemoryDBNetworkTimestamp: "a2560a9c-dcf7-4746-ac14-347188518cf2",
22 UUID_Node_MemoryDBNetworkResponseCode: "1eeb6d3d-751f-444f-91c8-ed940e65f8bd",
23 createRequestObject: function(){
27 // XMLHttpRequest オブジェクトを作成
28 rq = new XMLHttpRequest();
35 rq = new ActiveXObject('MSXML2.XMLHTTP.6.0');
41 rq = new ActiveXObject('MSXML2.XMLHTTP.3.0');
47 rq = new ActiveXObject('MSXML2.XMLHTTP');
54 requestObjectDisableCache: function(rq){
55 //call after open request.
57 //http://vird2002.s8.xrea.com/javascript/XMLHttpRequest.html
58 rq.setRequestHeader('Pragma', 'no-cache'); // HTTP/1.0 における汎用のヘッダフィールド
59 rq.setRequestHeader('Cache-Control', 'no-cache'); // HTTP/1.1 におけるキャッシュ制御のヘッダフィールド
60 rq.setRequestHeader('If-Modified-Since', 'Thu, 01 Jun 1970 00:00:00 GMT');
62 sendRequestSync: function(mode, url, data){
64 var q = this.createRequestObject();
65 q.open(mode, url, false);
66 q.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
67 this.requestObjectDisableCache(q);
71 console.log("sendRequestSync:Network Error.\n");
72 this.isEnabledNetDB = false;
76 console.log("ネットワークにアクセスできません。" + q.status + ":" + q.statusText);
77 }else if((200 <= q.status && q.status < 300) || (q.status == 304)){
78 var res = q.responseText;
81 console.log("サーバーがエラーを返しました。" + request.status + ":" + request.statusText);
83 this.isEnabledNetDB = false;
92 syncDBNode: function(){
94 if(this.nodeList.length == 0){
96 r = this.sendRequestSync("GET", this.syncPHPURL + "?action=getallnode", null);
99 d = this.getNodeFromUUID(this.UUID_Node_MemoryDBNetworkTimestamp).identifier;
100 r = this.sendRequestSync("GET", this.syncPHPURL + "?action=getallnodemod&t=" + d, null);
103 for(var i = 0, iLen = a.length; i < iLen; i++){
107 console.log(i + ": " + e + "\n");
113 this.updateNodeInternal(d[2], d[1], d[0]);
116 syncDBEdge: function(){
118 if(this.edgeList.length == 0){
120 r = this.sendRequestSync("GET", this.syncPHPURL + "?action=getalledge", null);
124 d = this.getNodeFromUUID(this.UUID_Node_MemoryDBNetworkTimestamp).identifier;
125 r = this.sendRequestSync("GET", this.syncPHPURL + "?action=getallnodemod&t=" + d, null);
130 for(var i = 0, iLen = a.length; i < iLen; i++){
134 console.log(i + ": " + e + "\n");
140 if( d[0] != this.UUID_Node_MemoryDBNetworkTimestamp &&
141 d[0] != this.UUID_Node_MemoryDBNetworkResponseCode){
143 this.updateEdgeInternal(d[2], d[3], d[1], d[0]);
146 this.updateNodeInternal(d[2], d[1], d[0]);
150 updateNode: function(ident, tid, nid){
151 // 該当タグのデータを書き換え、もしくは新規作成する。
153 // nid(nodeid)は省略可能で、省略時は新たなUUIDが自動的に付与される
154 // tid(typeid)も省略可能で、省略時はNullUUIDが付与される。
155 // identはnullもしくは空文字でもかまわない。
156 // 戻り値はMemoryDBNodeTagインスタンス
157 // エラー発生時はundefinedを返す。
158 this.updateNodeInternal(ident, tid, nid, true);
160 updateNodeInternal: function(ident, tid, nid, enableSync){
162 // enableSync == trueでネットワーク同期する
165 tid = this.UUID_Null;
168 nid = this.createUUID();
171 t = this.getNodeFromUUID(nid);
175 t.identifier = ident;
176 if(enableSync && this.isEnabledNetDB){
177 s = this.syncPHPURL + "?action=updatenode";
178 s += "&nid=" + encodeURIComponent(nid);
179 s += "&tid=" + encodeURIComponent(tid);
180 s += "&ident=" + encodeURIComponent(ident);
181 r = this.sendRequestSync("GET", s, null);
186 t = new MemoryDBNodeTag(nid, tid, ident);
188 this.nodeList.push(t);
189 if(enableSync && this.isEnabledNetDB){
190 s = this.syncPHPURL + "?action=addnode";
191 s += "&nid=" + encodeURIComponent(nid);
192 s += "&tid=" + encodeURIComponent(tid);
193 s += "&ident=" + encodeURIComponent(ident);
194 r = this.sendRequestSync("GET", s, null);
198 if(this.callback_updatedNode){
199 this.callback_updatedNode(t);
202 updateEdge: function(nid0, nid1, tid, eid){
203 // 該当タグのデータを書き換え、もしくは新規作成する。
205 // eid(nodeid)は省略可能で、省略時は新たなUUIDが自動的に付与される
206 // tid(typeid)も省略可能で、省略時はNullUUIDが付与される
207 // 戻り値はMemoryDBEdgeTagインスタンス
208 // エラー発生時はundefinedを返す。
209 this.updateEdgeInternal(nid0, nid1, tid, eid, true);
211 updateEdgeInternal: function(nid0, nid1, tid, eid, enableSync){
213 // enableSync == trueでネットワーク同期する
216 tid = this.UUID_Null;
219 eid = this.createUUID();
222 t = this.getEdgeFromUUID(eid);
227 t.identifier = ident;
228 if(enableSync && this.isEnabledNetDB){
229 s = this.syncPHPURL + "?action=updatenode";
230 s += "&nid=" + encodeURIComponent(nid);
231 s += "&tid=" + encodeURIComponent(tid);
232 s += "&ident=" + encodeURIComponent(ident);
233 r = this.sendRequestSync("GET", s, null);
240 t = new MemoryDBEdgeTag(eid, tid, nid0, nid1);
242 this.edgeList.push(t);
243 if(enableSync && this.isEnabledNetDB){
244 s = this.syncPHPURL + "?action=addedge";
245 s += "&eid=" + encodeURIComponent(eid);
246 s += "&tid=" + encodeURIComponent(tid);
247 s += "&nid0=" + encodeURIComponent(nid0);
248 s += "&nid1=" + encodeURIComponent(nid1);
249 r = this.sendRequestSync("GET", s, null);
253 if(this.callback_updatedEdge){
254 this.callback_updatedEdge(t);
257 createUUID: function(){
258 var f = this.createUUIDSub;
259 return (f() + f() + "-" + f() + "-" + f() + "-" + f() + "-" + f() + f() + f());
261 createUUIDSub: function(){
262 return (((1 + Math.random()) * 0x10000) | 0).toString(16).toLowerCase().substring(1);
264 getNodeFromUUID: function(nodeid){
265 return this.nodeList.isIncluded(nodeid, function(a, b){ return a.nodeid == b; });
267 getEdgeFromUUID: function(edgeid){
268 return this.edgeList.isIncluded(edgeid, function(a, b){ return a.edgeid == b; });
272 function MemoryDBNodeTag(nodeid, typeid, identifier){
273 this.nodeid = nodeid;
274 this.typeid = typeid;
275 this.identifier = identifier;
278 MemoryDBNodeTag.prototype = {
282 function MemoryDBEdgeTag(edgeid, typeid, nodeid0, nodeid1){
283 this.edgeid = edgeid;
284 this.typeid = typeid;
285 this.nodeid0 = nodeid0;
286 this.nodeid1 = nodeid1;
288 MemoryDBEdgeTag.prototype = {