Mercurial > hg > Members > tatsuki > bench > jungle-core
changeset 130:bb53330364f1
add putIndexEditor and deleteIndexEditor
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/IndexJungleTreeEditor.java Tue Oct 14 15:11:17 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/IndexJungleTreeEditor.java Tue Oct 14 17:26:29 2014 +0900 @@ -89,8 +89,8 @@ DefaultTreeOperationLog treeOperationLog = new DefaultTreeOperationLog(iterable,newLog.length()); TreeOperationLog newTreeOpLog = log.append(treeOperationLog); - IndexJungleTreeEditor newEditor = new IndexJungleTreeEditor(newNode,txManager,editor,newTreeOpLog, index); - return DefaultEither.newB(newEditor); + Either<Error, IndexJungleTreeEditor> newEither = indexEditor.edit(newNode,txManager,editor,newTreeOpLog, index); + return newEither; } @Override @@ -129,7 +129,7 @@ public Either<Error,JungleTreeEditor> deleteAttribute(NodePath _path, String _key) { DeleteAttribute deleteAttribute = new DeleteAttribute(_key); - DeleteIndexEditor indexEditor = new DeleteIndexEditor(); + DeleteIndexEditor indexEditor = new DeleteIndexEditor(_key,_path,root); Either<Error,IndexJungleTreeEditor> either = _edit(_path,deleteAttribute,indexEditor); Either<Error,JungleTreeEditor> newEither = DefaultEither.newB(either.b()); return newEither;
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/DefaultIndexEditor.java Tue Oct 14 15:11:17 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/DefaultIndexEditor.java Tue Oct 14 17:26:29 2014 +0900 @@ -9,6 +9,7 @@ import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.IndexJungleTreeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.TransactionManager; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither; 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.shoshi.jungle.util.Pair; @@ -16,14 +17,10 @@ public class DefaultIndexEditor implements IndexEditor { @Override - public Either<Error, JungleTreeEditor> edit( - TreeNode root, - TransactionManager txManager, - TreeEditor editor, - TreeOperationLog log, - TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index) { - // TODO Auto-generated method stub - return null; + public Either<Error, IndexJungleTreeEditor> edit(TreeNode root,TransactionManager txManager,TreeEditor editor,TreeOperationLog log,TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index) { + IndexJungleTreeEditor newEditor = new IndexJungleTreeEditor(root,txManager,editor,log, index); + Either<Error, IndexJungleTreeEditor> either = DefaultEither.newB(newEditor); + return either; }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/DeleteChildIndexEditor.java Tue Oct 14 15:11:17 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/DeleteChildIndexEditor.java Tue Oct 14 17:26:29 2014 +0900 @@ -16,7 +16,7 @@ public class DeleteChildIndexEditor implements IndexEditor { @Override - public Either<Error, JungleTreeEditor> edit( + public Either<Error, IndexJungleTreeEditor> edit( TreeNode root, TransactionManager txManager, TreeEditor editor,
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/DeleteIndexEditor.java Tue Oct 14 15:11:17 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/DeleteIndexEditor.java Tue Oct 14 17:26:29 2014 +0900 @@ -1,6 +1,9 @@ package jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index; +import java.nio.ByteBuffer; + import fj.data.List; +import fj.data.Option; import fj.data.TreeMap; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; @@ -9,23 +12,91 @@ import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.IndexJungleTreeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.TransactionManager; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither; 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.shoshi.jungle.util.Pair; public class DeleteIndexEditor implements IndexEditor { - - + String key; + String pathString; + NodePath path; + TreeNode node; + + public DeleteIndexEditor(String key, NodePath path, TreeNode node) { + this.key = key; + this.pathString = path.toString(); + this.path = path; + this.node = node; + } + @Override - public Either<Error, JungleTreeEditor> edit( - TreeNode root, - TransactionManager txManager, - TreeEditor editor, - TreeOperationLog log, - TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index) { - // TODO Auto-generated method stub - return null; + public Either<Error, IndexJungleTreeEditor> edit(TreeNode root,TransactionManager txManager,TreeEditor editor,TreeOperationLog log,TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index) { + NodePath newPath = path.pop().right(); + TreeNode target = getTarget(node, newPath); + String attribute = target.getAttributes().getString(key); + TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = editIndex(attribute, index); + IndexJungleTreeEditor newEditor = new IndexJungleTreeEditor(root,txManager,editor,log, newIndex); + Either<Error, IndexJungleTreeEditor> either = DefaultEither.newB(newEditor); + return either; + } + + public TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> editIndex(String attribute, TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index){ + + Option<TreeMap<String, List<Pair<TreeNode, NodePath>>>> innerIndexOp = index.get(key); + if (innerIndexOp.isSome()) { + TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = innerIndexOp.some(); + Option<List<Pair<TreeNode, NodePath>>> listOp = innerIndex.get(attribute); + + if (listOp.isSome()) { + List<Pair<TreeNode, NodePath>> list = listOp.some(); + List<Pair<TreeNode, NodePath>> newList = List.nil(); + + for (Pair<TreeNode, NodePath> pathNode : list){ + System.out.println(pathString); + System.out.println(pathNode.right().toString()); + if (pathNode.right().toString() == pathString) + newList = newList.cons(pathNode); + } + + if (newList.isEmpty()) { + TreeMap<String, List<Pair<TreeNode, NodePath>>> newInnerIndex = innerIndex.delete(attribute); + + if (newInnerIndex.isEmpty()) { + TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = index.delete(key); + return newIndex; + } + + TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = index.set(key, newInnerIndex); + return newIndex; + } + + TreeMap<String, List<Pair<TreeNode, NodePath>>> newInnerIndex = innerIndex.set(attribute,newList); + TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = index.set(key, newInnerIndex); + return newIndex; + + } else { + return index; + } + } else { + return index; + } } + public TreeNode getTarget(TreeNode node , NodePath path){ + Pair<Integer, NodePath> pathNode = path.pop(); + Either<Error, TreeNode> either = node.getChildren().at(pathNode.left()); + if (either.isA()) + return node; + + TreeNode child = either.b(); + if (pathNode.right().size() == 0) + return child; + + TreeNode target = getTarget(child,pathNode.right()); + return target; + } + + }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/IndexEditor.java Tue Oct 14 15:11:17 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/IndexEditor.java Tue Oct 14 17:26:29 2014 +0900 @@ -14,5 +14,5 @@ import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Pair; public interface IndexEditor { - Either<Error, JungleTreeEditor> edit(TreeNode root,TransactionManager txManager, TreeEditor editor,TreeOperationLog log,TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index); + Either<Error, IndexJungleTreeEditor> edit(TreeNode root,TransactionManager txManager, TreeEditor editor,TreeOperationLog log,TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/PutIndexEditor.java Tue Oct 14 15:11:17 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/PutIndexEditor.java Tue Oct 14 17:26:29 2014 +0900 @@ -38,11 +38,12 @@ } @Override - public Either<Error, JungleTreeEditor> edit(TreeNode root,TransactionManager txManager,TreeEditor editor,TreeOperationLog log,TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index) { - TreeNode target = getTarget(root, path); + public Either<Error, IndexJungleTreeEditor> edit(TreeNode root,TransactionManager txManager,TreeEditor editor,TreeOperationLog log,TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index) { + NodePath newPath = path.pop().right(); + TreeNode target = getTarget(root, newPath); TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = editIndex(target, path, key, attribute,index); IndexJungleTreeEditor newEditor = new IndexJungleTreeEditor(root,txManager,editor,log, newIndex); - Either<Error, JungleTreeEditor> either = DefaultEither.newB(newEditor); + Either<Error, IndexJungleTreeEditor> either = DefaultEither.newB(newEditor); return either; } @@ -91,7 +92,11 @@ Either<Error, TreeNode> either = node.getChildren().at(pathNode.left()); if (either.isA()) return node; + TreeNode child = either.b(); + if (pathNode.right().size() == 0) + return child; + TreeNode target = getTarget(child,pathNode.right()); return target; }
--- a/src/test/java/ip/ac/u_ryukyu/ie/cr/tatsuki/jungle/index/IndexTest.java Tue Oct 14 15:11:17 2014 +0900 +++ b/src/test/java/ip/ac/u_ryukyu/ie/cr/tatsuki/jungle/index/IndexTest.java Tue Oct 14 17:26:29 2014 +0900 @@ -20,15 +20,19 @@ import org.junit.Test; import fj.data.List; +import fj.data.Option; import fj.data.TreeMap; public class IndexTest { @Test public void IndexTest(){ + DefaultJungle jungle = new DefaultJungle(null,"hoge",new DefaultTreeEditor(new DefaultTraverser())); JungleTree tree = jungle.createNewTree("fuga"); - JungleTreeEditor editor = tree.getIndexTreeEditor(); + IndexJungleTreeEditor editor = tree.getIndexTreeEditor(); + TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> emptyIndex = editor.getIndex(); + Assert.assertTrue(emptyIndex.isEmpty()); NodePath path = new DefaultNodePath(); Either<Error, JungleTreeEditor> either = editor.addNewChildAt(path, 0); Assert.assertFalse(either.isA()); @@ -42,5 +46,16 @@ IndexJungleTreeEditor IndexEditor = tree.getIndexTreeEditor(); TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index = IndexEditor.getIndex(); Assert.assertFalse(index.isEmpty()); + + JungleTreeEditor editor4 = tree.getIndexTreeEditor(); + Either<Error, JungleTreeEditor> either3 = editor4.deleteAttribute(path.add(0), "key"); + Assert.assertFalse(either3.isA()); + JungleTreeEditor editor5 = either3.b(); + editor5.success(); + + IndexJungleTreeEditor IndexEditor2 = tree.getIndexTreeEditor(); + TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> deleteIndex = IndexEditor2.getIndex(); + Assert.assertTrue(deleteIndex.isEmpty()); + } }