view src/test/java/jp/ac/u_ryukyu/ie/cr/jungle/index/defaultTree/IndexTest.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.data.treemap.TreeMap;
import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.nodeiterator.DefaultNodeIterator;
import jp.ac.u_ryukyu.ie.cr.jungle.store.index.Index;
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.transaction.node.TreeNodeAttributes;
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.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

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


public class IndexTest {

    @Test
    public void IndexTests() {
        String key = "key";
        String indexKey = "indexKey";
        String addAttributeKey = "addAttributeKey";
        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, 4, path);
        TreeNode oldTreeRoot = tree.getRootNode();
        Index index = tree.getIndex();
        DefaultNodeIterator iterator = new DefaultNodeIterator(oldTreeRoot);
        while (iterator.hasNext()) {
            TreeNode node = iterator.next();
            TreeNodeAttributes attribute = node.getAttributes();
            Iterator<String> keys = attribute.getKeys();
            while (keys.hasNext()) {       //作った木にちゃんとIndexが張られているかを調べる
                String attributeKey = keys.next();
                String value = attribute.getString(attributeKey);
                Iterator<TreeNode> indexNode = index.get(attributeKey, value);
                Assert.assertTrue(indexNode.hasNext());
                Assert.assertEquals(indexNode.next(),node);
            }
        }

        JungleTreeEditor editor2 = tree.getJungleTreeEditor();
        path = path.add(0).add(0).add(2);
        Either<Error, JungleTreeEditor> either = editor2.putAttribute(path, addAttributeKey, ByteBuffer.wrap("value1".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("value2".getBytes()));
        Assert.assertFalse(either2.isA());
        JungleTreeEditor editor4 = either2.b();
        Either<Error, JungleTreeEditor> either3 = editor4.success();
        Assert.assertFalse(either3.isA());

        TreeNode newTreeRoot = tree.getRootNode();
        Index newIndex = tree.getIndex();
        iterator = new DefaultNodeIterator(newTreeRoot);
        while (iterator.hasNext()) { //木を編集した際に差分でIndexがアップデートされているかを調べる
            TreeNode node = iterator.next();
            TreeNodeAttributes attribute = node.getAttributes();
            Iterator<String> keys = attribute.getKeys();
            while (keys.hasNext()) {
                String attributeKey = keys.next();
                String value = attribute.getString(attributeKey);
                Iterator<TreeNode> indexNode = newIndex.get(attributeKey, value);
                Assert.assertTrue(indexNode.hasNext());
                Assert.assertEquals(indexNode.next(),node);
            }
            newIndex = newIndex.delete(node);
        }


        TreeMap<String, TreeMap<String, jp.ac.u_ryukyu.ie.cr.jungle.data.list.List<TreeNode>>>  indexList = newIndex.getIndexList(); //差分で更新した際不要な値がIndexに残っていないかを調べる
        Iterator<String> indexKeys = indexList.keys();
        while(indexKeys.hasNext()){
            String indexKeys2 = indexKeys.next();
            Optional<TreeMap<String, jp.ac.u_ryukyu.ie.cr.jungle.data.list.List<TreeNode>>> treeMapIndexOp = indexList.get(indexKeys2);
            Assert.assertTrue(treeMapIndexOp.isPresent());
            TreeMap<String, jp.ac.u_ryukyu.ie.cr.jungle.data.list.List<TreeNode>> treeMapIndex = treeMapIndexOp.get();
            Iterator<String> treeMapIndexKeys = treeMapIndex.keys();
            while (treeMapIndexKeys.hasNext()) {
                String treeMapIndexkey = treeMapIndexKeys.next();
                Optional<jp.ac.u_ryukyu.ie.cr.jungle.data.list.List<TreeNode>> nodeListOp = treeMapIndex.get(treeMapIndexkey);
                Assert.assertTrue(nodeListOp.isPresent());
                jp.ac.u_ryukyu.ie.cr.jungle.data.list.List<TreeNode> nodeList = nodeListOp.get();
                Assert.assertEquals(nodeList.length(),0);
            }
        }

        //編集する際に新しい木に残らないノードがIndexに混ざってないかをしらべる
        List<TreeNode> deletedNodeList = new LinkedList<>();
        deletedNodeList = getDeletedNodeList(deletedNodeList, oldTreeRoot, path);
        deletedNodeList = getDeletedNodeList(deletedNodeList, oldTreeRoot, path2);
        newIndex = tree.getIndex();
        for (TreeNode deletedNode : deletedNodeList) {
            TreeNodeAttributes attribute = deletedNode.getAttributes();
            Iterator<String> keys = attribute.getKeys();
            while (keys.hasNext()) {
                String attributeKey = keys.next();
                String value = attribute.getString(key);
                Iterator<TreeNode> newIndexNode = newIndex.get(attributeKey, value);
                Iterator<TreeNode> oldIndexNode = index.get(attributeKey, value);
                Assert.assertTrue(newIndexNode.hasNext());
                Assert.assertTrue(oldIndexNode.hasNext());
                Assert.assertNotEquals(newIndexNode.next(),oldIndexNode.next());
                Assert.assertFalse(newIndexNode.hasNext());
                Assert.assertFalse(oldIndexNode.hasNext());
            }
        }
    }

    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;
    }


}