view src/test/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/index/IndexFormTest.java @ 134:f46a6e0e4594

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

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

import java.nio.ByteBuffer;

import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
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.TreeNodeAttributes;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.DefaultTreeNode;
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 org.junit.Assert;
import org.junit.Test;

import fj.Ord;
import fj.data.List;
import fj.data.Option;
import fj.data.TreeMap;

public class IndexFormTest {

	TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index = TreeMap
			.empty(Ord.stringOrd);

	@Test
	public void IndexFormTest() {

		NodePath path = new DefaultNodePath();
		TreeNode node = createTree(3, 0, 3, path);
		TreeMap<String, List<Pair<TreeNode, NodePath>>> map = index.get(key).some();
		Option<List<Pair<TreeNode, NodePath>>> opList = map.get("<-1,0,0,2>");
		List<Pair<TreeNode, NodePath>> list = opList.some();
		Pair<TreeNode, NodePath> Pair = list.head();
		NodePath newPath = Pair.right();
		String pathStr = newPath.toString();
		Assert.assertEquals(pathStr,"<-1,0,0,2>");
		System.out.println("aaa");
	}

	public static String key = "KEY";
	public static ByteBuffer value = ByteBuffer.wrap(key.getBytes());
	public static DefaultTreeNode factory = new DefaultTreeNode();

	public TreeNode createTree(int _curX, int _curY, int _maxHeight,
			NodePath _address) {
		TreeNode parent = factory.createNewNode();
		Either<Error, TreeNode> either = parent.getAttributes().put(key,ByteBuffer.wrap(_address.toString().getBytes()));
		if (either.isA()) {
			Assert.fail();
		}
		editIndex(parent, _address, _address.toString(), key);
		parent = either.b();

		if (_curY == _maxHeight) {
			return parent;
		}

		for (int i = 0; i < _curY + 1; i++) {
			TreeNode ch = createTree(i, _curY + 1, _maxHeight, _address.add(i));
			either = parent.getChildren().addNewChildAt(i, ch);
			if (either.isA()) {
				Assert.fail();
			}

			parent = either.b();
		}

		return parent;
	}


	public void editIndex(TreeNode node, NodePath path, String attribute,String key) {

		Pair<TreeNode, NodePath> pair = new Pair<TreeNode, NodePath>(node, path);
		Option<TreeMap<String, List<Pair<TreeNode, NodePath>>>> opAttributeList = index.get(key);

		if (opAttributeList.isNone()) {
			TreeMap<String, List<Pair<TreeNode, NodePath>>> nodeIndex = TreeMap.empty(Ord.stringOrd);
			List<Pair<TreeNode, NodePath>> list = List.nil();
			list = list.cons(pair);
			nodeIndex = nodeIndex.set(attribute, list);
			index = index.set(key, nodeIndex);
		} else {
			TreeMap<String, List<Pair<TreeNode, NodePath>>> indexMap = opAttributeList.some();
			Option<List<Pair<TreeNode, NodePath>>> opNodeIndex = indexMap.get(attribute);

			if (opNodeIndex.isNone()) {
				List<Pair<TreeNode, NodePath>> pairList = List.nil();
				pairList = pairList.cons(pair);
				indexMap = indexMap.set(attribute, pairList);

			} else {
				List<Pair<TreeNode, NodePath>> pairList = opNodeIndex.some();
				pairList = pairList.cons(pair);
				indexMap = indexMap.set(attribute, pairList);
			}
		index = index.set(key, indexMap);
		}
	System.out.println(attribute);

	}

}