Mercurial > hg > Members > shoshi > jungle > jungle-core
changeset 301:d23faf84eec6
implement GetFilteringKey for RedBlackTree
and that test
author | tatsuki |
---|---|
date | Thu, 05 Jan 2017 22:44:35 +0900 |
parents | b060ece7f8e4 |
children | 0767620e6f5f |
files | src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/transaction/node/redBlackTree/RedBlackTreeNodeAttribute.java src/test/java/jp/ac/u_ryukyu/ie/cr/jungle/impl/redblacktreenode/GetKeysTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/jungle/impl/redblacktreenode/GetfilteringKeyTest.java |
diffstat | 3 files changed, 200 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/transaction/node/redBlackTree/RedBlackTreeNodeAttribute.java Thu Jan 05 22:13:54 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/transaction/node/redBlackTree/RedBlackTreeNodeAttribute.java Thu Jan 05 22:44:35 2017 +0900 @@ -42,7 +42,7 @@ } TreeMap<String, ByteBuffer> newMap = attrs.delete(key); - ColorlessTreeNode newNode = node.createNode(newMap,balanceKey,value,node.left(),node.right()); + ColorlessTreeNode newNode = node.createNode(newMap, balanceKey, value, node.left(), node.right()); return DefaultEither.newB(newNode); } @@ -53,7 +53,7 @@ } TreeMap<String, ByteBuffer> newMap = attrs.put(key, insertValue); - TreeNode newNode = node.createNode(newMap,balanceKey,value,node.left(),node.right()); + TreeNode newNode = node.createNode(newMap, balanceKey, value, node.left(), node.right()); return DefaultEither.newB(newNode); } @@ -79,22 +79,11 @@ } @Override - public Iterator<String> getKeys() { - return attrs.keys(); - } - - @Override - public boolean contain(String key) { - if (key.equals(balanceKey)) - return true; - return attrs.contain(key); - } - - @Override - public Iterator<String> getFilteringKey(List<String> filter) { - return new Iterator<String>(){ + public Iterator<String> getKeys() { + return new Iterator<String>() { String next = balanceKey; Iterator<String> keys = attrs.keys(); + @Override public boolean hasNext() { return next != null; @@ -112,4 +101,42 @@ }; } + @Override + public boolean contain(String key) { + if (key.equals(balanceKey)) + return true; + return attrs.contain(key); + } + + @Override + public Iterator<String> getFilteringKey(List<String> filter) { + return new Iterator<String>() { + Iterator<String> keys = attrs.keys(); + String next = filter.contains(balanceKey) ? init() : balanceKey; + + @Override + public boolean hasNext() { + return next != null; + } + + private String init() { + while (keys.hasNext()) { + String key = keys.next(); + if (filter.contains(key)) + continue; + return key; + } + return null; + } + + @Override + public String next() { + String tmp = next; + next = init(); + return tmp; + } + }; + } + } +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/jungle/impl/redblacktreenode/GetKeysTest.java Thu Jan 05 22:44:35 2017 +0900 @@ -0,0 +1,70 @@ +package jp.ac.u_ryukyu.ie.cr.jungle.impl.redblacktreenode; + +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.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; +import java.util.LinkedList; +import java.util.List; + + +public class GetKeysTest { + @Test + public void GetKeys() { + String balanceKey = "balanceKey"; + String[] keys = new String[]{ + "kanagawa","atton","parusu","Dpop" + }; + List<String> keyList = new LinkedList<String>(); + for (String key : keys) {//keyのListの作成 + keyList.add(key); + } + Jungle jungle = new DefaultJungle(null, "hogehoge", new DefaultTraverser()); + JungleTree tree = jungle.createNewRedBlackTree("TreeName", "balanceKey"); + NodePath path = new DefaultNodePath(); + + JungleTreeEditor editor = tree.getJungleTreeEditor(); + ByteBuffer value = ByteBuffer.wrap(("value").getBytes()); + Either<Error, JungleTreeEditor> either = editor.addNewChildAndPutAttribute(path, 0, balanceKey, value); //ノードの追加 + Assert.assertFalse(either.isA()); + editor = either.b(); + either = editor.success(); + Assert.assertFalse(either.isA()); + + editor = tree.getJungleTreeEditor(); + for (String key : keys) { + value = ByteBuffer.wrap(path.toString().getBytes()); + either = editor.putAttribute(path, key, value); + Assert.assertFalse(either.isA()); //AttributeのPutが成功したかどうか調べる + editor = either.b(); + } + either = editor.success(); + Assert.assertFalse(either.isA()); + + keyList.add(balanceKey);//balanceKeyはputAttributeできないのでここで追加する + TreeNode root = tree.getRootNode(); + Attributes attribute = root.getAttributes(); + Iterator<String> nodeKeys = attribute.getKeys(); + + while (nodeKeys.hasNext()) { //全てのKeyをちゃんと返すか調べる + String nodeKey = nodeKeys.next(); + System.out.println(nodeKey); + int index = keyList.indexOf(nodeKey); + keyList.remove(index); + } + + Assert.assertEquals(keyList.size(),0); //全部のKeyをAttributeが返すなら Listのサイズは0になるはず + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/jungle/impl/redblacktreenode/GetfilteringKeyTest.java Thu Jan 05 22:44:35 2017 +0900 @@ -0,0 +1,87 @@ +package jp.ac.u_ryukyu.ie.cr.jungle.impl.redblacktreenode; + +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.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; +import java.util.LinkedList; +import java.util.List; + +/** + * Created by e115731 on 2017/01/05. + */ +public class GetfilteringKeyTest { + @Test + public void GetFilteringKey() { + String balanceKey = "balanceKey"; + String[] keys = new String[]{ + "kanagawa","atton","parusu","Dpop" + }; + + String[] filterKeys = new String[]{ + "atton", balanceKey + }; + + List<String> keyList = createKeyList(keys); + List<String> filterKeysList = createKeyList(filterKeys); + + + Jungle jungle = new DefaultJungle(null, "hogehoge", new DefaultTraverser()); + JungleTree tree = jungle.createNewRedBlackTree("TreeName", "balanceKey"); + NodePath path = new DefaultNodePath(); + + JungleTreeEditor editor = tree.getJungleTreeEditor(); + ByteBuffer value = ByteBuffer.wrap(("value").getBytes()); + Either<Error, JungleTreeEditor> either = editor.addNewChildAndPutAttribute(path, 0, balanceKey, value); //ノードの追加 + Assert.assertFalse(either.isA()); + editor = either.b(); + either = editor.success(); + Assert.assertFalse(either.isA()); + + editor = tree.getJungleTreeEditor(); + for (String key : keys) { + value = ByteBuffer.wrap(path.toString().getBytes()); + either = editor.putAttribute(path, key, value); + Assert.assertFalse(either.isA()); //AttributeのPutが成功したかどうか調べる + editor = either.b(); + } + either = editor.success(); + Assert.assertFalse(either.isA()); + + keyList.add(balanceKey);//balanceKeyはputAttributeできないのでここで追加する + TreeNode root = tree.getRootNode(); + Attributes attribute = root.getAttributes(); + Iterator<String> nodeKeys = attribute.getFilteringKey(filterKeysList); + + int expectSize = filterKeysList.size(); //最終的にKeyListの中に残ってほしいKeyの個数 FilteringKeyListの個数 + + while (nodeKeys.hasNext()) { //FIlteringされてないKeyだけを返すか調べる + String nodeKey = nodeKeys.next(); + System.out.println(nodeKey); + int index = keyList.indexOf(nodeKey); + keyList.remove(index); + } + + Assert.assertEquals(keyList.size(),expectSize); //全部のKeyをAttributeが返すなら Listのサイズは0になるはず + } + + private List<String> createKeyList(String[] keys) { + List<String> keyList = new LinkedList<String>(); + for (String key : keys) {//keyのListの作成 + keyList.add(key); + } + return keyList; + } +}