20
|
1 using UnityEngine;
|
|
2 using System.Collections.Generic;
|
|
3
|
|
4 namespace JungleDB {
|
|
5 public class DefaultJungleTreeEditor : JungleTreeEditor {
|
|
6
|
|
7 private TransactionManager txManager;
|
|
8 private TreeNode root;
|
|
9 private TreeEditor editor;
|
|
10 private TreeOperationLog log;
|
|
11
|
|
12 public DefaultJungleTreeEditor(TreeNode _root,TransactionManager _txManager,TreeEditor _editor)
|
|
13 : this(_root, _txManager, _editor, new DefaultTreeOperationLog())
|
|
14 {
|
|
15 }
|
|
16
|
|
17
|
|
18
|
|
19 public DefaultJungleTreeEditor(TreeNode newNode,TransactionManager txManager,TreeEditor editor,TreeOperationLog log)
|
|
20 {
|
|
21 this.root = newNode;
|
|
22 this.txManager = txManager;
|
|
23 this.editor = editor;
|
|
24 this.log = log;
|
|
25 }
|
|
26
|
|
27
|
|
28
|
|
29 private Either<Error,JungleTreeEditor> _edit(NodePath _path,NodeEditor _e)
|
|
30 {
|
|
31 Either<Error, LoggingNode> either = editor.edit (root, _path, _e);
|
|
32 if (either.isA ()) {
|
|
33 return DefaultEither<Error, JungleTreeEditor>.newA (either.a ());
|
|
34 }
|
|
35
|
|
36 LoggingNode newLogging = either.b ();
|
|
37 OperationLog newLog = newLogging.getOperationLog ();
|
|
38 TreeNode newNode = newLogging.getWrap ();
|
|
39
|
|
40 IterableConverter<TreeOperation,NodeOperation>.Converter<TreeOperation, NodeOperation> converter = new InnerConverter (_path);
|
|
41
|
|
42
|
|
43 IEnumerable<TreeOperation> iterable = new IterableConverter<TreeOperation, NodeOperation> (newLog, converter);
|
|
44 DefaultTreeOperationLog treeOperationLog = new DefaultTreeOperationLog (iterable, newLog.length ());
|
|
45 TreeOperationLog newTreeOpLog = log.append (treeOperationLog);
|
|
46
|
|
47 JungleTreeEditor newEditor = new DefaultJungleTreeEditor (newNode, txManager, editor, newTreeOpLog);
|
|
48 return DefaultEither<Error, JungleTreeEditor>.newB (newEditor);
|
|
49
|
|
50 }
|
|
51
|
|
52
|
|
53 public Either<Error, JungleTreeEditor> replaceNewRootNode() {
|
|
54 replaceRootNodeAt appendChildAt = new replaceRootNodeAt ();
|
|
55 return _edit (new DefaultNodePath(), appendChildAt);
|
|
56 }
|
|
57
|
|
58 public Either<Error, JungleTreeEditor> addNewChildAt(NodePath _path, int _pos) {
|
|
59 AppendChildAt appendChildAt = new AppendChildAt (_pos);
|
|
60 return _edit (_path, appendChildAt);
|
|
61 }
|
|
62
|
|
63 public Either<Error,JungleTreeEditor> deleteChildAt(NodePath _path, int _pos) {
|
|
64 DeleteChildAt deleteChildAt = new DeleteChildAt(_pos);
|
|
65 return _edit(_path,deleteChildAt);
|
|
66 }
|
|
67
|
|
68 public Either<Error, JungleTreeEditor> putAttribute(NodePath _path, string _key, byte[] _value) {
|
|
69 PutAttribute putAttribute = new PutAttribute (_key, _value);
|
|
70 return _edit (_path, putAttribute);
|
|
71 }
|
|
72
|
|
73 public Either<Error, JungleTreeEditor> deleteAttribute(NodePath _path, string _key) {
|
|
74 DeleteAttribute deleteAttribute = new DeleteAttribute (_key);
|
|
75 return _edit (_path, deleteAttribute);
|
|
76 }
|
|
77
|
|
78 public Either<Error,JungleTreeEditor> edit(NodePath _path,NodeEditor _editor) {
|
|
79 return _edit(_path,_editor);
|
|
80 }
|
|
81
|
|
82 /// <summary>
|
|
83 /// Treeを変更したあとSuccess(push)を行う
|
|
84 /// </summary>
|
|
85
|
|
86 public Either<Error,JungleTreeEditor> commit() {
|
|
87 Either<Error,TransactionManager> either = this.txManager.commit(this.root, this.log);
|
|
88 // このlogをサーバにpushする?
|
|
89 if(either.isA()){
|
|
90 return DefaultEither<Error, JungleTreeEditor>.newA(either.a());
|
|
91 }
|
|
92
|
|
93 TransactionManager newTxManager = either.b();
|
|
94 JungleTreeEditor newTreeEditor = new DefaultJungleTreeEditor(this.root, newTxManager, this.editor);
|
|
95
|
|
96 return DefaultEither<Error, JungleTreeEditor>.newB(newTreeEditor);
|
|
97 }
|
|
98
|
|
99 public Either<Error, JungleTreeEditor> flushSuccess() {
|
|
100 return commit();
|
|
101 }
|
|
102
|
|
103 public class InnerConverter : IterableConverter<TreeOperation,NodeOperation>.Converter<TreeOperation,NodeOperation>{
|
|
104
|
|
105 NodePath path;
|
|
106
|
|
107 public InnerConverter(NodePath _path) {
|
|
108 path = _path;
|
|
109 }
|
|
110
|
|
111
|
|
112 public TreeOperation conv(NodeOperation _b){
|
|
113 return new DefaultTreeOperation(path,_b);
|
|
114 }
|
|
115 }
|
|
116 }
|
|
117 }
|