Mercurial > hg > Database > jungle-network
annotate src/jungle/test/bbs/JungleManager.java @ 59:8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
author | one |
---|---|
date | Mon, 15 Jul 2013 10:59:53 +0900 |
parents | ccfe9b5e8f11 |
children | ebf42371454b |
rev | line source |
---|---|
39 | 1 package jungle.test.bbs; |
2 | |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
3 import java.io.IOException; |
39 | 4 import java.nio.ByteBuffer; |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
5 import java.util.Iterator; |
15 | 6 |
31 | 7 import alice.jungle.core.NetworkDefaultJungle; |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
8 import alice.jungle.datasegment.store.operations.DefaultTreeOperationLogContainer; |
20 | 9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.DefaultJungle; |
15 | 10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle; |
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree; | |
39 | 12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor; |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; |
39 | 14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.Command; |
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; | |
56 | 16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath; |
15 | 17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor; |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.DefaultTreeOperationLog; |
39 | 19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog; |
20 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.NodeOperation; | |
21 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation; | |
15 | 22 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser; |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
23 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
24 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultError; |
39 | 25 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; |
26 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; | |
15 | 27 |
28 public class JungleManager { | |
45 | 29 private static JungleManager jm = new JungleManager(); |
43 | 30 private Jungle jungle; |
31 | |
45 | 32 private JungleManager() { |
15 | 33 |
34 } | |
35 | |
45 | 36 public static void setJungle(Jungle _j) { |
37 jm.jungle = _j; | |
15 | 38 } |
39 | |
43 | 40 public static Jungle getJungle() { |
41 return jm.jungle; | |
42 } | |
43 | |
44 public static JungleTree createNewTree(String name) { | |
45 return jm.jungle.createNewTree(name); | |
15 | 46 } |
47 | |
56 | 48 public static Either<Error, JungleTreeEditor> edit(JungleTreeEditor _editor ,TreeOperationLog _log, int pos) { |
49 JungleTreeEditor editor = _editor; | |
50 Either<Error, JungleTreeEditor> either = null; | |
51 for (TreeOperation op : _log) { | |
52 either = _edit(editor, op, pos); | |
53 if(either.isA()) { | |
54 return either; | |
55 } | |
56 editor = either.b(); | |
57 } | |
58 return either; | |
59 } | |
60 | |
61 private static Either<Error, JungleTreeEditor> _edit(JungleTreeEditor editor, | |
62 TreeOperation op, int pos) { | |
63 DefaultNodePath path = new DefaultNodePath(); | |
64 NodeOperation nodeOp = op.getNodeOperation(); | |
65 Command c = nodeOp.getCommand(); | |
66 String key = ""; | |
67 switch (c) { | |
68 case PUT_ATTRIBUTE: | |
69 path = path.add(pos); | |
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 | |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
84 public static Either<Error, JungleTreeEditor> update(DefaultTreeOperationLogContainer container) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
85 DefaultTreeOperationLog log = null; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
86 try { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
87 log = container.convert(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
88 } catch (IOException e) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
89 e.printStackTrace(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
90 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
91 String treeName = container.getTreeName(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
92 if (JungleManager.getJungle().getTreeByName(treeName) == null) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
93 if(null == JungleManager.getJungle().createNewTree(treeName)){ |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
94 throw new IllegalStateException(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
95 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
96 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
97 JungleTree tree = JungleManager.getJungle().getTreeByName(treeName); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
98 JungleTreeEditor editor = tree.getTreeEditor(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
99 int pos = checkTimeStamp(tree.getRootNode(), container.getTimeStamp(), container.getPosition()); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
100 Either<Error, JungleTreeEditor> either = JungleManager.edit(editor, log, pos); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
101 if(either.isA()) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
102 throw new IllegalStateException(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
103 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
104 editor = either.b(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
105 either = editor.success(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
106 if(either.isA()) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
107 throw new IllegalStateException(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
108 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
109 return either; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
110 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
111 |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
112 |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
113 |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
114 private static int checkTimeStamp(Node node, long newNodeTimeStamp, int containerPosition) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
115 int count = 0; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
116 long childTimeStamp = 0; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
117 for(Iterator<Node> iter = node.getChildren().iterator();iter.hasNext();) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
118 Node n = iter.next(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
119 if(n.getAttributes().get("timestamp") == null) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
120 return containerPosition; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
121 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
122 if(n.getAttributes().get("timestamp") != null) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
123 childTimeStamp = n.getAttributes().get("timestamp").getLong(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
124 if (newNodeTimeStamp < childTimeStamp) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
125 break; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
126 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
127 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
128 count++; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
129 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
130 return count; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
131 } |
15 | 132 } |