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_addNode = 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 createRequestObject: function(){
24 // XMLHttpRequest オブジェクトを作成
25 rq = new XMLHttpRequest();
29 rq = new ActiveXObject('MSXML2.XMLHTTP.6.0');
32 rq = new ActiveXObject('MSXML2.XMLHTTP.3.0');
35 rq = new ActiveXObject('MSXML2.XMLHTTP');
42 requestObjectDisableCache: function(rq){
43 //call after open request.
45 //http://vird2002.s8.xrea.com/javascript/XMLHttpRequest.html
46 rq.setRequestHeader('Pragma', 'no-cache'); // HTTP/1.0 における汎用のヘッダフィールド
47 rq.setRequestHeader('Cache-Control', 'no-cache'); // HTTP/1.1 におけるキャッシュ制御のヘッダフィールド
48 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;
78 if(this.root.length == 0){
80 r = this.sendRequestSync("GET", this.syncPHPURL + "?action=getallnode", null);
86 for(var i = 0, iLen = a.length; i < iLen; i++){
90 console.log(i + ": " + e + "\n");
96 t = new MemoryDBNodeTag(d[0], d[1], d[2]);
99 this.nodeList.push(t);
102 console.log(this.root);
104 editNode: function(ident, tid, nid, disableSync){
105 // Nodeを追加し、データベースにも反映し、可能ならばネットワークにも反映させる
106 // nid(nodeid)は省略可能で、省略時は新たなUUIDが自動的に付与される
107 // tid(typeid)も省略可能で、省略時はNullUUIDが付与される。
108 // 戻り値はMemoryDBNodeTagインスタンス
109 // すでに同じnodeidのNodeが存在している場合はundefinedを返しデータベースへの変更は行われない。
115 tid = this.UUID_Null;
118 nid = this.createUUID();
121 t = this.getNodeFromUUID(nid);
125 t = new MemoryDBNodeTag(nid, tid, ident);
127 if(this.isEnabledNetDB){
128 s = this.syncPHPURL + "?action=addnode";
129 s += "&nid=" + encodeURIComponent(nid);
130 s += "&tid=" + encodeURIComponent(tid);
131 s += "&ident=" + encodeURIComponent(ident);
132 r = this.sendRequestSync("GET", s, null);
136 addNode: function(ident, tid, nid){
137 // Nodeを追加し、データベースにも反映し、可能ならばネットワークにも反映させる
138 // nid(nodeid)は省略可能で、省略時は新たなUUIDが自動的に付与される
139 // tid(typeid)も省略可能で、省略時はNullUUIDが付与される。
140 // 戻り値はMemoryDBNodeTagインスタンス
141 // すでに同じnodeidのNodeが存在している場合はundefinedを返しデータベースへの変更は行われない。
147 tid = this.UUID_Null;
150 nid = this.createUUID();
153 t = this.getNodeFromUUID(nid);
157 t = new MemoryDBNodeTag(nid, tid, ident);
159 if(this.isEnabledNetDB){
160 s = this.syncPHPURL + "?action=addnode";
161 s += "&nid=" + encodeURIComponent(nid);
162 s += "&tid=" + encodeURIComponent(tid);
163 s += "&ident=" + encodeURIComponent(ident);
164 r = this.sendRequestSync("GET", s, null);
168 createUUID: function(){
169 var f = this.createUUIDSub;
170 return (f() + f() + "-" + f() + "-" + f() + "-" + f() + "-" + f() + f() + f());
172 createUUIDSub: function(){
173 return (((1 + Math.random()) * 0x10000) | 0).toString(16).toLowerCase().substring(1);
175 getNodeFromUUID: function(nodeid){
176 return this.nodeList.isIncluded(nodeid, function(a, b){ return a.nodeid == b; });
180 function MemoryDBNodeTag(nodeid, typeid, identifier){
181 this.nodeid = nodeid;
182 this.typeid = typeid;
183 this.identifier = identifier;
187 MemoryDBNodeTag.prototype = {
191 function MemoryDBEdgeTag(typeUUIDStr){
194 MemoryDBEdgeTag.prototype = {