Mercurial > hg > Members > tatsuki > bench > jungle-core
view src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/DefaultIndexEditor.java @ 151:d9fbddf77bf6
add class Index
author | one |
---|---|
date | Sat, 22 Nov 2014 14:46:44 +0900 |
parents | feb2346ace19 |
children | 8a0aa8fc137c |
line wrap: on
line source
package jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index; 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 fj.Ord; import fj.data.List; import fj.data.Option; import fj.data.TreeMap; public class DefaultIndexEditor implements IndexEditor { Index index; public DefaultIndexEditor(Index index) { this.index = index; } @Override public IndexEditor delete(TreeNode node) { TreeNodeAttributes attribute = node.getAttributes(); List<String> keys = attribute.getKeys(); Index 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) { return this; } }