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

Stream state subjects as a map #1177

Merged
merged 1 commit into from
Jul 10, 2024
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
20 changes: 18 additions & 2 deletions src/main/java/io/nats/client/api/StreamState.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import io.nats.client.support.JsonValue;

import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static io.nats.client.support.ApiConstants.*;
import static io.nats.client.support.JsonValueUtils.*;
Expand All @@ -34,6 +36,7 @@ public class StreamState {
private final List<Subject> subjects;
private final List<Long> deletedStreamSequences;
private final LostStreamData lostStreamData;
private final Map<String, Long> subjectMap;

StreamState(JsonValue vStreamState) {
msgs = readLong(vStreamState, MESSAGES, 0);
Expand All @@ -48,6 +51,11 @@ public class StreamState {
subjects = Subject.listOf(readValue(vStreamState, SUBJECTS));
deletedStreamSequences = readLongList(vStreamState, DELETED);
lostStreamData = LostStreamData.optionalInstance(readValue(vStreamState, LOST));

subjectMap = new HashMap<>();
for (Subject s : subjects) {
subjectMap.put(s.getName(), s.getCount());
}
}

/**
Expand Down Expand Up @@ -122,14 +130,22 @@ public long getSubjectCount() {
}

/**
* Get a list of the Subject objects. May be null if the Stream Info request did not ask for subjects
* or if there are no subjects.
* Get a list of the Subject objects. May be empty, for instance
* if the Stream Info request did not ask for subjects or if there are no subjects.
* @return the list of subjects
*/
public List<Subject> getSubjects() {
return subjects;
}

/**
* Get a map of subjects instead of a list of Subject objects.
* @return the map
*/
public Map<String, Long> getSubjectMap() {
return subjectMap;
}

/**
* Gets the count of deleted messages
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ public void testGetStreamInfo() throws Exception {
assertEquals(0, si.getStreamState().getSubjects().size());
assertEquals(0, si.getStreamState().getDeletedCount());
assertEquals(0, si.getStreamState().getDeleted().size());
assertTrue(si.getStreamState().getSubjectMap().isEmpty());

if (nc.getServerInfo().isOlderThanVersion("2.10")) {
assertNull(si.getTimestamp());
Expand All @@ -427,6 +428,7 @@ public void testGetStreamInfo() throws Exception {
assertEquals(0, si.getStreamState().getSubjects().size());
assertEquals(5, si.getStreamState().getDeletedCount());
assertEquals(0, si.getStreamState().getDeleted().size());
assertTrue(si.getStreamState().getSubjectMap().isEmpty());

si = jsm.getStreamInfo(stream, StreamInfoOptions.builder().allSubjects().deletedDetails().build());
assertEquals(stream, si.getConfiguration().getName());
Expand All @@ -448,6 +450,7 @@ public void testGetStreamInfo() throws Exception {
Subject sf = map.get(subjectIx5 + ".bar");
assertNotNull(sf);
assertEquals(6, sf.getCount());
assertEquals(6, si.getStreamState().getSubjectMap().size());

for (PublishAck pa : packs) {
assertTrue(si.getStreamState().getDeleted().contains(pa.getSeqno()));
Expand Down
Loading