Skip to content

Commit

Permalink
Stream state subjects as a map (#1177)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottf authored Jul 10, 2024
1 parent c2e2d18 commit f80d42d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
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

0 comments on commit f80d42d

Please sign in to comment.