345
|
1 package alice.datasegment;
|
|
2
|
|
3 import java.io.IOException;
|
443
|
4 import java.nio.ByteBuffer;
|
345
|
5 import java.util.concurrent.BlockingQueue;
|
|
6
|
443
|
7 import org.msgpack.MessagePack;
|
345
|
8 import alice.codesegment.CodeSegment;
|
|
9 import alice.codesegment.SingletonMessage;
|
|
10 import alice.daemon.CommandMessage;
|
|
11 import alice.daemon.Connection;
|
|
12
|
|
13 public class Command {
|
419
|
14 public CommandType type;
|
|
15 public String key;
|
|
16 public Receiver receiver;
|
443
|
17 public byte[] val;
|
419
|
18 public int index;
|
|
19 public int seq;
|
|
20 public Connection connection; // for remote
|
|
21 public BlockingQueue<Command> replyQueue;
|
|
22 public CodeSegment cs;
|
|
23 public String reverseKey;
|
|
24 public Object obj;
|
446
|
25 public boolean quickFlag = false;
|
445
|
26 private boolean serializeFlag = true;
|
446
|
27 private boolean compressFlag = true;
|
419
|
28
|
443
|
29 public Command(CommandType cmdType, Receiver receiver, String key, byte[] val, int index, int seq, BlockingQueue<Command> replyQueue, CodeSegment cs, String reverseKey) {
|
419
|
30 this.type = cmdType;
|
|
31 this.receiver = receiver;
|
|
32 this.key = key;
|
|
33 this.val = val;
|
|
34 this.index = index;
|
|
35 this.seq = seq;
|
|
36 this.replyQueue = replyQueue;
|
|
37 this.cs = cs;
|
|
38 this.reverseKey = reverseKey;
|
445
|
39 this.quickFlag = false;
|
419
|
40 }
|
|
41
|
446
|
42 public Command(CommandType cmdType, Receiver receiver, String key, byte[] val, int index, int seq, CodeSegment cs, String reverseKey, Connection connection) {
|
419
|
43 this.type = cmdType;
|
|
44 this.receiver = receiver;
|
|
45 this.key = key;
|
|
46 this.val = val;
|
|
47 this.index = index;
|
|
48 this.seq = seq;
|
|
49 this.connection = connection;
|
|
50 this.cs = cs;
|
|
51 this.reverseKey = reverseKey;
|
|
52 }
|
|
53
|
|
54 public Command(CommandType cmdType, Receiver receiver, String key, Object obj, int index, int seq, BlockingQueue<Command> replyQueue, CodeSegment cs, String reverseKey) {
|
|
55 this.type = cmdType;
|
|
56 this.receiver = receiver;
|
|
57 this.key = key;
|
|
58 this.obj = obj;
|
|
59 this.index = index;
|
|
60 this.seq = seq;
|
|
61 this.replyQueue = replyQueue;
|
|
62 this.cs = cs;
|
|
63 this.reverseKey = reverseKey;
|
|
64 }
|
|
65
|
443
|
66 public Command(CommandType cmdType, Receiver receiver, String key, byte[] val, Object obj, int index, int seq, BlockingQueue<Command> replyQueue, CodeSegment cs, String reverseKey) {
|
419
|
67 this.type = cmdType;
|
|
68 this.receiver = receiver;
|
|
69 this.key = key;
|
|
70 this.val = val;
|
|
71 this.obj = obj;
|
|
72 this.index = index;
|
|
73 this.seq = seq;
|
|
74 this.replyQueue = replyQueue;
|
|
75 this.cs = cs;
|
|
76 this.reverseKey = reverseKey;
|
|
77 }
|
|
78
|
|
79 public String getCommandString() {
|
|
80 String csName = "null";
|
|
81 if (cs != null) {
|
|
82 csName = cs.toString();
|
|
83 }
|
|
84 return this.type + "\t" + key + "\t" + val + "\tindex=" + index + "\tcs=" + csName;
|
|
85 }
|
445
|
86
|
|
87 /**
|
|
88 * @return serialized ByteBuffer
|
|
89 */
|
443
|
90 public ByteBuffer convert() {
|
|
91 ByteBuffer buf = null;
|
|
92 MessagePack msg = SingletonMessage.getInstance();
|
|
93 try {
|
445
|
94 byte[] header = msg.write(new CommandMessage(type.id, index, seq, key, quickFlag));
|
443
|
95
|
|
96 switch (type) {
|
|
97 case UPDATE:
|
|
98 case PUT:
|
|
99 case REPLY:
|
|
100 byte[] data = null;
|
445
|
101 if (val!=null) {
|
|
102 data = val;
|
|
103 } else if (!serializeFlag) {
|
|
104 data = (byte[]) obj;
|
|
105 } else if (val==null && obj!=null) {
|
443
|
106 data = msg.write(obj);
|
|
107 }
|
|
108 byte[] dataSize = msg.write(data.length);
|
|
109
|
|
110 buf = ByteBuffer.allocate(header.length+dataSize.length+data.length);
|
|
111 buf.put(header);
|
|
112 buf.put(dataSize);
|
|
113 buf.put(data);
|
|
114 break;
|
|
115 default:
|
|
116 buf = ByteBuffer.allocate(header.length);
|
|
117 buf.put(header);
|
|
118 break;
|
419
|
119 }
|
443
|
120
|
|
121 buf.flip();
|
|
122 } catch (IOException e) {
|
|
123 e.printStackTrace();
|
419
|
124 }
|
443
|
125 return buf;
|
419
|
126 }
|
445
|
127
|
446
|
128 public void setQuickFlag(boolean flag){
|
|
129 quickFlag = flag;
|
|
130 }
|
|
131
|
445
|
132 public void setSerializeFlag(boolean flag){
|
|
133 serializeFlag = flag;
|
|
134 }
|
446
|
135
|
|
136 public void setCompressFlag(boolean flag){
|
|
137 compressFlag = flag;
|
|
138 }
|
345
|
139 }
|