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_refreshedNode = 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');
50 sendRequestSync: function(mode, url, data){
52 var q = this.createRequestObject();
53 q.open(mode, url, false);
54 q.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
55 this.requestObjectDisableCache(q);
59 console.log("sendRequestSync:Network Error.\n");
60 this.isEnabledNetDB = false;
64 console.log("ネットワークにアクセスできません。" + q.status + ":" + q.statusText);
65 }else if((200 <= q.status && q.status < 300) || (q.status == 304)){
66 var res = q.responseText;
69 console.log("サーバーがエラーを返しました。" + request.status + ":" + request.statusText);
71 this.isEnabledNetDB = false;
78 if(this.root.length == 0){
80 r = this.sendRequestSync("GET", this.syncPHPURL + "?action=getallnode", null);
83 d = this.getNodeFromUUID(this.UUID_Node_MemoryDBNetworkTimestamp).identifier;
84 r = this.sendRequestSync("GET", this.syncPHPURL + "?action=getnodemod&t=" + d, null);
87 for(var i = 0, iLen = a.length; i < iLen; i++){
91 console.log(i + ": " + e + "\n");
97 this.refreshNodeInternal(d[2], d[1], d[0]);
99 console.log(this.root);
101 refreshNode: function(ident, tid, nid){
102 // 該当タグのデータを書き換え、もしくは新規作成する。
104 // nid(nodeid)は省略可能で、省略時は新たなUUIDが自動的に付与される
105 // tid(typeid)も省略可能で、省略時はNullUUIDが付与される。
106 // identはnullもしくは空文字でもかまわない。
107 // 戻り値はMemoryDBNodeTagインスタンス
108 // エラー発生時はundefinedを返す。
109 this.refreshNodeInternal(ident, tid, nid, true);
111 refreshNodeInternal: function(ident, tid, nid, enableSync){
113 // enableSync == trueでネットワーク同期する
116 tid = this.UUID_Null;
119 nid = this.createUUID();
122 t = this.getNodeFromUUID(nid);
128 t = new MemoryDBNodeTag(nid, tid, ident);
130 this.nodeList.push(t);
131 if(enableSync && this.isEnabledNetDB){
132 s = this.syncPHPURL + "?action=addnode";
133 s += "&nid=" + encodeURIComponent(nid);
134 s += "&tid=" + encodeURIComponent(tid);
135 s += "&ident=" + encodeURIComponent(ident);
136 r = this.sendRequestSync("GET", s, null);
140 if(this.callback_refreshedNode){
141 this.callback_refreshedNode(t);
144 createUUID: function(){
145 var f = this.createUUIDSub;
146 return (f() + f() + "-" + f() + "-" + f() + "-" + f() + "-" + f() + f() + f());
148 createUUIDSub: function(){
149 return (((1 + Math.random()) * 0x10000) | 0).toString(16).toLowerCase().substring(1);
151 getNodeFromUUID: function(nodeid){
152 return this.nodeList.isIncluded(nodeid, function(a, b){ return a.nodeid == b; });
156 function MemoryDBNodeTag(nodeid, typeid, identifier){
157 this.nodeid = nodeid;
158 this.typeid = typeid;
159 this.identifier = identifier;
162 MemoryDBNodeTag.prototype = {
166 function MemoryDBEdgeTag(typeUUIDStr){
169 MemoryDBEdgeTag.prototype = {