Mercurial > hg > Members > kazuma > jungle-ormapper
diff src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/index/IndexCreater.java @ 0:44465893e8b8
first Commit
author | Kazuma |
---|---|
date | Wed, 30 Nov 2016 01:47:55 +0900 |
parents | |
children |
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/jungle/store/index/IndexCreater.java Wed Nov 30 01:47:55 2016 +0900 @@ -0,0 +1,117 @@ +package jp.ac.u_ryukyu.ie.cr.jungle.store.index; + + +import jp.ac.u_ryukyu.ie.cr.jungle.data.list.List; +import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNode; +import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNodeChildren; +import jp.ac.u_ryukyu.ie.cr.jungle.data.treemap.TreeMap; + +import java.util.Iterator; +import java.util.Optional; +import java.util.Stack; + +public class IndexCreater { + + TreeNode node; + int childNumber; + private TreeNodeChildren children; + ParentIndex parentIndex = new ParentIndex(); + TreeMap<String, TreeMap<String, List<TreeNode>>> indexList = new TreeMap<>(); + + public IndexCreater(TreeNode rootNode) { + Stack<TreeNode> nodeStack = new Stack<>(); + Stack<Integer> searchStack = new Stack<>(); + this.node = rootNode; + while (node != null) { + TreeNode targetNode = node; + Iterator<String> keys = targetNode.getAttributes().getKeys(); + for (; keys.hasNext(); ) { + String key = keys.next(); + String value = targetNode.getAttributes().getString(key); + if (value != null) + indexList = set(key, value, targetNode); + } + if (node.getChildren().size() > 0) { + nodeStack.push(node); + TreeNode parent = node; + children = node.getChildren(); + node = children.at(0).b(); + parentIndex.set(parent, node); + childNumber = 1; + searchStack.push(childNumber); + } else if (node == rootNode) { + node = null; // no more node + children = null; + return; + } else if (children != null && children.size() > childNumber) { + childNumber = searchStack.pop(); + TreeNode parent = nodeStack.pop(); + nodeStack.push(parent); + node = children.at(childNumber).b(); + parentIndex.set(parent, node); + searchStack.push(++childNumber); + } else { + node = nodeStack.pop(); + children = node.getChildren(); + childNumber = searchStack.pop(); + for (; children.size() == childNumber; ) { + if (node == rootNode) { + node = null; // no more node + children = null; + return; + } + node = nodeStack.pop(); + children = node.getChildren(); + childNumber = searchStack.pop(); + } + if (node != null && childNumber < children.size()) { + nodeStack.push(node); + TreeNode parent = node; + node = children.at(childNumber).b(); + parentIndex.set(parent, node); + searchStack.push(++childNumber); + } + } + } + } + + public TreeMap<String, TreeMap<String, List<TreeNode>>> set(String key, String value, TreeNode node) { + if (key == null) + System.out.println(""); + Optional<TreeMap<String, List<TreeNode>>> indexOp = indexList.get(key); + if (!indexOp.isPresent()) { + TreeMap<String, List<TreeNode>> index = new TreeMap<>(); + List<TreeNode> nodeList = new List<>(); + nodeList = nodeList.addLast(node); + TreeMap newIndex = index.put(value, nodeList); + indexList = indexList.put(key, newIndex); + return indexList; + } + + TreeMap<String, List<TreeNode>> index = indexOp.get(); + Optional<List<TreeNode>> nodeListOp = index.get(value); + + List<TreeNode> newNodeList; + + if (nodeListOp.isPresent()) { + newNodeList = nodeListOp.get().addLast(node); + + } else { + List<TreeNode> nodeList = new List<>(); + newNodeList = nodeList.addLast(node); + + } + TreeMap newIndex = index.put(value, newNodeList); + indexList = indexList.put(key, newIndex); + + return indexList; + } + + public TreeMap<String, TreeMap<String, List<TreeNode>>> getIndex() { + return indexList; + } + + public ParentIndex getParentIndex() { + return parentIndex; + } +}