Mercurial > hg > Database > jungle-network
annotate 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 |
rev | line source |
---|---|
112 | 1 package app.bbs; |
39 | 2 |
3 import java.nio.ByteBuffer; | |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
4 import java.util.Iterator; |
15 | 5 |
96 | 6 import alice.jungle.operations.NetworkTreeOperationLog; |
93 | 7 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.DefaultJungle; |
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; |
93 | 15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor; |
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; | |
93 | 19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser; |
39 | 20 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; |
21 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; | |
15 | 22 |
23 public class JungleManager { | |
45 | 24 private static JungleManager jm = new JungleManager(); |
43 | 25 private Jungle jungle; |
90 | 26 private static int NOT_CHANGE_POSITION = 0; |
43 | 27 |
45 | 28 private JungleManager() { |
93 | 29 jungle = new DefaultJungle(null,"hogehoge",new DefaultTreeEditor(new DefaultTraverser())); |
15 | 30 } |
31 | |
45 | 32 public static void setJungle(Jungle _j) { |
33 jm.jungle = _j; | |
15 | 34 } |
35 | |
43 | 36 public static Jungle getJungle() { |
37 return jm.jungle; | |
38 } | |
39 | |
40 public static JungleTree createNewTree(String name) { | |
41 return jm.jungle.createNewTree(name); | |
15 | 42 } |
43 | |
56 | 44 public static Either<Error, JungleTreeEditor> edit(JungleTreeEditor _editor ,TreeOperationLog _log, int pos) { |
45 JungleTreeEditor editor = _editor; | |
46 Either<Error, JungleTreeEditor> either = null; | |
90 | 47 for (TreeOperation op : _log) { |
56 | 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, | |
90 | 58 TreeOperation op, int _pos) { |
59 NodePath path = new DefaultNodePath(); | |
56 | 60 NodeOperation nodeOp = op.getNodeOperation(); |
90 | 61 int pos = _pos; |
62 if (_pos == NOT_CHANGE_POSITION ) { | |
63 pos = nodeOp.getPosition(); | |
64 } | |
56 | 65 Command c = nodeOp.getCommand(); |
66 String key = ""; | |
67 switch (c) { | |
68 case PUT_ATTRIBUTE: | |
90 | 69 path = op.getNodePath(); |
56 | 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 | |
96 | 84 public static Either<Error, JungleTreeEditor> update(NetworkTreeOperationLog netLog) { |
85 String treeName = netLog.getTreeName(); | |
93 | 86 Jungle jungle = JungleManager.getJungle(); |
87 if (jungle.getTreeByName(treeName) == null) { | |
88 if(null == jungle.createNewTree(treeName)){ | |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
89 throw new IllegalStateException(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
90 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
91 } |
93 | 92 JungleTree tree = jungle.getTreeByName(treeName); |
95
dcd767b76f8d
Modified putDataSement for to use NetworkTreeOperationLog
one
parents:
93
diff
changeset
|
93 JungleTreeEditor editor = tree.getLocalTreeEditor(); |
96 | 94 |
95 // int pos = calculatePosition(tree.getRootNode(), netLog.getTimeStamp()); | |
96 int pos = 0; | |
97 Either<Error, JungleTreeEditor> either = JungleManager.edit(editor, netLog, pos); | |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
98 if(either.isA()) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
99 throw new IllegalStateException(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
100 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
101 editor = either.b(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
102 either = editor.success(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
103 if(either.isA()) { |
67 | 104 throw new IllegalStateException(); |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
105 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
106 return either; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
107 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
108 |
90 | 109 private static int calculatePosition(Node node, long newNodeTimeStamp) { |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
110 int count = 0; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
111 long childTimeStamp = 0; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
112 for(Iterator<Node> iter = node.getChildren().iterator();iter.hasNext();) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
113 Node n = iter.next(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
114 if(n.getAttributes().get("timestamp") == null) { |
90 | 115 return NOT_CHANGE_POSITION; |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
116 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
117 if(n.getAttributes().get("timestamp") != null) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
118 childTimeStamp = n.getAttributes().get("timestamp").getLong(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
119 if (newNodeTimeStamp < childTimeStamp) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
120 break; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
121 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
122 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
123 count++; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
124 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
125 return count; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
126 } |
15 | 127 } |