Future 3 年之前
父节点
当前提交
a5f5dd669e

+ 63 - 90
src/main/java/group04/BasicNode.java

@@ -11,8 +11,6 @@ import group04.myBatis.Nodeinfo;
  */
 public class BasicNode implements Iterable<BasicNode>
 {
-
-    public boolean needUpload = true;
     /**
      * <0 : invalidity node
      * =0 : undifineded node
@@ -26,9 +24,8 @@ public class BasicNode implements Iterable<BasicNode>
     
     private BasicNode childLevel = null;
     private BasicNode parentNode = null;
-    
 
-    private static ArrayList<ArrayList<BasicNode>> loadedNodes = new ArrayList<>();
+    private static ArrayList<BasicNode> NeedUpload = new ArrayList<>();
 
     /**
      * create new node
@@ -104,6 +101,17 @@ public class BasicNode implements Iterable<BasicNode>
         return childLevel;
     }
 
+    public void setNeedUpload()
+    {
+        synchronized(NeedUpload)
+        {
+            if(!NeedUpload.contains(this))
+            {
+                NeedUpload.add(this);
+            }
+        }
+    }
+
     /**
      * set Node id,also it can destroy this node by seting id to minus value
      * @param id new id
@@ -122,7 +130,6 @@ public class BasicNode implements Iterable<BasicNode>
                 NodeDao.getCurrentMapper().deleteByPrimaryKey(this.id);
                 //if database vaildity, it will going on.
                 dropFromLevel();
-                removeLoadedNode(this);
                 //delete all child nodes
                 BasicNode node = this.getChildLevel();
                 while(node != null)
@@ -160,7 +167,6 @@ public class BasicNode implements Iterable<BasicNode>
             {
                 InitAfterLoad(id);
             }
-            if(!defineded()) addLoadedNode(this);
             this.id = id;
         }
     }
@@ -218,12 +224,8 @@ public class BasicNode implements Iterable<BasicNode>
     {
         synchronized(this)
         {
-            if(needUpload)
-            {
-                Nodeinfo data = getNodeInfo();
-                NodeDao.getCurrentMapper().updateByPrimaryKey(data);
-                needUpload = false;
-            }
+            Nodeinfo data = getNodeInfo();
+            NodeDao.getCurrentMapper().updateByPrimaryKey(data);
         }
     }
 
@@ -264,23 +266,24 @@ public class BasicNode implements Iterable<BasicNode>
             if(this.nextNode != null)
             {
                 this.nextNode.pervNode = this.pervNode;
-                this.nextNode.needUpload = true;
+                this.nextNode.setNeedUpload();
+                
             }
             if(this.pervNode != null)
             {
                 this.pervNode.nextNode = this.nextNode;
-                this.pervNode.needUpload = true;
+                this.pervNode.setNeedUpload();
             }
             else
             if(this.parentNode != null)
             {
                 this.parentNode.childLevel = this.nextNode;
-                this.parentNode.needUpload = true;
+                this.parentNode.setNeedUpload();
             }
             this.parentNode = null;
             this.nextNode = null;
             this.pervNode = null;
-            this.needUpload = true;
+            this.setNeedUpload();
         }
     }
     
@@ -311,10 +314,10 @@ public class BasicNode implements Iterable<BasicNode>
                     if(nextNode != null)
                     {
                         nextNode.pervNode = node;
-                        nextNode.needUpload = true;
+                        nextNode.setNeedUpload();
                     }
     
-                    this.needUpload = true;
+                    this.setNeedUpload();
                     return true;
                 }
             }
@@ -349,16 +352,16 @@ public class BasicNode implements Iterable<BasicNode>
                     if(pervNode != null)
                     {
                         pervNode.nextNode = node;
-                        pervNode.needUpload = true;
+                        pervNode.setNeedUpload();
                     }
                     else //remove if parrentNode exiet
                     if(this.parentNode != null)
                     {
                         this.parentNode.childLevel = node;
-                        this.parentNode.needUpload = true;
+                        this.parentNode.setNeedUpload();
                     }
     
-                    this.needUpload = true;
+                    this.setNeedUpload();
                     return true;
                 }
             }
@@ -396,7 +399,7 @@ public class BasicNode implements Iterable<BasicNode>
                     if(node.parentNode != null)
                     {
                         node.parentNode.childLevel = node;
-                        node.parentNode.needUpload = true;
+                        node.parentNode.setNeedUpload();
                     }
                     return true;
                 }
@@ -439,46 +442,46 @@ public class BasicNode implements Iterable<BasicNode>
                     node.nextNode = this.nextNode;
                     node.pervNode = this.pervNode;
                     node.parentNode = this.parentNode;
-                    node.needUpload = true;
+                    node.setNeedUpload();
                     //check near by node update
                     if(node.nextNode != null)
                     {
                         node.nextNode.pervNode = node;
-                        node.nextNode.needUpload = true;
+                        node.nextNode.setNeedUpload();
                     }
                     if(node.pervNode != null)
                     {
                         node.pervNode.nextNode = node;
-                        node.pervNode.needUpload = true;
+                        node.pervNode.setNeedUpload();
                     }
                     else
                     if(node.parentNode != null)
                     {
                         node.parentNode.childLevel = node;
-                        node.parentNode.needUpload = true;
+                        node.parentNode.setNeedUpload();
                     }
     
                     //set this to next
                     this.nextNode = next;
                     this.pervNode = perv;
                     this.parentNode = parent;
-                    this.needUpload = true;
+                    this.setNeedUpload();
                     //check near by node update
                     if(this.nextNode != null)
                     {
                         this.nextNode.pervNode = this;
-                        this.nextNode.needUpload = true;
+                        this.nextNode.setNeedUpload();
                     }
                     if(this.pervNode != null)
                     {
                         this.pervNode.nextNode = this;
-                        this.pervNode.needUpload = true;
+                        this.pervNode.setNeedUpload();
                     }
                     else
                     if(this.parentNode != null)
                     {
                         this.parentNode.childLevel = this;
-                        this.parentNode.needUpload = true;
+                        this.parentNode.setNeedUpload();
                     }
                     return true;
                 }
@@ -586,7 +589,7 @@ public class BasicNode implements Iterable<BasicNode>
                 do
                 {
                     //check 2 node's perv node at same time
-                    if(cache1 == this || cache2 == node)
+                    if(cache1.equals(this) || cache2.equals(node))
                         return true;
                     if(cache1.pervNode != null)
                         cache1 = cache1.pervNode;
@@ -625,7 +628,7 @@ public class BasicNode implements Iterable<BasicNode>
         {
             do
             {
-                if(node == this)
+                if(node.equals(this))
                 {
                     return true;
                 }
@@ -663,91 +666,61 @@ public class BasicNode implements Iterable<BasicNode>
         return result + " }";
     }
 
-
     public void deleteNode() throws IOException
     {
         this.setId(-1l);
     }
 
-
-    private static void addLoadedNode(BasicNode node)
+    /**
+     * check level
+     * @param levelA value a
+     * @param levelB value b
+     * @return if is same level,it will return true
+     */
+    public static boolean levelEqul(BasicNode levelA,BasicNode levelB)
     {
-        for (ArrayList<BasicNode> list : loadedNodes)
-        {
-            if(list.size() < Integer.MAX_VALUE)
-            {
-                list.add(node);
-                return;
-            }
-        }
-        ArrayList<BasicNode> nodes = new ArrayList<BasicNode>();
-        nodes.add(node);
-        loadedNodes.add(nodes);
+        return levelA.startOfNodeLevel().id == levelB.startOfNodeLevel().id;
     }
 
-
-    private static void removeLoadedNode(BasicNode node)
-    {
-        for (int i = 0; i < loadedNodes.size(); ++i)
-        {
-            ArrayList<BasicNode> list = loadedNodes.get(i);
-            list.remove(node);
-            if(list.size() <= 0)
-            {
-                loadedNodes.remove(i);
-                --i;
-            }
-        }
+    @Override
+    public boolean equals(Object o) {
+        // TODO Auto-generated method stub
+        BasicNode other = (BasicNode)o;
+        if(other != null) return other.id == this.id;
+        else return super.equals(o);
     }
 
     /**
-     * find node from node pool, if target is not exiets, it will return null
-     * @param id target id
-     * @return target
+     * check all loaded node and upload data
+     * @throws IOException no sql mapper
      */
-    public static BasicNode getLoadedNodeById(long id)
+    public static void Commit() throws IOException
     {
-        for (ArrayList<BasicNode> list : loadedNodes)
+        synchronized(NeedUpload)
         {
-            for (BasicNode basicNode : list)
+            for (BasicNode basicNode : NeedUpload)
             {
-                if(basicNode.getId() == id)
-                {
-                    return basicNode;
-                }
+                basicNode.uploadNode();
             }
+            NodeDao.getCurrentMapper().commit();
+            NeedUpload.clear();
         }
-        return null;
     }
 
-    /**
-     * check level
-     * @param levelA value a
-     * @param levelB value b
-     * @return if is same level,it will return true
-     */
-    public static boolean levelEqul(BasicNode levelA,BasicNode levelB)
-    {
-        return levelA.startOfNodeLevel() == levelB.startOfNodeLevel();
-    }
 
-    /**
-     * check all loaded node and upload data
-     * @throws IOException no sql mapper
-     */
-    public static void checkAndUploadAll() throws IOException
+    public static BasicNode TryGetNotCommitNodeById(long id)
     {
-        for (ArrayList<BasicNode> list : loadedNodes)
+        synchronized(NeedUpload)
         {
-            for (BasicNode basicNode : list)
+            for (BasicNode basicNode : NeedUpload)
             {
-                if(basicNode.needUpload)
+                if(basicNode.getId() == id)
                 {
-                    basicNode.uploadNode();
+                    return basicNode;
                 }
             }
+            return null;
         }
-        NodeDao.getCurrentMapper().commit();
     }
 
     /**

+ 36 - 31
src/main/java/group04/NodeDao.java

@@ -45,51 +45,55 @@ public class NodeDao
         BasicNode result = null;
         if(id > 0)
         {
-            result = BasicNode.getLoadedNodeById(id);
+            daoState = DaoState.Reading;
+            result = BasicNode.TryGetNotCommitNodeById(id);
             if(result == null)
+            try
             {
-                daoState = DaoState.Reading;
-                try
+                ArrayList<BasicNode> tree = new ArrayList<>();
+                Nodeinfo data = getCurrentMapper().selectByPrimaryKey(id);
+                if(data != null)
                 {
-                    Nodeinfo data = getCurrentMapper().selectByPrimaryKey(id);
-                    if(data != null)
+                    while(data.getParentId() != null)
                     {
-                        while(data.getParentId() != null)
+                        Nodeinfo cache = data;
+                        data = getCurrentMapper().selectByPrimaryKey(data.getParentId());
+                        if(data == null)
                         {
-                            Nodeinfo cache = data;
-                            data = getCurrentMapper().selectByPrimaryKey(data.getParentId());
-                            if(data == null)
-                            {
-                                data = cache;
-                                break;
-                            }
+                            data = cache;
+                            break;
                         }
-                        while(data.getPrevId() != null)
+                    }
+                    while(data.getPrevId() != null)
+                    {
+                        Nodeinfo cache = data;
+                        data = getCurrentMapper().selectByPrimaryKey(data.getPrevId());
+                        if(data == null)
                         {
-                            Nodeinfo cache = data;
-                            data = getCurrentMapper().selectByPrimaryKey(data.getPrevId());
-                            if(data == null)
-                            {
-                                data = cache;
-                                break;
-                            }
+                            data = cache;
+                            break;
+                        }
+                    }
+                    loadAllInLevel(data,tree);
+                    for (BasicNode basicNode : tree)
+                    {
+                        if(basicNode != null && basicNode.getId() == id)
+                        {
+                            return basicNode;
                         }
-                        loadAllInLevel(data);
                     }
                 }
-                catch(Exception ex)
-                {
-                    System.out.println(ex.toString());
-                    return null;
-                }
-                result = BasicNode.getLoadedNodeById(id);
-                daoState = DaoState.None;
+            }
+            catch(Exception ex)
+            {
+                System.out.println(ex.toString());
+                return null;
             }
         }
         return result;
     }
 
-    private static BasicNode loadAllInLevel(Nodeinfo nodeinfo) throws IOException
+    private static BasicNode loadAllInLevel(Nodeinfo nodeinfo, ArrayList<BasicNode> tree) throws IOException
     {
         if(nodeinfo != null)
         {
@@ -113,8 +117,9 @@ public class NodeDao
                     {
                         nodes.get(i).insertNext(nodes.get(i + 1));
                     }
-                    nodes.get(i).setChildLevel(loadAllInLevel(infos.get(i)));
+                    nodes.get(i).setChildLevel(loadAllInLevel(infos.get(i),tree));
                 }
+                tree.addAll(nodes);
                 return nodes.get(0);
             } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
                     | NoSuchMethodException | SecurityException | ClassNotFoundException e) {

+ 1 - 1
src/main/java/group04/nodeOptConsol.java

@@ -218,7 +218,7 @@ public class nodeOptConsol {
                 }
                 break;
                 case "ex":
-                BasicNode.checkAndUploadAll();
+                BasicNode.Commit();
                 return;
             }
         }

+ 1 - 0
src/main/resources/DataBase.sql

@@ -37,6 +37,7 @@ create table if not exists user_collect
     +   goods
         +   selectionGroup
             +   selection
+        +   comment
 */
 create table if not exists store
 (

+ 1 - 0
target/classes/DataBase.sql

@@ -37,6 +37,7 @@ create table if not exists user_collect
     +   goods
         +   selectionGroup
             +   selection
+        +   comment
 */
 create table if not exists store
 (

二进制
target/classes/group04/BasicNode$NodeIterator.class


二进制
target/classes/group04/BasicNode.class


二进制
target/classes/group04/NodeDao$DaoState.class


二进制
target/classes/group04/NodeDao.class


二进制
target/classes/group04/nodeOptConsol.class