Mercurial > hg > Members > shoshi > jungle > jungle-core
changeset 291:070e9cabcd27
add difference Tree Index Test
author | tatsuki |
---|---|
date | Sat, 31 Dec 2016 03:30:14 +0900 |
parents | 9975e10187e8 |
children | 20fac8350822 |
files | src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/index/Index.java src/test/java/jp/ac/u_ryukyu/ie/cr/jungle/index/difference/IndexTest.java |
diffstat | 2 files changed, 125 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/index/Index.java Sat Dec 31 02:21:05 2016 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/index/Index.java Sat Dec 31 03:30:14 2016 +0900 @@ -117,4 +117,26 @@ } return false; } + + public boolean isEmpty() { + Iterator<String> indexListKeys = indexList.keys(); + while (indexListKeys.hasNext()) { + String indexListKey = indexListKeys.next(); + Optional<TreeMap<String, List<TreeNode>>> indexOp = indexList.get(indexListKey); + if (!indexOp.isPresent()) + continue; + TreeMap<String,List<TreeNode>> index = indexOp.get(); + Iterator<String> indexKeys = index.keys(); + while(indexKeys.hasNext()) { + String indexKey = indexKeys.next(); + Optional<List<TreeNode>> nodeListOp = index.get(indexKey); + if (!nodeListOp.isPresent()) + continue; + List<TreeNode> nodeList = nodeListOp.get(); + if (nodeList.length() > 0) + return false; + } + } + return true; + } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/jungle/index/difference/IndexTest.java Sat Dec 31 03:30:14 2016 +0900 @@ -0,0 +1,103 @@ +package jp.ac.u_ryukyu.ie.cr.jungle.index.difference; + +import jp.ac.u_ryukyu.ie.cr.jungle.DefaultJungle; +import jp.ac.u_ryukyu.ie.cr.jungle.Jungle; +import jp.ac.u_ryukyu.ie.cr.jungle.core.Attributes; +import jp.ac.u_ryukyu.ie.cr.jungle.core.Children; +import jp.ac.u_ryukyu.ie.cr.jungle.store.index.Index; +import jp.ac.u_ryukyu.ie.cr.jungle.store.index.ParentIndex; +import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.DefaultNodePath; +import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.NodePath; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.jungleTreeEditor.JungleTreeEditor; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode; +import jp.ac.u_ryukyu.ie.cr.jungle.traverser.DefaultTraverser; +import jp.ac.u_ryukyu.ie.cr.jungle.tree.JungleTree; +import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; +import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.Error; +import org.junit.Assert; +import org.junit.Test; + +import java.nio.ByteBuffer; +import java.util.Iterator; + +/** + * Created by e115731 on 2016/12/31. + */ +public class IndexTest { + private Index index; + + @Test + public void Index() { + Jungle jungle = new DefaultJungle(null, "hogehoge", new DefaultTraverser()); + jungle.createNewTree("tree"); + JungleTree tree = jungle.createNewDifferenceTree("Tree"); + JungleTreeEditor editor = tree.getJungleTreeEditor(); + NodePath path = new DefaultNodePath(); + for (int j = 0; j < 10; j++) { //木の構築 + Either<Error, JungleTreeEditor> either = editor.addNewChildAt(path, 0); + Assert.assertFalse(either.isA()); + editor = either.b(); + either = editor.putAttribute(path, "key", ByteBuffer.wrap(path.toString().getBytes())); + Assert.assertFalse(either.isA()); + editor = either.b(); + path = path.add(0); + } + Either<Error, JungleTreeEditor> either = editor.success(); + Assert.assertFalse(either.isA()); + editor = either.b(); + + index = tree.getIndex(); + TreeNode root = tree.getRootNode(); + checkIndex(root);//indexが張られているかを調べる + Assert.assertTrue(index.isEmpty()); + + + //一回木を更新する + editor = tree.getJungleTreeEditor(); + path = new DefaultNodePath(); + either = editor.addNewChildAt(path, 0); + Assert.assertFalse(either.isA()); + editor = either.b(); + either = editor.putAttribute(path, "key", ByteBuffer.wrap(path.toString().getBytes())); + Assert.assertFalse(either.isA()); + editor = either.b(); + either = editor.success(); + Assert.assertFalse(either.isA()); + + //更新後にちゃんとIndexが貼れているかを調べる + index = tree.getIndex(); + root = tree.getRootNode(); + checkIndex(root); + Assert.assertTrue(index.isEmpty()); + + Either<Error, JungleTree> oldTreeEither = tree.getOldTree(1); + Assert.assertFalse(oldTreeEither.isA()); + JungleTree oldTree = oldTreeEither.b(); //過去のTreeのIndexが上書きされてないかを調べる + ParentIndex oldTreeParentIndex = oldTree.getParentIndex(); + index = tree.getIndex(); + Assert.assertNotEquals(oldTreeParentIndex, index); + } + + public void checkIndex(TreeNode currentNode) { + Children children = currentNode.getChildren(); + for (TreeNode child : children) { + checkIndex(child); + } + + Attributes attribute = currentNode.getAttributes(); + Iterator<String> keys = attribute.getKeys(); + while (keys.hasNext()) { + String key = keys.next(); + String value = attribute.getString(key); + Iterator<TreeNode> nodeIterator = index.get(key, value); + boolean existNode = false; + while(nodeIterator.hasNext()) { + TreeNode node = nodeIterator.next(); + if (node.equals(currentNode)) + existNode = true; + } + Assert.assertTrue(existNode); + } + index = index.delete(currentNode); + } +}