diff --git a/pdb-api/src/main/java/org/lucares/pdb/api/Tag.java b/pdb-api/src/main/java/org/lucares/pdb/api/Tag.java index 3a352f8..5a9ca11 100644 --- a/pdb-api/src/main/java/org/lucares/pdb/api/Tag.java +++ b/pdb-api/src/main/java/org/lucares/pdb/api/Tag.java @@ -1,6 +1,6 @@ package org.lucares.pdb.api; -public class Tag { +public class Tag implements Comparable { 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; } diff --git a/pdb-api/src/main/java/org/lucares/pdb/api/Tags.java b/pdb-api/src/main/java/org/lucares/pdb/api/Tags.java index 7315a49..f3d9d62 100644 --- a/pdb-api/src/main/java/org/lucares/pdb/api/Tags.java +++ b/pdb-api/src/main/java/org/lucares/pdb/api/Tags.java @@ -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 { 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); diff --git a/performanceDb/src/main/java/org/lucares/performance/db/TagsToFile.java b/performanceDb/src/main/java/org/lucares/performance/db/TagsToFile.java index 82fdc54..cd14956 100644 --- a/performanceDb/src/main/java/org/lucares/performance/db/TagsToFile.java +++ b/performanceDb/src/main/java/org/lucares/performance/db/TagsToFile.java @@ -22,7 +22,7 @@ public class TagsToFile implements AutoCloseable { private final static Logger METRICS_LOGGER_NEW_WRITER = LoggerFactory .getLogger("org.lucares.metrics.ingestion.tagsToFile.newPdbWriter"); - private static final class CacheKey { + private static final class CacheKey implements Comparable { private final Tags tags; public CacheKey(final Tags tags) { @@ -30,6 +30,11 @@ public class TagsToFile implements AutoCloseable { this.tags = tags; } + @Override + public int compareTo(final CacheKey o) { + return tags.compareTo(o.tags); + } + @Override public int hashCode() { final int prime = 31; @@ -54,7 +59,6 @@ public class TagsToFile implements AutoCloseable { return false; return true; } - } private final static class RemovalListener implements EventListener {