1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.memcache.binary;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.handler.codec.memcache.AbstractMemcacheObjectAggregator;
20 import io.netty.handler.codec.memcache.FullMemcacheMessage;
21 import io.netty.handler.codec.memcache.MemcacheContent;
22 import io.netty.handler.codec.memcache.MemcacheObject;
23 import io.netty.util.internal.UnstableApi;
24
25 import static io.netty.util.internal.StringUtil.className;
26
27
28
29
30
31
32
33 @UnstableApi
34 public class BinaryMemcacheObjectAggregator extends AbstractMemcacheObjectAggregator<BinaryMemcacheMessage> {
35
36 public BinaryMemcacheObjectAggregator(int maxContentLength) {
37 super(maxContentLength);
38 }
39
40 @Override
41 protected boolean isStartMessage(MemcacheObject msg) throws Exception {
42 return msg instanceof BinaryMemcacheMessage;
43 }
44
45 @Override
46 protected FullMemcacheMessage beginAggregation(BinaryMemcacheMessage start, ByteBuf content) throws Exception {
47 if (start instanceof BinaryMemcacheRequest) {
48 return toFullRequest((BinaryMemcacheRequest) start, content);
49 }
50
51 if (start instanceof BinaryMemcacheResponse) {
52 return toFullResponse((BinaryMemcacheResponse) start, content);
53 }
54
55
56 throw new Error("Unexpected memcache message type: " + className(start));
57 }
58
59 private static FullBinaryMemcacheRequest toFullRequest(BinaryMemcacheRequest request, ByteBuf content) {
60 ByteBuf key = request.key() == null ? null : request.key().retain();
61 ByteBuf extras = request.extras() == null ? null : request.extras().retain();
62 DefaultFullBinaryMemcacheRequest fullRequest =
63 new DefaultFullBinaryMemcacheRequest(key, extras, content);
64
65 fullRequest.setMagic(request.magic());
66 fullRequest.setOpcode(request.opcode());
67 fullRequest.setKeyLength(request.keyLength());
68 fullRequest.setExtrasLength(request.extrasLength());
69 fullRequest.setDataType(request.dataType());
70 fullRequest.setTotalBodyLength(request.totalBodyLength());
71 fullRequest.setOpaque(request.opaque());
72 fullRequest.setCas(request.cas());
73 fullRequest.setReserved(request.reserved());
74
75 return fullRequest;
76 }
77
78 private static FullBinaryMemcacheResponse toFullResponse(BinaryMemcacheResponse response, ByteBuf content) {
79 ByteBuf key = response.key() == null ? null : response.key().retain();
80 ByteBuf extras = response.extras() == null ? null : response.extras().retain();
81 DefaultFullBinaryMemcacheResponse fullResponse =
82 new DefaultFullBinaryMemcacheResponse(key, extras, content);
83
84 fullResponse.setMagic(response.magic());
85 fullResponse.setOpcode(response.opcode());
86 fullResponse.setKeyLength(response.keyLength());
87 fullResponse.setExtrasLength(response.extrasLength());
88 fullResponse.setDataType(response.dataType());
89 fullResponse.setTotalBodyLength(response.totalBodyLength());
90 fullResponse.setOpaque(response.opaque());
91 fullResponse.setCas(response.cas());
92 fullResponse.setStatus(response.status());
93
94 return fullResponse;
95 }
96 }