view Main/jungle-network/transaction/NetworkDefaultJungleTreeEditor.cs @ 20:1f99e150f336

fix folder and add Object Mapper.
author Kazuma Takeda
date Thu, 15 Dec 2016 22:52:48 +0900
parents
children f7616084d3ab
line wrap: on
line source

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace JungleDB {
	public class NetworkDefaultJungleTreeEditor : JungleTreeEditor {
		private readonly TransactionManager TxManager;
		private readonly TreeNode Root;
		private readonly TreeEditor Editor;
		private readonly string TreeName;
		private readonly TreeOperationLog Log;
		private bool ExportLog;

		public NetworkDefaultJungleTreeEditor(string tname, TreeNode root, TransactionManager txMan, TreeEditor edit) 
		{
			this.TreeName = tname;
			this.Root = root;
			this.TxManager = txMan;
			this.Editor = edit;
			this.Log = new DefaultTreeOperationLog();
			this.ExportLog = true;
		}

		public NetworkDefaultJungleTreeEditor(string tname, TreeNode root, TransactionManager txMan, TreeEditor edit, TreeOperationLog log) {
			this.TreeName = tname;
			this.Root = root;
			this.TxManager = txMan;
			this.Editor = edit;
			this.Log = log;
			this.ExportLog = true;
		}

		public static NetworkDefaultJungleTreeEditor NewLocalTreeEditor(string tname, TreeNode root, TransactionManager txMan, TreeEditor edit) {
			NetworkDefaultJungleTreeEditor treeEditor = new NetworkDefaultJungleTreeEditor(tname, root, txMan, edit, new DefaultTreeOperationLog());
			treeEditor.ExportLog = false;
			return treeEditor;
		}

		public static NetworkDefaultJungleTreeEditor NewLocalTreeEditor(string tname, TreeNode root, TransactionManager txMan, TreeEditor edit, TreeOperationLog log) {
			NetworkDefaultJungleTreeEditor treeEditor = new NetworkDefaultJungleTreeEditor(tname, root, txMan, edit, log);
			treeEditor.ExportLog = false;
			return treeEditor;
		}


		private Either<Error,JungleTreeEditor> TreeEditor(NodePath path, NodeEditor editor)
		{
			//LoggingNodeHook hook = new LoggingNodeHook(_e);
			Either<Error,LoggingNode> either = this.Editor.edit(this.Root, path, editor);
			if(either.isA()){
				return DefaultEither<Error, JungleTreeEditor>.newA(either.a());
			}

			TreeNode newNode = either.b().getWrap();
			OperationLog newLog = either.b().getOperationLog();

	//		IterableConverter.Converter<TreeOperation,NodeOperation> converter = new IterableConverter.Converter<TreeOperation,NodeOperation>(){
	//		public TreeOperation conv(NodeOperation _b){
	//				return new DefaultTreeOperation(_path,_b);
	//			}
	//		};

			// I must fix this code.
			IterableConverter<TreeOperation,NodeOperation>.Converter<TreeOperation,NodeOperation> converter = new InnerConverter(path);

			IEnumerable<TreeOperation> iterable = new IterableConverter<TreeOperation,NodeOperation>(newLog, converter);
			DefaultTreeOperationLog treeOperationLog = new DefaultTreeOperationLog(iterable,newLog.length());
			TreeOperationLog newTreeOpLog = Log.append(treeOperationLog);

			JungleTreeEditor newEditor;
			if(this.ExportLog) {
				newEditor = new NetworkDefaultJungleTreeEditor(this.TreeName, newNode, this.TxManager, this.Editor, newTreeOpLog);
			} else {
				newEditor = NetworkDefaultJungleTreeEditor.NewLocalTreeEditor(this.TreeName, newNode, TxManager, this.Editor, newTreeOpLog);
			}
			return DefaultEither<Error, JungleTreeEditor>.newB(newEditor);
		}


		public Either<Error,JungleTreeEditor> addNewChildAt(NodePath _path, int _pos)
		{
			AppendChildAt appendChildAt = new AppendChildAt(_pos);
			return this.TreeEditor(_path,appendChildAt);
		}

		public Either<Error,JungleTreeEditor> deleteChildAt(NodePath _path, int _pos)
		{
			DeleteChildAt deleteChildAt = new DeleteChildAt(_pos);
			return this.TreeEditor(_path,deleteChildAt);
		}

		public Either<Error,JungleTreeEditor> putAttribute(NodePath _path, string _key, byte[] _value)
		{
			PutAttribute putAttribute = new PutAttribute(_key,_value);
			return this.TreeEditor(_path,putAttribute);
		}

		public Either<Error,JungleTreeEditor> deleteAttribute(NodePath _path, string _key)
		{
			DeleteAttribute deleteAttribute = new DeleteAttribute(_key);
			return this.TreeEditor(_path,deleteAttribute);
		}

		public Either<Error,JungleTreeEditor> edit(NodePath _path, NodeEditor _editor)
		{
			return this.TreeEditor(_path,_editor);
		}

		public Either<Error,JungleTreeEditor> commit()
		{
			Either<Error,TransactionManager> either = TxManager.commit(this.Root, this.Log);
			if(either.isA()){
				return DefaultEither<Error, JungleTreeEditor>.newA(either.a());
			}
	//		if(this.ExportLog) {
	//			try {
	//				putTreeOperationLog(this.Log);
	//			} catch (IOException e) {
	//				return DefaultEither.newA(either.a());
	//			}	
	//		}

			TransactionManager newTxManager = either.b();
			JungleTreeEditor newTreeEditor = new NetworkDefaultJungleTreeEditor(this.TreeName, this.Root, newTxManager, this.Editor);

			return DefaultEither<Error, JungleTreeEditor>.newB(newTreeEditor);
		}

		private string getID()
		{
			return this.TxManager.getUUID();
		}

		private string getRevision()
		{
			return this.TxManager.getRevision().ToString();
		}

		public string getTreeName() {
			return this.TreeName;
		}

		public TreeOperationLog getTreeOperationLog() {
			return this.Log;
		}

		public void putTreeOperationLog(IEnumerable<TreeOperation> newLog) {
			string uuid = getID();
			string treeName = getTreeName();
			string updaterName = getID();
			string revision = getRevision();
			putDataSegment(uuid, treeName, updaterName, newLog, revision);
		}

		public void putDataSegment(string _uuid, string _treeName, string _updaterName, IEnumerable<TreeOperation> newLog, string nextRevision) {
			// NetworkTreeOperationLog netLog = new NetworkTreeOperationLog(_uuid, _treeName,newLog);
	//		CodeSegment cs = new LogPutCodeSegment(netLog);
	//		cs.execute();
		}

		public Either<Error, JungleTreeEditor> replaceNewRootNode() {
			// TODO Auto-generated method stub
			return null;
		}

		public Either<Error, JungleTreeEditor> flushSuccess() {
			// TODO Auto-generated method stub
			return null;
		}

		public class InnerConverter : IterableConverter<TreeOperation,NodeOperation>.Converter<TreeOperation,NodeOperation>{
			NodePath path;

			public InnerConverter(NodePath _path) {
				path = _path;
			}


			public TreeOperation conv(NodeOperation _b){
				return new DefaultTreeOperation(path,_b);
			}
		}
	}
}