20
|
1 using UnityEngine;
|
|
2 using System.Collections;
|
|
3 using System.Collections.Generic;
|
|
4
|
|
5 namespace JungleDB {
|
|
6 public class NetworkDefaultJungleTreeEditor : JungleTreeEditor {
|
|
7 private readonly TransactionManager TxManager;
|
|
8 private readonly TreeNode Root;
|
|
9 private readonly TreeEditor Editor;
|
|
10 private readonly string TreeName;
|
|
11 private readonly TreeOperationLog Log;
|
|
12 private bool ExportLog;
|
|
13
|
29
|
14 private NodePath prevPath;
|
|
15
|
20
|
16 public NetworkDefaultJungleTreeEditor(string tname, TreeNode root, TransactionManager txMan, TreeEditor edit)
|
|
17 {
|
|
18 this.TreeName = tname;
|
|
19 this.Root = root;
|
|
20 this.TxManager = txMan;
|
|
21 this.Editor = edit;
|
|
22 this.Log = new DefaultTreeOperationLog();
|
|
23 this.ExportLog = true;
|
|
24 }
|
|
25
|
|
26 public NetworkDefaultJungleTreeEditor(string tname, TreeNode root, TransactionManager txMan, TreeEditor edit, TreeOperationLog log) {
|
|
27 this.TreeName = tname;
|
|
28 this.Root = root;
|
|
29 this.TxManager = txMan;
|
|
30 this.Editor = edit;
|
|
31 this.Log = log;
|
|
32 this.ExportLog = true;
|
|
33 }
|
|
34
|
|
35 public static NetworkDefaultJungleTreeEditor NewLocalTreeEditor(string tname, TreeNode root, TransactionManager txMan, TreeEditor edit) {
|
|
36 NetworkDefaultJungleTreeEditor treeEditor = new NetworkDefaultJungleTreeEditor(tname, root, txMan, edit, new DefaultTreeOperationLog());
|
|
37 treeEditor.ExportLog = false;
|
|
38 return treeEditor;
|
|
39 }
|
|
40
|
|
41 public static NetworkDefaultJungleTreeEditor NewLocalTreeEditor(string tname, TreeNode root, TransactionManager txMan, TreeEditor edit, TreeOperationLog log) {
|
|
42 NetworkDefaultJungleTreeEditor treeEditor = new NetworkDefaultJungleTreeEditor(tname, root, txMan, edit, log);
|
|
43 treeEditor.ExportLog = false;
|
|
44 return treeEditor;
|
|
45 }
|
|
46
|
|
47
|
|
48 private Either<Error,JungleTreeEditor> TreeEditor(NodePath path, NodeEditor editor)
|
|
49 {
|
|
50 //LoggingNodeHook hook = new LoggingNodeHook(_e);
|
|
51 Either<Error,LoggingNode> either = this.Editor.edit(this.Root, path, editor);
|
|
52 if(either.isA()){
|
|
53 return DefaultEither<Error, JungleTreeEditor>.newA(either.a());
|
|
54 }
|
|
55
|
|
56 TreeNode newNode = either.b().getWrap();
|
|
57 OperationLog newLog = either.b().getOperationLog();
|
|
58
|
|
59 // IterableConverter.Converter<TreeOperation,NodeOperation> converter = new IterableConverter.Converter<TreeOperation,NodeOperation>(){
|
|
60 // public TreeOperation conv(NodeOperation _b){
|
|
61 // return new DefaultTreeOperation(_path,_b);
|
|
62 // }
|
|
63 // };
|
|
64
|
|
65 // I must fix this code.
|
|
66 IterableConverter<TreeOperation,NodeOperation>.Converter<TreeOperation,NodeOperation> converter = new InnerConverter(path);
|
|
67
|
|
68 IEnumerable<TreeOperation> iterable = new IterableConverter<TreeOperation,NodeOperation>(newLog, converter);
|
|
69 DefaultTreeOperationLog treeOperationLog = new DefaultTreeOperationLog(iterable,newLog.length());
|
|
70 TreeOperationLog newTreeOpLog = Log.append(treeOperationLog);
|
|
71
|
|
72 JungleTreeEditor newEditor;
|
|
73 if(this.ExportLog) {
|
|
74 newEditor = new NetworkDefaultJungleTreeEditor(this.TreeName, newNode, this.TxManager, this.Editor, newTreeOpLog);
|
|
75 } else {
|
|
76 newEditor = NetworkDefaultJungleTreeEditor.NewLocalTreeEditor(this.TreeName, newNode, TxManager, this.Editor, newTreeOpLog);
|
|
77 }
|
|
78 return DefaultEither<Error, JungleTreeEditor>.newB(newEditor);
|
|
79 }
|
|
80
|
|
81
|
29
|
82 public Either<Error,JungleTreeEditor> addNewChildAt(NodePath _path, int _pos) {
|
|
83 prevPath = _path.add (_pos);
|
20
|
84 AppendChildAt appendChildAt = new AppendChildAt(_pos);
|
|
85 return this.TreeEditor(_path,appendChildAt);
|
|
86 }
|
|
87
|
|
88 public Either<Error,JungleTreeEditor> deleteChildAt(NodePath _path, int _pos)
|
|
89 {
|
|
90 DeleteChildAt deleteChildAt = new DeleteChildAt(_pos);
|
|
91 return this.TreeEditor(_path,deleteChildAt);
|
|
92 }
|
|
93
|
|
94 public Either<Error,JungleTreeEditor> putAttribute(NodePath _path, string _key, byte[] _value)
|
|
95 {
|
|
96 PutAttribute putAttribute = new PutAttribute(_key,_value);
|
|
97 return this.TreeEditor(_path,putAttribute);
|
|
98 }
|
|
99
|
29
|
100 public Either<Error,JungleTreeEditor> putAttribute(string _key, byte[] _value)
|
|
101 {
|
|
102 PutAttribute putAttribute = new PutAttribute(_key,_value);
|
|
103 return this.TreeEditor(prevPath, putAttribute);
|
|
104 }
|
|
105
|
20
|
106 public Either<Error,JungleTreeEditor> deleteAttribute(NodePath _path, string _key)
|
|
107 {
|
|
108 DeleteAttribute deleteAttribute = new DeleteAttribute(_key);
|
|
109 return this.TreeEditor(_path,deleteAttribute);
|
|
110 }
|
|
111
|
|
112 public Either<Error,JungleTreeEditor> edit(NodePath _path, NodeEditor _editor)
|
|
113 {
|
|
114 return this.TreeEditor(_path,_editor);
|
|
115 }
|
|
116
|
|
117 public Either<Error,JungleTreeEditor> commit()
|
|
118 {
|
|
119 Either<Error,TransactionManager> either = TxManager.commit(this.Root, this.Log);
|
|
120 if(either.isA()){
|
|
121 return DefaultEither<Error, JungleTreeEditor>.newA(either.a());
|
|
122 }
|
|
123 // if(this.ExportLog) {
|
|
124 // try {
|
|
125 // putTreeOperationLog(this.Log);
|
|
126 // } catch (IOException e) {
|
|
127 // return DefaultEither.newA(either.a());
|
|
128 // }
|
|
129 // }
|
|
130
|
|
131 TransactionManager newTxManager = either.b();
|
|
132 JungleTreeEditor newTreeEditor = new NetworkDefaultJungleTreeEditor(this.TreeName, this.Root, newTxManager, this.Editor);
|
|
133
|
|
134 return DefaultEither<Error, JungleTreeEditor>.newB(newTreeEditor);
|
|
135 }
|
|
136
|
|
137 private string getID()
|
|
138 {
|
|
139 return this.TxManager.getUUID();
|
|
140 }
|
|
141
|
|
142 private string getRevision()
|
|
143 {
|
|
144 return this.TxManager.getRevision().ToString();
|
|
145 }
|
|
146
|
|
147 public string getTreeName() {
|
|
148 return this.TreeName;
|
|
149 }
|
|
150
|
|
151 public TreeOperationLog getTreeOperationLog() {
|
|
152 return this.Log;
|
|
153 }
|
|
154
|
|
155 public void putTreeOperationLog(IEnumerable<TreeOperation> newLog) {
|
|
156 string uuid = getID();
|
|
157 string treeName = getTreeName();
|
|
158 string updaterName = getID();
|
|
159 string revision = getRevision();
|
|
160 putDataSegment(uuid, treeName, updaterName, newLog, revision);
|
|
161 }
|
|
162
|
|
163 public void putDataSegment(string _uuid, string _treeName, string _updaterName, IEnumerable<TreeOperation> newLog, string nextRevision) {
|
|
164 // NetworkTreeOperationLog netLog = new NetworkTreeOperationLog(_uuid, _treeName,newLog);
|
|
165 // CodeSegment cs = new LogPutCodeSegment(netLog);
|
|
166 // cs.execute();
|
|
167 }
|
|
168
|
|
169 public Either<Error, JungleTreeEditor> replaceNewRootNode() {
|
|
170 // TODO Auto-generated method stub
|
|
171 return null;
|
|
172 }
|
|
173
|
|
174 public Either<Error, JungleTreeEditor> flushSuccess() {
|
|
175 // TODO Auto-generated method stub
|
|
176 return null;
|
|
177 }
|
|
178
|
|
179 public class InnerConverter : IterableConverter<TreeOperation,NodeOperation>.Converter<TreeOperation,NodeOperation>{
|
|
180 NodePath path;
|
|
181
|
|
182 public InnerConverter(NodePath _path) {
|
|
183 path = _path;
|
|
184 }
|
|
185
|
|
186
|
|
187 public TreeOperation conv(NodeOperation _b){
|
|
188 return new DefaultTreeOperation(path,_b);
|
|
189 }
|
|
190 }
|
|
191 }
|
|
192 }
|