Skip to content

Commit 0f769b8

Browse files
committed
Fix bug when EventStream max LRU size is hit
Also fix an issue where re-added items aren't made newer in the LRU cache. Fixes #48.
1 parent 6611176 commit 0f769b8

File tree

3 files changed

+88
-10
lines changed

3 files changed

+88
-10
lines changed

src/main/java/com/box/sdk/EventStream.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.ArrayList;
44
import java.util.Collection;
5-
import java.util.LinkedHashSet;
65

76
import com.eclipsesource.json.JsonArray;
87
import com.eclipsesource.json.JsonObject;
@@ -19,7 +18,6 @@
1918
*/
2019
public class EventStream {
2120
private static final int LIMIT = 800;
22-
private static final int LRU_SIZE = 512;
2321
private static final URLTemplate EVENT_URL = new URLTemplate("events?limit=" + LIMIT + "&stream_position=%s");
2422
private static final int STREAM_POSITION_NOW = -1;
2523

@@ -28,7 +26,7 @@ public class EventStream {
2826
private final Collection<EventListener> listeners;
2927
private final Object listenerLock;
3028

31-
private LinkedHashSet<String> receivedEvents;
29+
private LRUCache<String> receivedEvents;
3230
private boolean started;
3331
private Poller poller;
3432
private Thread pollerThread;
@@ -127,15 +125,10 @@ public void uncaughtException(Thread t, Throwable e) {
127125
*/
128126
protected boolean isDuplicate(String eventID) {
129127
if (this.receivedEvents == null) {
130-
this.receivedEvents = new LinkedHashSet<String>(LRU_SIZE);
128+
this.receivedEvents = new LRUCache<String>();
131129
}
132130

133-
boolean newEvent = this.receivedEvents.add(eventID);
134-
if (newEvent && this.receivedEvents.size() > LRU_SIZE) {
135-
this.receivedEvents.iterator().remove();
136-
}
137-
138-
return !newEvent;
131+
return !this.receivedEvents.add(eventID);
139132
}
140133

141134
private void notifyEvent(BoxEvent event) {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.box.sdk;
2+
3+
import java.util.Iterator;
4+
import java.util.LinkedHashSet;
5+
6+
class LRUCache<E> {
7+
static final int MAX_SIZE = 512;
8+
9+
private final LinkedHashSet<E> linkedHashSet;
10+
11+
public LRUCache() {
12+
this.linkedHashSet = new LinkedHashSet<E>(MAX_SIZE);
13+
}
14+
15+
boolean add(E item) {
16+
boolean newItem = !this.linkedHashSet.remove(item);
17+
if (newItem) {
18+
this.linkedHashSet.add(item);
19+
}
20+
21+
if (this.linkedHashSet.size() >= MAX_SIZE) {
22+
Iterator<E> it = this.linkedHashSet.iterator();
23+
it.next();
24+
it.remove();
25+
}
26+
27+
return newItem;
28+
}
29+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.box.sdk;
2+
3+
import static org.hamcrest.Matchers.is;
4+
import static org.junit.Assert.assertThat;
5+
6+
import org.junit.Test;
7+
import org.junit.experimental.categories.Category;
8+
9+
public class LRUCacheTest {
10+
@Test
11+
@Category(UnitTest.class)
12+
public void addReturnsTrueForNewItem() {
13+
LRUCache<Integer> lru = new LRUCache<Integer>();
14+
boolean added = lru.add(1);
15+
16+
assertThat(added, is(true));
17+
}
18+
19+
@Test
20+
@Category(UnitTest.class)
21+
public void addReturnsFalseForExistingItem() {
22+
LRUCache<Integer> lru = new LRUCache<Integer>();
23+
lru.add(1);
24+
boolean added = lru.add(1);
25+
26+
assertThat(added, is(false));
27+
}
28+
29+
@Test
30+
@Category(UnitTest.class)
31+
public void addRemovesOldestItemWhenMaxSizeIsReached() {
32+
LRUCache<Integer> lru = new LRUCache<Integer>();
33+
34+
for (int i = 0; i < LRUCache.MAX_SIZE + 1; i++) {
35+
lru.add(i);
36+
}
37+
38+
boolean added = lru.add(0);
39+
assertThat(added, is(true));
40+
}
41+
42+
@Test
43+
@Category(UnitTest.class)
44+
public void addMakesExistingItemNewer() {
45+
LRUCache<Integer> lru = new LRUCache<Integer>();
46+
47+
for (int i = 0; i < LRUCache.MAX_SIZE; i++) {
48+
lru.add(i);
49+
}
50+
51+
lru.add(0);
52+
lru.add(LRUCache.MAX_SIZE + 1);
53+
boolean added = lru.add(1);
54+
assertThat(added, is(true));
55+
}
56+
}

0 commit comments

Comments
 (0)