Mercurial > hg > Members > shoshi > jungle > jungle-core
changeset 147:af67dd0b5ba2
add DefaultIndexEditor
author | one |
---|---|
date | Fri, 21 Nov 2014 12:46:36 +0900 |
parents | 371b6ddb78f2 |
children | a2c374a2686b feb2346ace19 |
files | src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/DefaultIndexEditor.java |
diffstat | 1 files changed, 97 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/DefaultIndexEditor.java Fri Nov 21 12:46:36 2014 +0900 @@ -0,0 +1,97 @@ +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 { + + TreeMap<String, TreeMap<String, List<TreeNode>>> indexTreeMap; + + public DefaultIndexEditor(TreeMap<String, TreeMap<String, List<TreeNode>>> indexTreeMap) { + this.indexTreeMap = indexTreeMap; + } + + @Override + public IndexEditor delete(TreeNode node) { + TreeNodeAttributes attribute = node.getAttributes(); + List<String> keys = attribute.getKeys(); + TreeMap<String, TreeMap<String, List<TreeNode>>> newIndexTreeMap = indexTreeMap; + for (String key : keys) { + Option<TreeMap<String, List<TreeNode>>> indexOp = indexTreeMap.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; +// if (newNodeList.isEmpty()) +// newIndex = index.delete(value); +// else + 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 = indexTreeMap; + for (String key : keys) { + Option<TreeMap<String, List<TreeNode>>> indexOp = indexTreeMap.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 indexTreeMap; + } + + @Override + public IndexEditor edit(TreeNode node) { + return this; + } + +}