fix over counting of bytes in ByteCountingLRUMap (#4168)

This commit is contained in:
Himanshu 2017-04-13 17:06:34 -05:00 committed by Gian Merlino
parent b2954d5fea
commit 5c9198347b
2 changed files with 20 additions and 3 deletions

View File

@ -19,6 +19,8 @@
package io.druid.client.cache;
import io.druid.java.util.common.logger.Logger;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
@ -29,8 +31,6 @@ import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import io.druid.java.util.common.logger.Logger;
/**
*/
class ByteCountingLRUMap extends LinkedHashMap<ByteBuffer, byte[]>
@ -104,7 +104,11 @@ class ByteCountingLRUMap extends LinkedHashMap<ByteBuffer, byte[]>
remove(keyToRemove);
}
return super.put(key, value);
byte[] old = super.put(key, value);
if (old != null) {
numBytes.addAndGet(-key.remaining() - old.length);
}
return old;
}
@Override

View File

@ -86,6 +86,19 @@ public class ByteCountingLRUMapTest
assertMapValues(0, 0, 3);
}
@Test
public void testSameKeyUpdate() throws Exception
{
final ByteBuffer k = ByteBuffer.allocate(1);
assertMapValues(0, 0, 0);
map.put(k, new byte[1]);
map.put(k, new byte[2]);
map.put(k, new byte[5]);
map.put(k, new byte[3]);
assertMapValues(1, 4, 0);
}
private void assertMapValues(final int size, final int numBytes, final int evictionCount)
{
Assert.assertEquals(size, map.size());