|
@@ -11,8 +11,6 @@ import group04.myBatis.Nodeinfo;
|
|
*/
|
|
*/
|
|
public class BasicNode implements Iterable<BasicNode>
|
|
public class BasicNode implements Iterable<BasicNode>
|
|
{
|
|
{
|
|
-
|
|
|
|
- public boolean needUpload = true;
|
|
|
|
/**
|
|
/**
|
|
* <0 : invalidity node
|
|
* <0 : invalidity node
|
|
* =0 : undifineded node
|
|
* =0 : undifineded node
|
|
@@ -26,9 +24,8 @@ public class BasicNode implements Iterable<BasicNode>
|
|
|
|
|
|
private BasicNode childLevel = null;
|
|
private BasicNode childLevel = null;
|
|
private BasicNode parentNode = null;
|
|
private BasicNode parentNode = null;
|
|
-
|
|
|
|
|
|
|
|
- private static ArrayList<ArrayList<BasicNode>> loadedNodes = new ArrayList<>();
|
|
|
|
|
|
+ private static ArrayList<BasicNode> NeedUpload = new ArrayList<>();
|
|
|
|
|
|
/**
|
|
/**
|
|
* create new node
|
|
* create new node
|
|
@@ -104,6 +101,17 @@ public class BasicNode implements Iterable<BasicNode>
|
|
return childLevel;
|
|
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
|
|
* set Node id,also it can destroy this node by seting id to minus value
|
|
* @param id new id
|
|
* @param id new id
|
|
@@ -122,7 +130,6 @@ public class BasicNode implements Iterable<BasicNode>
|
|
NodeDao.getCurrentMapper().deleteByPrimaryKey(this.id);
|
|
NodeDao.getCurrentMapper().deleteByPrimaryKey(this.id);
|
|
//if database vaildity, it will going on.
|
|
//if database vaildity, it will going on.
|
|
dropFromLevel();
|
|
dropFromLevel();
|
|
- removeLoadedNode(this);
|
|
|
|
//delete all child nodes
|
|
//delete all child nodes
|
|
BasicNode node = this.getChildLevel();
|
|
BasicNode node = this.getChildLevel();
|
|
while(node != null)
|
|
while(node != null)
|
|
@@ -160,7 +167,6 @@ public class BasicNode implements Iterable<BasicNode>
|
|
{
|
|
{
|
|
InitAfterLoad(id);
|
|
InitAfterLoad(id);
|
|
}
|
|
}
|
|
- if(!defineded()) addLoadedNode(this);
|
|
|
|
this.id = id;
|
|
this.id = id;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -218,12 +224,8 @@ public class BasicNode implements Iterable<BasicNode>
|
|
{
|
|
{
|
|
synchronized(this)
|
|
synchronized(this)
|
|
{
|
|
{
|
|
- if(needUpload)
|
|
|
|
- {
|
|
|
|
- Nodeinfo data = getNodeInfo();
|
|
|
|
- NodeDao.getCurrentMapper().updateByPrimaryKey(data);
|
|
|
|
- needUpload = false;
|
|
|
|
- }
|
|
|
|
|
|
+ Nodeinfo data = getNodeInfo();
|
|
|
|
+ NodeDao.getCurrentMapper().updateByPrimaryKey(data);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -233,7 +235,14 @@ public class BasicNode implements Iterable<BasicNode>
|
|
*/
|
|
*/
|
|
public boolean vaildity()
|
|
public boolean vaildity()
|
|
{
|
|
{
|
|
- return getId() >= 0;
|
|
|
|
|
|
+ if(getId() >= 0)
|
|
|
|
+ {
|
|
|
|
+ for (BasicNode basicNode : NeedUpload) {
|
|
|
|
+ if(this.equals(basicNode) && basicNode != this) return false;
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -242,7 +251,7 @@ public class BasicNode implements Iterable<BasicNode>
|
|
*/
|
|
*/
|
|
public boolean defineded()
|
|
public boolean defineded()
|
|
{
|
|
{
|
|
- return getId() > 0;
|
|
|
|
|
|
+ return vaildity() && getId() > 0;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -264,23 +273,24 @@ public class BasicNode implements Iterable<BasicNode>
|
|
if(this.nextNode != null)
|
|
if(this.nextNode != null)
|
|
{
|
|
{
|
|
this.nextNode.pervNode = this.pervNode;
|
|
this.nextNode.pervNode = this.pervNode;
|
|
- this.nextNode.needUpload = true;
|
|
|
|
|
|
+ this.nextNode.setNeedUpload();
|
|
|
|
+
|
|
}
|
|
}
|
|
if(this.pervNode != null)
|
|
if(this.pervNode != null)
|
|
{
|
|
{
|
|
this.pervNode.nextNode = this.nextNode;
|
|
this.pervNode.nextNode = this.nextNode;
|
|
- this.pervNode.needUpload = true;
|
|
|
|
|
|
+ this.pervNode.setNeedUpload();
|
|
}
|
|
}
|
|
else
|
|
else
|
|
if(this.parentNode != null)
|
|
if(this.parentNode != null)
|
|
{
|
|
{
|
|
this.parentNode.childLevel = this.nextNode;
|
|
this.parentNode.childLevel = this.nextNode;
|
|
- this.parentNode.needUpload = true;
|
|
|
|
|
|
+ this.parentNode.setNeedUpload();
|
|
}
|
|
}
|
|
this.parentNode = null;
|
|
this.parentNode = null;
|
|
this.nextNode = null;
|
|
this.nextNode = null;
|
|
this.pervNode = null;
|
|
this.pervNode = null;
|
|
- this.needUpload = true;
|
|
|
|
|
|
+ this.setNeedUpload();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -311,10 +321,10 @@ public class BasicNode implements Iterable<BasicNode>
|
|
if(nextNode != null)
|
|
if(nextNode != null)
|
|
{
|
|
{
|
|
nextNode.pervNode = node;
|
|
nextNode.pervNode = node;
|
|
- nextNode.needUpload = true;
|
|
|
|
|
|
+ nextNode.setNeedUpload();
|
|
}
|
|
}
|
|
|
|
|
|
- this.needUpload = true;
|
|
|
|
|
|
+ this.setNeedUpload();
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -349,16 +359,16 @@ public class BasicNode implements Iterable<BasicNode>
|
|
if(pervNode != null)
|
|
if(pervNode != null)
|
|
{
|
|
{
|
|
pervNode.nextNode = node;
|
|
pervNode.nextNode = node;
|
|
- pervNode.needUpload = true;
|
|
|
|
|
|
+ pervNode.setNeedUpload();
|
|
}
|
|
}
|
|
else //remove if parrentNode exiet
|
|
else //remove if parrentNode exiet
|
|
if(this.parentNode != null)
|
|
if(this.parentNode != null)
|
|
{
|
|
{
|
|
this.parentNode.childLevel = node;
|
|
this.parentNode.childLevel = node;
|
|
- this.parentNode.needUpload = true;
|
|
|
|
|
|
+ this.parentNode.setNeedUpload();
|
|
}
|
|
}
|
|
|
|
|
|
- this.needUpload = true;
|
|
|
|
|
|
+ this.setNeedUpload();
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -396,7 +406,7 @@ public class BasicNode implements Iterable<BasicNode>
|
|
if(node.parentNode != null)
|
|
if(node.parentNode != null)
|
|
{
|
|
{
|
|
node.parentNode.childLevel = node;
|
|
node.parentNode.childLevel = node;
|
|
- node.parentNode.needUpload = true;
|
|
|
|
|
|
+ node.parentNode.setNeedUpload();
|
|
}
|
|
}
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
@@ -439,46 +449,46 @@ public class BasicNode implements Iterable<BasicNode>
|
|
node.nextNode = this.nextNode;
|
|
node.nextNode = this.nextNode;
|
|
node.pervNode = this.pervNode;
|
|
node.pervNode = this.pervNode;
|
|
node.parentNode = this.parentNode;
|
|
node.parentNode = this.parentNode;
|
|
- node.needUpload = true;
|
|
|
|
|
|
+ node.setNeedUpload();
|
|
//check near by node update
|
|
//check near by node update
|
|
if(node.nextNode != null)
|
|
if(node.nextNode != null)
|
|
{
|
|
{
|
|
node.nextNode.pervNode = node;
|
|
node.nextNode.pervNode = node;
|
|
- node.nextNode.needUpload = true;
|
|
|
|
|
|
+ node.nextNode.setNeedUpload();
|
|
}
|
|
}
|
|
if(node.pervNode != null)
|
|
if(node.pervNode != null)
|
|
{
|
|
{
|
|
node.pervNode.nextNode = node;
|
|
node.pervNode.nextNode = node;
|
|
- node.pervNode.needUpload = true;
|
|
|
|
|
|
+ node.pervNode.setNeedUpload();
|
|
}
|
|
}
|
|
else
|
|
else
|
|
if(node.parentNode != null)
|
|
if(node.parentNode != null)
|
|
{
|
|
{
|
|
node.parentNode.childLevel = node;
|
|
node.parentNode.childLevel = node;
|
|
- node.parentNode.needUpload = true;
|
|
|
|
|
|
+ node.parentNode.setNeedUpload();
|
|
}
|
|
}
|
|
|
|
|
|
//set this to next
|
|
//set this to next
|
|
this.nextNode = next;
|
|
this.nextNode = next;
|
|
this.pervNode = perv;
|
|
this.pervNode = perv;
|
|
this.parentNode = parent;
|
|
this.parentNode = parent;
|
|
- this.needUpload = true;
|
|
|
|
|
|
+ this.setNeedUpload();
|
|
//check near by node update
|
|
//check near by node update
|
|
if(this.nextNode != null)
|
|
if(this.nextNode != null)
|
|
{
|
|
{
|
|
this.nextNode.pervNode = this;
|
|
this.nextNode.pervNode = this;
|
|
- this.nextNode.needUpload = true;
|
|
|
|
|
|
+ this.nextNode.setNeedUpload();
|
|
}
|
|
}
|
|
if(this.pervNode != null)
|
|
if(this.pervNode != null)
|
|
{
|
|
{
|
|
this.pervNode.nextNode = this;
|
|
this.pervNode.nextNode = this;
|
|
- this.pervNode.needUpload = true;
|
|
|
|
|
|
+ this.pervNode.setNeedUpload();
|
|
}
|
|
}
|
|
else
|
|
else
|
|
if(this.parentNode != null)
|
|
if(this.parentNode != null)
|
|
{
|
|
{
|
|
this.parentNode.childLevel = this;
|
|
this.parentNode.childLevel = this;
|
|
- this.parentNode.needUpload = true;
|
|
|
|
|
|
+ this.parentNode.setNeedUpload();
|
|
}
|
|
}
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
@@ -586,7 +596,7 @@ public class BasicNode implements Iterable<BasicNode>
|
|
do
|
|
do
|
|
{
|
|
{
|
|
//check 2 node's perv node at same time
|
|
//check 2 node's perv node at same time
|
|
- if(cache1 == this || cache2 == node)
|
|
|
|
|
|
+ if(cache1.equals(this) || cache2.equals(node))
|
|
return true;
|
|
return true;
|
|
if(cache1.pervNode != null)
|
|
if(cache1.pervNode != null)
|
|
cache1 = cache1.pervNode;
|
|
cache1 = cache1.pervNode;
|
|
@@ -625,7 +635,7 @@ public class BasicNode implements Iterable<BasicNode>
|
|
{
|
|
{
|
|
do
|
|
do
|
|
{
|
|
{
|
|
- if(node == this)
|
|
|
|
|
|
+ if(node.equals(this))
|
|
{
|
|
{
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
@@ -663,91 +673,61 @@ public class BasicNode implements Iterable<BasicNode>
|
|
return result + " }";
|
|
return result + " }";
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
public void deleteNode() throws IOException
|
|
public void deleteNode() throws IOException
|
|
{
|
|
{
|
|
this.setId(-1l);
|
|
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();
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|