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

add deleteIndexEditor
author one
date Tue, 21 Oct 2014 19:47:25 +0900
parents bb53330364f1
children 6e9a8d26e0cf
line wrap: on
line source

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

import java.util.Iterator;

import fj.F;
import fj.Ord;
import fj.P2;
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.DefaultNodePath;
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 DeleteChildIndexEditor implements IndexEditor {

	NodePath editNodePath;

	public DeleteChildIndexEditor(int pos, NodePath path) {
		this.editNodePath = path.add(pos);
	}

	@Override
	public Either<Error, IndexJungleTreeEditor> edit(
			TreeNode root,
			TransactionManager txManager,
			TreeEditor editor,
			TreeOperationLog log,
			TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index) {

		TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = editIndex(index);
		IndexJungleTreeEditor newEditor = new IndexJungleTreeEditor(root, txManager, editor, newIndex);
		return DefaultEither.newB(newEditor);
	}

	public TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> editIndex(
			TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index) {

		if (!index.isEmpty()) {
			List<String> keyList = index.keys();
			TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = TreeMap.empty(Ord.stringOrd);
			TreeMap<String, List<Pair<TreeNode, NodePath>>> newInnerIndex = TreeMap.empty(Ord.stringOrd);

			for (String indexKey : keyList) {
				TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = index.get(indexKey).some();
				List<String> innerIndexKeyList = innerIndex.keys();

				for (String innerIndexKey : innerIndexKeyList) {
					List<Pair<TreeNode, NodePath>> pairList = innerIndex.get(innerIndexKey).some();
					List<Pair<TreeNode, NodePath>> list = checkPath(pairList);
					if (!list.isEmpty()){
						System.out.println(new String(list.head().left().getAttributes().get("KEY").array()));
						newInnerIndex = newInnerIndex.set(innerIndexKey, list);
					}
				}
				newIndex = newIndex.set(indexKey, newInnerIndex);
			}
			return newIndex;
		} else {
			return index;
		}
	}

	public List<Pair<TreeNode, NodePath>> checkPath(List<Pair<TreeNode, NodePath>> pairList){

		List<Pair<TreeNode, NodePath>> list = List.nil();
		for (Pair<TreeNode, NodePath> pair : pairList) {

			NodePath path = pair.right();
			System.out.println("oldPath = " + path.toString());
			NodePath newPath = new DefaultNodePath();
			
			if (path.toString().equals(editNodePath.toString())) {
				continue;
			}
			
			if (editNodePath.size() > path.size()) {
				list = list.cons(pair);
				continue;
			}

			Pair<Integer, NodePath> editNodePathCopy = editNodePath.pop();
			int loopCount = 0;

			for (Integer pathInt : path) {
				loopCount++;

				if (pathInt == -1)
					continue;
				if (editNodePathCopy.right().size() > 0) {
					editNodePathCopy = editNodePathCopy.right().pop();
					if (loopCount >= editNodePath.size()
							&& editNodePathCopy.left() < pathInt) {
						newPath = newPath.add(pathInt - 1);
						continue;
					}

					if (!(editNodePathCopy.left() == pathInt)) {
						newPath = path;
						break;
					}
				}

				newPath = newPath.add(pathInt);

			}

			System.out.println("newPath = " + newPath.toString());
			Pair<TreeNode, NodePath> newPair = new Pair<TreeNode, NodePath>(
					pair.left(), newPath);
			list = list.cons(newPair);
		}

		return list;
	}

}