using UnityEngine; using System.Collections; using System.Collections.Generic; 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 TreeEditor(NodePath path, NodeEditor editor) { //LoggingNodeHook hook = new LoggingNodeHook(_e); Either either = this.Editor.edit(this.Root, path, editor); if(either.isA()){ return DefaultEither.newA(either.a()); } TreeNode newNode = either.b().getWrap(); OperationLog newLog = either.b().getOperationLog(); // IterableConverter.Converter converter = new IterableConverter.Converter(){ // public TreeOperation conv(NodeOperation _b){ // return new DefaultTreeOperation(_path,_b); // } // }; // I must fix this code. IterableConverter.Converter converter = new InnerConverter(path); IEnumerable iterable = new IterableConverter(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.newB(newEditor); } public Either addNewChildAt(NodePath _path, int _pos) { AppendChildAt appendChildAt = new AppendChildAt(_pos); return this.TreeEditor(_path,appendChildAt); } public Either deleteChildAt(NodePath _path, int _pos) { DeleteChildAt deleteChildAt = new DeleteChildAt(_pos); return this.TreeEditor(_path,deleteChildAt); } public Either putAttribute(NodePath _path, string _key, byte[] _value) { PutAttribute putAttribute = new PutAttribute(_key,_value); return this.TreeEditor(_path,putAttribute); } public Either deleteAttribute(NodePath _path, string _key) { DeleteAttribute deleteAttribute = new DeleteAttribute(_key); return this.TreeEditor(_path,deleteAttribute); } public Either edit(NodePath _path, NodeEditor _editor) { return this.TreeEditor(_path,_editor); } public Either success() { Either either = TxManager.commit(this.Root, this.Log); if(either.isA()){ return DefaultEither.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.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 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 newLog, string nextRevision) { // NetworkTreeOperationLog netLog = new NetworkTreeOperationLog(_uuid, _treeName,newLog); // CodeSegment cs = new LogPutCodeSegment(netLog); // cs.execute(); } public Either replaceNewRootNode() { // TODO Auto-generated method stub return null; } public Either flushSuccess() { // TODO Auto-generated method stub return null; } public class InnerConverter : IterableConverter.Converter{ NodePath path; public InnerConverter(NodePath _path) { path = _path; } public TreeOperation conv(NodeOperation _b){ return new DefaultTreeOperation(path,_b); } } }