view src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/DeleteIndexEditor.java @ 130:bb53330364f1

add putIndexEditor and deleteIndexEditor
author one
date Tue, 14 Oct 2014 17:26:29 +0900
parents 8067fec660ab
children 1c3c3300716c
line wrap: on
line source

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

import java.nio.ByteBuffer;

import fj.data.List;
import fj.data.Option;
import fj.data.TreeMap;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.TreeEditor;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.IndexJungleTreeEditor;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.TransactionManager;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Pair;

public class DeleteIndexEditor implements IndexEditor {

	String key;
	String pathString;
	NodePath path;
	TreeNode node;
	
	public DeleteIndexEditor(String key, NodePath path, TreeNode node) {
		this.key = key;
		this.pathString = path.toString();
		this.path = path; 
		this.node = node;
	}
	
	@Override
	public Either<Error, IndexJungleTreeEditor> edit(TreeNode root,TransactionManager txManager,TreeEditor editor,TreeOperationLog log,TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index) {
		NodePath newPath = path.pop().right();
		TreeNode target = getTarget(node, newPath);
		String attribute = target.getAttributes().getString(key);
		TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = editIndex(attribute, index);
		IndexJungleTreeEditor newEditor = new IndexJungleTreeEditor(root,txManager,editor,log, newIndex);
		Either<Error, IndexJungleTreeEditor> either = DefaultEither.newB(newEditor);
		return either;
	}
	
	public TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> editIndex(String attribute, TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index){
	
		Option<TreeMap<String, List<Pair<TreeNode, NodePath>>>> innerIndexOp = index.get(key);
		if (innerIndexOp.isSome()) {
			TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = innerIndexOp.some();
			Option<List<Pair<TreeNode, NodePath>>> listOp = innerIndex.get(attribute);
			
			if (listOp.isSome()) {
				List<Pair<TreeNode, NodePath>> list = listOp.some();
				List<Pair<TreeNode, NodePath>> newList = List.nil();
				
				for (Pair<TreeNode, NodePath> pathNode : list){
					System.out.println(pathString);
					System.out.println(pathNode.right().toString());
					if (pathNode.right().toString() == pathString)
						newList = newList.cons(pathNode);
				}
				
				if (newList.isEmpty()) {
					TreeMap<String, List<Pair<TreeNode, NodePath>>> newInnerIndex = innerIndex.delete(attribute);
					
					if (newInnerIndex.isEmpty()) {
						TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = index.delete(key);
						return newIndex;	
					}
					
					TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = index.set(key, newInnerIndex);
					return newIndex;
				}
					
				TreeMap<String, List<Pair<TreeNode, NodePath>>> newInnerIndex = innerIndex.set(attribute,newList);
				TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = index.set(key, newInnerIndex);
				return newIndex;
				
			} else {
				return index;
			}
		} else {
			return index;
		}
	}

	public TreeNode getTarget(TreeNode node , NodePath path){
		Pair<Integer, NodePath> pathNode = path.pop();
		Either<Error, TreeNode> either = node.getChildren().at(pathNode.left());
		if (either.isA())
			return node;
		
		TreeNode child = either.b();
		if (pathNode.right().size() == 0)
			return child;
		
		TreeNode target = getTarget(child,pathNode.right());
		return target;
	}

	
}