view src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/logger/LoggingChildren.java @ 308:201cc75a9984

change Red Black Tree Edit Path Extends
author tatsuki
date Thu, 26 Jan 2017 15:23:25 +0900
parents 1a5f3d3f3437
children 5b9a3bc593a7
line wrap: on
line source

package jp.ac.u_ryukyu.ie.cr.jungle.store.logger;


import jp.ac.u_ryukyu.ie.cr.jungle.core.Children;
import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.*;
import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode;
import jp.ac.u_ryukyu.ie.cr.jungle.util.DefaultEither;
import jp.ac.u_ryukyu.ie.cr.jungle.util.Either;
import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.Error;

import java.nio.ByteBuffer;

public class LoggingChildren {
    private final TreeNode wrap;
    private final OperationLog log;

    public LoggingChildren(TreeNode _wrap, OperationLog _log) {
        wrap = _wrap;
        log = _log;
    }

    public int size() {
        Children children = wrap.getChildren();
        return children.size();
    }

    public Either<Error, LoggingNode> edit(NodeOperation _op) {
        Either<Error, TreeNode> either = _op.invoke(wrap);
        if (either.isA()) {
            return DefaultEither.newA(either.a());
        }

        TreeNode newWrap = either.b();
        OperationLog newLog = log.add(_op);
        LoggingNode newLoggingNode = new LoggingNode(newWrap, newLog);
        return DefaultEither.newB(newLoggingNode);
    }

    public Either<Error, LoggingNode> addNewChildAt(final int _pos) {
        NodeOperation addNewChildAt = new AppendChildAtOperation(_pos);
        return edit(addNewChildAt);
    }


    public Either<Error, LoggingNode> addNewChildAndPutAttribute(int pos, String key, ByteBuffer value) {
        NodeOperation addnewChildAndPutAttribute = new AddNewChildAndPutAttribute(pos, key, value);
        return edit(addnewChildAndPutAttribute);
    }

    public Either<Error, LoggingNode> deleteChildAt(final int pos) {
        NodeOperation deleteChildAt = new DeleteChildAtOperation(pos);
        return edit(deleteChildAt);
    }


    public Either<Error, LoggingNode> redBlackTreeDeleteChildAt(String key, ByteBuffer value) {
        NodeOperation deleteChildAt = new RedBlackTreeDeleteChildAtOperation(key, value);
        return edit(deleteChildAt);

    }

    public Either<Error, LoggingNode> moveChild(String move, int childNum) {
        NodeOperation moveChild = new ChildMoveOperation(move, childNum);
        return edit(moveChild);
    }

    public Either<Error, LoggingNode> at(int _pos) {
        Children children = wrap.getChildren();
        Either<Error, TreeNode> either = children.at(_pos);
        if (either.isA()) {
            return DefaultEither.newA(either.a());
        }

        TreeNode node = either.b();
        LoggingNode logNode = new LoggingNode(node);
        return DefaultEither.newB(logNode);
    }

}