5
5
import net .spy .memcached .compat .SpyObject ;
6
6
7
7
/**
8
- * The HeaderMessage implements the header of a tap message. This class cannot be instantiated.
9
- * Tap stream messages are created with the RequestMessage and ResponseMessage classes. Users
10
- * who want to take advantage of customizing their own tap messages should use the
11
- * CustomTapStream class since it provides flexibility to create all valid tap messages.
8
+ * The BaseMessage implements the header of a tap message. This class cannot be instantiated.
9
+ * Tap stream messages are created with the RequestMessage and ResponseMessage classes.
12
10
*/
13
- public class BaseMessage extends SpyObject {
14
- /**
15
- * The index of the magic field in a tap header.
16
- */
17
- public static final int MAGIC_INDEX = 0 ;
18
-
19
- /**
20
- * The length of the magic field in a tap header.
21
- */
22
- public static final int MAGIC_FIELD_LENGTH = 1 ;
23
-
24
- /**
25
- * The index of the opcode field in a tap header.
26
- */
27
- public static final int OPCODE_INDEX = 1 ;
28
-
29
- /**
30
- * The length of the opcode field in a tap header.
31
- */
32
- public static final int OPCODE_FIELD_LENGTH = 1 ;
33
-
34
- /**
35
- * The index of the key length field in a tap header.
36
- */
37
- public static final int KEY_LENGTH_INDEX = 2 ;
38
-
39
- /**
40
- * The length of the key length field in a tap header.
41
- */
42
- public static final int KEY_LENGTH_FIELD_LENGTH = 2 ;
43
-
44
- /**
45
- * The index of the extra length field in the tap header.
46
- */
47
- public static final int EXTRA_LENGTH_INDEX = 4 ;
48
-
49
- /**
50
- * The length of the extra length field in a tap header.
51
- */
52
- public static final int EXTRA_LENGTH_FIELD_LENGTH = 1 ;
53
-
54
- /**
55
- * The index of the data type field in the tap header.
56
- */
57
- public static final int DATA_TYPE_INDEX = 5 ;
58
-
59
- /**
60
- * The length of the data type field in a tap header.
61
- */
62
- public static final int DATA_TYPE_FIELD_LENGTH = 1 ;
63
-
64
- /**
65
- * The index of the vbucket field in the tap header.
66
- */
67
- public static final int VBUCKET_INDEX = 6 ;
68
-
69
- /**
70
- * The length of the vbucket field in a tap header.
71
- */
72
- public static final int VBUCKET_FIELD_LENGTH = 2 ;
73
-
74
- /**
75
- * The index of the total body field in the tap header.
76
- */
77
- public static final int TOTAL_BODY_INDEX = 8 ;
78
-
79
- /**
80
- * The length of the total body field in the tap header.
81
- */
82
- public static final int TOTAL_BODY_FIELD_LENGTH = 4 ;
83
-
84
- /**
85
- * The index of the opaque field in the tap header.
86
- */
87
- public static final int OPAQUE_INDEX = 12 ;
88
-
89
- /**
90
- * The length of the opaque field in the tap header.
91
- */
92
- public static final int OPAQUE_FIELD_LENGTH = 4 ;
93
-
94
- /**
95
- * The index of the cas field in the tap header.
96
- */
97
- public static final int CAS_INDEX = 16 ;
98
-
99
- /**
100
- * The length of the cas field in the tap header.
101
- */
102
- public static final int CAS_FIELD_LENGTH = 8 ;
103
-
104
- /**
105
- * The header length
106
- */
11
+ public abstract class BaseMessage extends SpyObject {
12
+ private static final int MAGIC_OFFSET = 0 ;
13
+ private static final int OPCODE_OFFSET = 1 ;
14
+ private static final int KEYLENGTH_OFFSET = 2 ;
15
+ private static final int EXTRALENGTH_OFFSET = 4 ;
16
+ private static final int DATATYPE_OFFSET = 5 ;
17
+ private static final int VBUCKET_OFFSET = 6 ;
18
+ private static final int TOTALBODY_OFFSET = 8 ;
19
+ private static final int OPAQUE_OFFSET = 12 ;
20
+ private static final int CAS_OFFSET = 16 ;
107
21
public static final int HEADER_LENGTH = 24 ;
108
22
109
- /**
110
- * Holds the binary data for the header field.
111
- */
23
+ protected TapMagic magic ;
24
+ protected TapOpcode opcode ;
25
+ protected short keylength ;
26
+ protected byte extralength ;
27
+ protected byte datatype ;
28
+ protected short vbucket ;
29
+ protected int totalbody ;
30
+ protected int opaque ;
31
+ protected long cas ;
112
32
113
- protected byte [] mbytes ;
114
- /**
115
- * Instantiates a tap header.
116
- */
117
33
protected BaseMessage () {
118
- mbytes = new byte [HEADER_LENGTH ];
34
+ // Empty
35
+ }
36
+
37
+ protected BaseMessage (byte [] b ) {
38
+ magic = TapMagic .getMagicByByte (b [MAGIC_OFFSET ]);
39
+ opcode = TapOpcode .getOpcodeByByte (b [OPCODE_OFFSET ]);
40
+ keylength = decodeShort (b , KEYLENGTH_OFFSET );
41
+ extralength = b [EXTRALENGTH_OFFSET ];
42
+ datatype = b [DATATYPE_OFFSET ];
43
+ vbucket = decodeShort (b , VBUCKET_OFFSET );
44
+ totalbody = decodeInt (b , TOTALBODY_OFFSET );
45
+ opaque = decodeInt (b , OPAQUE_OFFSET );
46
+ cas = decodeLong (b , CAS_OFFSET );
119
47
}
120
48
121
49
/**
122
50
* Sets the value of the tap messages magic field.
123
51
* @param m The new value for the magic field.
124
52
*/
125
53
public final void setMagic (TapMagic m ) {
126
- mbytes [ MAGIC_INDEX ] = ( byte ) m . magic ;
54
+ magic = m ;
127
55
}
128
56
129
57
/**
130
58
* Gets the value of the tap messages magic field.
131
59
* @return The value of the magic field.
132
60
*/
133
- public final int getMagic () {
134
- return mbytes [ MAGIC_INDEX ] ;
61
+ public final TapMagic getMagic () {
62
+ return magic ;
135
63
}
136
64
137
65
/**
138
66
* Sets the value of the tap messages opcode field
139
67
* @param o The new value of the opcode field.
140
68
*/
141
69
public final void setOpcode (TapOpcode o ) {
142
- mbytes [ OPCODE_INDEX ] = ( byte ) o . opcode ;
70
+ opcode = o ;
143
71
}
144
72
145
73
/**
146
74
* Gets the value of the tap messages opaque field.
147
75
* @return The value of the opaque field.
148
76
*/
149
77
public final TapOpcode getOpcode () {
150
- return TapOpcode .getOpcodeByByte (mbytes [OPCODE_INDEX ]);
151
- }
152
-
153
- /**
154
- * Sets the key length for this message. This function should never be called by
155
- * the user since changes to fields that affect key length call this function
156
- * automatically.
157
- * @param l The new value for the key length field.
158
- */
159
- protected final void setKeylength (long l ) {
160
- Util .valueToFieldOffest (mbytes , KEY_LENGTH_INDEX , KEY_LENGTH_FIELD_LENGTH , l );
78
+ return opcode ;
161
79
}
162
80
163
81
/**
164
82
* Gets the value of the tap messages key length field.
165
83
* @return The value of the key length field.
166
84
*/
167
- public final int getKeylength () {
168
- return ( int ) Util . fieldToValue ( mbytes , KEY_LENGTH_INDEX , KEY_LENGTH_FIELD_LENGTH ) ;
85
+ public final short getKeylength () {
86
+ return keylength ;
169
87
}
170
88
171
89
/**
172
90
* Sets the value of the tap messages data type field.
173
91
* @param b The new value for the data type field.
174
92
*/
175
- public final void setDatatype (byte b ) {
176
- mbytes [ DATA_TYPE_INDEX ] = b ;
93
+ public final void setDatatype (byte d ) {
94
+ datatype = d ;
177
95
}
178
96
179
97
/**
180
98
* Gets the value of the tap messages data type field.
181
99
* @return The value of the data type field.
182
100
*/
183
101
public final byte getDatatype () {
184
- return mbytes [ DATA_TYPE_INDEX ] ;
102
+ return datatype ;
185
103
}
186
104
187
105
/**
188
106
* Sets the value of the tap messages extra length field.
189
107
* @param i The new value for the extra length field.
190
108
*/
191
- public final void setExtralength (int i ) {
192
- mbytes [ EXTRA_LENGTH_INDEX ] = ( byte ) i ;
109
+ public final void setExtralength (byte e ) {
110
+ extralength = e ;
193
111
}
194
112
195
113
/**
196
114
* Gets the value of the tap messages extra length field.
197
115
* @return The value of the extra length field.
198
116
*/
199
- public final int getExtralength () {
200
- return mbytes [ EXTRA_LENGTH_INDEX ] ;
117
+ public final byte getExtralength () {
118
+ return extralength ;
201
119
}
202
120
203
121
/**
204
122
* Sets the value of the tap messages vbucket field.
205
123
* @param vb The new value for the vbucket field.
206
124
*/
207
- public final void setVbucket (int vb ) {
208
- Util . valueToFieldOffest ( mbytes , VBUCKET_INDEX , VBUCKET_FIELD_LENGTH , vb ) ;
125
+ public final void setVbucket (short vb ) {
126
+ vbucket = vb ;
209
127
}
210
128
211
129
/**
212
130
* Gets the value of the tap messages vbucket field.
213
131
* @return The value of the vbucket field.
214
132
*/
215
- public final int getVbucket () {
216
- return ( int ) Util . fieldToValue ( mbytes , VBUCKET_INDEX , VBUCKET_FIELD_LENGTH ) ;
133
+ public final short getVbucket () {
134
+ return vbucket ;
217
135
}
218
136
219
137
/**
220
138
* Sets the value of the tap messages total body field.
221
139
* @param l The new value for the total body field.
222
140
*/
223
- public final void setTotalbody (long l ) {
224
- Util . valueToFieldOffest ( mbytes , TOTAL_BODY_INDEX , TOTAL_BODY_FIELD_LENGTH , l ) ;
141
+ public final void setTotalbody (int t ) {
142
+ totalbody = t ;
225
143
}
226
144
227
145
/**
228
146
* Gets the value of the tap messages total body field.
229
147
* @return The value of the total body field.
230
148
*/
231
149
public final int getTotalbody () {
232
- return ( int ) Util . fieldToValue ( mbytes , TOTAL_BODY_INDEX , TOTAL_BODY_FIELD_LENGTH ) ;
150
+ return totalbody ;
233
151
}
234
152
235
153
/**
236
154
* Sets the value of the tap messages opaque field.
237
155
* @param op The new value for the opaque field.
238
156
*/
239
157
public final void setOpaque (int op ) {
240
- Util . valueToFieldOffest ( mbytes , OPAQUE_INDEX , OPAQUE_FIELD_LENGTH , op ) ;
158
+ opaque = op ;
241
159
}
242
160
243
161
/**
244
162
* Gets the value of the tap messages opaque field.
245
163
* @return The value of the opaque field.
246
164
*/
247
165
public final int getOpaque () {
248
- return ( int ) Util . fieldToValue ( mbytes , OPAQUE_INDEX , OPAQUE_FIELD_LENGTH ) ;
166
+ return opaque ;
249
167
}
250
168
251
169
/**
252
170
* Sets the value of the tap messages cas field.
253
171
* @param cas The new value for the cas field.
254
172
*/
255
- public final void setCas (long cas ) {
256
- Util . valueToFieldOffest ( mbytes , CAS_INDEX , CAS_FIELD_LENGTH , cas ) ;
173
+ public final void setCas (long c ) {
174
+ cas = c ;
257
175
}
258
176
259
177
/**
260
178
* Gets the value of the tap messages cas field.
261
179
* @return The value of the cas field.
262
180
*/
263
181
public final long getCas () {
264
- return Util . fieldToValue ( mbytes , CAS_INDEX , CAS_FIELD_LENGTH ) ;
182
+ return cas ;
265
183
}
266
184
267
185
/**
@@ -276,7 +194,27 @@ public final int getMessageLength() {
276
194
* Creates a ByteBuffer representation of the message.
277
195
* @return The ByteBuffer representation of the message.
278
196
*/
279
- public final ByteBuffer getBytes () {
280
- return ByteBuffer .wrap (mbytes );
197
+ public abstract ByteBuffer getBytes ();
198
+
199
+ protected short decodeShort (byte [] data , int i ) {
200
+ return (short )((data [i ] & 0xff ) << 8 | (data [i + 1 ] & 0xff ));
201
+ }
202
+
203
+ protected int decodeInt (byte [] data , int i ) {
204
+ return (data [i ] & 0xff ) << 24
205
+ | (data [i + 1 ] & 0xff ) << 16
206
+ | (data [i + 2 ] & 0xff ) << 8
207
+ | (data [i + 3 ] & 0xff );
208
+ }
209
+
210
+ protected long decodeLong (byte [] data , int i ) {
211
+ return (data [i ] & 0xffL ) << 56
212
+ | (data [i + 1 ] & 0xffL ) << 48
213
+ | (data [i + 2 ] & 0xffL ) << 40
214
+ | (data [i + 3 ] & 0xffL ) << 32
215
+ | (data [i + 4 ] & 0xffL ) << 24
216
+ | (data [i + 5 ] & 0xffL ) << 16
217
+ | (data [i + 6 ] & 0xffL ) << 8
218
+ | (data [i + 7 ] & 0xffL );
281
219
}
282
220
}
0 commit comments