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:
@@ -1,6 +1,6 @@
|
|||||||
package org.lucares.pdb.api;
|
package org.lucares.pdb.api;
|
||||||
|
|
||||||
public class Tag {
|
public class Tag implements Comparable<Tag> {
|
||||||
private final int key;
|
private final int key;
|
||||||
|
|
||||||
private final int value;
|
private final int value;
|
||||||
@@ -15,6 +15,18 @@ public class Tag {
|
|||||||
this.value = Tags.STRING_COMPRESSOR.put(value);
|
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() {
|
public int getKey() {
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import java.util.function.Function;
|
|||||||
import org.lucares.collections.LongList;
|
import org.lucares.collections.LongList;
|
||||||
import org.lucares.utils.byteencoder.VariableByteEncoder;
|
import org.lucares.utils.byteencoder.VariableByteEncoder;
|
||||||
|
|
||||||
public class Tags {
|
public class Tags implements Comparable<Tags> {
|
||||||
|
|
||||||
public static StringCompressor STRING_COMPRESSOR = null;
|
public static StringCompressor STRING_COMPRESSOR = null;
|
||||||
private static final byte[] EMPTY_BYTES = new byte[0];
|
private static final byte[] EMPTY_BYTES = new byte[0];
|
||||||
@@ -109,6 +109,23 @@ public class Tags {
|
|||||||
return result;
|
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) {
|
public String getValue(final String key) {
|
||||||
final Tag needle = new Tag(STRING_COMPRESSOR.put(key), 0);
|
final Tag needle = new Tag(STRING_COMPRESSOR.put(key), 0);
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ public class TagsToFile implements AutoCloseable {
|
|||||||
private final static Logger METRICS_LOGGER_NEW_WRITER = LoggerFactory
|
private final static Logger METRICS_LOGGER_NEW_WRITER = LoggerFactory
|
||||||
.getLogger("org.lucares.metrics.ingestion.tagsToFile.newPdbWriter");
|
.getLogger("org.lucares.metrics.ingestion.tagsToFile.newPdbWriter");
|
||||||
|
|
||||||
private static final class CacheKey {
|
private static final class CacheKey implements Comparable<CacheKey> {
|
||||||
private final Tags tags;
|
private final Tags tags;
|
||||||
|
|
||||||
public CacheKey(final Tags tags) {
|
public CacheKey(final Tags tags) {
|
||||||
@@ -30,6 +30,11 @@ public class TagsToFile implements AutoCloseable {
|
|||||||
this.tags = tags;
|
this.tags = tags;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(final CacheKey o) {
|
||||||
|
return tags.compareTo(o.tags);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
final int prime = 31;
|
||||||
@@ -54,7 +59,6 @@ public class TagsToFile implements AutoCloseable {
|
|||||||
return false;
|
return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private final static class RemovalListener implements EventListener<CacheKey, PdbWriter> {
|
private final static class RemovalListener implements EventListener<CacheKey, PdbWriter> {
|
||||||
|
|||||||
Reference in New Issue
Block a user