OSDN Git Service

ノードの同期はだいたいできた
[chnosproject/AI004.git] / memdb / memdb.js
1 // 知識表現のデータベース
2 // php経由でMySQLデータベースに同期する機能と
3 // データへのアクセスを提供する
4
5 function MemoryDB(syncPHPURL){
6         // 全データ
7         this.root = new Array();
8         //
9         this.nodeList = new Array();
10         //
11         this.syncPHPURL = syncPHPURL;
12         this.isEnabledNetDB = true;
13         //
14         this.callback_refreshedNode = null;     // function(nodeinstance){};
15 }
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(){
21                 var rq = null;
22                 // XMLHttpRequest
23                 try{
24                         // XMLHttpRequest オブジェクトを作成
25                         rq = new XMLHttpRequest();
26                 }catch(e){}
27                 // Internet Explorer
28                 try{
29                         rq = new ActiveXObject('MSXML2.XMLHTTP.6.0');
30                 }catch(e){}
31                 try{
32                         rq = new ActiveXObject('MSXML2.XMLHTTP.3.0');
33                 }catch(e){}
34                 try{
35                         rq = new ActiveXObject('MSXML2.XMLHTTP');
36                 }catch(e){}
37                 if(rq == null){
38                         return null;
39                 }
40                 return rq;
41         },
42         requestObjectDisableCache: function(rq){
43                 //call after open request.
44                 //disable cache
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');
49         },
50         sendRequestSync: function(mode, url, data){
51                 //同期モード
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);
56                 try {
57                         q.send(data);
58                 } catch(e){
59                         console.log("sendRequestSync:Network Error.\n");
60                         this.isEnabledNetDB = false;
61                         return null;
62                 }
63                 if(q.status == 0){
64                         console.log("ネットワークにアクセスできません。" + q.status + ":" + q.statusText);
65                 }else if((200 <= q.status && q.status < 300) || (q.status == 304)){
66                         var res = q.responseText;
67                         return res;
68                 } else{
69                         console.log("サーバーがエラーを返しました。" + request.status + ":" + request.statusText);
70                 }
71                 this.isEnabledNetDB = false;
72                 return null;
73         },
74         syncDB: function(){
75                 // MySQLと同期
76                 // 定期的に呼び出されることを想定
77                 var r, a, d;
78                 if(this.root.length == 0){
79                         // 初回同期時は全て取得
80                         r = this.sendRequestSync("GET", this.syncPHPURL + "?action=getallnode", null);
81                 } else{
82                         // 差分のみを取得
83                         d = this.getNodeFromUUID(this.UUID_Node_MemoryDBNetworkTimestamp).identifier;
84                         r = this.sendRequestSync("GET", this.syncPHPURL + "?action=getnodemod&t=" + d, null);
85                 }
86                 a = r.split("\n");
87                 for(var i = 0, iLen = a.length; i < iLen; i++){
88                         try{
89                                 d = eval(a[i]);
90                         } catch(e){
91                                 console.log(i + ": " + e + "\n");
92                                 continue;
93                         }
94                         if(d === undefined){
95                                 continue;
96                         }
97                         this.refreshNodeInternal(d[2], d[1], d[0]);
98                 }
99                 console.log(this.root);
100         },
101         refreshNode: function(ident, tid, nid){
102                 // 該当タグのデータを書き換え、もしくは新規作成する。
103                 // 可能であればネットワークに反映する
104                 // nid(nodeid)は省略可能で、省略時は新たなUUIDが自動的に付与される
105                 // tid(typeid)も省略可能で、省略時はNullUUIDが付与される。
106                 // identはnullもしくは空文字でもかまわない。
107                 // 戻り値はMemoryDBNodeTagインスタンス
108                 // エラー発生時はundefinedを返す。
109                 this.refreshNodeInternal(ident, tid, nid, true);
110         },
111         refreshNodeInternal: function(ident, tid, nid, enableSync){
112                 // 基本的にローカルデータのみ変更
113                 // enableSync == trueでネットワーク同期する
114                 var t, s, r;
115                 if(!tid){
116                         tid = this.UUID_Null;
117                 }
118                 if(!nid){
119                         nid = this.createUUID();
120                 }
121                 // 存在確認
122                 t = this.getNodeFromUUID(nid);
123                 if(t){
124                         // 変更
125                         return undefined;
126                 } else{
127                         // 新規作成
128                         t = new MemoryDBNodeTag(nid, tid, ident);
129                         this.root.push(t);
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);
137                                 console.log(r);
138                         }
139                 }
140                 if(this.callback_refreshedNode){
141                         this.callback_refreshedNode(t);
142                 }
143         },
144         createUUID: function(){
145                 var f = this.createUUIDSub;
146                 return (f() + f() + "-" + f() + "-" + f() + "-" + f() + "-" + f() + f() + f());
147         },
148         createUUIDSub: function(){
149                 return (((1 + Math.random()) * 0x10000) | 0).toString(16).toLowerCase().substring(1);
150         },
151         getNodeFromUUID: function(nodeid){
152                 return this.nodeList.isIncluded(nodeid, function(a, b){ return a.nodeid == b; });
153         },
154 }
155
156 function MemoryDBNodeTag(nodeid, typeid, identifier){
157         this.nodeid = nodeid;
158         this.typeid = typeid;
159         this.identifier = identifier;
160         //
161 }
162 MemoryDBNodeTag.prototype = {
163
164 }
165
166 function MemoryDBEdgeTag(typeUUIDStr){
167         this.uuid = null;
168 }
169 MemoryDBEdgeTag.prototype = {
170
171 }