Commit bd83aca6 authored by Phillip Webb's avatar Phillip Webb

Fix InMemoryAuditEventRepository search by date

Update InMemoryAuditEventRepository to consider the date when searching
for events. Also switch to a circular buffer implementation and update
the capacity to limit the total number of items rather than limiting
per principal.

Fixes gh-2291
parent 0622b3e9
...@@ -16,54 +16,67 @@ ...@@ -16,54 +16,67 @@
package org.springframework.boot.actuate.audit; package org.springframework.boot.actuate.audit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* In-memory {@link AuditEventRepository} implementation. * In-memory {@link AuditEventRepository} implementation.
* *
* @author Dave Syer * @author Dave Syer
* @author Phillip Webb
*/ */
public class InMemoryAuditEventRepository implements AuditEventRepository { public class InMemoryAuditEventRepository implements AuditEventRepository {
private int capacity = 100; private static final int DEFAULT_CAPACITY = 4000;
private final Map<String, List<AuditEvent>> events = new HashMap<String, List<AuditEvent>>(); /**
* Circular buffer of the event with tail pointing to the last element.
*/
private AuditEvent[] events;
private volatile int tail = -1;
public InMemoryAuditEventRepository() {
this(DEFAULT_CAPACITY);
}
public InMemoryAuditEventRepository(int capacity) {
this.events = new AuditEvent[capacity];
}
/** /**
* @param capacity the capacity to set * @param capacity the capacity to set
*/ */
public void setCapacity(int capacity) { public synchronized void setCapacity(int capacity) {
this.capacity = capacity; this.events = new AuditEvent[capacity];
} }
@Override @Override
public List<AuditEvent> find(String principal, Date after) { public synchronized List<AuditEvent> find(String principal, Date after) {
synchronized (this.events) { LinkedList<AuditEvent> events = new LinkedList<AuditEvent>();
return Collections.unmodifiableList(getEvents(principal)); for (int i = 0; i < this.events.length; i++) {
int index = ((this.tail + this.events.length - i) % this.events.length);
AuditEvent event = this.events[index];
if (event == null) {
break;
}
if (isMatch(event, principal, after)) {
events.addFirst(event);
}
} }
return events;
} }
private List<AuditEvent> getEvents(String principal) { private boolean isMatch(AuditEvent auditEvent, String principal, Date after) {
if (!this.events.containsKey(principal)) { return (principal == null || auditEvent.getPrincipal().equals(principal))
this.events.put(principal, new ArrayList<AuditEvent>()); && (after == null || auditEvent.getTimestamp().compareTo(after) >= 0);
}
return this.events.get(principal);
} }
@Override @Override
public void add(AuditEvent event) { public synchronized void add(AuditEvent event) {
synchronized (this.events) { this.tail = (this.tail + 1) % this.events.length;
List<AuditEvent> list = getEvents(event.getPrincipal()); this.events[this.tail] = event;
while (list.size() >= this.capacity) {
list.remove(0);
}
list.add(event);
}
} }
} }
...@@ -16,30 +16,85 @@ ...@@ -16,30 +16,85 @@
package org.springframework.boot.actuate.audit; package org.springframework.boot.actuate.audit;
import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
/** /**
* Tests for {@link InMemoryAuditEventRepository}. * Tests for {@link InMemoryAuditEventRepository}.
* *
* @author Dave Syer * @author Dave Syer
* @author Phillip Webb
*/ */
public class InMemoryAuditEventRepositoryTests { public class InMemoryAuditEventRepositoryTests {
private final InMemoryAuditEventRepository repository = new InMemoryAuditEventRepository(); @Test
public void lessThanCapacity() throws Exception {
InMemoryAuditEventRepository repository = new InMemoryAuditEventRepository();
repository.add(new AuditEvent("dave", "a"));
repository.add(new AuditEvent("dave", "b"));
List<AuditEvent> events = repository.find("dave", null);
assertThat(events.size(), equalTo(2));
assertThat(events.get(0).getType(), equalTo("a"));
assertThat(events.get(1).getType(), equalTo("b"));
}
@Test
public void capacity() throws Exception {
InMemoryAuditEventRepository repository = new InMemoryAuditEventRepository(2);
repository.add(new AuditEvent("dave", "a"));
repository.add(new AuditEvent("dave", "b"));
repository.add(new AuditEvent("dave", "c"));
List<AuditEvent> events = repository.find("dave", null);
assertThat(events.size(), equalTo(2));
assertThat(events.get(0).getType(), equalTo("b"));
assertThat(events.get(1).getType(), equalTo("c"));
}
@Test
public void findByPrincipal() throws Exception {
InMemoryAuditEventRepository repository = new InMemoryAuditEventRepository();
repository.add(new AuditEvent("dave", "a"));
repository.add(new AuditEvent("phil", "b"));
repository.add(new AuditEvent("dave", "c"));
repository.add(new AuditEvent("phil", "d"));
List<AuditEvent> events = repository.find("dave", null);
assertThat(events.size(), equalTo(2));
assertThat(events.get(0).getType(), equalTo("a"));
assertThat(events.get(1).getType(), equalTo("c"));
}
@Test @Test
public void testAddToCapacity() throws Exception { public void findByDate() throws Exception {
this.repository.setCapacity(2); Calendar calendar = Calendar.getInstance();
this.repository.add(new AuditEvent("phil", "UNKNOWN")); calendar.set(2000, 1, 1, 0, 0, 0);
this.repository.add(new AuditEvent("phil", "UNKNOWN")); calendar.set(Calendar.MILLISECOND, 0);
this.repository.add(new AuditEvent("dave", "UNKNOWN")); Map<String, Object> data = new HashMap<String, Object>();
this.repository.add(new AuditEvent("dave", "UNKNOWN")); InMemoryAuditEventRepository repository = new InMemoryAuditEventRepository();
this.repository.add(new AuditEvent("phil", "UNKNOWN")); repository.add(new AuditEvent(calendar.getTime(), "dave", "a", data));
assertEquals(2, this.repository.find("phil", new Date(0L)).size()); calendar.add(Calendar.DAY_OF_YEAR, 1);
repository.add(new AuditEvent(calendar.getTime(), "phil", "b", data));
calendar.add(Calendar.DAY_OF_YEAR, 1);
Date after = calendar.getTime();
repository.add(new AuditEvent(calendar.getTime(), "dave", "c", data));
calendar.add(Calendar.DAY_OF_YEAR, 1);
repository.add(new AuditEvent(calendar.getTime(), "phil", "d", data));
calendar.add(Calendar.DAY_OF_YEAR, 1);
List<AuditEvent> events = repository.find(null, after);
assertThat(events.size(), equalTo(2));
assertThat(events.get(0).getType(), equalTo("c"));
assertThat(events.get(1).getType(), equalTo("d"));
events = repository.find("dave", after);
assertThat(events.size(), equalTo(1));
assertThat(events.get(0).getType(), equalTo("c"));
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment