view src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/AddNewChildrenIndexEditor.java @ 143:afbe19c98f53

change Index form TreeMap<String,TreeMap<String<List<Pair<TreeNode,NodePath>>>> → TreeMap<String,TreeMap<String<List<NodePath>>> bag
author one
date Sat, 15 Nov 2014 17:48:07 +0900
parents ec166c8ff079
children
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.TreeMap;
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 AddNewChildrenIndexEditor implements IndexEditor {

	NodePath editNodePath;

	public AddNewChildrenIndexEditor(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<NodePath>>> index) {

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

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

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

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

				for (String innerIndexKey : innerIndexKeyList) {
					List<NodePath> pairList = innerIndex.get(innerIndexKey).some();
					List<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<NodePath> checkPath(List<NodePath> pairList){

		List<NodePath> list = List.nil();
		for (NodePath path : pairList) {

	
			//System.out.println("oldPath = " + path.toString());
			NodePath newPath = new DefaultNodePath();
			
			
			if (editNodePath.size() > path.size()) {
				list = list.cons(path);
				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());
			list = list.cons(path);
		}

		return list;
	}

}