view src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/DeleteChildIndexEditor.java @ 151:d9fbddf77bf6

add class Index
author one
date Sat, 22 Nov 2014 14:46:44 +0900
parents 371b6ddb78f2
children 8a0aa8fc137c
line wrap: on
line source

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

import fj.Ord;
import fj.data.List;
import fj.data.Option;
import fj.data.TreeMap;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
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 jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Pair;
import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.PathNodeIterator;

public class DeleteChildIndexEditor implements IndexEditor {

  Index index;
  int pos;

  public DeleteChildIndexEditor(int pos, Index index) {
    this.index = index;
    this.pos = pos;
  }

  @Override
  public IndexEditor delete(TreeNode node) {
    TreeNodeAttributes attribute = node.getAttributes();
    List<String> keys = attribute.getKeys();
    TreeMap<String, TreeMap<String, List<TreeNode>>> newIndexTreeMap = index;
    for (String key : keys) {
      Option<TreeMap<String, List<TreeNode>>> indexOp = index.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;
          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 = index;
    for (String key : keys) {
      Option<TreeMap<String, List<TreeNode>>> indexOp = index.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 index;
  }

  @Override
  public IndexEditor edit(TreeNode node) {
    PathNodeIterator nodeSearcher = new PathNodeIterator(new Pair<TreeNode, NodePath>(node, new DefaultNodePath()));
    TreeMap<String, TreeMap<String, List<TreeNode>>> newIndexTreeMap = index;

    for (; nodeSearcher.hasNext();) {
      TreeNode deleteNode = nodeSearcher.next().left();
      TreeNodeAttributes attribute = deleteNode.getAttributes();
      List<String> keys = attribute.getKeys();
      for (String key : keys) {
        Option<TreeMap<String, List<TreeNode>>> indexOp = newIndexTreeMap.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 != deleteNode)
                newNodeList = newNodeList.cons(indexingNode);
            }
            TreeMap<String, List<TreeNode>> newIndex = index.set(value, newNodeList);
            newIndexTreeMap = newIndexTreeMap.set(key, newIndex);
          }

        }
      }
    }
    return null;
  }

}