Mercurial > hg > Database > jungle-network
annotate src/main/java/app/bbs/JungleManager.java @ 115:3f9c6ab43461
Adde PersistentJournalTest.java
author | one |
---|---|
date | Fri, 20 Dec 2013 06:51:35 +0900 |
parents | 7d9b7fcb4d9a |
children | 895ab2907db3 |
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 { | |
115 | 24 private static JungleManager instance = 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 | |
113 | 32 public static JungleManager getInstantce() { |
115 | 33 return instance; |
113 | 34 } |
35 | |
45 | 36 public static void setJungle(Jungle _j) { |
115 | 37 instance.jungle = _j; |
15 | 38 } |
39 | |
43 | 40 public static Jungle getJungle() { |
115 | 41 return instance.jungle; |
43 | 42 } |
43 | |
44 public static JungleTree createNewTree(String name) { | |
115 | 45 return instance.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; | |
90 | 51 for (TreeOperation op : _log) { |
56 | 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, | |
90 | 62 TreeOperation op, int _pos) { |
63 NodePath path = new DefaultNodePath(); | |
56 | 64 NodeOperation nodeOp = op.getNodeOperation(); |
90 | 65 int pos = _pos; |
66 if (_pos == NOT_CHANGE_POSITION ) { | |
67 pos = nodeOp.getPosition(); | |
68 } | |
56 | 69 Command c = nodeOp.getCommand(); |
70 String key = ""; | |
71 switch (c) { | |
72 case PUT_ATTRIBUTE: | |
90 | 73 path = op.getNodePath(); |
56 | 74 key = nodeOp.getKey(); |
75 ByteBuffer value = nodeOp.getValue(); | |
76 return editor.putAttribute(path, key, value); | |
77 case DELETE_ATTRIBUTE: | |
78 key = nodeOp.getKey(); | |
79 return editor.deleteAttribute(path, key); | |
80 case APPEND_CHILD: | |
81 return editor.addNewChildAt(path, pos); | |
82 case DELETE_CHILD: | |
83 return editor.deleteChildAt(path, 0); | |
84 } | |
85 return null; | |
86 } | |
87 | |
96 | 88 public static Either<Error, JungleTreeEditor> update(NetworkTreeOperationLog netLog) { |
89 String treeName = netLog.getTreeName(); | |
93 | 90 Jungle jungle = JungleManager.getJungle(); |
91 if (jungle.getTreeByName(treeName) == null) { | |
92 if(null == jungle.createNewTree(treeName)){ | |
59
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 } |
93 | 96 JungleTree tree = jungle.getTreeByName(treeName); |
95
dcd767b76f8d
Modified putDataSement for to use NetworkTreeOperationLog
one
parents:
93
diff
changeset
|
97 JungleTreeEditor editor = tree.getLocalTreeEditor(); |
96 | 98 |
99 // int pos = calculatePosition(tree.getRootNode(), netLog.getTimeStamp()); | |
100 int pos = 0; | |
101 Either<Error, JungleTreeEditor> either = JungleManager.edit(editor, netLog, pos); | |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
102 if(either.isA()) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
103 throw new IllegalStateException(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
104 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
105 editor = either.b(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
106 either = editor.success(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
107 if(either.isA()) { |
67 | 108 throw new IllegalStateException(); |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
109 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
110 return either; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
111 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
112 |
90 | 113 private static int calculatePosition(Node node, long newNodeTimeStamp) { |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
114 int count = 0; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
115 long childTimeStamp = 0; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
116 for(Iterator<Node> iter = node.getChildren().iterator();iter.hasNext();) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
117 Node n = iter.next(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
118 if(n.getAttributes().get("timestamp") == null) { |
90 | 119 return NOT_CHANGE_POSITION; |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
120 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
121 if(n.getAttributes().get("timestamp") != null) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
122 childTimeStamp = n.getAttributes().get("timestamp").getLong(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
123 if (newNodeTimeStamp < childTimeStamp) { |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
124 break; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
125 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
126 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
127 count++; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
128 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
129 return count; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
130 } |
15 | 131 } |