view src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/index/IndexCreater.java @ 317:d6b81870216b

Persisitent Differential Tree implement
author tatsuki
date Wed, 01 Feb 2017 04:04:17 +0900
parents 5da8a19dbe76
children
line wrap: on
line source

package jp.ac.u_ryukyu.ie.cr.jungle.store.index;


import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode;
import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNodeChildren;

import java.util.Iterator;
import java.util.Stack;

public class IndexCreater {

    private int childNumber;
    private TreeNodeChildren children;
    private ParentIndex parentIndex = new ParentIndex();
    private Index index = new Index();

    public IndexCreater(TreeNode rootNode) {
        Stack<TreeNode> nodeStack = new Stack<>();
        Stack<Integer> searchStack = new Stack<>();
        TreeNode 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)
                    index = index.set(key, value, targetNode);
            }
            if (node.getChildren().size() > 0) {
                nodeStack.push(node);
                TreeNode parent = node;
                children = node.getChildren();
                node = children.at(0).b();
                parentIndex = 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 = 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 = parentIndex.set(parent, node);
                    searchStack.push(++childNumber);
                }
            }
        }
    }



    public Index getIndex() {
        return index;
    }

    public ParentIndex getParentIndex() {
        return parentIndex;
    }
}