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
26
27
28
29
30
31 @UnstableApi
32 public class BinaryMemcacheObjectAggregator extends AbstractMemcacheObjectAggregator<BinaryMemcacheMessage> {
33
34 public BinaryMemcacheObjectAggregator(int maxContentLength) {
35 super(maxContentLength);
36 }
37
38 @Override
39 protected boolean isStartMessage(MemcacheObject msg) throws Exception {
40 return msg instanceof BinaryMemcacheMessage;
41 }
42
43 @Override
44 protected FullMemcacheMessage beginAggregation(BinaryMemcacheMessage start, ByteBuf content) throws Exception {
45 if (start instanceof BinaryMemcacheRequest) {
46 return toFullRequest((BinaryMemcacheRequest) start, content);
47 }
48
49 if (start instanceof BinaryMemcacheResponse) {
50 return toFullResponse((BinaryMemcacheResponse) start, content);
51 }
52
53
54 throw new Error();
55 }
56
57 private static FullBinaryMemcacheRequest toFullRequest(BinaryMemcacheRequest request, ByteBuf content) {
58 ByteBuf key = request.key() == null ? null : request.key().retain();
59 ByteBuf extras = request.extras() == null ? null : request.extras().retain();
60 DefaultFullBinaryMemcacheRequest fullRequest =
61 new DefaultFullBinaryMemcacheRequest(key, extras, content);
62
63 fullRequest.setMagic(request.magic());
64 fullRequest.setOpcode(request.opcode());
65 fullRequest.setKeyLength(request.keyLength());
66 fullRequest.setExtrasLength(request.extrasLength());
67 fullRequest.setDataType(request.dataType());
68 fullRequest.setTotalBodyLength(request.totalBodyLength());
69 fullRequest.setOpaque(request.opaque());
70 fullRequest.setCas(request.cas());
71 fullRequest.setReserved(request.reserved());
72
73 return fullRequest;
74 }
75
76 private static FullBinaryMemcacheResponse toFullResponse(BinaryMemcacheResponse response, ByteBuf content) {
77 ByteBuf key = response.key() == null ? null : response.key().retain();
78 ByteBuf extras = response.extras() == null ? null : response.extras().retain();
79 DefaultFullBinaryMemcacheResponse fullResponse =
80 new DefaultFullBinaryMemcacheResponse(key, extras, content);
81
82 fullResponse.setMagic(response.magic());
83 fullResponse.setOpcode(response.opcode());
84 fullResponse.setKeyLength(response.keyLength());
85 fullResponse.setExtrasLength(response.extrasLength());
86 fullResponse.setDataType(response.dataType());
87 fullResponse.setTotalBodyLength(response.totalBodyLength());
88 fullResponse.setOpaque(response.opaque());
89 fullResponse.setCas(response.cas());
90 fullResponse.setStatus(response.status());
91
92 return fullResponse;
93 }
94 }