annotate src/main/java/alice/datasegment/Command.java @ 467:6e304a7a60e7 dispose

remove white space
author sugi
date Sat, 22 Nov 2014 12:08:24 +0900
parents 99e309768ac9
children 7ef0ebb40c9b
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 compressFlag = false;
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
30
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
31 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
32 this.type = cmdType;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
33 this.receiver = receiver;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
34 this.key = key;
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
35 this.rData = rData;
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
36 this.index = index;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
37 this.seq = seq;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
38 this.replyQueue = replyQueue;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
39 this.cs = cs;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
40 this.reverseKey = reverseKey;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
41 }
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
42
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
43 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
44 this.type = cmdType;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
45 this.receiver = receiver;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
46 this.key = key;
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
47 this.rData = rData;
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
48 this.index = index;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
49 this.seq = seq;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
50 this.connection = connection;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
51 this.cs = cs;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
52 this.reverseKey = reverseKey;
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
53 }
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
54
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
55 public String getCommandString() {
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
56 String csName = "null";
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
57 if (cs != null) {
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
58 csName = cs.toString();
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
59 }
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
60 return this.type + "\t" + key + "\t" + rData + "\tindex=" + index + "\tcs=" + csName;
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
61 }
467
6e304a7a60e7 remove white space
sugi
parents: 461
diff changeset
62
445
86b74532e66c change Protocol
sugi
parents: 443
diff changeset
63 /**
86b74532e66c change Protocol
sugi
parents: 443
diff changeset
64 * @return serialized ByteBuffer
86b74532e66c change Protocol
sugi
parents: 443
diff changeset
65 */
443
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
66 public ByteBuffer convert() {
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
67 ByteBuffer buf = null;
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
68 MessagePack msg = SingletonMessage.getInstance();
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
69 try {
452
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
70 byte[] header = null;
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
71 byte[] data = null;
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
72 byte[] dataSize = null;
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
73 boolean serialized = false;
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
74 boolean compressed = false;
443
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
75 switch (type) {
452
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
76 /*
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
77 * UPDATE, PUT, REPLY need send DataSegment to RemoteDataSegment
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
78 * case UPDATE and PUT
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
79 * compress and serialize flag are selected by user, so if true, need convert.
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
80 * case REPLY
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
81 * these flags represent DataSegment status.
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
82 * for example, serializeFlag is true. DataSegment had already converted, so no need convert.
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
83 */
443
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
84 case UPDATE:
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
85 case PUT:
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
86 case REPLY:
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
87 if (rData.compressed()) {
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
88 // have already converted
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
89 data = (byte[]) rData.getObj();
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
90 compressed = rData.compressed(); // true
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
91 serialized = rData.serialized();
452
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
92 } else {
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
93 if (!rData.serialized() && !rData.isByteArray()) {
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
94 data = msg.write(rData.getObj());
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
95 serialized = true;
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
96 } else { // rData is RAW ByteArray or already serialized
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
97 data = (byte[]) rData.getObj();
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
98 serialized = rData.serialized();
461
99e309768ac9 use zip method high cost. need improve
sugi
parents: 459
diff changeset
99 }
99e309768ac9 use zip method high cost. need improve
sugi
parents: 459
diff changeset
100 if (compressFlag) {
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
101 data = zip(data);
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
102 compressed = true;
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
103 }
443
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
104 }
467
6e304a7a60e7 remove white space
sugi
parents: 461
diff changeset
105
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
106 header = msg.write(new CommandMessage(type.id, index, seq, key, false, serialized, compressed));
461
99e309768ac9 use zip method high cost. need improve
sugi
parents: 459
diff changeset
107 dataSize = msg.write(data.length);
443
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
108 buf = ByteBuffer.allocate(header.length+dataSize.length+data.length);
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
109 buf.put(header);
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
110 buf.put(dataSize);
452
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
111 buf.put(data);
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
112 break;
443
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
113 default:
459
4419a2415661 can use compress option
sugi
parents: 458
diff changeset
114 header = msg.write(new CommandMessage(type.id, index, seq, key, quickFlag, false, compressFlag));
443
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
115 buf = ByteBuffer.allocate(header.length);
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
116 buf.put(header);
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
117 break;
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
118 }
443
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
119
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
120 buf.flip();
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
121 } catch (IOException e) {
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
122 e.printStackTrace();
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
123 }
443
2f2623484b77 change protocol
sugi
parents: 419
diff changeset
124 return buf;
419
aefbe41fcf12 change tab to space
sugi
parents: 345
diff changeset
125 }
467
6e304a7a60e7 remove white space
sugi
parents: 461
diff changeset
126
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
127 /**
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
128 * If this flag is true, command isn't send queue.
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
129 * command is executed right now.
467
6e304a7a60e7 remove white space
sugi
parents: 461
diff changeset
130 *
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
131 * @param flag
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
132 */
467
6e304a7a60e7 remove white space
sugi
parents: 461
diff changeset
133
446
a91890dff56e refactor
sugi
parents: 445
diff changeset
134 public void setQuickFlag(boolean flag){
a91890dff56e refactor
sugi
parents: 445
diff changeset
135 quickFlag = flag;
a91890dff56e refactor
sugi
parents: 445
diff changeset
136 }
467
6e304a7a60e7 remove white space
sugi
parents: 461
diff changeset
137
448
4840d0e2b605 add compress and serialize Flag.
sugi
parents: 446
diff changeset
138 public boolean getQuickFlag(){
4840d0e2b605 add compress and serialize Flag.
sugi
parents: 446
diff changeset
139 return quickFlag;
4840d0e2b605 add compress and serialize Flag.
sugi
parents: 446
diff changeset
140 }
467
6e304a7a60e7 remove white space
sugi
parents: 461
diff changeset
141
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
142 /**
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
143 * Before sending Remote DataSegment, DataSegment type is ByteArray.
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
144 * If this flag true, ByteArray is compressed with ZRLEE(ZRIB) algorithm
467
6e304a7a60e7 remove white space
sugi
parents: 461
diff changeset
145 *
458
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
146 * @param flag
bcf6f4a6fcd0 need set Meta DataSegment PUT API
sugi
parents: 455
diff changeset
147 */
467
6e304a7a60e7 remove white space
sugi
parents: 461
diff changeset
148
446
a91890dff56e refactor
sugi
parents: 445
diff changeset
149 public void setCompressFlag(boolean flag){
a91890dff56e refactor
sugi
parents: 445
diff changeset
150 compressFlag = flag;
a91890dff56e refactor
sugi
parents: 445
diff changeset
151 }
467
6e304a7a60e7 remove white space
sugi
parents: 461
diff changeset
152
448
4840d0e2b605 add compress and serialize Flag.
sugi
parents: 446
diff changeset
153 public boolean getCompressFlag(){
4840d0e2b605 add compress and serialize Flag.
sugi
parents: 446
diff changeset
154 return compressFlag;
4840d0e2b605 add compress and serialize Flag.
sugi
parents: 446
diff changeset
155 }
467
6e304a7a60e7 remove white space
sugi
parents: 461
diff changeset
156
452
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
157 public byte[] zip(byte[] input) throws IOException{
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
158 Deflater deflater = new Deflater();
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
159 ByteArrayOutputStream os = new ByteArrayOutputStream();
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
160 DeflaterOutputStream dos = new DeflaterOutputStream(os, deflater);
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
161 dos.write(input);
467
6e304a7a60e7 remove white space
sugi
parents: 461
diff changeset
162 dos.finish();
452
f68d103498e0 refactor (InputDataSegment holder class changed)
sugi
parents: 450
diff changeset
163 return os.toByteArray();
467
6e304a7a60e7 remove white space
sugi
parents: 461
diff changeset
164 }
345
8f71c3e6f11d Change directory structure Maven standard
sugi
parents:
diff changeset
165 }