annotate src/main/java/alice/datasegment/Command.java @ 446:a91890dff56e dispose

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