Skip to content

Commit 664cd18

Browse files
authored
Merge pull request osheroff#2 from osheroff/ben/lru_cache
prevent TableMapEventData hash from growing unbounded.
2 parents 0d9905e + 01476e2 commit 664cd18

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.github.shyiko.mysql.binlog.event;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
5+
6+
public class LRUCache<K,V> extends LinkedHashMap<K,V> {
7+
private int maxSize;
8+
9+
// and other constructors for load factor and hashtable capacity
10+
public LRUCache(int initialCapacity, float loadFactor, int maxSize) {
11+
super(initialCapacity, loadFactor, true);
12+
this.maxSize = maxSize;
13+
}
14+
15+
@Override
16+
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
17+
return size() > maxSize;
18+
}
19+
}

src/main/java/com/github/shyiko/mysql/binlog/event/deserialization/EventDeserializer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.github.shyiko.mysql.binlog.event.EventHeader;
2121
import com.github.shyiko.mysql.binlog.event.EventType;
2222
import com.github.shyiko.mysql.binlog.event.FormatDescriptionEventData;
23+
import com.github.shyiko.mysql.binlog.event.LRUCache;
2324
import com.github.shyiko.mysql.binlog.event.TableMapEventData;
2425
import com.github.shyiko.mysql.binlog.io.ByteArrayInputStream;
2526

@@ -65,7 +66,7 @@ public EventDeserializer(
6566
this.eventHeaderDeserializer = eventHeaderDeserializer;
6667
this.defaultEventDataDeserializer = defaultEventDataDeserializer;
6768
this.eventDataDeserializers = new IdentityHashMap<EventType, EventDataDeserializer>();
68-
this.tableMapEventByTableId = new HashMap<Long, TableMapEventData>();
69+
this.tableMapEventByTableId = new LRUCache<>(100, 0.75f, 10000);
6970
registerDefaultEventDataDeserializers();
7071
afterEventDataDeserializerSet(null);
7172
}

0 commit comments

Comments
 (0)