view src/test/java/jp/ac/u_ryukyu/ie/cr/jungle/index/difference/ParentIndexPutTest.java @ 308:201cc75a9984

change Red Black Tree Edit Path Extends
author tatsuki
date Thu, 26 Jan 2017 15:23:25 +0900
parents 9ebe0ca360be
children 2a0cb1f0ba4e
line wrap: on
line source

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

import jp.ac.u_ryukyu.ie.cr.jungle.DefaultJungle;
import jp.ac.u_ryukyu.ie.cr.jungle.Jungle;
import jp.ac.u_ryukyu.ie.cr.jungle.core.Children;
import jp.ac.u_ryukyu.ie.cr.jungle.store.index.ParentIndex;
import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.DefaultNodePath;
import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.NodePath;
import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.jungleTreeEditor.JungleTreeEditor;
import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode;
import jp.ac.u_ryukyu.ie.cr.jungle.tree.JungleTree;
import jp.ac.u_ryukyu.ie.cr.jungle.util.Either;
import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.Error;
import org.junit.Assert;
import org.junit.Test;

import java.nio.ByteBuffer;
import java.util.Optional;


public class ParentIndexPutTest {
    private ParentIndex parentIndex;

    @Test
    public void ParentIndexPut() {
        Jungle jungle = new DefaultJungle(null, "hogehoge");
        jungle.createNewTree("tree");
        JungleTree tree = jungle.createNewDifferenceTree("Tree");
        JungleTreeEditor editor = tree.getJungleTreeEditor();
        NodePath path = new DefaultNodePath();
        for (int j = 0; j < 10; j++) { //木の構築
            Either<Error, JungleTreeEditor> either = editor.addNewChildAt(path, 0);
            Assert.assertFalse(either.isA());
            editor = either.b();
            either = editor.putAttribute(path, "key", ByteBuffer.wrap("value".getBytes()));
            Assert.assertFalse(either.isA());
            editor = either.b();
            path = path.add(0);
        }
        Either<Error, JungleTreeEditor> either = editor.success();
        Assert.assertFalse(either.isA());
        editor = either.b();

        parentIndex = tree.getParentIndex();
        TreeNode root = tree.getRootNode();
        checkIndex(root);//indexが張られているかを調べる
        Assert.assertTrue(parentIndex.isEmpty());


        //一回木を更新する
        editor = tree.getJungleTreeEditor();
        path = new DefaultNodePath();
        either = editor.addNewChildAt(path, 0);
        Assert.assertFalse(either.isA());
        editor = either.b();
        either = editor.putAttribute(path, "key", ByteBuffer.wrap("value".getBytes()));
        Assert.assertFalse(either.isA());
        editor = either.b();
        either = editor.success();
        Assert.assertFalse(either.isA());

        //更新後にちゃんとIndexが貼れているかを調べる
        parentIndex = tree.getParentIndex();
        root = tree.getRootNode();
        checkIndex(root);
        Assert.assertTrue(parentIndex.isEmpty());

        Either<Error, JungleTree> oldTreeEither = tree.getOldTree(1);
        Assert.assertFalse(oldTreeEither.isA());
        JungleTree oldTree = oldTreeEither.b(); //過去のTreeのIndexが上書きされてないかを調べる
        ParentIndex oldTreeParentIndex = oldTree.getParentIndex();
        parentIndex = tree.getParentIndex();
        Assert.assertNotEquals(oldTreeParentIndex,parentIndex);
    }

    public void checkIndex(TreeNode currentNode) {
        Children children = currentNode.getChildren();
        for (TreeNode child : children) {
            Optional<TreeNode> parentOp = parentIndex.get(child);
            Assert.assertTrue(parentOp.isPresent());
            TreeNode parent = parentOp.get();
            Assert.assertEquals(parent,currentNode);
            checkIndex(child);
            parentIndex = parentIndex.delete(child);
        }

    }
}