view src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/logger/LoggingAttributes.java @ 329:2a0cb1f0ba4e

rename Error package
author kono
date Sat, 08 Jul 2017 21:05:55 +0900
parents c62462c28807
children
line wrap: on
line source

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

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.store.operations.DeleteAttributeOperation;
import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.NodeOperation;
import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.PutAttributeOperation;
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.jungleError.Error;

import java.nio.ByteBuffer;

public class LoggingAttributes //implements EditableAttributes
{
	private final TreeNode wrap;
	private final OperationLog log;
	
	public LoggingAttributes(TreeNode _wrap,OperationLog _log)
	{
		wrap = _wrap;
		log = _log;
	}

	public ByteBuffer get(String _key)
	{
		TreeNodeAttributes attributes = wrap.getAttributes();
		return attributes.get(_key);
	}
	
	private Either<Error,LoggingNode> edit(NodeOperation _op)
	{
		Either<Error,TreeNode> either = _op.invoke(wrap);
		if(either.isA()){
			return DefaultEither.newA(either.a());
		}
		
		TreeNode newNode = either.b();
		OperationLog newLog = log.add(_op); 
		LoggingNode newLogNode = new LoggingNode(newNode,newLog);
		
		return DefaultEither.newB(newLogNode);
	}

	public Either<Error,LoggingNode> delete(final String _key)
	{
		
		DeleteAttributeOperation deleteAttribute = new DeleteAttributeOperation(_key);
		return edit(deleteAttribute);
	}

	public Either<Error,LoggingNode> put(final String _key,final ByteBuffer _value)
	{
		PutAttributeOperation putAttribute = new PutAttributeOperation(_key,_value);
		return edit(putAttribute);
	}
}