comparison src/jungle/test/bbs/JungleManager.java @ 59:8a532ca5df80

refactoring LogUpdateCodeSegment and ChildLogCheckCodeSegment
author one
date Mon, 15 Jul 2013 10:59:53 +0900
parents ccfe9b5e8f11
children ebf42371454b
comparison
equal deleted inserted replaced
58:4851344e120e 59:8a532ca5df80
1 package jungle.test.bbs; 1 package jungle.test.bbs;
2 2
3 import java.io.IOException;
3 import java.nio.ByteBuffer; 4 import java.nio.ByteBuffer;
5 import java.util.Iterator;
4 6
5 import alice.jungle.core.NetworkDefaultJungle; 7 import alice.jungle.core.NetworkDefaultJungle;
8 import alice.jungle.datasegment.store.operations.DefaultTreeOperationLogContainer;
6 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.DefaultJungle; 9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.DefaultJungle;
7 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle; 10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle;
8 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree; 11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor; 12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node;
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.Command; 14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.Command;
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; 15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath; 16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor; 17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor;
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.DefaultTreeOperationLog;
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog; 19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog;
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.NodeOperation; 20 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.NodeOperation;
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation; 21 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation;
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser; 22 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser;
23 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither;
24 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultError;
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; 25 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; 26 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
20 27
21 public class JungleManager { 28 public class JungleManager {
22 private static JungleManager jm = new JungleManager(); 29 private static JungleManager jm = new JungleManager();
36 43
37 public static JungleTree createNewTree(String name) { 44 public static JungleTree createNewTree(String name) {
38 return jm.jungle.createNewTree(name); 45 return jm.jungle.createNewTree(name);
39 } 46 }
40 47
41
42 public static Either<Error, JungleTreeEditor> edit(JungleTreeEditor _editor ,TreeOperationLog _log) {
43 JungleTreeEditor editor = _editor;
44 Either<Error, JungleTreeEditor> either = null;
45 for (TreeOperation op : _log) {
46 either = _edit(editor, op);
47 if(either.isA()) {
48 return either;
49 }
50 editor = either.b();
51 }
52 return either;
53 }
54
55 private static Either<Error, JungleTreeEditor> _edit(JungleTreeEditor editor,
56 TreeOperation op) {
57 NodePath path = op.getNodePath();
58 NodeOperation nodeOp = op.getNodeOperation();
59 Command c = nodeOp.getCommand();
60 String key = "";
61 switch (c) {
62 case PUT_ATTRIBUTE:
63 key = nodeOp.getKey();
64 ByteBuffer value = nodeOp.getValue();
65 return editor.putAttribute(path, key, value);
66 case DELETE_ATTRIBUTE:
67 key = nodeOp.getKey();
68 return editor.deleteAttribute(path, key);
69 case APPEND_CHILD:
70 return editor.addNewChildAt(path, 0);
71 case DELETE_CHILD:
72 return editor.deleteChildAt(path, 0);
73 }
74 return null;
75 }
76
77
78 public static Either<Error, JungleTreeEditor> edit(JungleTreeEditor _editor ,TreeOperationLog _log, int pos) { 48 public static Either<Error, JungleTreeEditor> edit(JungleTreeEditor _editor ,TreeOperationLog _log, int pos) {
79 System.out.println("--path editor--");
80 JungleTreeEditor editor = _editor; 49 JungleTreeEditor editor = _editor;
81 Either<Error, JungleTreeEditor> either = null; 50 Either<Error, JungleTreeEditor> either = null;
82 for (TreeOperation op : _log) { 51 for (TreeOperation op : _log) {
83 either = _edit(editor, op, pos); 52 either = _edit(editor, op, pos);
84 if(either.isA()) { 53 if(either.isA()) {
109 case DELETE_CHILD: 78 case DELETE_CHILD:
110 return editor.deleteChildAt(path, 0); 79 return editor.deleteChildAt(path, 0);
111 } 80 }
112 return null; 81 return null;
113 } 82 }
114
115 83
84 public static Either<Error, JungleTreeEditor> update(DefaultTreeOperationLogContainer container) {
85 DefaultTreeOperationLog log = null;
86 try {
87 log = container.convert();
88 } catch (IOException e) {
89 e.printStackTrace();
90 }
91 String treeName = container.getTreeName();
92 if (JungleManager.getJungle().getTreeByName(treeName) == null) {
93 if(null == JungleManager.getJungle().createNewTree(treeName)){
94 throw new IllegalStateException();
95 }
96 }
97 JungleTree tree = JungleManager.getJungle().getTreeByName(treeName);
98 JungleTreeEditor editor = tree.getTreeEditor();
99 int pos = checkTimeStamp(tree.getRootNode(), container.getTimeStamp(), container.getPosition());
100 Either<Error, JungleTreeEditor> either = JungleManager.edit(editor, log, pos);
101 if(either.isA()) {
102 throw new IllegalStateException();
103 }
104 editor = either.b();
105 either = editor.success();
106 if(either.isA()) {
107 throw new IllegalStateException();
108 }
109 return either;
110 }
111
112
113
114 private static int checkTimeStamp(Node node, long newNodeTimeStamp, int containerPosition) {
115 int count = 0;
116 long childTimeStamp = 0;
117 for(Iterator<Node> iter = node.getChildren().iterator();iter.hasNext();) {
118 Node n = iter.next();
119 if(n.getAttributes().get("timestamp") == null) {
120 return containerPosition;
121 }
122 if(n.getAttributes().get("timestamp") != null) {
123 childTimeStamp = n.getAttributes().get("timestamp").getLong();
124 if (newNodeTimeStamp < childTimeStamp) {
125 break;
126 }
127 }
128 count++;
129 }
130 return count;
131 }
116 } 132 }