DATAREDIS-744 - Polishing

Move binary lookup methods to ByteUtils, update license headers and restore altered tests by creating additional ones.

Original Pull Request: #298
This commit is contained in:
Christoph Strobl
2018-01-11 14:09:05 +01:00
parent 5de73903c1
commit d8ec35f48b
7 changed files with 136 additions and 44 deletions

View File

@@ -35,7 +35,7 @@ public class RedisKeyExpiredEvent<T> extends RedisKeyspaceEvent {
/**
* Use {@literal UTF-8} as default charset.
*/
public static final Charset CHARSET = StandardCharsets.UTF_8;
static final Charset CHARSET = StandardCharsets.UTF_8;
private final BinaryKeyspaceIdentifier objectId;
private final @Nullable Object value;

View File

@@ -1264,11 +1264,11 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
Assert.isTrue(isValid(key), String.format("Invalid key %s", new String(key)));
boolean phantomKey = startsWith(key, PHANTOM_SUFFIX, key.length - PHANTOM_SUFFIX.length);
boolean phantomKey = ByteUtils.startsWith(key, PHANTOM_SUFFIX, key.length - PHANTOM_SUFFIX.length);
int keyspaceEndIndex = find(key, DELIMITTER);
byte[] keyspace = getKeyspace(key, keyspaceEndIndex);
byte[] id = getId(key, phantomKey, keyspaceEndIndex);
int keyspaceEndIndex = ByteUtils.indexOf(key, DELIMITTER);
byte[] keyspace = extractKeyspace(key, keyspaceEndIndex);
byte[] id = extractId(key, phantomKey, keyspaceEndIndex);
return new BinaryKeyspaceIdentifier(keyspace, id, phantomKey);
}
@@ -1286,12 +1286,12 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
return false;
}
int keyspaceEndIndex = find(key, DELIMITTER);
int keyspaceEndIndex = ByteUtils.indexOf(key, DELIMITTER);
return keyspaceEndIndex > 0 && key.length > keyspaceEndIndex;
}
private static byte[] getId(byte[] key, boolean phantomKey, int keyspaceEndIndex) {
private static byte[] extractId(byte[] key, boolean phantomKey, int keyspaceEndIndex) {
int idSize;
@@ -1308,42 +1308,12 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean {
return id;
}
private static byte[] getKeyspace(byte[] key, int keyspaceEndIndex) {
private static byte[] extractKeyspace(byte[] key, int keyspaceEndIndex) {
byte[] keyspace = new byte[keyspaceEndIndex];
System.arraycopy(key, 0, keyspace, 0, keyspaceEndIndex);
return keyspace;
}
private static boolean startsWith(byte[] haystack, byte[] prefix, int offset) {
int to = offset;
int prefixOffset = 0;
int prefixLength = prefix.length;
if ((offset < 0) || (offset > haystack.length - prefixLength)) {
return false;
}
while (--prefixLength >= 0) {
if (haystack[to++] != prefix[prefixOffset++]) {
return false;
}
}
return true;
}
private static int find(byte[] haystack, byte needle) {
for (int i = 0; i < haystack.length; i++) {
if (haystack[i] == needle) {
return i;
}
}
return -1;
}
}
}

View File

@@ -142,4 +142,65 @@ public final class ByteUtils {
duplicate.get(bytes);
return bytes;
}
/**
* Tests if the {@code haystack} starts with the given {@code prefix}.
*
* @param haystack the source to scan.
* @param prefix the prefix to find.
* @return {@literal true} if {@code haystack} at position {@code offset} starts with {@code prefix}.
* @since 1.8.10
* @see #startsWith(byte[], byte[], int)
*/
public static boolean startsWith(byte[] haystack, byte[] prefix) {
return startsWith(haystack, prefix, 0);
}
/**
* Tests if the {@code haystack} beginning at the specified {@code offset} starts with the given {@code prefix}.
*
* @param haystack the source to scan.
* @param prefix the prefix to find.
* @param offset the offset to start at.
* @return {@literal true} if {@code haystack} at position {@code offset} starts with {@code prefix}.
* @since 1.8.10
*/
public static boolean startsWith(byte[] haystack, byte[] prefix, int offset) {
int to = offset;
int prefixOffset = 0;
int prefixLength = prefix.length;
if ((offset < 0) || (offset > haystack.length - prefixLength)) {
return false;
}
while (--prefixLength >= 0) {
if (haystack[to++] != prefix[prefixOffset++]) {
return false;
}
}
return true;
}
/**
* Searches the specified array of bytes for the specified value. Returns the index of the first matching value in the
* {@code haystack}s natural order or {@code -1} of {@code needle} could not be found.
*
* @param haystack the source to scan.
* @param needle the value to scan for.
* @return index of first appearance, or -1 if not found.
* @since 1.8.10
*/
public static int indexOf(byte[] haystack, byte needle) {
for (int i = 0; i < haystack.length; i++) {
if (haystack[i] == needle) {
return i;
}
}
return -1;
}
}