view src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/index/Index.java @ 282:5da8a19dbe76

fix index difference update
author tatsuki
date Fri, 30 Dec 2016 03:55:55 +0900
parents
children 1a36841024f2
line wrap: on
line source

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

import jp.ac.u_ryukyu.ie.cr.jungle.core.Attributes;
import jp.ac.u_ryukyu.ie.cr.jungle.data.list.List;
import jp.ac.u_ryukyu.ie.cr.jungle.data.treemap.TreeMap;
import jp.ac.u_ryukyu.ie.cr.jungle.store.NulIterator;
import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode;

import java.util.Iterator;
import java.util.Optional;


public class Index {
    private final TreeMap<String, TreeMap<String, List<TreeNode>>> indexList;

    public Index(){
        this.indexList = new TreeMap<>();
    }

    private Index(TreeMap<String, TreeMap<String, List<TreeNode>>> indexList){
        this.indexList = indexList;
    }

    Index set(String key, String value, TreeNode node) {
        TreeMap<String, TreeMap<String, List<TreeNode>>> newIndexList = indexList;
        if (key == null) {
            return this;
        }
        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<String, List<TreeNode>> newIndex = index.put(value, nodeList);
            newIndexList = newIndexList.put(key, newIndex);
            return new Index(newIndexList);
        }

        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<String, List<TreeNode>> newIndex = index.put(value, newNodeList);
        newIndexList = newIndexList.put(key, newIndex);

        return new Index(newIndexList);
    }

    public Iterator<TreeNode> get(String key, String value) {
        Optional<TreeMap<String, List<TreeNode>>> indexOp = indexList.get(key);
        if (!indexOp.isPresent())
            return null;

        TreeMap<String, List<TreeNode>> index = indexOp.get();
        Optional<List<TreeNode>> nodeListOp = index.get(value);
        if (!nodeListOp.isPresent())
            return new NulIterator<TreeNode>();

        return nodeListOp.get().iterator();
    }

    public Index delete(TreeNode node) {
        TreeMap<String, TreeMap<String, List<TreeNode>>> newIndexList = indexList;
        Attributes attribute = node.getAttributes();
        Iterator<String> keys = attribute.getKeys();
        while(keys.hasNext()) {
            String key = keys.next();
            String value = attribute.getString(key);
            Optional<TreeMap<String, List<TreeNode>>> indexOp = newIndexList.get(key);
            if (!indexOp.isPresent())
                continue;
            TreeMap<String, List<TreeNode>> index = indexOp.get();
            Optional<List<TreeNode>> nodeListOp = index.get(value);
            if (!nodeListOp.isPresent())
                continue;
            List<TreeNode> nodeList = nodeListOp.get();
            List<TreeNode> newNodeList = nodeList.delete(node);
            TreeMap<String, List<TreeNode>> newIndex = index.put(value, newNodeList);
            newIndexList = newIndexList.put(key, newIndex);
        }
    return new Index(newIndexList);
    }
}