Skip to content

Package: MemcachedRequest$Builder

MemcachedRequest$Builder

nameinstructionbranchcomplexitylinemethod
MemcachedRequest.Builder(boolean, boolean, boolean)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
build()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
cas(long)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
create(boolean, boolean, boolean)
M: 20 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
dataType(byte)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
delta(Long)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
expirationInSecs(Integer)
M: 13 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
flags(Integer)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
initial(Long)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
initialize(boolean, boolean, boolean)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
key(Buffer)
M: 27 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
noReply(boolean)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
op(CommandOpcodes)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
opaque(int)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
originKey(Object)
M: 7 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
originKeyType(BufferWrapper.BufferType)
M: 7 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
recycle()
M: 56 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 19 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
vBucketId(short)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
value(Buffer)
M: 27 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
verbosity(Integer)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2012, 2017 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package org.glassfish.grizzly.memcached;
18:
19: import org.glassfish.grizzly.Buffer;
20: import org.glassfish.grizzly.Cacheable;
21: import org.glassfish.grizzly.ThreadCache;
22:
23: import java.util.concurrent.CountDownLatch;
24: import java.util.concurrent.atomic.AtomicBoolean;
25:
26: /**
27: * Memcached request
28: * <p>
29: * {@code response} and {@code responseStatus} will be set by the filter when the response will be received.
30: * And the filter will notify {@code notify} instance after it will complete the processing for the received message.
31: *
32: * @author Bongjae Chang
33: */
34: public class MemcachedRequest {
35:
36: private static final int MAX_KEY_LENGTH = 250; // 250bytes
37: private static final int MAX_VALUE_LENGTH = 1024 * 1024; // 1M
38:
39: private final boolean hasExtras;
40: private final boolean hasKey;
41: private final boolean hasValue;
42:
43: private final CommandOpcodes op;
44: private final boolean noReply;
45: private final int opaque;
46: private final long cas;
47: private final byte dataType;
48: private final short vBucketId;
49:
50: // extras and body
51: private final Integer flags;
52: private final Long delta;
53: private final Long initial;
54: private final Integer expirationInSecs;
55: private final Integer verbosity;
56: private final BufferWrapper.BufferType originKeyType;
57: private final Object originKey;
58: private final Buffer key;
59: private final Buffer value;
60:
61: final AtomicBoolean disposed = new AtomicBoolean();
62: final CountDownLatch notify = new CountDownLatch(1);
63: Object response;
64: Boolean isError;
65:
66: private MemcachedRequest(final Builder builder) {
67: this.hasExtras = builder.hasExtras;
68: this.hasKey = builder.hasKey;
69: this.hasValue = builder.hasValue;
70: this.op = builder.op;
71: this.noReply = builder.noReply;
72: this.opaque = builder.opaque;
73: this.cas = builder.cas;
74: this.dataType = builder.dataType;
75: this.vBucketId = builder.vBucketId;
76: this.flags = builder.flags;
77: this.delta = builder.delta;
78: this.initial = builder.initial;
79: this.expirationInSecs = builder.expirationInSecs;
80: this.verbosity = builder.verbosity;
81: this.originKeyType = builder.originKeyType;
82: this.originKey = builder.originKey;
83: this.key = builder.key;
84: this.value = builder.value;
85: }
86:
87: public boolean hasExtras() {
88: return hasExtras;
89: }
90:
91: public boolean hasKey() {
92: return hasKey;
93: }
94:
95: public boolean hasValue() {
96: return hasValue;
97: }
98:
99: public CommandOpcodes getOp() {
100: return op;
101: }
102:
103: public boolean isNoReply() {
104: return noReply;
105: }
106:
107: public int getOpaque() {
108: return opaque;
109: }
110:
111: public long getCas() {
112: return cas;
113: }
114:
115: public byte getDataType() {
116: return dataType;
117: }
118:
119: public short getvBucketId() {
120: return vBucketId;
121: }
122:
123: public Integer getFlags() {
124: return flags;
125: }
126:
127: public Long getDelta() {
128: return delta;
129: }
130:
131: public Long getInitial() {
132: return initial;
133: }
134:
135: public Integer getExpirationInSecs() {
136: return expirationInSecs;
137: }
138:
139: public Integer getVerbosity() {
140: return verbosity;
141: }
142:
143: public BufferWrapper.BufferType getOriginKeyType() {
144: return originKeyType;
145: }
146:
147: public Object getOriginKey() {
148: return originKey;
149: }
150:
151: public Buffer getKey() {
152: return key;
153: }
154:
155: public Buffer getValue() {
156: return value;
157: }
158:
159: public byte getExtrasLength() {
160: if (!hasExtras) {
161: return 0;
162: } else {
163: return (byte) (((flags != null ? 4 : 0) +
164: (delta != null ? 8 : 0) +
165: (initial != null ? 8 : 0) +
166: (expirationInSecs != null ? 4 : 0) +
167: (verbosity != null ? 4 : 0)) & 0x7f);
168: }
169: }
170:
171: public void fillExtras(final Buffer buffer) {
172: if (!hasExtras || buffer == null) {
173: return;
174: }
175: switch (op) {
176: case Set:
177: case SetQ:
178: case Add:
179: case AddQ:
180: case Replace:
181: case ReplaceQ:
182: if (flags != null) {
183: buffer.putInt(flags);
184: }
185: if (expirationInSecs != null) {
186: buffer.putInt(expirationInSecs);
187: }
188: break;
189: case Increment:
190: case IncrementQ:
191: case Decrement:
192: case DecrementQ:
193: if (delta != null) {
194: buffer.putLong(delta);
195: }
196: if (initial != null) {
197: buffer.putLong(initial);
198: }
199: if (expirationInSecs != null) {
200: buffer.putInt(expirationInSecs);
201: }
202: break;
203: case Verbosity:
204: if (verbosity != null) {
205: buffer.putInt(verbosity);
206: }
207: break;
208: case GAT:
209: case GATQ:
210: case Touch:
211: case Flush:
212: case FlushQ:
213: if (expirationInSecs != null) {
214: buffer.putInt(expirationInSecs);
215: }
216: break;
217: default:
218: break;
219: }
220: }
221:
222: public short getKeyLength() {
223: return hasKey && key != null ? (short) (key.remaining() & 0x7fff) : 0;
224: }
225:
226: public int getValueLength() {
227: return hasValue && value != null ? value.remaining() : 0;
228: }
229:
230: public static class Builder implements Cacheable {
231:
232: private static final ThreadCache.CachedTypeIndex<Builder> CACHE_IDX = ThreadCache.obtainIndex(Builder.class, 16);
233:
234: private boolean hasExtras;
235: private boolean hasKey;
236: private boolean hasValue;
237:
238: private CommandOpcodes op;
239: private boolean noReply;
240: private int opaque;
241: private long cas;
242: private byte dataType;
243: private short vBucketId;
244:
245: // extras and body
246: private Integer flags;
247: private Long delta;
248: private Long initial;
249: private Integer expirationInSecs;
250: private Integer verbosity;
251: private BufferWrapper.BufferType originKeyType;
252: private Object originKey;
253: private Buffer key;
254: private Buffer value;
255:
256: public static Builder create(final boolean hasExtras, final boolean hasKey, final boolean hasValue) {
257: final Builder builder = ThreadCache.takeFromCache(CACHE_IDX);
258:• if (builder != null) {
259: builder.initialize(hasExtras, hasKey, hasValue);
260: return builder;
261: }
262: return new Builder(hasExtras, hasKey, hasValue);
263: }
264:
265: private Builder(final boolean hasExtras, final boolean hasKey, final boolean hasValue) {
266: initialize(hasExtras, hasKey, hasValue);
267: }
268:
269: private void initialize(final boolean hasExtras, final boolean hasKey, final boolean hasValue) {
270: this.hasExtras = hasExtras;
271: this.hasKey = hasKey;
272: this.hasValue = hasValue;
273: }
274:
275: public Builder op(final CommandOpcodes op) {
276: this.op = op;
277: return this;
278: }
279:
280: public Builder noReply(final boolean noReply) {
281: this.noReply = noReply;
282: return this;
283: }
284:
285: public Builder opaque(final int opaque) {
286: this.opaque = opaque;
287: return this;
288: }
289:
290: public Builder cas(final long cas) {
291: this.cas = cas;
292: return this;
293: }
294:
295: public Builder dataType(final byte dataType) {
296: this.dataType = dataType;
297: return this;
298: }
299:
300: public Builder vBucketId(final short vBucketId) {
301: this.vBucketId = vBucketId;
302: return this;
303: }
304:
305: public Builder flags(final Integer flags) {
306: this.flags = flags;
307: return this;
308: }
309:
310: public Builder delta(final Long delta) {
311: this.delta = delta;
312: return this;
313: }
314:
315: public Builder initial(final Long initial) {
316: this.initial = initial;
317: return this;
318: }
319:
320: public Builder expirationInSecs(final Integer expirationInSecs) throws IllegalArgumentException {
321:• if (expirationInSecs < 0) {
322: throw new IllegalArgumentException("expiration must be greater than 0");
323: }
324: this.expirationInSecs = expirationInSecs;
325: return this;
326: }
327:
328: public Builder verbosity(final Integer verbosity) {
329: this.verbosity = verbosity;
330: return this;
331: }
332:
333: public Builder originKeyType(final BufferWrapper.BufferType originKeyType) throws IllegalArgumentException {
334:• if (originKeyType != null) {
335: this.originKeyType = originKeyType;
336: }
337: return this;
338: }
339:
340: public Builder originKey(final Object originKey) throws IllegalArgumentException {
341:• if (originKey != null) {
342: this.originKey = originKey;
343: }
344: return this;
345: }
346:
347: public Builder key(final Buffer key) throws IllegalArgumentException {
348:• if (key != null) {
349: final int keyLen = key.remaining();
350:• if (keyLen > MAX_KEY_LENGTH) {
351: throw new IllegalArgumentException("key length is in excess of " + MAX_KEY_LENGTH + "bytes. keyLen=" + keyLen + "bytes");
352: }
353: this.key = key;
354: }
355: return this;
356: }
357:
358: public Builder value(final Buffer value) throws IllegalArgumentException {
359:• if (value != null) {
360: final int valueLen = value.remaining();
361:• if (valueLen > MAX_VALUE_LENGTH) {
362: throw new IllegalArgumentException("value length is in excess of " + MAX_VALUE_LENGTH + "bytes. valueLen=" + valueLen + "bytes");
363: }
364: this.value = value;
365: }
366: return this;
367: }
368:
369: public MemcachedRequest build() {
370: return new MemcachedRequest(this);
371: }
372:
373: @Override
374: public void recycle() {
375: hasExtras = false;
376: hasKey = false;
377: hasValue = false;
378: op = null;
379: noReply = false;
380: opaque = 0;
381: cas = 0;
382: dataType = 0;
383: vBucketId = 0;
384: flags = null;
385: delta = null;
386: initial = null;
387: expirationInSecs = null;
388: verbosity = null;
389: originKey = null;
390: key = null;
391: value = null;
392: ThreadCache.putToCache(CACHE_IDX, this);
393: }
394: }
395:
396: @Override
397: public String toString() {
398: return "MemcachedRequest{" +
399: "hasExtras=" + hasExtras +
400: ", hasKey=" + hasKey +
401: ", hasValue=" + hasValue +
402: ", op=" + op +
403: ", noReply=" + noReply +
404: ", opaque=" + opaque +
405: ", cas=" + cas +
406: ", dataType=" + dataType +
407: ", vBucketId=" + vBucketId +
408: ", flags=" + flags +
409: ", delta=" + delta +
410: ", initial=" + initial +
411: ", expirationInSecs=" + expirationInSecs +
412: ", verbosity=" + verbosity +
413: ", originKeyType=" + originKeyType +
414: ", originKey=" + originKey +
415: ", key=" + key +
416: ", response=" + response +
417: ", isError=" + isError +
418: ", value=" + value +
419: '}';
420: }
421: }