OSDN Git Service

ノードの同期はだいたいできた
authorhikarupsp <hikarupsp@users.sourceforge.jp>
Mon, 28 Apr 2014 11:50:51 +0000 (20:50 +0900)
committerhikarupsp <hikarupsp@users.sourceforge.jp>
Mon, 28 Apr 2014 11:50:51 +0000 (20:50 +0900)
memdb/memdb.js
memdb/memdb.php
mgcanvas/index.html
mgcanvas/mgcanvas.js

index 042b179..faf069d 100644 (file)
@@ -11,7 +11,7 @@ function MemoryDB(syncPHPURL){
        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",
@@ -46,7 +46,6 @@ MemoryDB.prototype = {
                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){
                //同期モード
@@ -74,13 +73,15 @@ MemoryDB.prototype = {
        },
        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++){
@@ -93,56 +94,24 @@ MemoryDB.prototype = {
                        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;
                }
@@ -152,17 +121,24 @@ MemoryDB.prototype = {
                // 存在確認
                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(){
@@ -182,7 +158,6 @@ function MemoryDBNodeTag(nodeid, typeid, identifier){
        this.typeid = typeid;
        this.identifier = identifier;
        //
-       
 }
 MemoryDBNodeTag.prototype = {
 
index 3ad4f51..e3b055a 100644 (file)
@@ -21,7 +21,7 @@ define("QUERY_CREATE_TABLE_Node", "
 create table Node (
        nodeid binary(16) primary key,
        typeid binary(16) not null,
-       identifier text character set utf8 not null,
+       identifier text character set utf8,
        modtimestamp bigint
 )
 ");
index 5d02701..fdc3dc8 100755 (executable)
@@ -22,11 +22,8 @@ var memdb;
 
 onload = function() {
        memdb = new MemoryDB("./../memdb/memdb.php");
-       memdb.syncDB();
-       
-       var c = document.getElementById("mainCanvas");
-       mgmain = new MGCanvas(c);
-       mgmain.loadGraphFromMemoryDB(memdb);
+       mgmain = new MGCanvas(document.getElementById("mainCanvas"));
+       mgmain.setSourceMemoryDB(memdb);
 }
 </script>
 </head>
@@ -46,7 +43,7 @@ onload = function() {
 identifier:<input id="identBox" type="text"></input>
 <br />
 <button onclick="mgmain.setIdentifierForSelectedNode(getElementById('identBox').value);">setNodeIdent</button>
-<button onclick="memdb.addNode(getElementById('identBox').value);">addNode</button>
+<button onclick="memdb.refreshNode(getElementById('identBox').value);">addNode</button>
 
 </body>
 </html>
\ No newline at end of file
index 28fac96..e3cae90 100755 (executable)
@@ -12,6 +12,9 @@ function MGCanvas(canvasDOMObj){
        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){
@@ -79,22 +82,13 @@ MGCanvas.prototype = {
                        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(){
@@ -158,11 +152,19 @@ MGCanvas.prototype = {
                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();
                }