Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a bounded-size version of the in-memory message queue #652

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package quickfix;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* A bounded-size version of InMemoryMessageQueue.
* @see InMemoryMessageQueue
*/
public class BoundInMemoryMessageQueue implements MessageQueue {
// The map should be accessed from a single thread
private final Map<Integer, Message> backingMap = new LinkedHashMap<>();
private final int maxSize;

public BoundInMemoryMessageQueue(int maxSize) {
this.maxSize = maxSize;
}

public void enqueue(int sequence, Message message) {
if (backingMap.size() >= maxSize) {
List<Integer> keys = backingMap.keySet().stream().sorted().collect(Collectors.toList());
if (sequence < keys.get(0)) {
backingMap.remove(keys.get(keys.size()-1));
backingMap.put(sequence, message);
}
} else {
this.backingMap.put(sequence, message);
}
}

public Message dequeue(int sequence) {
return (Message) this.backingMap.remove(sequence);
}

public void clear() {
this.backingMap.clear();
}

Map<Integer, Message> getBackingMap() {
return this.backingMap;
}
}

Loading