make CacheKey comparable

The CacheKey is used as a key in a HashMap. Lookup can
be faster if the CacheKey is comparable when there are
hash collisions.
In this case I was not able to measure any effect. I am
keeping the comparables nonetheless, because the can
only have a positive effect.
This commit is contained in:
2019-01-01 08:47:48 +01:00
parent 4cde10a9f2
commit f2d16b6758
3 changed files with 37 additions and 4 deletions

View File

@@ -1,6 +1,6 @@
package org.lucares.pdb.api;
public class Tag {
public class Tag implements Comparable<Tag> {
private final int key;
private final int value;
@@ -15,6 +15,18 @@ public class Tag {
this.value = Tags.STRING_COMPRESSOR.put(value);
}
@Override
public int compareTo(final Tag o) {
if (key != o.key) {
return key - o.key;
} else if (value != o.value) {
return value - o.value;
}
return 0;
}
public int getKey() {
return key;
}

View File

@@ -11,7 +11,7 @@ import java.util.function.Function;
import org.lucares.collections.LongList;
import org.lucares.utils.byteencoder.VariableByteEncoder;
public class Tags {
public class Tags implements Comparable<Tags> {
public static StringCompressor STRING_COMPRESSOR = null;
private static final byte[] EMPTY_BYTES = new byte[0];
@@ -109,6 +109,23 @@ public class Tags {
return result;
}
@Override
public int compareTo(final Tags o) {
if (tags.size() != o.tags.size()) {
return tags.size() - o.tags.size();
} else {
for (int i = 0; i < tags.size(); i++) {
final int compareResult = tags.get(i).compareTo(o.tags.get(i));
if (compareResult != 0) {
return compareResult;
}
}
}
return 0;
}
public String getValue(final String key) {
final Tag needle = new Tag(STRING_COMPRESSOR.put(key), 0);