Mercurial > hg > Database > jungle-network
comparison src/jungle/app/bbs/JungleManager.java @ 66:29127ac788a6
move some files
author | one |
---|---|
date | Tue, 20 Aug 2013 17:38:09 +0900 |
parents | src/jungle/test/bbs/JungleManager.java@ebf42371454b |
children | 89e39301ccaa |
comparison
equal
deleted
inserted
replaced
65:ebf42371454b | 66:29127ac788a6 |
---|---|
1 package jungle.app.bbs; | |
2 | |
3 import java.io.IOException; | |
4 import java.nio.ByteBuffer; | |
5 import java.util.Iterator; | |
6 | |
7 import alice.jungle.datasegment.store.operations.DefaultTreeOperationLogContainer; | |
8 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle; | |
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree; | |
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor; | |
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; | |
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.Command; | |
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath; | |
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.DefaultTreeOperationLog; | |
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog; | |
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.NodeOperation; | |
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation; | |
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; | |
19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; | |
20 | |
21 public class JungleManager { | |
22 private static JungleManager jm = new JungleManager(); | |
23 private Jungle jungle; | |
24 | |
25 private JungleManager() { | |
26 | |
27 } | |
28 | |
29 public static void setJungle(Jungle _j) { | |
30 jm.jungle = _j; | |
31 } | |
32 | |
33 public static Jungle getJungle() { | |
34 return jm.jungle; | |
35 } | |
36 | |
37 public static JungleTree createNewTree(String name) { | |
38 return jm.jungle.createNewTree(name); | |
39 } | |
40 | |
41 public static Either<Error, JungleTreeEditor> edit(JungleTreeEditor _editor ,TreeOperationLog _log, int pos) { | |
42 JungleTreeEditor editor = _editor; | |
43 Either<Error, JungleTreeEditor> either = null; | |
44 for (TreeOperation op : _log) { | |
45 either = _edit(editor, op, pos); | |
46 if(either.isA()) { | |
47 return either; | |
48 } | |
49 editor = either.b(); | |
50 } | |
51 return either; | |
52 } | |
53 | |
54 private static Either<Error, JungleTreeEditor> _edit(JungleTreeEditor editor, | |
55 TreeOperation op, int pos) { | |
56 DefaultNodePath path = new DefaultNodePath(); | |
57 NodeOperation nodeOp = op.getNodeOperation(); | |
58 Command c = nodeOp.getCommand(); | |
59 String key = ""; | |
60 switch (c) { | |
61 case PUT_ATTRIBUTE: | |
62 path = path.add(pos); | |
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, pos); | |
71 case DELETE_CHILD: | |
72 return editor.deleteChildAt(path, 0); | |
73 } | |
74 return null; | |
75 } | |
76 | |
77 public static Either<Error, JungleTreeEditor> update(DefaultTreeOperationLogContainer container) { | |
78 DefaultTreeOperationLog log = null; | |
79 try { | |
80 log = container.convert(); | |
81 } catch (IOException e) { | |
82 e.printStackTrace(); | |
83 } | |
84 String treeName = container.getTreeName(); | |
85 if (JungleManager.getJungle().getTreeByName(treeName) == null) { | |
86 if(null == JungleManager.getJungle().createNewTree(treeName)){ | |
87 throw new IllegalStateException(); | |
88 } | |
89 } | |
90 JungleTree tree = JungleManager.getJungle().getTreeByName(treeName); | |
91 JungleTreeEditor editor = tree.getTreeEditor(); | |
92 int pos = checkTimeStamp(tree.getRootNode(), container.getTimeStamp(), container.getPosition()); | |
93 Either<Error, JungleTreeEditor> either = JungleManager.edit(editor, log, pos); | |
94 if(either.isA()) { | |
95 throw new IllegalStateException(); | |
96 } | |
97 editor = either.b(); | |
98 either = editor.success(); | |
99 if(either.isA()) { | |
100 throw new IllegalStateException(); | |
101 } | |
102 return either; | |
103 } | |
104 | |
105 | |
106 | |
107 private static int checkTimeStamp(Node node, long newNodeTimeStamp, int containerPosition) { | |
108 int count = 0; | |
109 long childTimeStamp = 0; | |
110 for(Iterator<Node> iter = node.getChildren().iterator();iter.hasNext();) { | |
111 Node n = iter.next(); | |
112 if(n.getAttributes().get("timestamp") == null) { | |
113 return containerPosition; | |
114 } | |
115 if(n.getAttributes().get("timestamp") != null) { | |
116 childTimeStamp = n.getAttributes().get("timestamp").getLong(); | |
117 if (newNodeTimeStamp < childTimeStamp) { | |
118 break; | |
119 } | |
120 } | |
121 count++; | |
122 } | |
123 return count; | |
124 } | |
125 } |