this.syncPHPURL = syncPHPURL;
this.isEnabledNetDB = true;
//
- this.callback_addNode = null; // function(nodeinstance){};
+ this.callback_refreshedNode = null; // function(nodeinstance){};
}
MemoryDB.prototype = {
UUID_Null: "00000000-0000-0000-0000-000000000000",
rq.setRequestHeader('Pragma', 'no-cache'); // HTTP/1.0 における汎用のヘッダフィールド
rq.setRequestHeader('Cache-Control', 'no-cache'); // HTTP/1.1 におけるキャッシュ制御のヘッダフィールド
rq.setRequestHeader('If-Modified-Since', 'Thu, 01 Jun 1970 00:00:00 GMT');
-
},
sendRequestSync: function(mode, url, data){
//同期モード
},
syncDB: function(){
// MySQLと同期
- var r, a, d, t;
+ // 定期的に呼び出されることを想定
+ var r, a, d;
if(this.root.length == 0){
// 初回同期時は全て取得
r = this.sendRequestSync("GET", this.syncPHPURL + "?action=getallnode", null);
} else{
// 差分のみを取得
-
+ d = this.getNodeFromUUID(this.UUID_Node_MemoryDBNetworkTimestamp).identifier;
+ r = this.sendRequestSync("GET", this.syncPHPURL + "?action=getnodemod&t=" + d, null);
}
a = r.split("\n");
for(var i = 0, iLen = a.length; i < iLen; i++){
if(d === undefined){
continue;
}
- t = new MemoryDBNodeTag(d[0], d[1], d[2]);
- if(t){
- this.root.push(t);
- this.nodeList.push(t);
- }
+ this.refreshNodeInternal(d[2], d[1], d[0]);
}
console.log(this.root);
},
- editNode: function(ident, tid, nid, disableSync){
- // Nodeを追加し、データベースにも反映し、可能ならばネットワークにも反映させる
+ refreshNode: function(ident, tid, nid){
+ // 該当タグのデータを書き換え、もしくは新規作成する。
+ // 可能であればネットワークに反映する
// nid(nodeid)は省略可能で、省略時は新たなUUIDが自動的に付与される
// tid(typeid)も省略可能で、省略時はNullUUIDが付与される。
+ // identはnullもしくは空文字でもかまわない。
// 戻り値はMemoryDBNodeTagインスタンス
- // すでに同じnodeidのNodeが存在している場合はundefinedを返しデータベースへの変更は行われない。
- var t, s, r;
- if(!ident){
- return undefined;
- }
- if(!tid){
- tid = this.UUID_Null;
- }
- if(!nid){
- nid = this.createUUID();
- }
- // 存在確認
- t = this.getNodeFromUUID(nid);
- if(t){
- return undefined;
- }
- t = new MemoryDBNodeTag(nid, tid, ident);
-
- if(this.isEnabledNetDB){
- s = this.syncPHPURL + "?action=addnode";
- s += "&nid=" + encodeURIComponent(nid);
- s += "&tid=" + encodeURIComponent(tid);
- s += "&ident=" + encodeURIComponent(ident);
- r = this.sendRequestSync("GET", s, null);
- console.log(r);
- }
+ // エラー発生時はundefinedを返す。
+ this.refreshNodeInternal(ident, tid, nid, true);
},
- addNode: function(ident, tid, nid){
- // Nodeを追加し、データベースにも反映し、可能ならばネットワークにも反映させる
- // nid(nodeid)は省略可能で、省略時は新たなUUIDが自動的に付与される
- // tid(typeid)も省略可能で、省略時はNullUUIDが付与される。
- // 戻り値はMemoryDBNodeTagインスタンス
- // すでに同じnodeidのNodeが存在している場合はundefinedを返しデータベースへの変更は行われない。
+ refreshNodeInternal: function(ident, tid, nid, enableSync){
+ // 基本的にローカルデータのみ変更
+ // enableSync == trueでネットワーク同期する
var t, s, r;
- if(!ident){
- return undefined;
- }
if(!tid){
tid = this.UUID_Null;
}
// 存在確認
t = this.getNodeFromUUID(nid);
if(t){
+ // 変更
return undefined;
+ } else{
+ // 新規作成
+ t = new MemoryDBNodeTag(nid, tid, ident);
+ this.root.push(t);
+ this.nodeList.push(t);
+ if(enableSync && this.isEnabledNetDB){
+ s = this.syncPHPURL + "?action=addnode";
+ s += "&nid=" + encodeURIComponent(nid);
+ s += "&tid=" + encodeURIComponent(tid);
+ s += "&ident=" + encodeURIComponent(ident);
+ r = this.sendRequestSync("GET", s, null);
+ console.log(r);
+ }
}
- t = new MemoryDBNodeTag(nid, tid, ident);
-
- if(this.isEnabledNetDB){
- s = this.syncPHPURL + "?action=addnode";
- s += "&nid=" + encodeURIComponent(nid);
- s += "&tid=" + encodeURIComponent(tid);
- s += "&ident=" + encodeURIComponent(ident);
- r = this.sendRequestSync("GET", s, null);
- console.log(r);
+ if(this.callback_refreshedNode){
+ this.callback_refreshedNode(t);
}
},
createUUID: function(){
this.typeid = typeid;
this.identifier = identifier;
//
-
}
MemoryDBNodeTag.prototype = {
this.edgeList = new Array();
this.selectedNode = null;
this.selectedEdge = null;
+ this.srcMemoryDB = null;
+ this.srcMemoryDBSyncPerTick = 60;
+ this.srcMemoryDBSyncCount = this.srcMemoryDBSyncPerTick;
var that = this;
window.addEventListener('keydown', function(event){
this.edgeList.push(new MGEdge(this, p[i][2], n(p[i][0]), n(p[i][1])));
}
},
- loadGraphFromMemoryDB: function(mdb){
- var a, l, t, u, m, e, f;
- a = mdb.nodeList;
- l = a.length;
- //m = 12 * l;
- t = new MGNode(this, "Node");
- this.nodeList.push(t);
- f = function(){};
- for(var i = 0; i < l; i++){
- u = new MGNode(this, a[i].identifier);
- this.nodeList.push(u);
- //e = new MGEdge(this, null, t, u);
- //this.edgeList.push(e);
- //t = u;
- //e.freeLength = m;
- //e.draw = f;
+ setSourceMemoryDB: function(mdb){
+ var that = this;
+ this.srcMemoryDB = mdb;
+ mdb.callback_refreshedNode = function(t){
+ var n;
+ n = new MGNode(that, t.identifier);
+ that.nodeList.push(n);
}
},
bringToCenter: function(){
var nTemp;
this.tickCount++;
+ this.srcMemoryDBSyncCount++;
//
- // AutomaticTracking
+ // Sync
//
+ if(this.srcMemoryDB && this.srcMemoryDBSyncCount > this.srcMemoryDBSyncPerTick){
+ this.srcMemoryDB.syncDB();
+ this.srcMemoryDBSyncCount = 0;
+ }
+ //
+ // AutomaticTracking
+ //
if(this.isEnabledAutomaticTracking && (this.tickCount % 30 == 0)){
this.bringInScreen();
}