Permalink
Browse files

Merge pull request #11 from sjundt/master

Hank refactored to use commons's LruHashMap and MemoryBoundLruHashMap
  • Loading branch information...
2 parents 4514953 + 5769334 commit 4ffef2089b1cdb70d209daa0e9d06be94069f6c3 @thomas-kielbus thomas-kielbus committed Jun 26, 2014
@@ -65,4 +65,8 @@ protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return result;
}
+ public int getMaxCapacity() {
+ return sizeLimit;
+ }
+
}
@@ -91,11 +91,9 @@ public MemoryBoundLruHashMap(int numItemsCapacity, long numBytesCapacity, Memory
iterator.remove();
}
}
-
if (evicted == null) {
evicted = Collections.emptyList();
}
-
return evicted;
}
@@ -155,6 +153,14 @@ private void unmanage(Map.Entry<K, V> entry) {
}
}
+ public long getMaxNumManagedBytes() {
+ return numBytesCapacity;
+ }
+
+ public int getMaxNumItems() {
+ return map.getMaxCapacity();
+ }
+
@Override
public Iterator<Map.Entry<K, V>> iterator() {
return new EntryIterator();
@@ -185,7 +191,5 @@ public void remove() {
}
iterator.remove();
}
-
}
-
}
@@ -0,0 +1,29 @@
+/**
+ * Copyright 2011 LiveRamp
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.liveramp.commons.util;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+
+public class ByteBufferMemoryUsageEstimator implements MemoryUsageEstimator<ByteBuffer>, Serializable {
+ @Override
+ public long estimateMemorySize(ByteBuffer item) {
+ // 8 bytes for ByteBufferManagedBytes itself
+ // 40 bytes for the corresponding ByteBuffer
+ // Plus the capacity of the underlying byte array
+ return 8 + 40 + item.capacity();
+ }
+}
@@ -16,14 +16,15 @@
package com.liveramp.commons.collections;
-import com.liveramp.commons.util.IntegerMemoryUsageEstimator;
-import com.liveramp.commons.util.LongMemoryUsageEstimator;
-import com.liveramp.commons.util.StringMemoryUsageEstimator;
+import java.util.Iterator;
+import java.util.Map;
+
import junit.framework.TestCase;
import org.junit.Test;
-import java.util.Iterator;
-import java.util.Map;
+import com.liveramp.commons.util.IntegerMemoryUsageEstimator;
+import com.liveramp.commons.util.LongMemoryUsageEstimator;
+import com.liveramp.commons.util.StringMemoryUsageEstimator;
public class TestMemoryBoundLruHashMap extends TestCase {
@@ -149,15 +150,16 @@ public void testIterator() {
map.putAndEvict(3L, 3L);
Iterator<Map.Entry<Long, Long>> itr = map.iterator();
- assertEquals(1L, (long) itr.next().getKey());
- assertEquals(2L, (long) itr.next().getKey());
+ assertEquals(1L, (long)itr.next().getKey());
+ assertEquals(2L, (long)itr.next().getKey());
assertEquals(3, map.size());
assertEquals(96, map.getNumManagedBytes());
itr.remove();
assertEquals(2, map.size());
assertEquals(64, map.getNumManagedBytes());
- assertEquals(3L, (long) itr.next().getKey());
+ assertEquals(3L, (long)itr.next().getKey());
assertFalse(itr.hasNext());
}
+
}

0 comments on commit 4ffef20

Please sign in to comment.