Mercurial > hg > Members > tatsuki > bench > jungle-core
view src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/DeleteChildIndexEditor.java @ 152:8a0aa8fc137c
Merge with a2c374a2686b82b0ad30f520412e191b84288fa9
author | one |
---|---|
date | Sat, 22 Nov 2014 15:25:09 +0900 |
parents | d9fbddf77bf6 a2c374a2686b |
children |
line wrap: on
line source
package jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index; import fj.Ord; import fj.data.List; import fj.data.Option; import fj.data.TreeMap; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; 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.TreeNodeAttributes; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Pair; import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.PathNodeIterator; public class DeleteChildIndexEditor implements IndexEditor { Index index; int pos; public DeleteChildIndexEditor(int pos, Index index) { this.index = index; this.pos = pos; } @Override public IndexEditor delete(TreeNode node) { TreeNodeAttributes attribute = node.getAttributes(); List<String> keys = attribute.getKeys(); TreeMap<String, TreeMap<String, List<TreeNode>>> newIndexTreeMap = index; for (String key : keys) { Option<TreeMap<String, List<TreeNode>>> indexOp = index.get(key); if (indexOp.isSome()) { TreeMap<String, List<TreeNode>> index = indexOp.some(); String value = attribute.getString(key); Option<List<TreeNode>> nodeListOp = index.get(value); if (nodeListOp.isSome()) { List<TreeNode> nodeList = nodeListOp.some(); List<TreeNode> newNodeList = List.nil(); for (TreeNode indexingNode : nodeList) { if (indexingNode != node) newNodeList = newNodeList.cons(indexingNode); } TreeMap<String, List<TreeNode>> newIndex; newIndex = index.set(value, newNodeList); newIndexTreeMap = newIndexTreeMap.set(key, newIndex); } } } return new DefaultIndexEditor(newIndexTreeMap); } @Override public IndexEditor add(TreeNode node) { TreeNodeAttributes attribute = node.getAttributes(); List<String> keys = attribute.getKeys(); TreeMap<String, TreeMap<String, List<TreeNode>>> newIndexTreeMap = index; for (String key : keys) { Option<TreeMap<String, List<TreeNode>>> indexOp = index.get(key); if (indexOp.isSome()) { TreeMap<String, List<TreeNode>> index = indexOp.some(); String value = attribute.getString(key); Option<List<TreeNode>> nodeListOp = index.get(value); if (nodeListOp.isSome()) { List<TreeNode> nodeList = nodeListOp.some(); List<TreeNode> newNodeList = nodeList.cons(node); TreeMap<String, List<TreeNode>> newIndex = index.set(value, newNodeList); newIndexTreeMap = newIndexTreeMap.set(key, newIndex); } else { // test List<TreeNode> nodeList = List.nil(); value = attribute.getString(key); List<TreeNode> newNodeList = nodeList.cons(node); TreeMap<String, List<TreeNode>> newIndex = index.set(value, newNodeList); newIndexTreeMap = newIndexTreeMap.set(key, newIndex); } } else { // test TreeMap<String, List<TreeNode>> index = TreeMap.empty(Ord.stringOrd); List<TreeNode> nodeList = List.nil(); String value = attribute.getString(key); List<TreeNode> newNodeList = nodeList.cons(node); TreeMap<String, List<TreeNode>> newIndex = index.set(value, newNodeList); newIndexTreeMap = newIndexTreeMap.set(key, newIndex); } } return new DefaultIndexEditor(newIndexTreeMap); } @Override public TreeMap<String, TreeMap<String, List<TreeNode>>> getIndex() { return index; } @Override public IndexEditor edit(TreeNode node) { PathNodeIterator nodeSearcher = new PathNodeIterator(new Pair<TreeNode, NodePath>(node, new DefaultNodePath())); TreeMap<String, TreeMap<String, List<TreeNode>>> newIndexTreeMap = index; for (; nodeSearcher.hasNext();) { TreeNode deleteNode = nodeSearcher.next().left(); TreeNodeAttributes attribute = deleteNode.getAttributes(); List<String> keys = attribute.getKeys(); for (String key : keys) { Option<TreeMap<String, List<TreeNode>>> indexOp = newIndexTreeMap.get(key); if (indexOp.isSome()) { TreeMap<String, List<TreeNode>> index = indexOp.some(); String value = attribute.getString(key); Option<List<TreeNode>> nodeListOp = index.get(value); if (nodeListOp.isSome()) { List<TreeNode> nodeList = nodeListOp.some(); List<TreeNode> newNodeList = List.nil(); for (TreeNode indexingNode : nodeList) { if (indexingNode != deleteNode) newNodeList = newNodeList.cons(indexingNode); } TreeMap<String, List<TreeNode>> newIndex = index.set(value, newNodeList); newIndexTreeMap = newIndexTreeMap.set(key, newIndex); } } } } return null; } }