Mercurial > hg > Members > tatsuki > bench > jungle-core
view src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/AddNewChildrenIndexEditor.java @ 139:ec166c8ff079
add findAll and findInSubTreeAll
author | one |
---|---|
date | Tue, 28 Oct 2014 06:35:34 +0900 |
parents | 6e9a8d26e0cf |
children | afbe19c98f53 72f454eb04ec |
line wrap: on
line source
package jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index; import java.util.Iterator; import fj.F; import fj.Ord; import fj.P2; 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; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.TreeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode; 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 AddNewChildrenIndexEditor implements IndexEditor { NodePath editNodePath; public AddNewChildrenIndexEditor(int pos, NodePath path) { this.editNodePath = path.add(pos); } @Override public Either<Error, IndexJungleTreeEditor> edit( TreeNode root, TransactionManager txManager, TreeEditor editor, TreeOperationLog log, TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index) { TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = editIndex(index); IndexJungleTreeEditor newEditor = new IndexJungleTreeEditor(root, txManager, editor, newIndex); return DefaultEither.newB(newEditor); } public TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> editIndex( TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index) { if (!index.isEmpty()) { List<String> keyList = index.keys(); TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = TreeMap.empty(Ord.stringOrd); TreeMap<String, List<Pair<TreeNode, NodePath>>> newInnerIndex = TreeMap.empty(Ord.stringOrd); for (String indexKey : keyList) { TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = index.get(indexKey).some(); List<String> innerIndexKeyList = innerIndex.keys(); for (String innerIndexKey : innerIndexKeyList) { List<Pair<TreeNode, NodePath>> pairList = innerIndex.get(innerIndexKey).some(); List<Pair<TreeNode, NodePath>> list = checkPath(pairList); if (!list.isEmpty()){ //System.out.println(new String(list.head().left().getAttributes().get("KEY").array())); newInnerIndex = newInnerIndex.set(innerIndexKey, list); } } newIndex = newIndex.set(indexKey, newInnerIndex); } return newIndex; } else { return index; } } public List<Pair<TreeNode, NodePath>> checkPath(List<Pair<TreeNode, NodePath>> pairList){ List<Pair<TreeNode, NodePath>> list = List.nil(); for (Pair<TreeNode, NodePath> pair : pairList) { NodePath path = pair.right(); //System.out.println("oldPath = " + path.toString()); NodePath newPath = new DefaultNodePath(); if (editNodePath.size() > path.size()) { list = list.cons(pair); continue; } Pair<Integer, NodePath> editNodePathCopy = editNodePath.pop(); int loopCount = 0; for (Integer pathInt : path) { loopCount++; if (pathInt == -1) continue; if (editNodePathCopy.right().size() > 0) { editNodePathCopy = editNodePathCopy.right().pop(); if (loopCount == editNodePath.size() && editNodePathCopy.left() <= pathInt) { newPath = newPath.add(pathInt + 1); continue; } if (!(editNodePathCopy.left() == pathInt)) { newPath = path; break; } } newPath = newPath.add(pathInt); } //System.out.println("newPath = " + newPath.toString()); Pair<TreeNode, NodePath> newPair = new Pair<TreeNode, NodePath>(pair.left(), newPath); list = list.cons(newPair); } return list; } }