Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #11449] Support RESP3 for codec-redis #11448

Open
wants to merge 12 commits into
base: 4.1
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2021 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License, version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package io.netty.handler.codec.redis;

import io.netty.util.AbstractReferenceCounted;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.StringUtil;
import io.netty.util.internal.UnstableApi;

import java.util.Collection;

/**
* Abstract class for Aggregate data types message.
*/
@UnstableApi
public abstract class AbstractCollectionRedisMessage extends AbstractReferenceCounted
implements RedisMessage, AggregatedRedisMessage {

protected Collection<RedisMessage> children;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private final ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sub class ArrayRedisMessage and SetRedisMessage need access this children field on the abstract method children() for cast to List<Message> or Set<Message>, so it can't be private? but I had made it final


public AbstractCollectionRedisMessage() {
jjz921024 marked this conversation as resolved.
Show resolved Hide resolved
}

public AbstractCollectionRedisMessage(Collection<RedisMessage> children) {
jjz921024 marked this conversation as resolved.
Show resolved Hide resolved
this.children = ObjectUtil.checkNotNull(children, "children");
}

/**
*
* @return
*/
public abstract Collection<RedisMessage> children();
jjz921024 marked this conversation as resolved.
Show resolved Hide resolved

@Override
protected void deallocate() {
for (RedisMessage msg : children) {
jjz921024 marked this conversation as resolved.
Show resolved Hide resolved
ReferenceCountUtil.release(msg);
}
}

@Override
public AbstractCollectionRedisMessage touch(Object hint) {
for (RedisMessage msg : children) {
jjz921024 marked this conversation as resolved.
Show resolved Hide resolved
ReferenceCountUtil.touch(msg);
}
return this;
}

/**
* Returns whether the content of this message is {@code null}.
*
* @return indicates whether the content of this message is {@code null}.
*/
public boolean isNull() {
return false;
}

@Override
public String toString() {
return new StringBuilder(StringUtil.simpleClassName(this))
.append('[')
.append("children=")
.append(children.size())
.append(']').toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2021 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License, version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package io.netty.handler.codec.redis;

import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.UnstableApi;

import java.util.Map;

@UnstableApi
public abstract class AbstractMapRedisMessage implements AggregatedRedisMessage {

public Map<RedisMessage, RedisMessage> children;
jjz921024 marked this conversation as resolved.
Show resolved Hide resolved

public AbstractMapRedisMessage(Map<RedisMessage, RedisMessage> children) {
jjz921024 marked this conversation as resolved.
Show resolved Hide resolved
this.children = ObjectUtil.checkNotNull(children, "children");
}

/**
* Get children of this Map. It can be null or empty.
*
* @return Map of {@link RedisMessage}s.
*/
public Map<RedisMessage, RedisMessage> children() {
return children;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2021 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License, version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package io.netty.handler.codec.redis;

import io.netty.util.internal.StringUtil;
import io.netty.util.internal.UnstableApi;

import java.math.BigInteger;

/**
* Abstract class for Number types message.
*/
@UnstableApi
public abstract class AbstractNumberRedisMessage implements RedisMessage {

protected final Number value;

/**
* For create a {@link IntegerRedisMessage} the given int {@code value}.
*
* @param value the message content.
*/
AbstractNumberRedisMessage(long value) {
jjz921024 marked this conversation as resolved.
Show resolved Hide resolved
this.value = value;
}

/**
* For create a {@link DoubleRedisMessage} the given double {@code value}.
*
* @param value the message content.
*/
AbstractNumberRedisMessage(double value) {
this.value = value;
}

/**
* For create a {@link BigNumberRedisMessage} the given BigInteger {@code value}.
*
* @param value the message content.
*/
AbstractNumberRedisMessage(BigInteger value) {
this.value = value;
}

@Override
public String toString() {
return new StringBuilder(StringUtil.simpleClassName(this))
.append('[')
.append("value=")
.append(value)
.append(']').toString();
}

jjz921024 marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2021 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License, version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package io.netty.handler.codec.redis;

import io.netty.util.internal.StringUtil;
import io.netty.util.internal.UnstableApi;

/**
* Header of Redis Aggregated types Message.
*/
@UnstableApi
public abstract class AggregatedHeaderRedisMessage implements RedisMessage {

private final long length;

/**
* Creates a {@link AggregatedHeaderRedisMessage} for the given {@code length}.
*/
public AggregatedHeaderRedisMessage(long length) {
jjz921024 marked this conversation as resolved.
Show resolved Hide resolved
if (length < RedisConstants.NULL_VALUE) {
throw new RedisCodecException("length: " + length + " (expected: >= " + RedisConstants.NULL_VALUE + ")");
}
this.length = length;
}

/**
* Get length of this array object.
*/
public final long length() {
return length;
}

/**
* Returns whether the content of this message is {@code null}.
*
* @return indicates whether the content of this message is {@code null}.
*/
public boolean isNull() {
return length == RedisConstants.NULL_VALUE;
}

@Override
public String toString() {
return new StringBuilder(StringUtil.simpleClassName(this))
.append('[')
.append("length=")
.append(length)
.append(']').toString();
}

jjz921024 marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.netty.handler.codec.redis;
jjz921024 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Just a marker interface for Aggregated data types
*/
public interface AggregatedRedisMessage {
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,21 @@

package io.netty.handler.codec.redis;

import io.netty.util.internal.StringUtil;
import io.netty.util.internal.UnstableApi;

/**
* Header of Redis Array Message.
*/
@UnstableApi
public class ArrayHeaderRedisMessage implements RedisMessage {

private final long length;
public class ArrayHeaderRedisMessage extends AggregatedHeaderRedisMessage {

/**
* Creates a {@link ArrayHeaderRedisMessage} for the given {@code length}.
*/
public ArrayHeaderRedisMessage(long length) {
if (length < RedisConstants.NULL_VALUE) {
throw new RedisCodecException("length: " + length + " (expected: >= " + RedisConstants.NULL_VALUE + ")");
}
this.length = length;
}

/**
* Get length of this array object.
*/
public final long length() {
return length;
}

/**
* Returns whether the content of this message is {@code null}.
*
* @return indicates whether the content of this message is {@code null}.
* @param length
*/
public boolean isNull() {
return length == RedisConstants.NULL_VALUE;
public ArrayHeaderRedisMessage(long length) {
super(length);
}

@Override
public String toString() {
return new StringBuilder(StringUtil.simpleClassName(this))
.append('[')
.append("length=")
.append(length)
.append(']').toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@

package io.netty.handler.codec.redis;

import io.netty.util.AbstractReferenceCounted;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.StringUtil;
import io.netty.util.internal.UnstableApi;

import java.util.Collections;
Expand All @@ -28,12 +24,10 @@
* Arrays of <a href="https://redis.io/topics/protocol">RESP</a>.
*/
@UnstableApi
public class ArrayRedisMessage extends AbstractReferenceCounted implements RedisMessage {

private final List<RedisMessage> children;
public class ArrayRedisMessage extends AbstractCollectionRedisMessage {

private ArrayRedisMessage() {
children = Collections.emptyList();
super(Collections.emptyList());
}

/**
Expand All @@ -43,49 +37,17 @@ private ArrayRedisMessage() {
*/
public ArrayRedisMessage(List<RedisMessage> children) {
// do not retain here. children are already retained when created.
this.children = ObjectUtil.checkNotNull(children, "children");
super(children);
}

/**
* Get children of this Arrays. It can be null or empty.
*
* @return list of {@link RedisMessage}s.
*/
public final List<RedisMessage> children() {
return children;
}

/**
* Returns whether the content of this message is {@code null}.
*
* @return indicates whether the content of this message is {@code null}.
*/
public boolean isNull() {
return false;
}

@Override
protected void deallocate() {
for (RedisMessage msg : children) {
ReferenceCountUtil.release(msg);
}
}

@Override
public ArrayRedisMessage touch(Object hint) {
for (RedisMessage msg : children) {
ReferenceCountUtil.touch(msg);
}
return this;
}

@Override
public String toString() {
return new StringBuilder(StringUtil.simpleClassName(this))
.append('[')
.append("children=")
.append(children.size())
.append(']').toString();
public final List<RedisMessage> children() {
return (List<RedisMessage>) children;
}

/**
Expand Down