DATAREDIS-1212 - Polishing.

Remove overly pessimistic null checks. Use constant when channel/body are empty to avoid allocations. Remove superfluous final keywords.

Original pull request: #559.
This commit is contained in:
Mark Paluch
2020-10-16 13:46:21 +02:00
parent 588bf0a7ea
commit 7f9de716c8
7 changed files with 15 additions and 26 deletions

View File

@@ -23,9 +23,11 @@ import org.springframework.util.Assert;
*
* @author Costin Leau
* @author Christoph Strobl
* @author Mark Paluch
*/
public class DefaultMessage implements Message {
private static final byte[] EMPTY = new byte[0];
private final byte[] channel;
private final byte[] body;
private @Nullable String toString;
@@ -39,15 +41,14 @@ public class DefaultMessage implements Message {
this.channel = channel;
}
/**
* @return
*/
@Override
public byte[] getChannel() {
return channel.clone();
return channel.length == 0 ? EMPTY : channel.clone();
}
@Override
public byte[] getBody() {
return body.clone();
return body.length == 0 ? EMPTY : body.clone();
}
@Override

View File

@@ -808,8 +808,9 @@ public class RedisKeyValueAdapter extends AbstractKeyValueAdapter
Object value = CollectionUtils.isEmpty(hash) ? null : converter.read(Object.class, new RedisData(hash));
String channel = !ObjectUtils.isEmpty(message.getChannel())
? converter.getConversionService().convert(message.getChannel(), String.class)
byte[] channelAsBytes = message.getChannel();
String channel = !ObjectUtils.isEmpty(channelAsBytes)
? converter.getConversionService().convert(channelAsBytes, String.class)
: null;
RedisKeyExpiredEvent event = new RedisKeyExpiredEvent(channel, key, value);
@@ -825,11 +826,6 @@ public class RedisKeyValueAdapter extends AbstractKeyValueAdapter
}
private boolean isKeyExpirationMessage(Message message) {
if (message == null) {
return false;
}
return BinaryKeyspaceIdentifier.isValid(message.getBody());
}
}

View File

@@ -1303,7 +1303,7 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
*/
public static boolean isValid(byte[] key) {
if (key == null) {
if (key.length == 0) {
return false;
}

View File

@@ -60,7 +60,7 @@ public abstract class KeyspaceEventMessageListener implements MessageListener, I
@Override
public void onMessage(Message message, @Nullable byte[] pattern) {
if (message == null || ObjectUtils.isEmpty(message.getChannel()) || ObjectUtils.isEmpty(message.getBody())) {
if (ObjectUtils.isEmpty(message.getChannel()) || ObjectUtils.isEmpty(message.getBody())) {
return;
}

View File

@@ -979,10 +979,11 @@ public class RedisMessageListenerContainer implements InitializingBean, Disposab
}
}
private void dispatchMessage(Collection<MessageListener> listeners, final Message message, final byte[] pattern) {
final byte[] source = (pattern != null ? pattern.clone() : message.getChannel());
private void dispatchMessage(Collection<MessageListener> listeners, Message message, @Nullable byte[] pattern) {
for (final MessageListener messageListener : listeners) {
byte[] source = (pattern != null ? pattern.clone() : message.getChannel());
for (MessageListener messageListener : listeners) {
taskExecutor.execute(() -> processMessage(messageListener, message, source));
}
}