Mercurial > hg > Members > tatsuki > bench > jungle-core
view src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/Index.java @ 151:d9fbddf77bf6
add class Index
author | one |
---|---|
date | Sat, 22 Nov 2014 14:46:44 +0900 |
parents | |
children | 20af7f25ef32 |
line wrap: on
line source
package jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index; import java.util.Iterator; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode; import fj.Ord; import fj.P2; import fj.data.List; import fj.data.Option; import fj.data.TreeMap; public class Index { TreeMap<String, TreeMap<String, List<TreeNode>>> indexList; public Index() { this.indexList = TreeMap.empty(Ord.stringOrd); } public Index(TreeMap<String, TreeMap<String, List<TreeNode>>> indexList) { this.indexList = indexList; } public Index(Index indexList) { this.indexList = indexList.getIndex(); } public Index set(String key, String value, TreeNode node) { Option<TreeMap<String, List<TreeNode>>> indexOp = indexList.get(key); if (indexOp.isNone()) return this; TreeMap<String, List<TreeNode>> index = indexOp.some(); Option<List<TreeNode>> nodeListOp = index.get(value); List<TreeNode> newNodeList; if (nodeListOp.isSome()) { List<TreeNode> nodeList = nodeListOp.some(); newNodeList = nodeList.cons(node); } else { List<TreeNode> nodeList = List.nil(); newNodeList = nodeList.cons(node); } TreeMap<String, List<TreeNode>> newIndex = index.set(value, newNodeList); TreeMap<String, TreeMap<String, List<TreeNode>>> newIndexList = indexList.set(key, newIndex); return new Index(newIndexList); } public Index delete(String key, String value, TreeNode node) { Option<TreeMap<String, List<TreeNode>>> indexOp = indexList.get(key); if (indexOp.isNone()) return this; TreeMap<String, List<TreeNode>> index = indexOp.some(); TreeMap<String, List<TreeNode>> newIndex = index; 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.equals(node)) newNodeList = newNodeList.cons(indexingNode); } newIndex = index.set(value, newNodeList); } else { return this; } TreeMap<String, TreeMap<String, List<TreeNode>>> newIndexList = indexList.set(key, newIndex); return new Index(newIndexList); } public Index deleteNodeAll(TreeNode node) { List<String> keys = node.getAttributes().getKeys(); TreeMap<String, TreeMap<String, List<TreeNode>>> newIndexList = indexList; for (String key : keys) { Option<TreeMap<String, List<TreeNode>>> indexOp = indexList.get(key); if (indexOp.isNone()) continue; TreeMap<String, List<TreeNode>> index = indexOp.some(); Iterator<P2<String, List<TreeNode>>> indexIterator = index.iterator(); TreeMap<String, List<TreeNode>> newIndex = index; for (; indexIterator.hasNext();) { List<TreeNode> newNodeList = List.nil(); P2<String, List<TreeNode>> NodeListP2 = indexIterator.next(); String value = NodeListP2._1(); List<TreeNode> targetNodeList = NodeListP2._2(); for (TreeNode targetNode : targetNodeList) { if (!node.equals(targetNode)) newNodeList = newNodeList.cons(targetNode); } newIndex = newIndex.set(value, newNodeList); } newIndexList = indexList.set(key, newIndex); } return new Index(newIndexList); } public Index putNodeAll(TreeNode node) { List<String> keys = node.getAttributes().getKeys(); TreeMap<String, TreeMap<String, List<TreeNode>>> newIndexList = indexList; for (String key : keys) { String value = node.getAttributes().getString(key); if (value == null) continue; Option<TreeMap<String, List<TreeNode>>> indexOp = indexList.get(key); if (indexOp.isNone()) continue; TreeMap<String, List<TreeNode>> index = indexOp.some(); Option<List<TreeNode>> nodeListOp = index.get(value); TreeMap<String, List<TreeNode>> newIndex = index; if (nodeListOp.isNone()) { List<TreeNode> newNodeList = List.nil(); newNodeList = newNodeList.cons(node); newIndex = newIndex.set(value, newNodeList); } else { List<TreeNode> newNodeList = nodeListOp.some(); newNodeList = newNodeList.cons(node); newIndex = newIndex.set(value, newNodeList); } newIndexList = newIndexList.set(key, newIndex); } return new Index(newIndexList); } public List<TreeNode> get(String key, String value) { Option<TreeMap<String, List<TreeNode>>> indexOp = indexList.get(key); if (indexOp.isNone()) return List.nil(); TreeMap<String, List<TreeNode>> index = indexOp.some(); Option<List<TreeNode>> nodeListOp = index.get(value); if (nodeListOp.isNone()) return List.nil(); return nodeListOp.some(); } private TreeMap<String, TreeMap<String, List<TreeNode>>> getIndex() { return indexList; } }