comparison src/main/java/app/bbs/JungleManager.java @ 112:8f9811a1e00c

Moved app files
author one
date Fri, 29 Nov 2013 04:31:22 +0900
parents src/main/java/jungle/app/bbs/JungleManager.java@f9e29a52efd3
children 7d9b7fcb4d9a
comparison
equal deleted inserted replaced
111:6e7724d55a72 112:8f9811a1e00c
1 package app.bbs;
2
3 import java.nio.ByteBuffer;
4 import java.util.Iterator;
5
6 import alice.jungle.operations.NetworkTreeOperationLog;
7 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.DefaultJungle;
8 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle;
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node;
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.Command;
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor;
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog;
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.NodeOperation;
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation;
19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser;
20 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
21 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
22
23 public class JungleManager {
24 private static JungleManager jm = new JungleManager();
25 private Jungle jungle;
26 private static int NOT_CHANGE_POSITION = 0;
27
28 private JungleManager() {
29 jungle = new DefaultJungle(null,"hogehoge",new DefaultTreeEditor(new DefaultTraverser()));
30 }
31
32 public static void setJungle(Jungle _j) {
33 jm.jungle = _j;
34 }
35
36 public static Jungle getJungle() {
37 return jm.jungle;
38 }
39
40 public static JungleTree createNewTree(String name) {
41 return jm.jungle.createNewTree(name);
42 }
43
44 public static Either<Error, JungleTreeEditor> edit(JungleTreeEditor _editor ,TreeOperationLog _log, int pos) {
45 JungleTreeEditor editor = _editor;
46 Either<Error, JungleTreeEditor> either = null;
47 for (TreeOperation op : _log) {
48 either = _edit(editor, op, pos);
49 if(either.isA()) {
50 return either;
51 }
52 editor = either.b();
53 }
54 return either;
55 }
56
57 private static Either<Error, JungleTreeEditor> _edit(JungleTreeEditor editor,
58 TreeOperation op, int _pos) {
59 NodePath path = new DefaultNodePath();
60 NodeOperation nodeOp = op.getNodeOperation();
61 int pos = _pos;
62 if (_pos == NOT_CHANGE_POSITION ) {
63 pos = nodeOp.getPosition();
64 }
65 Command c = nodeOp.getCommand();
66 String key = "";
67 switch (c) {
68 case PUT_ATTRIBUTE:
69 path = op.getNodePath();
70 key = nodeOp.getKey();
71 ByteBuffer value = nodeOp.getValue();
72 return editor.putAttribute(path, key, value);
73 case DELETE_ATTRIBUTE:
74 key = nodeOp.getKey();
75 return editor.deleteAttribute(path, key);
76 case APPEND_CHILD:
77 return editor.addNewChildAt(path, pos);
78 case DELETE_CHILD:
79 return editor.deleteChildAt(path, 0);
80 }
81 return null;
82 }
83
84 public static Either<Error, JungleTreeEditor> update(NetworkTreeOperationLog netLog) {
85 String treeName = netLog.getTreeName();
86 Jungle jungle = JungleManager.getJungle();
87 if (jungle.getTreeByName(treeName) == null) {
88 if(null == jungle.createNewTree(treeName)){
89 throw new IllegalStateException();
90 }
91 }
92 JungleTree tree = jungle.getTreeByName(treeName);
93 JungleTreeEditor editor = tree.getLocalTreeEditor();
94
95 // int pos = calculatePosition(tree.getRootNode(), netLog.getTimeStamp());
96 int pos = 0;
97 Either<Error, JungleTreeEditor> either = JungleManager.edit(editor, netLog, pos);
98 if(either.isA()) {
99 throw new IllegalStateException();
100 }
101 editor = either.b();
102 either = editor.success();
103 if(either.isA()) {
104 throw new IllegalStateException();
105 }
106 return either;
107 }
108
109 private static int calculatePosition(Node node, long newNodeTimeStamp) {
110 int count = 0;
111 long childTimeStamp = 0;
112 for(Iterator<Node> iter = node.getChildren().iterator();iter.hasNext();) {
113 Node n = iter.next();
114 if(n.getAttributes().get("timestamp") == null) {
115 return NOT_CHANGE_POSITION;
116 }
117 if(n.getAttributes().get("timestamp") != null) {
118 childTimeStamp = n.getAttributes().get("timestamp").getLong();
119 if (newNodeTimeStamp < childTimeStamp) {
120 break;
121 }
122 }
123 count++;
124 }
125 return count;
126 }
127 }