Mercurial > hg > Database > jungle-network
annotate src/main/java/app/bbs/BulletinBoardJungleManager.java @ 133:00fcb468de27
Rename to BulletinBoardJungleManager
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 11 Jan 2014 09:30:57 +0900 |
parents | src/main/java/app/bbs/BullentInBoardJungleManager.java@0b3375dee51d |
children | 2e8034524259 |
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 |
133
00fcb468de27
Rename to BulletinBoardJungleManager
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
132
diff
changeset
|
17 public class BulletinBoardJungleManager { |
00fcb468de27
Rename to BulletinBoardJungleManager
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
132
diff
changeset
|
18 private static BulletinBoardJungleManager instance = new BulletinBoardJungleManager(); |
43 | 19 private Jungle jungle; |
20 | |
133
00fcb468de27
Rename to BulletinBoardJungleManager
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
132
diff
changeset
|
21 private BulletinBoardJungleManager() { |
116 | 22 jungle = new DefaultJungle(null,"default",new DefaultTreeEditor(new DefaultTraverser())); |
15 | 23 } |
24 | |
133
00fcb468de27
Rename to BulletinBoardJungleManager
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
132
diff
changeset
|
25 public static BulletinBoardJungleManager 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(); | |
133
00fcb468de27
Rename to BulletinBoardJungleManager
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
132
diff
changeset
|
43 Jungle jungle = BulletinBoardJungleManager.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 } |
123 | 77 childTimeStamp = timestamp.getLong(); |
78 if (newNodeTimeStamp < childTimeStamp) { | |
79 break; | |
59
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
80 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
81 count++; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
82 } |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
83 return count; |
8a532ca5df80
refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents:
56
diff
changeset
|
84 } |
15 | 85 } |