comparison src/main/csharp/jp.ac.u-ryukyu.ie.cr/jungle-network/transaction/NetworkDefaultJungleTreeEditor.cs @ 10:abe0c247f5a5

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