using UnityEngine; using System.Collections.Generic; namespace JungleDB { public class DefaultJungleTreeEditor : JungleTreeEditor { private TransactionManager txManager; private TreeNode root; private TreeEditor editor; private TreeOperationLog log; private NodePath prevPath; public void SetPrevPath (NodePath path) { this.prevPath = path; } 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 _edit(NodePath _path, NodeEditor _e) { Either 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 converter = new InnerConverter (_path); IEnumerable 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); newEditor.SetPrevPath (this.prevPath); return DefaultEither.newB (newEditor); } public Either replaceNewRootNode() { replaceRootNodeAt appendChildAt = new replaceRootNodeAt (); return _edit (new DefaultNodePath(), appendChildAt); } public Either addNewChildAt(NodePath _path, int _pos) { prevPath = _path.add (_pos); AppendChildAt appendChildAt = new AppendChildAt (_pos); return _edit (_path, appendChildAt); } public Either deleteChildAt(NodePath _path, int _pos) { DeleteChildAt deleteChildAt = new DeleteChildAt(_pos); return _edit(_path,deleteChildAt); } public Either putAttribute(NodePath _path, string _key, object _value) { PutAttribute putAttribute = new PutAttribute (_key, _value); return _edit (_path, putAttribute); } public Either putAttribute(NodePath _path, object _value) { PutAttribute putAttribute = new PutAttribute (_value.ToString(), _value); return _edit (_path, putAttribute); } public Either putAttribute(string _key, object _value) { PutAttribute putAttribute = new PutAttribute (_key, _value); return _edit (prevPath, putAttribute); } public Either putAttribute(object _value) { PutAttribute putAttribute = new PutAttribute (_value); return _edit (prevPath, putAttribute); } public Either deleteAttribute(NodePath _path, string _key) { DeleteAttribute deleteAttribute = new DeleteAttribute (_key); return _edit (_path, deleteAttribute); } public Either edit(NodePath _path, NodeEditor _editor) { return _edit(_path,_editor); } public Either commit() { Either either = this.txManager.commit(this.root, this.log); if(either.isA()){ return DefaultEither.newA(either.a()); } TransactionManager newTxManager = either.b(); JungleTreeEditor newTreeEditor = new DefaultJungleTreeEditor(this.root, newTxManager, this.editor); return DefaultEither.newB(newTreeEditor); } public Either flushSuccess() { return commit(); } public class InnerConverter : IterableConverter.Converter{ NodePath path; public InnerConverter(NodePath _path) { path = _path; } public TreeOperation conv(NodeOperation _b){ return new DefaultTreeOperation(path,_b); } } } }