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;
|
28
|
11 private NodePath prevPath;
|
20
|
12
|
30
|
13 public void SetPrevPath (NodePath path) {
|
|
14 this.prevPath = path;
|
|
15 }
|
|
16
|
20
|
17 public DefaultJungleTreeEditor(TreeNode _root,TransactionManager _txManager,TreeEditor _editor)
|
|
18 : this(_root, _txManager, _editor, new DefaultTreeOperationLog())
|
|
19 {
|
|
20 }
|
|
21
|
|
22
|
|
23
|
30
|
24 public DefaultJungleTreeEditor(TreeNode newNode, TransactionManager txManager,TreeEditor editor,TreeOperationLog log)
|
20
|
25 {
|
|
26 this.root = newNode;
|
|
27 this.txManager = txManager;
|
|
28 this.editor = editor;
|
|
29 this.log = log;
|
|
30 }
|
|
31
|
|
32
|
|
33
|
28
|
34 private Either<Error,JungleTreeEditor> _edit(NodePath _path, NodeEditor _e)
|
20
|
35 {
|
|
36 Either<Error, LoggingNode> either = editor.edit (root, _path, _e);
|
30
|
37 if (either.isA ())
|
20
|
38 return DefaultEither<Error, JungleTreeEditor>.newA (either.a ());
|
|
39
|
|
40 LoggingNode newLogging = either.b ();
|
|
41 OperationLog newLog = newLogging.getOperationLog ();
|
|
42 TreeNode newNode = newLogging.getWrap ();
|
|
43
|
|
44 IterableConverter<TreeOperation,NodeOperation>.Converter<TreeOperation, NodeOperation> converter = new InnerConverter (_path);
|
30
|
45
|
20
|
46
|
|
47 IEnumerable<TreeOperation> iterable = new IterableConverter<TreeOperation, NodeOperation> (newLog, converter);
|
|
48 DefaultTreeOperationLog treeOperationLog = new DefaultTreeOperationLog (iterable, newLog.length ());
|
|
49 TreeOperationLog newTreeOpLog = log.append (treeOperationLog);
|
|
50
|
|
51 JungleTreeEditor newEditor = new DefaultJungleTreeEditor (newNode, txManager, editor, newTreeOpLog);
|
30
|
52 newEditor.SetPrevPath (this.prevPath);
|
20
|
53 return DefaultEither<Error, JungleTreeEditor>.newB (newEditor);
|
|
54
|
|
55 }
|
|
56
|
|
57
|
|
58 public Either<Error, JungleTreeEditor> replaceNewRootNode() {
|
|
59 replaceRootNodeAt appendChildAt = new replaceRootNodeAt ();
|
|
60 return _edit (new DefaultNodePath(), appendChildAt);
|
|
61 }
|
|
62
|
|
63 public Either<Error, JungleTreeEditor> addNewChildAt(NodePath _path, int _pos) {
|
29
|
64 prevPath = _path.add (_pos);
|
20
|
65 AppendChildAt appendChildAt = new AppendChildAt (_pos);
|
|
66 return _edit (_path, appendChildAt);
|
|
67 }
|
|
68
|
|
69 public Either<Error,JungleTreeEditor> deleteChildAt(NodePath _path, int _pos) {
|
|
70 DeleteChildAt deleteChildAt = new DeleteChildAt(_pos);
|
|
71 return _edit(_path,deleteChildAt);
|
|
72 }
|
29
|
73
|
20
|
74 public Either<Error, JungleTreeEditor> putAttribute(NodePath _path, string _key, byte[] _value) {
|
|
75 PutAttribute putAttribute = new PutAttribute (_key, _value);
|
|
76 return _edit (_path, putAttribute);
|
|
77 }
|
|
78
|
29
|
79 /// <summary>
|
|
80 /// When use path, before create path.
|
|
81 /// * Problem -> copy to path from root everytime.
|
|
82 /// </summary>
|
|
83 /// <returns>The attribute.</returns>
|
|
84 /// <param name="_key">Key.</param>
|
|
85 /// <param name="_value">Value.</param>
|
|
86 public Either<Error, JungleTreeEditor> putAttribute(string _key, byte[] _value) {
|
|
87 PutAttribute putAttribute = new PutAttribute (_key, _value);
|
|
88 return _edit (prevPath, putAttribute);
|
|
89 }
|
|
90
|
20
|
91 public Either<Error, JungleTreeEditor> deleteAttribute(NodePath _path, string _key) {
|
|
92 DeleteAttribute deleteAttribute = new DeleteAttribute (_key);
|
|
93 return _edit (_path, deleteAttribute);
|
|
94 }
|
|
95
|
29
|
96 public Either<Error,JungleTreeEditor> edit(NodePath _path, NodeEditor _editor) {
|
20
|
97 return _edit(_path,_editor);
|
|
98 }
|
28
|
99
|
20
|
100 public Either<Error,JungleTreeEditor> commit() {
|
|
101 Either<Error,TransactionManager> either = this.txManager.commit(this.root, this.log);
|
|
102 // このlogをサーバにpushする?
|
|
103 if(either.isA()){
|
|
104 return DefaultEither<Error, JungleTreeEditor>.newA(either.a());
|
|
105 }
|
|
106
|
|
107 TransactionManager newTxManager = either.b();
|
|
108 JungleTreeEditor newTreeEditor = new DefaultJungleTreeEditor(this.root, newTxManager, this.editor);
|
|
109
|
|
110 return DefaultEither<Error, JungleTreeEditor>.newB(newTreeEditor);
|
|
111 }
|
|
112
|
|
113 public Either<Error, JungleTreeEditor> flushSuccess() {
|
|
114 return commit();
|
|
115 }
|
|
116
|
|
117 public class InnerConverter : IterableConverter<TreeOperation,NodeOperation>.Converter<TreeOperation,NodeOperation>{
|
|
118
|
|
119 NodePath path;
|
|
120
|
|
121 public InnerConverter(NodePath _path) {
|
|
122 path = _path;
|
|
123 }
|
|
124
|
|
125
|
|
126 public TreeOperation conv(NodeOperation _b){
|
|
127 return new DefaultTreeOperation(path,_b);
|
|
128 }
|
|
129 }
|
|
130 }
|
|
131 }
|