annotate src/main/java/alice/datasegment/Command.java @ 458:bcf6f4a6fcd0 dispose

need set Meta DataSegment PUT API
author sugi
date Mon, 03 Nov 2014 17:12:53 +0900
parents b004f62b83e5
children 4419a2415661
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
1 package alice.datasegment;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
2
452
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
3 import java.io.ByteArrayOutputStream;
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
4 import java.io.IOException;
443
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
5 import java.nio.ByteBuffer;
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
6 import java.util.concurrent.BlockingQueue;
452
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
7 import java.util.zip.Deflater;
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
8 import java.util.zip.DeflaterOutputStream;
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
9
443
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
10 import org.msgpack.MessagePack;
452
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
11
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
12 import alice.codesegment.CodeSegment;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
13 import alice.codesegment.SingletonMessage;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
14 import alice.daemon.CommandMessage;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
15 import alice.daemon.Connection;
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
16
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
17 public class Command {
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
18 public CommandType type;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
19 public String key;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
20 public Receiver receiver;
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
21 public ReceiveData rData;
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
22 public int index;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
23 public int seq;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
24 public Connection connection; // for remote
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
25 public BlockingQueue<Command> replyQueue;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
26 public CodeSegment cs;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
27 public String reverseKey;
448
4840d0e2b605 add compress and serialize Flag.
sugi
parents: 446
diff changeset
28 private boolean quickFlag = false;
452
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
29 private boolean serializeFlag = false;
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
30 private boolean compressFlag = false;
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
31
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
32 public Command(CommandType cmdType, Receiver receiver, String key, ReceiveData rData, int index, int seq, BlockingQueue<Command> replyQueue, CodeSegment cs, String reverseKey) {
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
33 this.type = cmdType;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
34 this.receiver = receiver;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
35 this.key = key;
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
36 this.rData = rData;
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
37 this.index = index;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
38 this.seq = seq;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
39 this.replyQueue = replyQueue;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
40 this.cs = cs;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
41 this.reverseKey = reverseKey;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
42 }
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
43
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
44 public Command(CommandType cmdType, Receiver receiver, String key, ReceiveData rData, int index, int seq, CodeSegment cs, String reverseKey, Connection connection) {
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
45 this.type = cmdType;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
46 this.receiver = receiver;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
47 this.key = key;
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
48 this.rData = rData;
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
49 this.index = index;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
50 this.seq = seq;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
51 this.connection = connection;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
52 this.cs = cs;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
53 this.reverseKey = reverseKey;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
54 }
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
55
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
56 public String getCommandString() {
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
57 String csName = "null";
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
58 if (cs != null) {
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
59 csName = cs.toString();
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
60 }
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
61 return this.type + "\t" + key + "\t" + rData + "\tindex=" + index + "\tcs=" + csName;
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
62 }
445
86b74532e66c change Protocol
sugi
parents: 443
diff changeset
63
86b74532e66c change Protocol
sugi
parents: 443
diff changeset
64 /**
86b74532e66c change Protocol
sugi
parents: 443
diff changeset
65 * @return serialized ByteBuffer
86b74532e66c change Protocol
sugi
parents: 443
diff changeset
66 */
443
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
67 public ByteBuffer convert() {
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
68 ByteBuffer buf = null;
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
69 MessagePack msg = SingletonMessage.getInstance();
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
70 try {
452
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
71 byte[] header = null;
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
72 byte[] data = null;
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
73 byte[] dataSize = null;
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
74 boolean serialized = false;
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
75 boolean compressed = false;
443
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
76 switch (type) {
452
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
77 /*
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
78 * UPDATE, PUT, REPLY need send DataSegment to RemoteDataSegment
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
79 * case UPDATE and PUT
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
80 * compress and serialize flag are selected by user, so if true, need convert.
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
81 * case REPLY
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
82 * these flags represent DataSegment status.
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
83 * for example, serializeFlag is true. DataSegment had already converted, so no need convert.
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
84 */
443
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
85 case UPDATE:
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
86 case PUT:
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
87 case REPLY:
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
88 if (rData.compressed()) {
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
89 // have already converted
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
90 data = (byte[]) rData.getObj();
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
91 compressed = rData.compressed(); // true
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
92 serialized = rData.serialized();
452
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
93 } else {
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
94 if (!rData.serialized() && !rData.isByteArray()) {
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
95 data = msg.write(rData.getObj());
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
96 serialized = true;
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
97 } else { // rData is RAW ByteArray or already serialized
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
98 data = (byte[]) rData.getObj();
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
99 serialized = rData.serialized();
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
100 }
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
101 if (compressFlag) {
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
102 data = zip(data);
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
103 compressed = true;
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
104 }
443
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
105 }
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
106
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
107 header = msg.write(new CommandMessage(type.id, index, seq, key, false, serialized, compressed));
452
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
108 dataSize = msg.write(data.length);
443
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
109 buf = ByteBuffer.allocate(header.length+dataSize.length+data.length);
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
110 buf.put(header);
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
111 buf.put(dataSize);
452
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
112 buf.put(data);
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
113 break;
443
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
114 default:
452
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
115 header = msg.write(new CommandMessage(type.id, index, seq, key, quickFlag, serializeFlag, compressFlag));
443
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
116 buf = ByteBuffer.allocate(header.length);
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
117 buf.put(header);
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
118 break;
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
119 }
443
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
120
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
121 buf.flip();
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
122 } catch (IOException e) {
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
123 e.printStackTrace();
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
124 }
443
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
125 return buf;
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
126 }
445
86b74532e66c change Protocol
sugi
parents: 443
diff changeset
127
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
128 /**
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
129 * If this flag is true, command isn't send queue.
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
130 * command is executed right now.
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
131 *
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
132 * @param flag
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
133 */
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
134
446
a91890dff56e refactor
sugi
parents: 445
diff changeset
135 public void setQuickFlag(boolean flag){
a91890dff56e refactor
sugi
parents: 445
diff changeset
136 quickFlag = flag;
a91890dff56e refactor
sugi
parents: 445
diff changeset
137 }
a91890dff56e refactor
sugi
parents: 445
diff changeset
138
448
4840d0e2b605 add compress and serialize Flag.
sugi
parents: 446
diff changeset
139 public boolean getQuickFlag(){
4840d0e2b605 add compress and serialize Flag.
sugi
parents: 446
diff changeset
140 return quickFlag;
4840d0e2b605 add compress and serialize Flag.
sugi
parents: 446
diff changeset
141 }
4840d0e2b605 add compress and serialize Flag.
sugi
parents: 446
diff changeset
142
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
143 /**
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
144 * If this flag is true, DataSegment isn't serialized.
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
145 * Alice auto select true or false.
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
146 *
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
147 * @param flag
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
148 */
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
149
445
86b74532e66c change Protocol
sugi
parents: 443
diff changeset
150 public void setSerializeFlag(boolean flag){
86b74532e66c change Protocol
sugi
parents: 443
diff changeset
151 serializeFlag = flag;
86b74532e66c change Protocol
sugi
parents: 443
diff changeset
152 }
446
a91890dff56e refactor
sugi
parents: 445
diff changeset
153
448
4840d0e2b605 add compress and serialize Flag.
sugi
parents: 446
diff changeset
154 public boolean getSerializeFlag(){
4840d0e2b605 add compress and serialize Flag.
sugi
parents: 446
diff changeset
155 return serializeFlag;
4840d0e2b605 add compress and serialize Flag.
sugi
parents: 446
diff changeset
156 }
4840d0e2b605 add compress and serialize Flag.
sugi
parents: 446
diff changeset
157
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
158 /**
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
159 * Before sending Remote DataSegment, DataSegment type is ByteArray.
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
160 * If this flag true, ByteArray is compressed with ZRLEE(ZRIB) algorithm
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
161 *
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
162 * @param flag
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
163 */
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
164
446
a91890dff56e refactor
sugi
parents: 445
diff changeset
165 public void setCompressFlag(boolean flag){
a91890dff56e refactor
sugi
parents: 445
diff changeset
166 compressFlag = flag;
a91890dff56e refactor
sugi
parents: 445
diff changeset
167 }
448
4840d0e2b605 add compress and serialize Flag.
sugi
parents: 446
diff changeset
168
4840d0e2b605 add compress and serialize Flag.
sugi
parents: 446
diff changeset
169 public boolean getCompressFlag(){
4840d0e2b605 add compress and serialize Flag.
sugi
parents: 446
diff changeset
170 return compressFlag;
4840d0e2b605 add compress and serialize Flag.
sugi
parents: 446
diff changeset
171 }
452
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
172
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
173 public byte[] zip(byte[] input) throws IOException{
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
174 Deflater deflater = new Deflater();
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
175 ByteArrayOutputStream os = new ByteArrayOutputStream();
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
176 DeflaterOutputStream dos = new DeflaterOutputStream(os, deflater);
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
177 dos.write(input);
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
178 dos.finish();
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
179 return os.toByteArray();
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
180 }
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
181 }