Mercurial > hg > Database > jungle-network
annotate src/alice/jungle/datasegment/store/operations/DefaultTreeOperationLogContainer.java @ 43:12d87bb4b84d
modified LogUpdateCodeSegment
author | one |
---|---|
date | Fri, 12 Jul 2013 09:13:54 +0900 |
parents | 1255ae20d52e |
children | a89c3539bff2 |
rev | line source |
---|---|
31 | 1 package alice.jungle.datasegment.store.operations; |
7 | 2 |
3 import java.io.IOException; | |
4 import java.nio.ByteBuffer; | |
5 import java.util.LinkedList; | |
6 import java.util.List; | |
7 | |
8 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.Command; | |
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; | |
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath; | |
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.DefaultTreeOperationLog; | |
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog; | |
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.DefaultTreeOperation; | |
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.NodeOperation; | |
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.PutAttributeOperation; | |
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation; | |
17 | |
18 import org.msgpack.MessagePack; | |
19 import org.msgpack.annotation.Message; | |
20 import org.msgpack.template.ListTemplate; | |
21 import org.msgpack.template.ValueTemplate; | |
22 import org.msgpack.type.Value; | |
23 | |
24 @Message | |
25 public class DefaultTreeOperationLogContainer { | |
26 | |
27 Value logValue; | |
34 | 28 String treeName; |
29 String uuid; | |
43 | 30 String serverName; |
31 long revision; | |
7 | 32 |
33 public static void main(String[] args) throws IOException { | |
34 String key = "hoge"; | |
35 ByteBuffer b = ByteBuffer.wrap("messagepack value".getBytes()); | |
36 PutAttributeOperation putOp = new PutAttributeOperation(key, b); | |
37 DefaultNodePath nodePath = new DefaultNodePath(); | |
38 nodePath = nodePath.add(1).add(2).add(3); | |
39 TreeOperation op = new DefaultTreeOperation(nodePath, putOp); | |
8
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
40 List<TreeOperation> list = new LinkedList<TreeOperation>(); |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
41 list.add(op); |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
42 list.add(op); |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
43 list.add(op); |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
44 DefaultTreeOperationLog log = new DefaultTreeOperationLog(list, list.size()); |
7 | 45 |
8
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
46 DefaultTreeOperationLogContainer logContainer = new DefaultTreeOperationLogContainer(); |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
47 logContainer.unconvert(log); |
34 | 48 logContainer.setUUID("uuid"); |
49 logContainer.setTreeName("treeName"); | |
9 | 50 |
51 MessagePack msgpack = new MessagePack(); | |
52 Value logContainerValue = msgpack.unconvert(logContainer); | |
53 DefaultTreeOperationLogContainer convertedLogContainer = msgpack.convert(logContainerValue, | |
54 DefaultTreeOperationLogContainer.class); | |
8
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
55 |
9 | 56 TreeOperationLog convertedLog = convertedLogContainer.convert(); |
34 | 57 System.out.println("uuid = "+ convertedLogContainer.getUUID()); |
58 System.out.println("treeName = "+ convertedLogContainer.getTreeName()); | |
8
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
59 for (TreeOperation treeOp : convertedLog) { |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
60 NodePath path = treeOp.getNodePath(); |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
61 NodeOperation nodeOp = treeOp.getNodeOperation(); |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
62 Command c = nodeOp.getCommand(); |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
63 String str = ""; |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
64 switch (c) { |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
65 case PUT_ATTRIBUTE: |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
66 String k = nodeOp.getKey(); |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
67 ByteBuffer value = nodeOp.getValue(); |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
68 if (value.limit() < 100) { |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
69 str = String.format("key:%s,value:%s", k, |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
70 new String(value.array())); |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
71 } else { |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
72 str = String.format("key:%s,value:%d", k, value.limit()); |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
73 } |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
74 break; |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
75 case DELETE_ATTRIBUTE: |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
76 str = String.format("key:%s", nodeOp.getKey()); |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
77 break; |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
78 case APPEND_CHILD: |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
79 str = String.format("pos:%d", nodeOp.getPosition()); |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
80 break; |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
81 case DELETE_CHILD: |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
82 str = String.format("pos:%d", nodeOp.getPosition()); |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
83 break; |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
84 } |
9 | 85 System.out.println(String.format("[%s:%s:%s]", c, path, str)); |
86 for (int i: path ) { | |
8
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
87 System.out.println(i); |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
88 } |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
89 |
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
90 } |
7 | 91 } |
92 | |
93 public DefaultTreeOperationLogContainer() { | |
34 | 94 logValue = null; |
95 treeName = ""; | |
96 uuid = ""; | |
97 } | |
98 | |
99 public void setTreeName(String _treeName) { | |
100 treeName = _treeName; | |
101 } | |
102 | |
43 | 103 public String getTreeName() { |
104 return treeName; | |
105 } | |
106 | |
34 | 107 public void setUUID(String _uuid) { |
108 uuid = _uuid; | |
109 } | |
110 | |
111 public String getUUID() { | |
112 return uuid; | |
113 } | |
43 | 114 |
115 public void setServerName(String _serverName) { | |
116 serverName = _serverName; | |
117 } | |
34 | 118 |
43 | 119 public String getServerName() { |
120 return serverName; | |
121 } | |
122 | |
123 public void setRevision(long _revision) { | |
124 revision = _revision; | |
125 } | |
126 | |
127 public long getRevision() { | |
128 return revision; | |
7 | 129 } |
130 | |
33 | 131 public void unconvert(Iterable<TreeOperation> _log) throws IOException { |
7 | 132 MessagePack msgpack = new MessagePack(); |
133 List<Value> list = new LinkedList<Value>(); | |
134 for(TreeOperation op : _log) { | |
135 NodeOperation nOp = op.getNodeOperation(); | |
136 NodePath nodePath = op.getNodePath(); | |
8
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
137 DefaultTreeOperation treeOp = new DefaultTreeOperation(nodePath, nOp); |
7 | 138 DefaultTreeOperationContainer container = new DefaultTreeOperationContainer(); |
8
ee93e16d5a3f
implement unconvert and convert method in DefaultTreeOperationLog class
one
parents:
7
diff
changeset
|
139 container.unconvert(treeOp); |
7 | 140 Value v = msgpack.unconvert(container); |
141 list.add(v); | |
142 } | |
143 Value listValue = msgpack.unconvert(list); | |
144 logValue = listValue; | |
145 } | |
146 | |
147 public DefaultTreeOperationLog convert() throws IOException { | |
148 MessagePack msgpack = new MessagePack(); | |
149 msgpack.register(List.class, new ListTemplate(ValueTemplate.getInstance())); | |
150 List<Value> listValue = msgpack.convert(logValue, List.class); | |
151 List<TreeOperation> logList = new LinkedList<TreeOperation>(); | |
152 for(Value v: listValue) { | |
153 DefaultTreeOperationContainer container = msgpack.convert(v, DefaultTreeOperationContainer.class); | |
154 logList.add(container.convert()); | |
155 } | |
156 DefaultTreeOperationLog log = new DefaultTreeOperationLog(logList, logList.size()); | |
157 return log; | |
158 } | |
159 | |
160 | |
161 | |
162 } |