001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.memorydatastore.message;
005:
006: import com.tc.bytes.TCByteBuffer;
007: import com.tc.io.TCByteBufferOutput;
008: import com.tc.net.protocol.tcm.MessageChannel;
009: import com.tc.net.protocol.tcm.MessageMonitor;
010: import com.tc.net.protocol.tcm.TCMessageHeader;
011: import com.tc.net.protocol.tcm.TCMessageType;
012: import com.tc.object.lockmanager.api.ThreadID;
013: import com.tc.object.msg.DSOMessageBase;
014: import com.tc.object.session.SessionID;
015:
016: import java.io.IOException;
017:
018: public class MemoryDataStoreRequestMessage extends DSOMessageBase {
019: private static final byte TYPE = 1;
020: private static final byte DATA_STORE_NAME = 2;
021: private static final byte THREAD_ID = 3;
022: private static final byte DATA = 4;
023: private static final byte REMOVE_ALL_FLAG = 5;
024: private static final byte GET_ALL_FLAG = 6;
025:
026: public static final int PUT = 1;
027: public static final int GET = 2;
028: public static final int REMOVE = 3;
029:
030: private int type;
031: private boolean removeAll;
032: private boolean getAll;
033: private String dataStoreName;
034: private ThreadID threadID;
035: private TCMemoryDataStoreMessageData data;
036:
037: public MemoryDataStoreRequestMessage(SessionID sessionID,
038: MessageMonitor monitor, TCByteBufferOutput out,
039: MessageChannel channel, TCMessageType type) {
040: super (sessionID, monitor, out, channel, type);
041: }
042:
043: public MemoryDataStoreRequestMessage(SessionID sessionID,
044: MessageMonitor monitor, MessageChannel channel,
045: TCMessageHeader header, TCByteBuffer[] data) {
046: super (sessionID, monitor, channel, header, data);
047: }
048:
049: protected void dehydrateValues() {
050: putNVPair(TYPE, this .type);
051: putNVPair(THREAD_ID, this .threadID.toLong());
052: putNVPair(DATA_STORE_NAME, this .dataStoreName);
053: if (isRemoveAll()) {
054: putNVPair(REMOVE_ALL_FLAG, this .removeAll);
055: }
056: if (isGetAll()) {
057: putNVPair(GET_ALL_FLAG, this .getAll);
058: }
059: putNVPair(DATA, data);
060: }
061:
062: protected String describePayload() {
063: StringBuffer rv = new StringBuffer();
064: rv.append("Type : ");
065:
066: if (isPut()) {
067: rv.append("PUT \n");
068: } else if (isGet()) {
069: rv.append("GET \n");
070: } else if (isRemove()) {
071: rv.append("REMOVE \n");
072: } else {
073: rv.append("UNKNOWN \n");
074: }
075:
076: rv.append(threadID.toLong()).append(" \n");
077: rv.append(data).append(" from ").append(dataStoreName).append(
078: '\n');
079:
080: return rv.toString();
081: }
082:
083: protected boolean hydrateValue(byte name) throws IOException {
084: switch (name) {
085: case TYPE:
086: this .type = getIntValue();
087: return true;
088: case THREAD_ID:
089: this .threadID = new ThreadID(getLongValue());
090: return true;
091: case REMOVE_ALL_FLAG:
092: this .removeAll = getBooleanValue();
093: return true;
094: case GET_ALL_FLAG:
095: this .getAll = getBooleanValue();
096: return true;
097: case DATA_STORE_NAME:
098: this .dataStoreName = getStringValue();
099: return true;
100: case DATA:
101: data = new TCMemoryDataStoreMessageData(type);
102: getObject(data);
103: return true;
104: default:
105: return false;
106: }
107: }
108:
109: public void initializePut(ThreadID threadID, String dataStoreName,
110: byte[] key, byte[] value) {
111: this .type = PUT;
112: this .threadID = threadID;
113: this .dataStoreName = dataStoreName;
114: this .data = new TCMemoryDataStoreMessageData(type, key, value);
115: }
116:
117: public void initializeGet(ThreadID threadID, String dataStoreName,
118: byte[] key, boolean getAll) {
119: this .type = GET;
120: this .threadID = threadID;
121: this .dataStoreName = dataStoreName;
122: this .getAll = getAll;
123: this .data = new TCMemoryDataStoreMessageData(type, key);
124: }
125:
126: public void initializeRemove(ThreadID threadID,
127: String dataStoreName, byte[] key, boolean removeAll) {
128: this .type = REMOVE;
129: this .threadID = threadID;
130: this .dataStoreName = dataStoreName;
131: this .removeAll = removeAll;
132: this .data = new TCMemoryDataStoreMessageData(type, key);
133: }
134:
135: public byte[] getKey() {
136: return this .data.getKey();
137: }
138:
139: public byte[] getValue() {
140: return this .data.getValue();
141: }
142:
143: public int getType() {
144: return this .type;
145: }
146:
147: public ThreadID getThreadID() {
148: return this .threadID;
149: }
150:
151: public String getDataStoreName() {
152: return this .dataStoreName;
153: }
154:
155: public boolean isRemoveAll() {
156: return removeAll;
157: }
158:
159: public boolean isGetAll() {
160: return getAll;
161: }
162:
163: private boolean isPut() {
164: return this .type == PUT;
165: }
166:
167: private boolean isGet() {
168: return this .type == GET;
169: }
170:
171: private boolean isRemove() {
172: return this.type == REMOVE;
173: }
174: }
|