Mercurial > hg > Members > tatsuki > bench > jungle-core
view src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/Index.java @ 153:20af7f25ef32
miner change
author | one |
---|---|
date | Tue, 25 Nov 2014 17:52:41 +0900 |
parents | d9fbddf77bf6 |
children | c23f70bb791f 89ed172137ab |
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.NulIterator; 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()) { TreeMap<String, List<TreeNode>> index = TreeMap.empty(Ord.stringOrd); List<TreeNode> nodeList = List.nil(); List<TreeNode> 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); } 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 List<TreeNode> get(String key, String value) { Option<TreeMap<String, List<TreeNode>>> indexOp = indexList.get(key); if (indexOp.isNone()) return null; TreeMap<String, List<TreeNode>> index = indexOp.some(); Option<List<TreeNode>> nodeListOp = index.get(value); if (nodeListOp.isNone()) return List.nil(); return nodeListOp.some(); } public Iterator<TreeNode> getAll(String key){ Option<TreeMap<String, List<TreeNode>>> indexOp = indexList.get(key); if (indexOp.isNone()) return null; TreeMap<String, List<TreeNode>> index = indexOp.some(); if (!index.isEmpty()) return new NulIterator<TreeNode>(); return new Iterator<TreeNode>(){ Iterator<P2<String, List<TreeNode>>> treeMapIterator = index.iterator(); List<TreeNode> nodeList = List.nil(); TreeNode node; @Override public boolean hasNext() { if (nodeList.isNotEmpty()) { node = nodeList.head(); nodeList = nodeList.tail(); return true; } for (;treeMapIterator.hasNext();) { nodeList = treeMapIterator.next()._2(); node = nodeList.head(); nodeList = nodeList.tail(); return true; } return false; } @Override public TreeNode next() { return node; } }; } private TreeMap<String, TreeMap<String, List<TreeNode>>> getIndex() { return indexList; } }