Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[versions]
awaitility = "4.2.1"
awaitility = "4.3.0"
checkstyle = "10.12.5"
jackson = "2.17.2"
jacoco = "0.8.12"
junit = "5.10.2"
junit-platform = "1.10.2"
spotbugs = "6.0.12"
spotbugs-annotations = "4.8.6"
junit = "6.1.0"
junit-platform = "6.1.0"
spotbugs = "6.5.6"
spotbugs-annotations = "4.10.2"
spotless = "6.25.0"

[libraries]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public final class ChatHistoryStore {
private final Path historyFile;
private final int historyLimit;
private final List<ChatMessage> messages = new ArrayList<>();
private final Object lock = new Object();
private final boolean enabled;
private int corruptRecordCount;

Expand All @@ -45,41 +46,49 @@ public static ChatHistoryStore open(Path historyFile, int historyLimit) {
return new ChatHistoryStore(historyFile, historyLimit, true);
}

public synchronized void save(ChatMessage message) {
if (!enabled || !isPersistable(message)) {
return;
public void save(ChatMessage message) {
synchronized (lock) {
if (!enabled || !isPersistable(message)) {
return;
}
messages.add(message);
trimToLimit();
rewrite();
}
messages.add(message);
trimToLimit();
rewrite();
}

public synchronized List<ChatMessage> recentRoomMessages(String roomName, int limit) {
if (limit <= 0) {
return List.of();
}
List<ChatMessage> roomMessages = new ArrayList<>();
for (ChatMessage message : messages) {
if (message.type() == MessageType.ROOM_TEXT && roomName.equals(message.room())) {
roomMessages.add(message);
public List<ChatMessage> recentRoomMessages(String roomName, int limit) {
synchronized (lock) {
if (limit <= 0) {
return List.of();
}
List<ChatMessage> roomMessages = new ArrayList<>();
for (ChatMessage message : messages) {
if (message.type() == MessageType.ROOM_TEXT && roomName.equals(message.room())) {
roomMessages.add(message);
}
}
return last(roomMessages, limit);
}
return last(roomMessages, limit);
}

public synchronized Set<String> knownRooms() {
Set<String> rooms = new TreeSet<>();
rooms.add(ChatMessage.GENERAL_ROOM);
for (ChatMessage message : messages) {
if (message.room() != null && !message.room().isBlank()) {
rooms.add(message.room());
public Set<String> knownRooms() {
synchronized (lock) {
Set<String> rooms = new TreeSet<>();
rooms.add(ChatMessage.GENERAL_ROOM);
for (ChatMessage message : messages) {
if (message.room() != null && !message.room().isBlank()) {
rooms.add(message.room());
}
}
return Collections.unmodifiableSet(rooms);
}
return Collections.unmodifiableSet(rooms);
}

public synchronized int corruptRecordCount() {
return corruptRecordCount;
public int corruptRecordCount() {
synchronized (lock) {
return corruptRecordCount;
}
}

private void load() {
Expand Down
Loading