view src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/PutIndexEditor.java @ 135:6e9a8d26e0cf

refactor
author one
date Fri, 24 Oct 2014 18:49:48 +0900
parents bb53330364f1
children 7334f78a92c3
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.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;
import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.PathNodeIndexIterator;

import java.nio.ByteBuffer;

public class PutIndexEditor implements IndexEditor {

	String key; 
	String attribute;
	NodePath path;
	TransactionManager txManager;
	TreeNode root;
	TreeEditor editor;
	TreeOperationLog log;
	
	public PutIndexEditor(String key, ByteBuffer attribute,NodePath path) {
	this.key = key;
	this.attribute = new String(attribute.array());
	this.path = path;
	}

	@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(root, newPath);
		TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = editIndex(target, path, key, 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(TreeNode target, NodePath path, String key, String attribute,TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index){

		Option<TreeMap<String, List<Pair<TreeNode, NodePath>>>> innerIndexOp = index.get(key);
		Pair<TreeNode, NodePath> pathNode = new Pair<TreeNode, NodePath>(target,path);
		
		if (attribute != null) {
			if (innerIndexOp.isNone()) {

//				TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = TreeMap.empty(Ord.stringOrd);
//				List<Pair<TreeNode, NodePath>> list = List.nil();
//				list = list.cons(pathNode);
//				innerIndex = innerIndex.set(attribute, list);
//				TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = index.set(key, innerIndex);
				return index;
			} else {

				TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = innerIndexOp.some();
				innerIndex.set(attribute, null);
				Option<List<Pair<TreeNode, NodePath>>> opList = innerIndex.get(attribute);

				if (opList.isNone()) {

					List<Pair<TreeNode, NodePath>> list = List.nil();
					list = list.cons(pathNode);
					innerIndex = innerIndex.set(attribute, list);

				} else {

					List<Pair<TreeNode, NodePath>> list = opList.some();
					list = list.cons(pathNode);
					innerIndex = innerIndex.set(attribute, list);

				}
				TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = index.set(key, innerIndex);
				return newIndex;
			}
		}
		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;
	}

}