reduce memory usage

Reduce memory usage by storing the filename as string instead of
individual tags.
This commit is contained in:
2018-03-19 19:21:57 +01:00
parent 181fce805d
commit 5343c0d427
20 changed files with 315 additions and 454 deletions

View File

@@ -1,142 +0,0 @@
package org.lucares.utils;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
/**
* A memory efficient map implementation. It doesn't implement {@link Map},
* because this class does not support the full API of {@link Map}.
*/
public class MiniMap<K, V> {
private static final Object[] EMPTY_ARRAY = new Object[0];
private static final MiniMap<?,?> EMPTY_MAP = new MiniMap<>();
// keys are on even indices (0,2,4,...) and values on uneven (1,3,5,...)
private Object[] keysValues;
public MiniMap() {
keysValues = EMPTY_ARRAY;
}
public MiniMap(final MiniMap<K, V> miniMap){
keysValues = miniMap.keysValues.clone();
}
@SuppressWarnings("unchecked")
public static final <K,V> MiniMap<K,V> emptyMap() {
return (MiniMap<K,V>) EMPTY_MAP;
}
public int size() {
return keysValues.length / 2;
}
public boolean isEmpty() {
return size() == 0;
}
public boolean containsKey(Object key) {
return get(key) != null;
}
@SuppressWarnings("unchecked")
public V get(Object key) {
final int size = size();
for (int i = 0; i < size; i++) {
Object object = keysValues[2*i];
if (Objects.equals(key, object)) {
return (V) keysValues[2*i+1];
}
}
return null;
}
public V put(K key, V value) {
V oldValue = get(key);
if (oldValue != null) {
final int size = size();
for (int i = 0; i < size; i++) {
Object object = keysValues[2*i];
if (Objects.equals(key, object)) {
keysValues[2*i+1] = value;
break;
}
}
} else {
final Object[] newKeysValues = new Object[keysValues.length + 2];
System.arraycopy(keysValues, 0, newKeysValues, 0, keysValues.length);
newKeysValues[newKeysValues.length - 2] = key;
newKeysValues[newKeysValues.length - 1] = value;
keysValues = newKeysValues;
}
return oldValue;
}
public void putAll(Map<? extends K, ? extends V> map) {
for (java.util.Map.Entry<? extends K, ? extends V> e : map.entrySet()) {
put(e.getKey(), e.getValue());
}
}
public void clear() {
keysValues = EMPTY_ARRAY;
}
@SuppressWarnings("unchecked")
public Set<K> keySet() {
final Set<K> result = new HashSet<>(size());
final int size = size();
for (int i = 0; i < size; i++) {
K k = (K) keysValues[2*i];
result.add(k);
}
return result;
}
@SuppressWarnings("unchecked")
public Set<V> values() {
final Set<V> result = new HashSet<>(size());
final int size = size();
for (int i = 0; i < size; i++) {
V v = (V) keysValues[2*i+1];
result.add(v);
}
return result;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(keysValues);
return result;
}
@SuppressWarnings("rawtypes")
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MiniMap other = (MiniMap) obj;
if (!Arrays.equals(keysValues, other.keysValues))
return false;
return true;
}
}

View File

@@ -1,24 +0,0 @@
package org.lucares.utils;
import org.testng.Assert;
import org.testng.annotations.Test;
@Test
public class MiniMapTest {
public void testInsertGet()
{
final MiniMap<String, String> map = new MiniMap<>();
String key1 = "key1";
String key2 = "key2";
String value1 = "value1";
String value2 = "value1";
map.put(key1, value1);
map.put(key2, value2);
Assert.assertEquals(map.get(key1), value1);
Assert.assertEquals(map.get(key2), value2);
}
}