view src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/Index.java @ 158:89ed172137ab

fj Index fix?
author one
date Sun, 07 Dec 2014 19:01:08 +0900
parents 20af7f25ef32
children 1749338f2366 383b08d1711c
line wrap: on
line source

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

import java.util.Iterator;

import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NulIterator;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode;
import fj.Ord;
import fj.P2;
import fj.data.List;
import fj.data.Option;
import fj.data.TreeMap;

public class Index {

  TreeMap<String, TreeMap<String, List<TreeNode>>> indexList;

  public Index() {
    this.indexList = TreeMap.empty(Ord.stringOrd);
  }

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


  public Index set(String key, String value, TreeNode node) {
    Option<TreeMap<String, List<TreeNode>>> indexOp = indexList.get(key);
    if (indexOp.isNone()) {
      TreeMap<String, List<TreeNode>> index = TreeMap.empty(Ord.stringOrd);
      List<TreeNode> nodeList = List.nil();
      List<TreeNode> newNodeList = nodeList.cons(node);
      TreeMap<String, List<TreeNode>> newIndex = index.set(value, newNodeList);
      indexList = indexList.set(key, newIndex);
      return this;
    }

    TreeMap<String, List<TreeNode>> index = indexOp.some();
    Option<List<TreeNode>> nodeListOp = index.get(value);

    List<TreeNode> newNodeList;
    if (nodeListOp.isSome()) {
      List<TreeNode> nodeList = nodeListOp.some();
      newNodeList = nodeList.cons(node);
    } else {
      List<TreeNode> nodeList = List.nil();
      newNodeList = nodeList.cons(node);
    }
    TreeMap<String, List<TreeNode>> newIndex = index.set(value, newNodeList);
    indexList = indexList.set(key, newIndex);

    return this;
  }
  

//  public Index delete(String key, String value, TreeNode node) {
//    Option<TreeMap<String, List<TreeNode>>> indexOp = indexList.get(key);
//    if (indexOp.isNone())
//      return this;
//
//    TreeMap<String, List<TreeNode>> index = indexOp.some();
//    TreeMap<String, List<TreeNode>> newIndex = index;
//    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.equals(node))
//          newNodeList = newNodeList.cons(indexingNode);
//      }
//
//      newIndex = index.set(value, newNodeList);
//    } else {
//      return this;
//    }
//    TreeMap<String, TreeMap<String, List<TreeNode>>> newIndexList = indexList.set(key, newIndex);
//    return new Index(newIndexList);
//  }



  public List<TreeNode> get(String key, String value) {

    Option<TreeMap<String, List<TreeNode>>> indexOp = indexList.get(key);
    if (indexOp.isNone())
      return null;

    TreeMap<String, List<TreeNode>> index = indexOp.some();
    Option<List<TreeNode>> nodeListOp = index.get(value);

    if (nodeListOp.isNone())
      return List.nil();

    return nodeListOp.some();
  }
  
  public Iterator<TreeNode> getAll(String key){

    Option<TreeMap<String, List<TreeNode>>> indexOp = indexList.get(key);
    if (indexOp.isNone())
      return null;
    
    final TreeMap<String, List<TreeNode>> index = indexOp.some();
    if (!index.isEmpty())
      return new NulIterator<TreeNode>();

    
    return new Iterator<TreeNode>(){
      
      Iterator<P2<String, List<TreeNode>>> treeMapIterator = index.iterator();
      List<TreeNode> nodeList = List.nil();
      TreeNode node;
      
      @Override
      public boolean hasNext() {
        
        if (nodeList.isNotEmpty()) {
          node = nodeList.head();
          nodeList = nodeList.tail();
          return true;
        }
        
        for (;treeMapIterator.hasNext();) {
          nodeList = treeMapIterator.next()._2();
          node = nodeList.head();
          nodeList = nodeList.tail();
          return true;
        }
        return false;
      }

      @Override
      public TreeNode next() {
        return node;
      }
      
    };
    
  }
  
  public TreeMap<String, TreeMap<String, List<TreeNode>>> getIndex() {
    return indexList;
  }
  
}