Mercurial > hg > Members > nobuyasu > jungle-network
annotate src/jungle/app/bbs/JungleManager.java @ 90:eef737ab3b2c
Modified JungleManager class
author | one |
---|---|
date | Mon, 11 Nov 2013 19:06:56 +0900 |
parents | 87ec5dd0dc27 |
children | 75cf01a430a6 |
rev | line source |
---|---|
66 | 1 package jungle.app.bbs; |
39 | 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 |
75
87ec5dd0dc27
Rename from alice.jungle.datasegment.store.operation to alice.jungle.datasegment.store.container
one
parents:
67
diff
changeset
|
7 import alice.jungle.datasegment.store.container.DefaultTreeOperationLogContainer; |
15 | 8 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle; |
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree; | |
39 | 10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor; |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; |
39 | 12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.Command; |
90 | 13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; |
56 | 14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath; |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.DefaultTreeOperationLog; |
39 | 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.util.Either; | |
20 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; | |
15 | 21 |
22 public class JungleManager { | |
45 | 23 private static JungleManager jm = new JungleManager(); |
43 | 24 private Jungle jungle; |
90 | 25 private static int NOT_CHANGE_POSITION = 0; |
43 | 26 |
45 | 27 private JungleManager() { |
15 | 28 |
29 } | |
30 | |
45 | 31 public static void setJungle(Jungle _j) { |
32 jm.jungle = _j; | |
15 | 33 } |
34 | |
43 | 35 public static Jungle getJungle() { |
36 return jm.jungle; | |
37 } | |
38 | |
39 public static JungleTree createNewTree(String name) { | |
40 return jm.jungle.createNewTree(name); | |
15 | 41 } |
42 | |
56 | 43 public static Either<Error, JungleTreeEditor> edit(JungleTreeEditor _editor ,TreeOperationLog _log, int pos) { |
44 JungleTreeEditor editor = _editor; | |
45 Either<Error, JungleTreeEditor> either = null; | |
90 | 46 for (TreeOperation op : _log) { |
56 | 47 either = _edit(editor, op, pos); |
48 if(either.isA()) { | |
49 return either; | |
50 } | |
51 editor = either.b(); | |
52 } | |
53 return either; | |
54 } | |
55 | |
56 private static Either<Error, JungleTreeEditor> _edit(JungleTreeEditor editor, | |
90 | 57 TreeOperation op, int _pos) { |
58 NodePath path = new DefaultNodePath(); | |
56 | 59 NodeOperation nodeOp = op.getNodeOperation(); |
90 | 60 int pos = _pos; |
61 if (_pos == NOT_CHANGE_POSITION ) { | |
62 pos = nodeOp.getPosition(); | |
63 } | |
56 | 64 Command c = nodeOp.getCommand(); |
65 String key = ""; | |
66 switch (c) { | |
67 case PUT_ATTRIBUTE: | |
90 | 68 path = op.getNodePath(); |
56 | 69 key = nodeOp.getKey(); |
70 ByteBuffer value = nodeOp.getValue(); | |
71 return editor.putAttribute(path, key, value); | |
72 case DELETE_ATTRIBUTE: | |
73 key = nodeOp.getKey(); | |
74 return editor.deleteAttribute(path, key); | |
75 case APPEND_CHILD: | |
76 return editor.addNewChildAt(path, pos); | |
77 case DELETE_CHILD: | |
78 return editor.deleteChildAt(path, 0); | |
79 } | |
80 return null; | |
81 } | |
82 | |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
83 public static Either<Error, JungleTreeEditor> update(DefaultTreeOperationLogContainer container) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
84 DefaultTreeOperationLog log = null; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
85 try { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
86 log = container.convert(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
87 } catch (IOException e) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
88 e.printStackTrace(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
89 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
90 String treeName = container.getTreeName(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
91 if (JungleManager.getJungle().getTreeByName(treeName) == null) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
92 if(null == JungleManager.getJungle().createNewTree(treeName)){ |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
93 throw new IllegalStateException(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
94 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
95 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
96 JungleTree tree = JungleManager.getJungle().getTreeByName(treeName); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
97 JungleTreeEditor editor = tree.getTreeEditor(); |
90 | 98 int pos = calculatePosition(tree.getRootNode(), container.getTimeStamp()); |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
99 Either<Error, JungleTreeEditor> either = JungleManager.edit(editor, log, pos); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
100 if(either.isA()) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
101 throw new IllegalStateException(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
102 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
103 editor = either.b(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
104 either = editor.success(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
105 if(either.isA()) { |
67 | 106 throw new IllegalStateException(); |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
107 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
108 return either; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
109 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
110 |
90 | 111 private static int calculatePosition(Node node, long newNodeTimeStamp) { |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
112 int count = 0; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
113 long childTimeStamp = 0; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
114 for(Iterator<Node> iter = node.getChildren().iterator();iter.hasNext();) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
115 Node n = iter.next(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
116 if(n.getAttributes().get("timestamp") == null) { |
90 | 117 return NOT_CHANGE_POSITION; |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
118 } |
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 childTimeStamp = n.getAttributes().get("timestamp").getLong(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
121 if (newNodeTimeStamp < childTimeStamp) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
122 break; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
123 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
124 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
125 count++; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
126 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
127 return count; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
128 } |
15 | 129 } |