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

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

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

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.query.traverser.nodeiterator.DefaultNodeIterator;
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.LinkedList;
import java.util.List;
import java.util.Optional;

import static jp.ac.u_ryukyu.ie.cr.jungle.CreateTreeMethod.createTree;


/**
 * Created by e115731 on 15/05/06.
 */
public class ParentIndexPutTest {
    private String key = "key";
    private String indexKey = "indexKey";
    private String addAttributeKey = "addAttributeKey";
    @Test
    public void ParentIndexPut() {
        Jungle jungle = new DefaultJungle(null, "hogehoge");
        jungle.createNewTree("tree");
        JungleTree tree = jungle.getTreeByName("tree");
        JungleTreeEditor editor = tree.getJungleTreeEditor();
        NodePath path = new DefaultNodePath();
        createTree(editor,key,indexKey,3,path);
        TreeNode oldTreeRoot = tree.getRootNode();
        ParentIndex parentIndex = tree.getParentIndex();
        DefaultNodeIterator iterator = new DefaultNodeIterator(oldTreeRoot);
        while(iterator.hasNext()){
            TreeNode parent = iterator.next();
            Children children = parent.getChildren();
            for (TreeNode child : children) {
                Optional<TreeNode> parentOp = parentIndex.get(child);
                Assert.assertTrue(parentOp.isPresent());
                TreeNode parentIndexGetNode = parentOp.get();
                Assert.assertEquals(parent,parentIndexGetNode);
            }
       }

        JungleTreeEditor editor2 = tree.getJungleTreeEditor();
        path = path.add(0).add(0).add(2);
        Either<Error, JungleTreeEditor> either = editor2.putAttribute(path,addAttributeKey, ByteBuffer.wrap("value".getBytes()) );
        Assert.assertFalse(either.isA());
        JungleTreeEditor editor3 = either.b();
        NodePath path2 = new DefaultNodePath();
        path2 = path2.add(1).add(1).add(2);
        Either<Error, JungleTreeEditor> either2 = editor3.putAttribute(path2,addAttributeKey, ByteBuffer.wrap("value".getBytes()) );
        Assert.assertFalse(either2.isA());
        JungleTreeEditor editor4 = either2.b();
        Either<Error, JungleTreeEditor> either3 = editor4.success();
        Assert.assertFalse(either3.isA());

        TreeNode newTreeRoot = tree.getRootNode();
        ParentIndex newTreeParentIndex = tree.getParentIndex();
        iterator = new DefaultNodeIterator(newTreeRoot);
        while(iterator.hasNext()){
            TreeNode parent = iterator.next();
            Children children = parent.getChildren();
            for (TreeNode child : children) {
                Optional<TreeNode> parentOp = newTreeParentIndex.get(child);
                Assert.assertTrue(parentOp.isPresent());
                TreeNode parentIndexGetNode = parentOp.get();
                Assert.assertEquals(parent,parentIndexGetNode);
            }
        }
        List<TreeNode> deletedNodeList = new LinkedList<>();
        deletedNodeList = getDeletedNodeList(deletedNodeList,oldTreeRoot,path);
        deletedNodeList = getDeletedNodeList(deletedNodeList,oldTreeRoot,path2);

        for (TreeNode deletedNode : deletedNodeList) {
            Optional<TreeNode> optional = newTreeParentIndex.get(deletedNode);
            Assert.assertFalse(optional.isPresent());
        }
    }

    private List<TreeNode> getDeletedNodeList(List<TreeNode> list, TreeNode oldTreeRoot, NodePath path) {
        TreeNode node = oldTreeRoot;
        for (Integer i : path.pop().right()) {
            Children children = node.getChildren();
            Either<Error, TreeNode> either = children.at(i);
            Assert.assertFalse(either.isA());
            node = either.b();
            list.add(node);
        }
        return list;
    }


}