Mercurial > hg > Members > nobuyasu > jungle-network
annotate src/main/java/app/bbs/BullentInBoardJungleManager.java @ 131:aa98fc3ec85f
Modified BulletinBoardJungleManager calculatePosition method
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 11 Jan 2014 09:17:07 +0900 |
parents | 292538b1de32 |
children | 0b3375dee51d |
rev | line source |
---|---|
112 | 1 package app.bbs; |
39 | 2 |
3 import java.nio.ByteBuffer; | |
15 | 4 |
96 | 5 import alice.jungle.operations.NetworkTreeOperationLog; |
123 | 6 import alice.jungle.transaction.JungleUpdater; |
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; |
93 | 12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor; |
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser; | |
39 | 14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; |
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; | |
15 | 16 |
123 | 17 public class BullentInBoardJungleManager { |
18 private static BullentInBoardJungleManager instance = new BullentInBoardJungleManager(); | |
43 | 19 private Jungle jungle; |
20 | |
123 | 21 private BullentInBoardJungleManager() { |
116 | 22 jungle = new DefaultJungle(null,"default",new DefaultTreeEditor(new DefaultTraverser())); |
15 | 23 } |
24 | |
123 | 25 public static BullentInBoardJungleManager getInstantce() { |
115 | 26 return instance; |
113 | 27 } |
28 | |
45 | 29 public static void setJungle(Jungle _j) { |
115 | 30 instance.jungle = _j; |
15 | 31 } |
32 | |
43 | 33 public static Jungle getJungle() { |
115 | 34 return instance.jungle; |
43 | 35 } |
36 | |
37 public static JungleTree createNewTree(String name) { | |
116 | 38 return instance.jungle.createNewTree(name); |
15 | 39 } |
40 | |
96 | 41 public static Either<Error, JungleTreeEditor> update(NetworkTreeOperationLog netLog) { |
42 String treeName = netLog.getTreeName(); | |
123 | 43 Jungle jungle = BullentInBoardJungleManager.getJungle(); |
93 | 44 if (jungle.getTreeByName(treeName) == null) { |
45 if(null == jungle.createNewTree(treeName)){ | |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
46 throw new IllegalStateException(); |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
47 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
48 } |
127
292538b1de32
Fixed BulletinBoardJungleManager bug
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
123
diff
changeset
|
49 Either<Error, JungleTreeEditor> either = null; |
292538b1de32
Fixed BulletinBoardJungleManager bug
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
123
diff
changeset
|
50 do { |
292538b1de32
Fixed BulletinBoardJungleManager bug
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
123
diff
changeset
|
51 JungleTree tree = jungle.getTreeByName(treeName); |
292538b1de32
Fixed BulletinBoardJungleManager bug
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
123
diff
changeset
|
52 JungleTreeEditor editor = tree.getLocalTreeEditor(); |
96 | 53 |
127
292538b1de32
Fixed BulletinBoardJungleManager bug
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
123
diff
changeset
|
54 /* |
292538b1de32
Fixed BulletinBoardJungleManager bug
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
123
diff
changeset
|
55 * Merge. |
292538b1de32
Fixed BulletinBoardJungleManager bug
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
123
diff
changeset
|
56 */ |
292538b1de32
Fixed BulletinBoardJungleManager bug
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
123
diff
changeset
|
57 int pos = calculatePosition(tree.getRootNode(), netLog.getTimeStamp()); |
292538b1de32
Fixed BulletinBoardJungleManager bug
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
123
diff
changeset
|
58 either = JungleUpdater.edit(editor, netLog, pos); |
292538b1de32
Fixed BulletinBoardJungleManager bug
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
123
diff
changeset
|
59 if(either.isA()) { |
292538b1de32
Fixed BulletinBoardJungleManager bug
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
123
diff
changeset
|
60 throw new IllegalStateException(); |
292538b1de32
Fixed BulletinBoardJungleManager bug
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
123
diff
changeset
|
61 } |
292538b1de32
Fixed BulletinBoardJungleManager bug
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
123
diff
changeset
|
62 |
292538b1de32
Fixed BulletinBoardJungleManager bug
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
123
diff
changeset
|
63 editor = either.b(); |
292538b1de32
Fixed BulletinBoardJungleManager bug
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
123
diff
changeset
|
64 either = editor.success(); |
292538b1de32
Fixed BulletinBoardJungleManager bug
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
123
diff
changeset
|
65 }while(either.isA()); |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
66 return either; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
67 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
68 |
90 | 69 private static int calculatePosition(Node node, long newNodeTimeStamp) { |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
70 int count = 0; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
71 long childTimeStamp = 0; |
123 | 72 for(Node n : node.getChildren()) { |
73 ByteBuffer timestamp = n.getAttributes().get("timestamp"); | |
74 if(timestamp == null) { | |
75 return count; | |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
76 } |
131
aa98fc3ec85f
Modified BulletinBoardJungleManager calculatePosition method
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
127
diff
changeset
|
77 Long t = timestamp.getLong(); |
aa98fc3ec85f
Modified BulletinBoardJungleManager calculatePosition method
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
127
diff
changeset
|
78 if(t == null) { |
aa98fc3ec85f
Modified BulletinBoardJungleManager calculatePosition method
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
127
diff
changeset
|
79 return count; |
aa98fc3ec85f
Modified BulletinBoardJungleManager calculatePosition method
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
127
diff
changeset
|
80 } |
123 | 81 childTimeStamp = timestamp.getLong(); |
82 if (newNodeTimeStamp < childTimeStamp) { | |
83 break; | |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
84 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
85 count++; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
86 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
87 return count; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
88 } |
15 | 89 } |