View Javadoc
1   /*
2    * Copyright 2016 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License, version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * 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 distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  
16  package io.netty.handler.codec.redis;
17  
18  import io.netty.channel.ChannelHandlerContext;
19  import io.netty.handler.codec.CodecException;
20  import io.netty.handler.codec.MessageToMessageDecoder;
21  import io.netty.util.ReferenceCountUtil;
22  import io.netty.util.internal.UnstableApi;
23  
24  import java.util.ArrayDeque;
25  import java.util.ArrayList;
26  import java.util.Deque;
27  import java.util.List;
28  
29  /**
30   * Aggregates {@link RedisMessage} parts into {@link ArrayRedisMessage}. This decoder
31   * should be used together with {@link RedisDecoder}.
32   */
33  @UnstableApi
34  public final class RedisArrayAggregator extends MessageToMessageDecoder<RedisMessage> {
35  
36      private final Deque<AggregateState> depths = new ArrayDeque<AggregateState>(4);
37  
38      public RedisArrayAggregator() {
39          super(RedisMessage.class);
40      }
41  
42      @Override
43      protected void decode(ChannelHandlerContext ctx, RedisMessage msg, List<Object> out) throws Exception {
44          if (msg instanceof ArrayHeaderRedisMessage) {
45              msg = decodeRedisArrayHeader((ArrayHeaderRedisMessage) msg);
46              if (msg == null) {
47                  return;
48              }
49          } else {
50              ReferenceCountUtil.retain(msg);
51          }
52  
53          while (!depths.isEmpty()) {
54              AggregateState current = depths.peek();
55              current.children.add(msg);
56  
57              // if current aggregation completed, go to parent aggregation.
58              if (current.children.size() == current.length) {
59                  msg = new ArrayRedisMessage(current.children);
60                  depths.pop();
61              } else {
62                  // not aggregated yet. try next time.
63                  return;
64              }
65          }
66  
67          out.add(msg);
68      }
69  
70      private RedisMessage decodeRedisArrayHeader(ArrayHeaderRedisMessage header) {
71          if (header.isNull()) {
72              return ArrayRedisMessage.NULL_INSTANCE;
73          } else if (header.length() == 0L) {
74              return ArrayRedisMessage.EMPTY_INSTANCE;
75          } else if (header.length() > 0L) {
76              // Currently, this codec doesn't support `long` length for arrays because Java's List.size() is int.
77              if (header.length() > Integer.MAX_VALUE) {
78                  throw new CodecException("this codec doesn't support longer length than " + Integer.MAX_VALUE);
79              }
80  
81              // start aggregating array
82              depths.push(new AggregateState((int) header.length()));
83              return null;
84          } else {
85              throw new CodecException("bad length: " + header.length());
86          }
87      }
88  
89      private static final class AggregateState {
90          private final int length;
91          private final List<RedisMessage> children;
92          AggregateState(int length) {
93              this.length = length;
94              this.children = new ArrayList<RedisMessage>(length);
95          }
96      }
97  }