Reduce memory consumption of Tags by 50%
by storing only the bytes instead of the string.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package org.lucares.pdb.api;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
@@ -14,6 +15,7 @@ import java.util.regex.Pattern;
|
||||
public class Tags {
|
||||
|
||||
public static StringCompressor STRING_COMPRESSOR = null;
|
||||
public static final byte[] EMPTY_BYTES = new byte[0];
|
||||
public static final Tags EMPTY = new Tags();
|
||||
|
||||
public static final String KEY_VALUE_SEPARATOR = "-";
|
||||
@@ -29,10 +31,10 @@ public class Tags {
|
||||
|
||||
private static final Pattern EXTRACT_TAGS_PATTERN = Pattern.compile(REGEX_STORAGE_FILE);
|
||||
|
||||
private final String filename;
|
||||
private final byte[] filenameBytes;
|
||||
|
||||
public Tags() {
|
||||
filename = "";
|
||||
filenameBytes = EMPTY_BYTES;
|
||||
}
|
||||
|
||||
public Tags(final String filename) {
|
||||
@@ -42,11 +44,13 @@ public class Tags {
|
||||
// after the $ is incremented
|
||||
// We only take the part until the $.
|
||||
final int end = filename.indexOf(KEY_VALUE_END_SEPARATOR);
|
||||
final String normalizedFilename;
|
||||
if (end >= 0) {
|
||||
this.filename = filename.substring(0, end);
|
||||
normalizedFilename = filename.substring(0, end);
|
||||
} else {
|
||||
this.filename = filename;
|
||||
normalizedFilename = filename;
|
||||
}
|
||||
this.filenameBytes = normalizedFilename.getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public static Tags create(final Collection<Tag> tags) {
|
||||
@@ -77,7 +81,7 @@ public class Tags {
|
||||
}
|
||||
|
||||
public String getFilename() {
|
||||
return filename;
|
||||
return new String(this.filenameBytes, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public String getValue(final String key) {
|
||||
@@ -93,6 +97,7 @@ public class Tags {
|
||||
|
||||
private SortedSet<Tag> toTags() {
|
||||
final SortedSet<Tag> result = new TreeSet<>(TagByKeyComparator.INSTANCE);
|
||||
final String filename = new String(this.filenameBytes, StandardCharsets.UTF_8);
|
||||
final Matcher matcher = EXTRACT_TAGS_PATTERN.matcher(filename);
|
||||
|
||||
if (matcher.find()) {
|
||||
@@ -160,14 +165,14 @@ public class Tags {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Tags [filename=" + filename + "]";
|
||||
return "Tags [filename=" + getFilename() + ", tags=" + toTags() + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((filename == null) ? 0 : filename.hashCode());
|
||||
result = prime * result + Arrays.hashCode(filenameBytes);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -180,10 +185,7 @@ public class Tags {
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
final Tags other = (Tags) obj;
|
||||
if (filename == null) {
|
||||
if (other.filename != null)
|
||||
return false;
|
||||
} else if (!filename.equals(other.filename))
|
||||
if (!Arrays.equals(filenameBytes, other.filenameBytes))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
@@ -204,7 +206,7 @@ public class Tags {
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return filename == null || filename.length() == 0;
|
||||
return filenameBytes == null || filenameBytes.length == 0;
|
||||
}
|
||||
|
||||
public static Tags create(final String filename) {
|
||||
|
||||
Reference in New Issue
Block a user