annotate src/jungle/test/bbs/codesegment/ChildLogCheckCodeSegment.java @ 58:4851344e120e

fix bug LogUpdateCodeSegment and ChildLogCheckCodeSegment
author one
date Mon, 15 Jul 2013 10:13:34 +0900
parents 1bfd13f831ea
children 8a532ca5df80
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
50
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
1 package jungle.test.bbs.codesegment;
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
2
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
3 import java.io.IOException;
56
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
4 import java.util.Iterator;
50
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
5
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
6 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
7 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
56
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
8 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node;
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
50
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.DefaultTreeOperationLog;
56
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither;
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultError;
50
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
16 import jungle.test.bbs.JungleManager;
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
17 import alice.codesegment.CodeSegment;
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
18 import alice.datasegment.CommandType;
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
19 import alice.datasegment.Receiver;
52
61b2de3f7730 add HashSetDataSegment and HashLogUpdateCodeSegment.
one
parents: 51
diff changeset
20 import alice.jungle.codesegment.HashLogUpdateCodeSegment;
61b2de3f7730 add HashSetDataSegment and HashLogUpdateCodeSegment.
one
parents: 51
diff changeset
21 import alice.jungle.datasegment.HashSetDataSegment;
50
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
22 import alice.jungle.datasegment.store.operations.DefaultTreeOperationLogContainer;
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
23
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
24 public class ChildLogCheckCodeSegment extends CodeSegment {
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
25
52
61b2de3f7730 add HashSetDataSegment and HashLogUpdateCodeSegment.
one
parents: 51
diff changeset
26 Receiver host = ids.create(CommandType.PEEK);
50
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
27 Receiver childLog = ids.create(CommandType.TAKE);
52
61b2de3f7730 add HashSetDataSegment and HashLogUpdateCodeSegment.
one
parents: 51
diff changeset
28 Receiver hashLog = ids.create(CommandType.PEEK);
50
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
29
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
30 public ChildLogCheckCodeSegment() {
51
9e782b4eb06e add HashSetConvertDataSegment
one
parents: 50
diff changeset
31 host.setKey("host");
9e782b4eb06e add HashSetConvertDataSegment
one
parents: 50
diff changeset
32 childLog.setKey("local","childLog");
52
61b2de3f7730 add HashSetDataSegment and HashLogUpdateCodeSegment.
one
parents: 51
diff changeset
33 hashLog.setKey("hashLog");
50
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
34 }
58
4851344e120e fix bug LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents: 57
diff changeset
35
4851344e120e fix bug LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents: 57
diff changeset
36 public ChildLogCheckCodeSegment(int index) {
4851344e120e fix bug LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents: 57
diff changeset
37 host.setKey("host");
4851344e120e fix bug LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents: 57
diff changeset
38 childLog.setKey("local","childLog", index);
4851344e120e fix bug LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents: 57
diff changeset
39 hashLog.setKey("hashLog");
4851344e120e fix bug LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents: 57
diff changeset
40 }
4851344e120e fix bug LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents: 57
diff changeset
41
50
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
42
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
43 public void run() {
51
9e782b4eb06e add HashSetConvertDataSegment
one
parents: 50
diff changeset
44 System.out.println("--ChildLogCheckCodeSegment--");
58
4851344e120e fix bug LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents: 57
diff changeset
45 int index = childLog.index;
4851344e120e fix bug LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents: 57
diff changeset
46 HashSetDataSegment ds = hashLog.asClass(HashSetDataSegment.class);
51
9e782b4eb06e add HashSetConvertDataSegment
one
parents: 50
diff changeset
47 String hostName = host.asString();
50
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
48 DefaultTreeOperationLogContainer container = childLog.asClass(DefaultTreeOperationLogContainer.class);
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
49 DefaultTreeOperationLog log = null;
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
50 try {
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
51 log = container.convert();
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
52 } catch (IOException e) {
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
53 e.printStackTrace();
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
54 }
51
9e782b4eb06e add HashSetConvertDataSegment
one
parents: 50
diff changeset
55 String treeName = container.getTreeName();
9e782b4eb06e add HashSetConvertDataSegment
one
parents: 50
diff changeset
56 if (JungleManager.getJungle().getTreeByName(treeName) == null) {
9e782b4eb06e add HashSetConvertDataSegment
one
parents: 50
diff changeset
57 if(null == JungleManager.getJungle().createNewTree(treeName)){
9e782b4eb06e add HashSetConvertDataSegment
one
parents: 50
diff changeset
58 throw new IllegalStateException();
9e782b4eb06e add HashSetConvertDataSegment
one
parents: 50
diff changeset
59 }
9e782b4eb06e add HashSetConvertDataSegment
one
parents: 50
diff changeset
60 }
9e782b4eb06e add HashSetConvertDataSegment
one
parents: 50
diff changeset
61 JungleTree tree = JungleManager.getJungle().getTreeByName(treeName);
50
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
62 JungleTreeEditor editor = tree.getTreeEditor();
56
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
63 Either<DefaultError, Integer> e = checkTimeStamp(tree.getRootNode(), container.getTimeStamp());
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
64 Either<Error, JungleTreeEditor> either;
58
4851344e120e fix bug LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents: 57
diff changeset
65 if(e.isA()) {
57
1bfd13f831ea add position field in DefaultTreeOperationLogContainer
one
parents: 56
diff changeset
66 either = JungleManager.edit(editor, log, container.getPosition());
56
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
67 } else {
58
4851344e120e fix bug LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents: 57
diff changeset
68 either = JungleManager.edit(editor, log, e.b());
56
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
69 }
50
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
70 if(either.isA()) {
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
71 throw new IllegalStateException();
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
72 }
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
73 editor = either.b();
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
74 either = editor.success();
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
75 if(either.isA()) {
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
76 throw new IllegalStateException();
52
61b2de3f7730 add HashSetDataSegment and HashLogUpdateCodeSegment.
one
parents: 51
diff changeset
77 }
61b2de3f7730 add HashSetDataSegment and HashLogUpdateCodeSegment.
one
parents: 51
diff changeset
78 ds.hash.add(container.getHashLogString());
61b2de3f7730 add HashSetDataSegment and HashLogUpdateCodeSegment.
one
parents: 51
diff changeset
79 ods.update("hashLog", ds);
50
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
80 ods.put("log", container);
58
4851344e120e fix bug LogUpdateCodeSegment and ChildLogCheckCodeSegment
one
parents: 57
diff changeset
81 new ChildLogCheckCodeSegment(index);
51
9e782b4eb06e add HashSetConvertDataSegment
one
parents: 50
diff changeset
82 if(!hostName.equals("node0")) {
9e782b4eb06e add HashSetConvertDataSegment
one
parents: 50
diff changeset
83 ods.put("parent", "childLog", container);
9e782b4eb06e add HashSetConvertDataSegment
one
parents: 50
diff changeset
84 }
50
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
85 }
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
86
56
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
87 private DefaultEither<DefaultError, Integer> checkTimeStamp(Node node, long newNodeTimeStamp) {
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
88 int count = 0;
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
89 long childTimeStamp = 0;
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
90 for(Iterator<Node> iter = node.getChildren().iterator();iter.hasNext();) {
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
91 Node n = iter.next();
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
92 if(n.getAttributes().get("timestamp") == null) {
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
93 return DefaultEither.newA(new DefaultError());
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
94 }
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
95 if(n.getAttributes().get("timestamp") != null) {
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
96 childTimeStamp = n.getAttributes().get("timestamp").getLong();
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
97 if (newNodeTimeStamp < childTimeStamp) {
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
98 break;
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
99 }
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
100 }
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
101 count++;
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
102 }
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
103 return DefaultEither.newB(count);
ccfe9b5e8f11 bug LogUpdateCodeSegment
one
parents: 53
diff changeset
104 }
50
459b50f1a6ee add ChildLogCheckCodeSegment.
one
parents:
diff changeset
105 }