changeset 197:6145866b2ebc

bag fix
author tatsuki
date Wed, 06 May 2015 15:04:58 +0900
parents 7480d8aa466f
children a7ec6978e725
files src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/TreeNode.java src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/DefaultTreeNode.java src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/DefaultTreeNodeAttribute.java src/test/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/core/GetNodeOfPathTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/index/parentIndexTest.java
diffstat 5 files changed, 143 insertions(+), 85 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/TreeNode.java	Wed May 06 06:30:19 2015 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/TreeNode.java	Wed May 06 15:04:58 2015 +0900
@@ -4,12 +4,14 @@
 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
 
 
-public interface TreeNode{
-	public TreeNodeChildren getChildren();
-	
-	public TreeNodeAttributes getAttributes();
-	
-	public TreeNode createNewNode();
-	
-	public Either<Error,TreeNode> appendRootNode();
+public interface TreeNode extends Comparable<TreeNode> {
+    public TreeNodeChildren getChildren();
+
+    public TreeNodeAttributes getAttributes();
+
+    public TreeNode createNewNode();
+
+    public Either<Error, TreeNode> appendRootNode();
+
+    public String getNodeId();
 }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/DefaultTreeNode.java	Wed May 06 06:30:19 2015 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/DefaultTreeNode.java	Wed May 06 15:04:58 2015 +0900
@@ -8,54 +8,58 @@
 import jp.ac.u_ryukyu.ie.cr.tatsuki.TreeMap.TreeMap;
 
 import java.nio.ByteBuffer;
+import java.rmi.dgc.VMID;
 
-public class DefaultTreeNode implements TreeNode
-{
-	//private final DefaultNode wrap;
-	private List<TreeNode> children;
-	private TreeMap<String,ByteBuffer> attrs;
-	
-	private static final List<TreeNode> NIL_LIST = List.nil();
+public class DefaultTreeNode implements TreeNode {
+    //private final DefaultNode wrap;
+    private List<TreeNode> children;
+    private TreeMap<String, ByteBuffer> attrs;
+    final String nodeId = new VMID().toString();
+
+    private static final List<TreeNode> NIL_LIST = List.nil();
 
-	public DefaultTreeNode()
-	{
-		this(NIL_LIST,new TreeMap());
-	}
-	
-	public DefaultTreeNode(List<TreeNode> _children,TreeMap<String,ByteBuffer> _attrs)
-	{
-		attrs = _attrs;
-		children = _children;
-	}
+    public DefaultTreeNode() {
+        this(NIL_LIST, new TreeMap());
+    }
 
-	@Override
-	public DefaultTreeNodeChildren getChildren()
-	{
-		return new DefaultTreeNodeChildren(children, attrs);
-	}
+    public DefaultTreeNode(List<TreeNode> _children, TreeMap<String, ByteBuffer> _attrs) {
+        attrs = _attrs;
+        children = _children;
+    }
+
+    @Override
+    public DefaultTreeNodeChildren getChildren() {
+        return new DefaultTreeNodeChildren(children, attrs);
+    }
 
-	@Override
-	public DefaultTreeNodeAttribute getAttributes()
-	{
-		return new DefaultTreeNodeAttribute(children, attrs);
-	}
-	
-	@Override
-	public DefaultTreeNode createNewNode(){
-		return new DefaultTreeNode();
-	}
-	
-	public DefaultTreeNode clone()
-	{
-		return new DefaultTreeNode(children,attrs);
-	}
+    @Override
+    public DefaultTreeNodeAttribute getAttributes() {
+        return new DefaultTreeNodeAttribute(children, attrs);
+    }
+
+    @Override
+    public DefaultTreeNode createNewNode() {
+        return new DefaultTreeNode();
+    }
+
+    public DefaultTreeNode clone() {
+        return new DefaultTreeNode(children, attrs);
+    }
 
-  @Override
-  public Either<Error, TreeNode> appendRootNode() {
-    TreeNodeChildren newRootChildren = new DefaultTreeNodeChildren(NIL_LIST, new TreeMap());
-    Either<Error, TreeNode> either = newRootChildren.addExistTreeNodeToChildren(this, 0);
-    return either;
-  }
+    @Override
+    public Either<Error, TreeNode> appendRootNode() {
+        TreeNodeChildren newRootChildren = new DefaultTreeNodeChildren(NIL_LIST, new TreeMap());
+        Either<Error, TreeNode> either = newRootChildren.addExistTreeNodeToChildren(this, 0);
+        return either;
+    }
 
-	
+    @Override
+    public String getNodeId() {
+        return nodeId;
+    }
+
+    @Override
+    public int compareTo(TreeNode o) {
+        return nodeId.compareTo(o.getNodeId());
+    }
 }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/DefaultTreeNodeAttribute.java	Wed May 06 06:30:19 2015 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/DefaultTreeNodeAttribute.java	Wed May 06 15:04:58 2015 +0900
@@ -11,6 +11,7 @@
 
 import java.nio.ByteBuffer;
 import java.util.Iterator;
+import java.util.Optional;
 
 public class DefaultTreeNodeAttribute implements TreeNodeAttributes {
     public List<TreeNode> children;
@@ -33,9 +34,9 @@
             return DefaultEither.newA(NodeEditorError.NULL_VALUE_NOT_ALLOWED);
         }
 
-        if (null != attrs.get(_key)) {
-        		return DefaultEither.newA(NodeEditorError.DELETE_KEY_NOT_FOUND);
-        	}
+        if (null == attrs.get(_key)) {
+            return DefaultEither.newA(NodeEditorError.DELETE_KEY_NOT_FOUND);
+        }
 
         TreeMap<String, ByteBuffer> newMap = attrs.delete(_key);
         TreeNode newNode = new DefaultTreeNode(children, newMap);
@@ -58,7 +59,10 @@
         if (_key == null) {
             return null;
         }
-        return attrs.get(_key).get();
+        Optional<ByteBuffer> op = attrs.get(_key);
+        if (op.isPresent())
+            return op.get();
+        return null;
     }
 
     @Override
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/core/GetNodeOfPathTest.java	Wed May 06 06:30:19 2015 +0900
+++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/core/GetNodeOfPathTest.java	Wed May 06 15:04:58 2015 +0900
@@ -1,8 +1,5 @@
 package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core;
 
-import java.nio.ByteBuffer;
-import java.util.Iterator;
-
 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.DefaultJungle;
 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle;
 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
@@ -15,33 +12,37 @@
 import junit.framework.Assert;
 import org.junit.Test;
 
+import java.nio.ByteBuffer;
+import java.util.Iterator;
+
 public class GetNodeOfPathTest {
 
-  @Test
-  public void getNodeOfPathTest() {
-    Jungle jungle = new DefaultJungle(null, "hogehoge", new DefaultTreeEditor(new DefaultTraverser()));
-    jungle.createNewTree("tree");
-    JungleTree tree = jungle.getTreeByName("tree");
-    JungleTreeEditor editor = tree.getTreeEditor();
-    DefaultNodePath path = new DefaultNodePath();
-    editor = editor.addNewChildAt(path, 0).b();
-    path = path.add(0);
-    editor = editor.addNewChildAt(path, 0).b();
-    path = path.add(0);
-    editor = editor.putAttribute(path, "KEY", ByteBuffer.wrap("tatsuki".getBytes())).b();
-    editor.success();
+    @Test
+    public void getNodeOfPathTest() {
+        Jungle jungle = new DefaultJungle(null, "hogehoge", new DefaultTreeEditor(new DefaultTraverser()));
+        jungle.createNewTree("tree");
+        JungleTree tree = jungle.getTreeByName("tree");
+        JungleTreeEditor editor = tree.getTreeEditor();
+        DefaultNodePath path = new DefaultNodePath();
+        editor = editor.addNewChildAt(path, 0).b();
+        path = path.add(0);
+        editor = editor.putAttribute(path, "KEY", ByteBuffer.wrap("tatsuki".getBytes())).b();
+        editor = editor.addNewChildAt(path, 0).b();
+        path = path.add(0);
+        editor = editor.putAttribute(path, "KEY", ByteBuffer.wrap("tatsuki".getBytes())).b();
+        editor.success();
+        InterfaceTraverser traverser = tree.getTraverser(true);
+        Iterator<TreeNode> nodeIterator = traverser.find((TreeNode node) -> {
+            String str = node.getAttributes().getString("KEY");
+            if (str == null)
+                return false;
+            if (str.equals("tatsuki"))
+                return true;
+            return false;
+        }, "KEY", "tatsuki");
 
-    InterfaceTraverser traverser = tree.getTraverser(true);
-    Iterator<TreeNode> nodeIterator = traverser.find((TreeNode node) -> {
-      String str = node.getAttributes().getString("KEY");
-      if (str == null)
-        return false;
-      if (str.equals("tatsuki"))
-        return true;
-      return false;
-    }, "KEY", "tatsuki");
-    TreeNode node = tree.getNodeOfPath(path).b();
-    Assert.assertTrue(nodeIterator.hasNext());
-    Assert.assertEquals(node, nodeIterator.next());
-  }
+        TreeNode node = tree.getNodeOfPath(path).b();
+        Assert.assertTrue(nodeIterator.hasNext());
+        Assert.assertEquals(node, nodeIterator.next());
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/index/parentIndexTest.java	Wed May 06 15:04:58 2015 +0900
@@ -0,0 +1,47 @@
+package jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.index;
+
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.DefaultJungle;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
+import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index.ParentIndex;
+import junit.framework.Assert;
+import org.junit.Test;
+
+import java.nio.ByteBuffer;
+
+/**
+ * Created by e115731 on 15/05/06.
+ */
+public class parentIndexTest {
+    @Test
+    public void parentIndex() {
+        Jungle jungle = new DefaultJungle(null, "hogehoge", new DefaultTreeEditor(new DefaultTraverser()));
+        jungle.createNewTree("tree");
+        JungleTree tree = jungle.getTreeByName("tree");
+        JungleTreeEditor editor = tree.getTreeEditor();
+
+        DefaultNodePath path = new DefaultNodePath();
+        for (int count = 0; count < 100; count++) {
+            editor = editor.addNewChildAt(path, 0).b();
+            path = path.add(0);
+            editor = editor.putAttribute(path, "KEY", ByteBuffer.wrap(("tatsuki" + count).getBytes())).b();
+        }
+
+        Either<Error, JungleTreeEditor> either = editor.success();
+        Assert.assertTrue(either.isB());
+        TreeNode node = tree.getNodeOfPath(path).b();
+        ParentIndex parentIndex = tree.getParentIndex();
+        for (int count = 99; count >= 0; count--) {
+            String attribute = node.getAttributes().getString("KEY");
+            Assert.assertEquals(attribute, "tatsuki" + count);
+            node = parentIndex.get(node);
+        }
+    }
+}