View Javadoc
1   /*
2    * Copyright 2013 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.memcache.binary;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.handler.codec.memcache.AbstractMemcacheObject;
20  import io.netty.util.internal.ObjectUtil;
21  import io.netty.util.internal.UnstableApi;
22  
23  /**
24   * Default implementation of a {@link BinaryMemcacheMessage}.
25   */
26  @UnstableApi
27  public abstract class AbstractBinaryMemcacheMessage
28      extends AbstractMemcacheObject
29      implements BinaryMemcacheMessage {
30  
31      /**
32       * Contains the optional key.
33       */
34      private ByteBuf key;
35  
36      /**
37       * Contains the optional extras.
38       */
39      private ByteBuf extras;
40  
41      private byte magic;
42      private byte opcode;
43      private short keyLength;
44      private byte extrasLength;
45      private byte dataType;
46      private int totalBodyLength;
47      private int opaque;
48      private long cas;
49  
50      /**
51       * Create a new instance with all properties set.
52       *
53       * @param key    the message key.
54       * @param extras the message extras.
55       */
56      protected AbstractBinaryMemcacheMessage(ByteBuf key, ByteBuf extras) {
57          this.key = key;
58          keyLength = (short) checkKeyLength(key);
59          this.extras = extras;
60          extrasLength = (byte) checkExtrasLength(extras);
61          totalBodyLength = (keyLength & 0xFFFF) + (extrasLength & 0xFF);
62      }
63  
64      @Override
65      public ByteBuf key() {
66          return key;
67      }
68  
69      @Override
70      public ByteBuf extras() {
71          return extras;
72      }
73  
74      @Override
75      public BinaryMemcacheMessage setKey(ByteBuf key) {
76          int newKeyLength = checkKeyLength(key);
77          if (this.key != null) {
78              this.key.release();
79          }
80          this.key = key;
81          int oldKeyLength = keyLength & 0xFFFF;
82          keyLength = (short) newKeyLength;
83          totalBodyLength = totalBodyLength + newKeyLength - oldKeyLength;
84          return this;
85      }
86  
87      @Override
88      public BinaryMemcacheMessage setExtras(ByteBuf extras) {
89          int newExtrasLength = checkExtrasLength(extras);
90          if (this.extras != null) {
91              this.extras.release();
92          }
93          this.extras = extras;
94          int oldExtrasLength = extrasLength & 0xFF;
95          extrasLength = (byte) newExtrasLength;
96          totalBodyLength = totalBodyLength + newExtrasLength - oldExtrasLength;
97          return this;
98      }
99  
100     // The key length and extras length are unsigned 16 and 8 bit fields in the header.
101     private static int checkKeyLength(ByteBuf key) {
102         return key == null ? 0 : ObjectUtil.checkInRange(key.readableBytes(), 0, 0xFFFF, "key length");
103     }
104 
105     private static int checkExtrasLength(ByteBuf extras) {
106         return extras == null ? 0 : ObjectUtil.checkInRange(extras.readableBytes(), 0, 0xFF, "extras length");
107     }
108 
109     @Override
110     public byte magic() {
111         return magic;
112     }
113 
114     @Override
115     public BinaryMemcacheMessage setMagic(byte magic) {
116         this.magic = magic;
117         return this;
118     }
119 
120     @Override
121     public long cas() {
122         return cas;
123     }
124 
125     @Override
126     public BinaryMemcacheMessage setCas(long cas) {
127         this.cas = cas;
128         return this;
129     }
130 
131     @Override
132     public int opaque() {
133         return opaque;
134     }
135 
136     @Override
137     public BinaryMemcacheMessage setOpaque(int opaque) {
138         this.opaque = opaque;
139         return this;
140     }
141 
142     @Override
143     public int totalBodyLength() {
144         return totalBodyLength;
145     }
146 
147     @Override
148     public BinaryMemcacheMessage setTotalBodyLength(int totalBodyLength) {
149         this.totalBodyLength = totalBodyLength;
150         return this;
151     }
152 
153     @Override
154     public byte dataType() {
155         return dataType;
156     }
157 
158     @Override
159     public BinaryMemcacheMessage setDataType(byte dataType) {
160         this.dataType = dataType;
161         return this;
162     }
163 
164     @Override
165     public byte extrasLength() {
166         return extrasLength;
167     }
168 
169     /**
170      * Set the extras length of the message.
171      * <p/>
172      * This may be 0, since the extras content is optional.
173      *
174      * @param extrasLength the extras length.
175      */
176     BinaryMemcacheMessage setExtrasLength(byte extrasLength) {
177         this.extrasLength = extrasLength;
178         return this;
179     }
180 
181     @Override
182     public short keyLength() {
183         return keyLength;
184     }
185 
186     /**
187      * Set the key length of the message.
188      * <p/>
189      * This may be 0, since the key is optional.
190      *
191      * @param keyLength the key length to use.
192      */
193     BinaryMemcacheMessage setKeyLength(short keyLength) {
194         this.keyLength = keyLength;
195         return this;
196     }
197 
198     @Override
199     public byte opcode() {
200         return opcode;
201     }
202 
203     @Override
204     public BinaryMemcacheMessage setOpcode(byte opcode) {
205         this.opcode = opcode;
206         return this;
207     }
208 
209     @Override
210     public BinaryMemcacheMessage retain() {
211         super.retain();
212         return this;
213     }
214 
215     @Override
216     public BinaryMemcacheMessage retain(int increment) {
217         super.retain(increment);
218         return this;
219     }
220 
221     @Override
222     protected void deallocate() {
223         if (key != null) {
224             key.release();
225         }
226         if (extras != null) {
227             extras.release();
228         }
229     }
230 
231     @Override
232     public BinaryMemcacheMessage touch() {
233         super.touch();
234         return this;
235     }
236 
237     @Override
238     public BinaryMemcacheMessage touch(Object hint) {
239         if (key != null) {
240             key.touch(hint);
241         }
242         if (extras != null) {
243             extras.touch(hint);
244         }
245         return this;
246     }
247 
248     /**
249      * Copies special metadata hold by this instance to the provided instance
250      *
251      * @param dst The instance where to copy the metadata of this instance to
252      */
253     void copyMeta(AbstractBinaryMemcacheMessage dst) {
254         dst.magic = magic;
255         dst.opcode = opcode;
256         dst.keyLength = keyLength;
257         dst.extrasLength = extrasLength;
258         dst.dataType = dataType;
259         dst.totalBodyLength = totalBodyLength;
260         dst.opaque = opaque;
261         dst.cas = cas;
262     }
263 }