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 f5663ed807
commit 0d9ade09c5
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

@@ -792,8 +792,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) : null;
byte[] channelAsBytes = message.getChannel();
String channel = !ObjectUtils.isEmpty(channelAsBytes)
? converter.getConversionService().convert(channelAsBytes, String.class) : null;
RedisKeyExpiredEvent event = new RedisKeyExpiredEvent(channel, key, value);
@@ -808,11 +809,6 @@ public class RedisKeyValueAdapter extends AbstractKeyValueAdapter
}
private boolean isKeyExpirationMessage(Message message) {
if (message == null) {
return false;
}
return BinaryKeyspaceIdentifier.isValid(message.getBody());
}
}

View File

@@ -1267,7 +1267,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));
}
}

View File

@@ -31,7 +31,6 @@ public class BinaryKeyspaceIdentifierUnitTests {
@Test // DATAREDIS-744
public void shouldReturnIfKeyIsValid() {
assertThat(BinaryKeyspaceIdentifier.isValid(null)).isFalse();
assertThat(BinaryKeyspaceIdentifier.isValid("foo".getBytes())).isFalse();
assertThat(BinaryKeyspaceIdentifier.isValid("".getBytes())).isFalse();
assertThat(BinaryKeyspaceIdentifier.isValid("foo:bar".getBytes())).isTrue();

View File

@@ -64,14 +64,6 @@ public class KeyExpirationEventMessageListenerUnitTests {
assertThat((byte[]) captor.getValue().getSource()).isEqualTo(MESSAGE_BODY.getBytes());
}
@Test // DATAREDIS-425
public void handleMessageShouldNotRespondToNullMessage() {
listener.onMessage(null, "*".getBytes());
verifyZeroInteractions(publisherMock);
}
@Test // DATAREDIS-425, DATAREDIS-692
public void handleMessageShouldNotRespondToEmptyMessage() {