view src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/JungleUpdater.java @ 337:c90d9c64aeda

add space
author suruga
date Tue, 08 Aug 2017 18:15:27 +0900
parents 2a0cb1f0ba4e
children
line wrap: on
line source

package jp.ac.u_ryukyu.ie.cr.jungleNetwork.transaction;


import jp.ac.u_ryukyu.ie.cr.jungle.store.Command;
import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.NodePath;
import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.NodeOperation;
import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.TreeOperation;
import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.jungleTreeEditor.JungleTreeEditor;
import jp.ac.u_ryukyu.ie.cr.jungle.util.Either;
import jp.ac.u_ryukyu.ie.cr.jungle.util.jungleError.Error;

import java.nio.ByteBuffer;

public class JungleUpdater {

    public JungleUpdater() {

    }

    public static Either<Error, JungleTreeEditor> edit(JungleTreeEditor _editor, Iterable<TreeOperation> _log) {
        JungleTreeEditor editor = _editor;
        Either<Error, JungleTreeEditor> either = null;
        for (TreeOperation op : _log) {
            NodePath path = op.getNodePath();
            NodeOperation nodeOp = op.getNodeOperation();
            either = _edit(editor, path, nodeOp, nodeOp.getPosition());
            if (either.isA()) {
                return either;
            }
            editor = either.b();
        }
        return either;
    }

    /*
     * This method decide node position by param.
     */
    public static Either<Error, JungleTreeEditor> edit(JungleTreeEditor _editor, Iterable<TreeOperation> _log, int pos) {
        JungleTreeEditor editor = _editor;
        Either<Error, JungleTreeEditor> either = null;
        for (TreeOperation op : _log) {
            NodePath path = op.getNodePath();
            NodeOperation nodeOp = op.getNodeOperation();
            either = _edit(editor, path, nodeOp, pos);
            if (either.isA()) {
                return either;
            }
            editor = either.b();
        }
        return either;
    }


    private static Either<Error, JungleTreeEditor> _edit(JungleTreeEditor editor, NodePath path, NodeOperation nodeOp, int pos) {//posはmergeの時に使う?
        String key = "";
        Command c = nodeOp.getCommand();
        int num;
        switch (c) {
            case PUT_ATTRIBUTE:
                key = nodeOp.getKey();
                ByteBuffer value = nodeOp.getValue();
                return editor.putAttribute(path, key, value);
            case DELETE_ATTRIBUTE:
                key = nodeOp.getKey();
                return editor.deleteAttribute(path, key);
            case APPEND_CHILD:
                num = nodeOp.getPosition();
                return editor.addNewChildAt(path, num);
            case DELETE_CHILD:
                num = nodeOp.getPosition();
                return editor.deleteChildAt(path, num);
        }
        return null;
    }

}