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