2
|
1 package jungle.test.datasegment.store.operations;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.nio.ByteBuffer;
|
|
5
|
|
6 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.Command;
|
|
7 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.AppendChildAtOperation;
|
|
8 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.DeleteAttributeOperation;
|
|
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.DeleteChildAtOperation;
|
|
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.NodeOperation;
|
|
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.PutAttributeOperation;
|
|
12
|
|
13 import org.msgpack.MessagePack;
|
|
14 import org.msgpack.annotation.Message;
|
|
15 import org.msgpack.template.OrdinalEnumTemplate;
|
|
16 import org.msgpack.type.Value;
|
|
17
|
|
18 @Message
|
|
19 public class DefaultNodeOperationContainer {
|
|
20
|
|
21 public int pos;
|
|
22 public String key;
|
|
23 public Value value;
|
|
24 public Value commandValue;
|
|
25
|
3
|
26 public static void main(String[] args) throws IOException {
|
|
27 String key = "hoge";
|
|
28 ByteBuffer b = ByteBuffer.wrap("messagepack value".getBytes());
|
|
29 PutAttributeOperation op = new PutAttributeOperation(key, b);
|
|
30 DefaultNodeOperationContainer container = new DefaultNodeOperationContainer();
|
|
31 container.unconvert(op);
|
|
32 NodeOperation convertedOp = container.convert();
|
|
33 System.out.println("pos : "+convertedOp.getPosition());
|
|
34 System.out.println("Command : "+convertedOp.getCommand());
|
|
35 System.out.println("key : "+convertedOp.getKey());
|
|
36 System.out.println("value : "+new String(convertedOp.getValue().array()));
|
|
37
|
|
38 }
|
2
|
39
|
|
40 public DefaultNodeOperationContainer() {
|
|
41
|
|
42 }
|
|
43
|
|
44 public void unconvert(NodeOperation op) throws IOException {
|
|
45 MessagePack msgpack = new MessagePack();
|
|
46 pos = op.getPosition();
|
|
47 key = op.getKey();
|
|
48 value = null;
|
|
49 if (op.getValue() != null) {
|
|
50 ByteBuffer b = op.getValue();
|
3
|
51 byte[] bytes = b.array();
|
|
52 Value v = msgpack.unconvert(bytes);
|
2
|
53 value = v;
|
|
54 }
|
|
55 Command c = op.getCommand();
|
3
|
56 msgpack.register(Command.class, new OrdinalEnumTemplate(Command.class));
|
2
|
57 Value cValue = msgpack.unconvert(c);
|
|
58 commandValue = cValue;
|
|
59 }
|
|
60
|
|
61 public NodeOperation convert() throws IOException{
|
|
62 MessagePack msgpack = new MessagePack();
|
3
|
63 msgpack.register(Command.class, new OrdinalEnumTemplate(Command.class));
|
2
|
64 Command c = msgpack.convert(commandValue, Command.class);
|
|
65 ByteBuffer b = null;
|
|
66 if (value != null) {
|
3
|
67 b = ByteBuffer.wrap(msgpack.convert(value, byte[].class));
|
2
|
68 }
|
|
69 if (c == Command.PUT_ATTRIBUTE) {
|
|
70 return new PutAttributeOperation(key, b);
|
|
71 } else if (c == Command.APPEND_CHILD) {
|
|
72 return new AppendChildAtOperation(pos);
|
|
73 } else if (c == Command.DELETE_CHILD) {
|
|
74 return new DeleteChildAtOperation(pos);
|
|
75 } else if (c == Command.DELETE_ATTRIBUTE){
|
|
76 return new DeleteAttributeOperation(key);
|
|
77 }
|
|
78 return null;
|
|
79 }
|
|
80
|
|
81 }
|