view Main/jungle-main/store/impl/logger/LoggingNode.cs @ 35:f2ea780b3e80

fix
author Kazuma Takeda
date Wed, 22 Feb 2017 16:30:19 +0900
parents 9588ad364fdd
children
line wrap: on
line source

using System.Collections;

namespace JungleDB {
	public class LoggingNode {
		private TreeNode wrap;
		private OperationLog log;

		public LoggingNode(TreeNode _wrap)
			: this(_wrap,new DefaultOperationLog())
		{
		}

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

		public LoggingAttributes getAttributes()
		{
			return new LoggingAttributes(wrap,log);
		}

		public LoggingChildren getChildren()
		{
			return new LoggingChildren(wrap,log);
		}


		public OperationLog getOperationLog()
		{
			return log;
		}

		public Either<Error, LoggingNode> replaceNewRootNode() {
			NodeOperation replaceRootNode = new ReplaceRootNodeOperation();
			return edit(replaceRootNode);
		}

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

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

		public TreeNode getWrap()
		{
			return wrap;
		}
	}
}