view src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/transaction/DefaultJungleTreeEditor.java @ 7:cd5f2ba0f894 default tip

modify jungleTest
author tatsuki
date Thu, 01 Dec 2016 16:23:13 +0900
parents 44465893e8b8
children
line wrap: on
line source

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

import jp.ac.u_ryukyu.ie.cr.jungle.JungleTreeEditor;
import jp.ac.u_ryukyu.ie.cr.jungle.store.NodePath;
import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeEditor;
import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultNodePath;
import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNode;
import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.logger.DefaultTreeOperationLog;
import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.logger.LoggingNode;
import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.logger.OperationLog;
import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.logger.TreeOperationLog;
import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.DefaultTreeOperation;
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.store.trasnformer.*;
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;
import jp.ac.u_ryukyu.ie.cr.jungle.util.IterableConverter;

import java.nio.ByteBuffer;

public class DefaultJungleTreeEditor implements JungleTreeEditor
{
	private final TransactionManager txManager;
	private final TreeNode root;
	private final TreeEditor editor;
	private final TreeOperationLog log;
	

	public DefaultJungleTreeEditor(TreeNode _root,TransactionManager _txManager,TreeEditor _editor)
	{
		this(_root,_txManager,_editor,new DefaultTreeOperationLog());
	}
	
	
	
	public DefaultJungleTreeEditor(TreeNode newNode,TransactionManager _txManager,TreeEditor _editor,TreeOperationLog _log)
	{
		this.root = newNode;
		this.txManager = _txManager;
		this.editor = _editor;
		this.log = _log;
	}
	
	private Either<Error,JungleTreeEditor> _edit(final NodePath _path,NodeEditor _e)
	{
		Either<jp.ac.u_ryukyu.ie.cr.jungle.util.Error,LoggingNode> either = editor.edit(root,_path,_e);
		if(either.isA()){
			return DefaultEither.newA(either.a());
		}
		
		LoggingNode newLogging = either.b();
		OperationLog newLog = newLogging.getOperationLog();
		TreeNode newNode = newLogging.getWrap();
		
		IterableConverter.Converter<TreeOperation,NodeOperation> converter = new IterableConverter.Converter<TreeOperation,NodeOperation>(){
			@Override
			public TreeOperation conv(NodeOperation _b){
				return new DefaultTreeOperation(_path,_b);
			}
		};
		
		Iterable<TreeOperation> iterable = new IterableConverter<>(newLog,converter);
		DefaultTreeOperationLog treeOperationLog = new DefaultTreeOperationLog(iterable,newLog.length());
		TreeOperationLog newTreeOpLog = log.append(treeOperationLog);
		
		JungleTreeEditor newEditor = new DefaultJungleTreeEditor(newNode,txManager,editor,newTreeOpLog);
		return DefaultEither.newB(newEditor);
	}
	
	 @Override
	  public Either<Error,JungleTreeEditor> replaceNewRootNode()
	  {
	   replaceRootNodeAt appendChildAt = new replaceRootNodeAt();
	    return _edit(new DefaultNodePath(),appendChildAt);
	  }
	
	@Override
	public Either<Error,JungleTreeEditor> addNewChildAt(NodePath _path, int _pos)
	{
		AppendChildAt appendChildAt = new AppendChildAt(_pos);
		return _edit(_path,appendChildAt);
	}

	@Override
	public Either<Error,JungleTreeEditor> deleteChildAt(NodePath _path, int _pos)
	{
		DeleteChildAt deleteChildAt = new DeleteChildAt(_pos);
		return _edit(_path,deleteChildAt);
	}

	@Override
	public Either<Error,JungleTreeEditor> putAttribute(NodePath _path,String _key,ByteBuffer _value)
	{
		PutAttribute putAttribute = new PutAttribute(_key,_value);
		return _edit(_path,putAttribute);
	}

	@Override
	public Either<Error,JungleTreeEditor> deleteAttribute(NodePath _path, String _key)
	{
		DeleteAttribute deleteAttribute = new DeleteAttribute(_key);
		return _edit(_path,deleteAttribute);
	}

	@Override
	public Either<Error,JungleTreeEditor> edit(NodePath _path,NodeEditor _editor)
	{
		return _edit(_path,_editor);
	}

	@Override
	public Either<Error,JungleTreeEditor> success()
	{
		Either<Error,TransactionManager> either = txManager.commit(root,log);
		if(either.isA()){
			return DefaultEither.newA(either.a());
		}
		
		TransactionManager newTxManager = either.b();
		JungleTreeEditor newTreeEditor = new DefaultJungleTreeEditor(root,newTxManager,editor);
		
		return DefaultEither.newB(newTreeEditor);
	}

    @Override
    public Either<Error, JungleTreeEditor> flushSuccess() {
        return success();
    }


}