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,73 @@
/*
* 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 AggregatedRedisMessage {

protected final Collection<RedisMessage> children;

protected AbstractCollectionRedisMessage(Collection<RedisMessage> children) {
this.children = ObjectUtil.checkNotNull(children, "children");
}

protected abstract Collection<RedisMessage> children();

@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,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.StringUtil;
import io.netty.util.internal.UnstableApi;

import java.util.Collections;
import java.util.Map;

@UnstableApi
public abstract class AbstractMapRedisMessage extends AbstractReferenceCounted
implements AggregatedRedisMessage {

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

protected AbstractMapRedisMessage(Map<RedisMessage, RedisMessage> children) {
this.children = Collections.unmodifiableMap(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;
}

@Override
protected void deallocate() {
for (Map.Entry<RedisMessage, RedisMessage> messageEntry : children.entrySet()) {
ReferenceCountUtil.release(messageEntry.getKey());
ReferenceCountUtil.release(messageEntry.getValue());
}
}

@Override
public AbstractMapRedisMessage touch(Object hint) {
for (Map.Entry<RedisMessage, RedisMessage> messageEntry : children.entrySet()) {
ReferenceCountUtil.touch(messageEntry.getKey());
ReferenceCountUtil.touch(messageEntry.getValue());
}
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,66 @@
/*
* 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.
*/
protected AbstractNumberRedisMessage(long value) {
this.value = value;
}

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

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

@Override
public String toString() {
return new StringBuilder(StringUtil.simpleClassName(this))
.append('[')
.append("value=")
.append(value)
.append(']').toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public abstract class AbstractStringRedisMessage implements RedisMessage {

private final String content;

AbstractStringRedisMessage(String content) {
protected AbstractStringRedisMessage(String content) {
this.content = ObjectUtil.checkNotNull(content, "content");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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}.
*/
protected AggregatedHeaderRedisMessage(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}.
*/
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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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;
jjz921024 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Just a marker interface for Aggregated data types
*/
public interface AggregatedRedisMessage extends RedisMessage {
}
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();
}
}
Loading