io.keen.client.java.RamEventStore Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of keen-client-java-core Show documentation
Show all versions of keen-client-java-core Show documentation
Java Client Core Library for Keen
package io.keen.client.java;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
/**
* Implementation of {@link KeenEventStore} which simply keeps a copy of each event in memory until
* it is explicitly removed.
*
* NOTE: This implementation synchronizes all operations in order to ensure thread safety, but as
* a result it may perform poorly under high load. For applications that require high throughput,
* a custom {@link io.keen.client.java.KeenEventStore} implementation is recommended.
*
* @author Kevin Litwack ([email protected])
* @since 2.0.0
*/
public class RamEventStore implements KeenAttemptCountingEventStore {
///// PUBLIC CONSTRUCTORS /////
/**
* Constructs a new RAM-based event store.
*/
public RamEventStore() {
collectionIds = new HashMap>();
events = new HashMap();
}
///// KeenEventStore METHODS /////
/**
* {@inheritDoc}
*/
@Override
public synchronized Object store(String projectId, String eventCollection,
String event) throws IOException {
// Create a key from the project ID and event collection.
String key = String.format(Locale.US, "%s$%s", projectId, eventCollection);
// Get the list of events for the specified key. If no list exists yet, create one.
List collectionEvents = collectionIds.get(key);
if (collectionEvents == null) {
collectionEvents = new ArrayList();
collectionIds.put(key, collectionEvents);
}
// Remove the oldest events until there is room for at least one more event.
while (collectionEvents.size() >= maxEventsPerCollection) {
long idToRemove = collectionEvents.remove(0);
events.remove(idToRemove);
}
// Add the event to the event store, add its ID to the collection's list, and return the ID.
long id = getNextId();
events.put(id, event);
collectionEvents.add(id);
return id;
}
/**
* {@inheritDoc}
*/
@Override
public synchronized String get(Object handle) throws IOException {
Long id = handleToId(handle);
return events.get(id);
}
/**
* {@inheritDoc}
*/
@Override
public synchronized void remove(Object handle) throws IOException {
Long id = handleToId(handle);
events.remove(id);
// Be lazy about removing handles from the collectionIds map - this can happen during the
// getHandles call.
}
/**
* {@inheritDoc}
*/
@Override
public synchronized Map> getHandles(String projectId) throws IOException {
Map> result = new HashMap>();
for (Map.Entry> entry : collectionIds.entrySet()) {
String key = entry.getKey();
// Skip collections for different projects.
if (!key.startsWith(projectId)) {
continue;
}
// Extract the collection name from the key.
String eventCollection = key.substring(projectId.length() + 1);
// Iterate over the list of handles, removing any "dead" events and adding the rest to
// the result map.
List ids = entry.getValue();
List