Mercurial > hg > Database > Alice
view src/main/java/alice/datasegment/Command.java @ 445:86b74532e66c dispose
change Protocol
author | sugi |
---|---|
date | Sun, 26 Oct 2014 18:21:48 +0900 |
parents | 2f2623484b77 |
children | a91890dff56e |
line wrap: on
line source
package alice.datasegment; import java.io.IOException; import java.nio.ByteBuffer; import java.util.concurrent.BlockingQueue; import org.msgpack.MessagePack; import alice.codesegment.CodeSegment; import alice.codesegment.SingletonMessage; import alice.daemon.CommandMessage; import alice.daemon.Connection; public class Command { public CommandType type; public String key; public Receiver receiver; public byte[] val; public int index; public int seq; public Connection connection; // for remote public BlockingQueue<Command> replyQueue; public CodeSegment cs; public String reverseKey; public Object obj; public boolean quickFlag; private boolean serializeFlag = true; public Command(CommandType cmdType, Receiver receiver, String key, byte[] val, int index, int seq, BlockingQueue<Command> replyQueue, CodeSegment cs, String reverseKey) { this.type = cmdType; this.receiver = receiver; this.key = key; this.val = val; this.index = index; this.seq = seq; this.replyQueue = replyQueue; this.cs = cs; this.reverseKey = reverseKey; this.quickFlag = false; } public Command(CommandType cmdType, Receiver receiver, String key, byte[] val, int index, int seq, BlockingQueue<Command> replyQueue, CodeSegment cs, String reverseKey, boolean flag) { this.type = cmdType; this.receiver = receiver; this.key = key; this.val = val; this.index = index; this.seq = seq; this.replyQueue = replyQueue; this.cs = cs; this.reverseKey = reverseKey; this.quickFlag = flag; } public Command(CommandType cmdType, Receiver receiver, String key, byte[] val, int index, int seq, Connection connection, CodeSegment cs, String reverseKey, boolean flag) { this.type = cmdType; this.receiver = receiver; this.key = key; this.val = val; this.index = index; this.seq = seq; this.connection = connection; this.cs = cs; this.reverseKey = reverseKey; this.quickFlag = flag; } public Command(CommandType cmdType, Receiver receiver, String key, Object obj, int index, int seq, BlockingQueue<Command> replyQueue, CodeSegment cs, String reverseKey) { this.type = cmdType; this.receiver = receiver; this.key = key; this.obj = obj; this.index = index; this.seq = seq; this.replyQueue = replyQueue; this.cs = cs; this.reverseKey = reverseKey; this.quickFlag = false; } public Command(CommandType cmdType, Receiver receiver, String key, byte[] val, Object obj, int index, int seq, BlockingQueue<Command> replyQueue, CodeSegment cs, String reverseKey) { this.type = cmdType; this.receiver = receiver; this.key = key; this.val = val; this.obj = obj; this.index = index; this.seq = seq; this.replyQueue = replyQueue; this.cs = cs; this.reverseKey = reverseKey; this.quickFlag = false; } public String getCommandString() { String csName = "null"; if (cs != null) { csName = cs.toString(); } return this.type + "\t" + key + "\t" + val + "\tindex=" + index + "\tcs=" + csName; } /** * @return serialized ByteBuffer */ public ByteBuffer convert() { ByteBuffer buf = null; MessagePack msg = SingletonMessage.getInstance(); try { byte[] header = msg.write(new CommandMessage(type.id, index, seq, key, quickFlag)); switch (type) { case UPDATE: case PUT: case REPLY: byte[] data = null; if (val!=null) { data = val; } else if (!serializeFlag) { data = (byte[]) obj; } else if (val==null && obj!=null) { data = msg.write(obj); } byte[] dataSize = msg.write(data.length); buf = ByteBuffer.allocate(header.length+dataSize.length+data.length); buf.put(header); buf.put(dataSize); buf.put(data); break; default: buf = ByteBuffer.allocate(header.length); buf.put(header); break; } buf.flip(); } catch (IOException e) { e.printStackTrace(); } return buf; } public void setSerializeFlag(boolean flag){ serializeFlag = flag; } }