38
|
1 package jungle.test.bbs;
|
|
2
|
46
|
3 import java.io.IOException;
|
38
|
4 import java.nio.ByteBuffer;
|
|
5 import java.util.concurrent.atomic.AtomicInteger;
|
39
|
6
|
52
|
7 import alice.jungle.codesegment.HashLogUpdateCodeSegment;
|
39
|
8 import alice.jungle.core.NetworkDefaultJungle;
|
46
|
9 import alice.jungle.datasegment.store.operations.DefaultTreeOperationLogContainer;
|
|
10 import alice.jungle.transaction.NetworkDefaultJungleTreeEditor;
|
38
|
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.DefaultJungle;
|
|
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle;
|
|
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
|
|
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
|
|
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Children;
|
|
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node;
|
|
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
|
|
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor;
|
46
|
19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation;
|
38
|
20 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableNode;
|
|
21 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor;
|
|
22 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser;
|
|
23 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither;
|
|
24 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
|
|
25 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
|
|
26 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableConverter;
|
46
|
27 import jungle.test.bbs.codesegment.NullCodeSegmentForUpdate;
|
38
|
28
|
39
|
29 public class NetworkJungleBulletinBoard implements BulletinBoard
|
38
|
30 {
|
|
31 private final Jungle jungle;
|
|
32
|
43
|
33 public NetworkJungleBulletinBoard(String _serverName)
|
38
|
34 {
|
51
|
35 jungle = new NetworkDefaultJungle(null,"hoge",new DefaultTreeEditor(new DefaultTraverser()), _serverName);
|
38
|
36 jungle.createNewTree("boards");
|
51
|
37 JungleManager.setJungle(jungle);
|
38
|
38 }
|
|
39
|
|
40 public Iterable<String> getBoards()
|
|
41 {
|
|
42 JungleTree tree = jungle.getTreeByName("boards");
|
|
43 Node node = tree.getRootNode();
|
|
44 Children<Node> chs = node.getChildren();
|
|
45
|
|
46 IterableConverter.Converter<String,Node> converter = new IterableConverter.Converter<String,Node>(){
|
|
47 public String conv(Node _b) {
|
|
48 ByteBuffer e = _b.getAttributes().get("name");
|
|
49 return new String(e.array());
|
|
50 }
|
|
51 };
|
|
52
|
|
53 return new IterableConverter<String,Node>(chs,converter);
|
|
54 }
|
|
55
|
|
56 public void createBoards(final String _name,final String _author,final String _initMessage,final String _editKey)
|
|
57 {
|
|
58 if(null == jungle.createNewTree(_name)){
|
|
59 throw new IllegalStateException();
|
|
60 }
|
|
61
|
|
62 JungleTree tree = jungle.getTreeByName("boards");
|
|
63 JungleTreeEditor editor = tree.getTreeEditor();
|
|
64 DefaultNodePath root = new DefaultNodePath();
|
|
65 Either<Error,JungleTreeEditor> either = editor.addNewChildAt(root,0);
|
|
66 if(either.isA()){
|
|
67 throw new IllegalStateException();
|
|
68 }
|
|
69 editor = either.b();
|
|
70
|
|
71 either = editor.putAttribute(root.add(0),"name",ByteBuffer.wrap(_name.getBytes()));
|
|
72 if(either.isA()){
|
|
73 throw new IllegalStateException();
|
|
74 }
|
|
75 editor = either.b();
|
|
76 Either<Error,JungleTreeEditor> result = editor.success();
|
|
77 if(result.isA()){
|
|
78 throw new IllegalStateException();
|
|
79 }
|
46
|
80 /* Put DataSegment */
|
|
81 try {
|
49
|
82 putTreeOperationLog((NetworkDefaultJungleTreeEditor)editor);
|
46
|
83 } catch (IOException e1) {
|
|
84 e1.printStackTrace();
|
|
85 }
|
38
|
86
|
|
87 tree = jungle.getTreeByName(_name);
|
|
88 editor = tree.getTreeEditor();
|
|
89 either = editor.addNewChildAt(root,0);
|
|
90 if(either.isA()){
|
|
91 throw new IllegalStateException();
|
|
92 }
|
|
93 editor = either.b();
|
|
94
|
|
95 NodeEditor e = new NodeEditor(){
|
|
96 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){
|
|
97 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b();
|
|
98 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_initMessage.getBytes())).b();
|
|
99 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b();
|
|
100 return DefaultEither.newB(_e);
|
|
101 }
|
|
102 };
|
|
103
|
|
104 either = editor.edit(root.add(0),e);
|
|
105 if(either.isA()){
|
|
106 throw new IllegalStateException();
|
|
107 }
|
|
108 editor = either.b();
|
|
109 editor.success();
|
46
|
110 /* Put DataSegment */
|
|
111 try {
|
49
|
112 putTreeOperationLog((NetworkDefaultJungleTreeEditor)editor);
|
46
|
113 } catch (IOException e1) {
|
|
114 e1.printStackTrace();
|
|
115 }
|
|
116
|
38
|
117 }
|
|
118
|
|
119 public void createBoardMessage(final String _board,final String _author,final String _message,final String _editKey)
|
|
120 {
|
|
121 JungleTree tree = jungle.getTreeByName(_board);
|
|
122 if(tree == null){
|
|
123 throw new IllegalStateException();
|
|
124 }
|
|
125
|
|
126 JungleTreeEditor editor;
|
46
|
127 Either<Error, JungleTreeEditor> either;
|
38
|
128 do{
|
|
129 Node node = tree.getRootNode();
|
|
130 int size = node.getChildren().size();
|
|
131 DefaultNodePath path = new DefaultNodePath();
|
|
132
|
|
133 editor = tree.getTreeEditor();
|
46
|
134 either = editor.addNewChildAt(path,size);
|
38
|
135 if(either.isA()){
|
|
136 throw new IllegalStateException();
|
|
137 }
|
|
138 editor = either.b();
|
|
139 NodeEditor e = new NodeEditor(){
|
|
140 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){
|
|
141 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b();
|
|
142 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b();
|
|
143 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b();
|
|
144 return DefaultEither.newB(_e);
|
|
145 }
|
|
146 };
|
|
147 path = path.add(size);
|
|
148 either = editor.edit(path,e);
|
|
149 if(either.isA()){
|
|
150 throw new IllegalStateException();
|
|
151 }
|
|
152 editor = either.b();
|
46
|
153 either = editor.success();
|
|
154 try {
|
|
155 putTreeOperationLog((NetworkDefaultJungleTreeEditor)editor);
|
|
156 } catch (IOException e1) {
|
|
157 e1.printStackTrace();
|
|
158 }
|
|
159 }while(either.isA());
|
51
|
160 /* Put DataSegment */
|
38
|
161 }
|
|
162
|
|
163 public void editMessage(String _board,String _uuid,final String _author,final String _message,final String _editKey)
|
|
164 {
|
|
165 JungleTreeEditor editor = null;
|
51
|
166 Either<Error,JungleTreeEditor> either = null;
|
38
|
167 do{
|
|
168 DefaultNodePath path = new DefaultNodePath();
|
|
169 path = path.add(Integer.parseInt(_uuid));
|
|
170
|
|
171 JungleTree tree = jungle.getTreeByName(_board);
|
|
172 editor = tree.getTreeEditor();
|
|
173 NodeEditor e = new NodeEditor(){
|
|
174 public <T extends EditableNode<T>> Either<Error, T> edit(T _e){
|
|
175 _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b();
|
|
176 _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b();
|
|
177 _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b();
|
|
178 return DefaultEither.newB(_e);
|
|
179 }
|
|
180 };
|
|
181
|
46
|
182 either = editor.edit(path,e);
|
38
|
183 if(either.isA()){
|
|
184 throw new IllegalStateException();
|
|
185 }
|
|
186 editor = either.b();
|
46
|
187 either = editor.success();
|
|
188 try {
|
|
189 putTreeOperationLog((NetworkDefaultJungleTreeEditor)editor);
|
|
190 } catch (IOException e1) {
|
|
191 e1.printStackTrace();
|
|
192 }
|
|
193 }while(either.isA());
|
38
|
194 }
|
|
195
|
|
196 public Iterable<BoardMessage> getMessages(String _boardName)
|
|
197 {
|
|
198 JungleTree tree = jungle.getTreeByName(_boardName);
|
|
199 Node node = tree.getRootNode();
|
|
200 Children<Node> chs = node.getChildren();
|
|
201
|
|
202 final AtomicInteger counter = new AtomicInteger(0);
|
|
203 IterableConverter.Converter<BoardMessage,Node> converter = new IterableConverter.Converter<BoardMessage,Node>(){
|
|
204 public BoardMessage conv(Node _b) {
|
|
205 String uuid = Integer.toString(counter.get());
|
|
206 String author = new String(_b.getAttributes().get("author").array());
|
|
207 String message = new String(_b.getAttributes().get("mes").array());
|
|
208 counter.incrementAndGet();
|
|
209 return new BoardMessageImpl(author,message,uuid);
|
|
210 }
|
|
211 };
|
|
212
|
|
213 return new IterableConverter<BoardMessage,Node>(chs,converter);
|
|
214 }
|
|
215
|
46
|
216 private void putTreeOperationLog(NetworkDefaultJungleTreeEditor editor) throws IOException {
|
|
217 String uuid = editor.getID();
|
|
218 String treeName = editor.getTreeName();
|
49
|
219 String updaterName = editor.getUpdaterName();
|
52
|
220 String revision = editor.getRevision();
|
46
|
221 Iterable<TreeOperation> log = editor.getTreeOperationLog();
|
49
|
222 putDataSegment(uuid, treeName, updaterName, log, revision);
|
46
|
223 }
|
|
224
|
52
|
225 private void putDataSegment(String _uuid, String _treeName, String _updaterName, Iterable<TreeOperation> _log, String nextRevision) throws IOException {
|
46
|
226 DefaultTreeOperationLogContainer container = new DefaultTreeOperationLogContainer();
|
|
227 container.setTreeName(_treeName);
|
|
228 container.setUUID(_uuid);
|
49
|
229 container.setUpdaterName(_updaterName);
|
46
|
230 container.setRevision(nextRevision);
|
|
231 container.unconvert(_log);
|
|
232 NullCodeSegmentForUpdate cs = new NullCodeSegmentForUpdate();
|
47
|
233 cs.ods.put("log", container);
|
52
|
234 new HashLogUpdateCodeSegment(container.getHashLogString());
|
|
235 System.out.println("putDataSegment");
|
|
236 System.out.println(container.getHashLogString());
|
|
237 /* If this node is not Root node, push log to parent node's DS */
|
50
|
238 if(!_updaterName.equals("node0")) {
|
51
|
239 cs.ods.put("parent", "childLog", container);
|
50
|
240 }
|
46
|
241 }
|
|
242
|
|
243
|
38
|
244 private static class BoardMessageImpl implements BoardMessage
|
|
245 {
|
|
246 private final String author;
|
|
247 private final String message;
|
|
248 private final String uuid;
|
|
249
|
|
250 public BoardMessageImpl(String _author,String _message,String _uuid)
|
|
251 {
|
|
252 author = _author;
|
|
253 message = _message;
|
|
254 uuid = _uuid;
|
|
255 }
|
|
256
|
|
257 public String getAuthor()
|
|
258 {
|
|
259 return author;
|
|
260 }
|
|
261
|
|
262 public String getMessage()
|
|
263 {
|
|
264 return message;
|
|
265 }
|
|
266
|
|
267 public String getUUID()
|
|
268 {
|
|
269 return uuid;
|
|
270 }
|
|
271 }
|
|
272 }
|