Skip to content

Commit 8aadfb5

Browse files
committed
Remove no header
1 parent a9e36c3 commit 8aadfb5

4 files changed

Lines changed: 18 additions & 116 deletions

File tree

counter/src/examples/java/io/synadia/examples/CounterContextExample.java

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import io.nats.client.api.StreamConfiguration;
1212
import io.synadia.counter.Counter;
1313
import io.synadia.counter.CounterEntryResponse;
14-
import io.synadia.counter.CounterValueResponse;
1514

1615
import java.math.BigInteger;
1716
import java.util.concurrent.LinkedBlockingQueue;
@@ -98,37 +97,20 @@ public static void main(String[] args) throws Exception {
9897
}
9998

10099
// ----------------------------------------------------------------------------------------------------
101-
System.out.println("\n4.1: getMultiple(\"cs.A\", \"cs.B\", \"cs.C\") - Get the CounterValueResponse objects for multiple subjects. Maybe to total them up?\"");
102-
LinkedBlockingQueue<CounterValueResponse> vResponses = counter.getMultiple("cs.A", "cs.B", "cs.C");
103-
BigInteger total = BigInteger.ZERO;
104-
CounterValueResponse vr = vResponses.poll(1, TimeUnit.SECONDS);
105-
while (vr != null && vr.isValue()) {
106-
System.out.println(" " + vr);
107-
total = total.add(vr.getValue());
108-
vr = vResponses.poll(10, TimeUnit.MILLISECONDS);
109-
}
110-
System.out.println(" " + vr + " -> No more values.");
111-
System.out.println(" Values totaled: " + total);
112-
113-
System.out.println("\n4.2: getEntries(\"cs.A\", \"cs.B\", \"cs.C\") - Get the CounterEntryResponse objects for multiple subjects.");
100+
System.out.println("\n4.1: getEntries(\"cs.A\", \"cs.B\", \"cs.C\") - Get the CounterEntryResponse objects for multiple subjects.");
114101
LinkedBlockingQueue<CounterEntryResponse> eResponses = counter.getEntries("cs.A", "cs.B", "cs.C");
102+
BigInteger total = BigInteger.ZERO;
115103
CounterEntryResponse er = eResponses.poll(1, TimeUnit.SECONDS);
116104
while (er != null && er.isEntry()) {
117105
System.out.println(" " + er);
106+
// the entry response has a method to simplify getting the value
107+
total = total.add(er.getValue());
118108
er = eResponses.poll(10, TimeUnit.MILLISECONDS);
119109
}
120110
System.out.println(" " + er + " -> No more entries.");
111+
System.out.println(" Values totaled: " + total);
121112

122-
System.out.println("\n4.3: getMultiple(\"cs.*\") - Get the CounterValueResponse objects for wildcard subject(s).");
123-
vResponses = counter.getMultiple("cs.*");
124-
vr = vResponses.poll(1, TimeUnit.SECONDS);
125-
while (vr != null && vr.isValue()) {
126-
System.out.println(" " + vr);
127-
vr = vResponses.poll(10, TimeUnit.MILLISECONDS);
128-
}
129-
System.out.println(" " + vr + " -> No more values.");
130-
131-
System.out.println("\n4.4: getEntries(\"cs.*\") - Get CounterEntryResponse objects for wildcard subject(s).");
113+
System.out.println("\n4.2: getEntries(\"cs.*\") - Get CounterEntryResponse objects for wildcard subject(s).");
132114
eResponses = counter.getEntries("cs.*");
133115
er = eResponses.poll(1, TimeUnit.SECONDS);
134116
while (er != null && er.isEntry()) {
@@ -162,16 +144,7 @@ public static void main(String[] args) throws Exception {
162144
System.out.println(" get(\"cs.did-not-exist\") -> " + counter.get("cs.did-not-exist"));
163145

164146
// ----------------------------------------------------------------------------------------------------
165-
System.out.println("\n6.1: getMultiple(\"cs.no-counters\", \"cs.also-counters\") - getMultiple but no subjects have counters.");
166-
vResponses = counter.getMultiple("cs.no-counters", "cs.also-counters");
167-
vr = vResponses.poll(1, TimeUnit.SECONDS);
168-
while (vr != null && vr.isValue()) {
169-
System.out.println(" " + er);
170-
vr = vResponses.poll(10, TimeUnit.MILLISECONDS);
171-
}
172-
System.out.println(" " + vr);
173-
174-
System.out.println("\n6.2: getEntries(\"cs.no-counters\", \"cs.also-counters\") - getEntries but no subjects have counters.");
147+
System.out.println("\n6.1: getEntries(\"cs.no-counters\", \"cs.also-counters\") - getEntries but no subjects have counters.");
175148
eResponses = counter.getEntries("cs.no-counters", "cs.also-counters");
176149
er = eResponses.poll(1, TimeUnit.SECONDS);
177150
while (er != null && er.isEntry()) {
@@ -181,16 +154,7 @@ public static void main(String[] args) throws Exception {
181154
System.out.println(" " + er);
182155

183156
// ----------------------------------------------------------------------------------------------------
184-
System.out.println("\n7.1: getMultiple(\"no-counters\", \"cs.A\", \"cs.B\", \"cs.C\") - getMultiple when some subjects have counters.");
185-
vResponses = counter.getMultiple("cs.no-counters", "cs.A", "cs.B", "cs.C");
186-
vr = vResponses.poll(1, TimeUnit.SECONDS);
187-
while (vr != null && vr.isValue()) {
188-
System.out.println(" " + vr);
189-
vr = vResponses.poll(10, TimeUnit.MILLISECONDS);
190-
}
191-
System.out.println(" " + vr + " -> No more values.");
192-
193-
System.out.println("\n7.2: getEntries(\"no-counters\", \"cs.A\", \"cs.B\", \"cs.C\") - getEntries when some subjects have counters.");
157+
System.out.println("\n7.1: getEntries(\"no-counters\", \"cs.A\", \"cs.B\", \"cs.C\") - getEntries when some subjects have counters.");
194158
eResponses = counter.getEntries("cs.no-counters", "cs.A", "cs.B", "cs.C");
195159
er = eResponses.poll(1, TimeUnit.SECONDS);
196160
while (er != null && er.isEntry()) {

counter/src/main/java/io/synadia/counter/Counter.java

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
import java.util.List;
1818
import java.util.concurrent.LinkedBlockingQueue;
1919

20-
import static io.nats.client.support.ApiConstants.LAST_BY_SUBJECT;
21-
import static io.nats.client.support.ApiConstants.NO_HDR;
22-
import static io.nats.client.support.JsonUtils.*;
2320
import static io.nats.client.support.Validator.required;
2421
import static io.synadia.counter.CounterUtils.INCREMENT_HEADER;
2522
import static io.synadia.counter.CounterUtils.extractVal;
@@ -135,30 +132,9 @@ public BigInteger setViaAdd(String subject, BigInteger value) throws JetStreamAp
135132
return _add(subject, value.subtract(bi).toString());
136133
}
137134

138-
static class NoHeadersMessageGetRequest extends MessageGetRequest {
139-
public NoHeadersMessageGetRequest(String lastBySubject) {
140-
super(-1, lastBySubject, null, null);
141-
}
142-
143-
@Override
144-
public boolean isLastBySubject() {
145-
// this makes sure that the underlying getMessage call
146-
// doesn't use the JSAPI_DIRECT_GET_LAST api
147-
return false;
148-
}
149-
150-
@Override
151-
public @NonNull String toJson() {
152-
StringBuilder sb = beginJson();
153-
addField(sb, LAST_BY_SUBJECT, getLastBySubject());
154-
addField(sb, NO_HDR, true);
155-
return endJson(sb).toString();
156-
}
157-
}
158-
159135
public BigInteger get(String subject) throws JetStreamApiException, IOException {
160136
validateSingleSubject(subject);
161-
MessageInfo mi = jsm.getMessage(streamName, new NoHeadersMessageGetRequest(subject));
137+
MessageInfo mi = jsm.getMessage(streamName, MessageGetRequest.lastForSubject(subject));
162138
return extractVal(mi.getData());
163139
}
164140

@@ -179,17 +155,6 @@ public BigInteger getOrElse(String subject, BigInteger dflt) throws IOException
179155
}
180156
}
181157

182-
public LinkedBlockingQueue<CounterValueResponse> getMultiple(String... subjects) {
183-
return getMultiple(Arrays.asList(subjects));
184-
}
185-
186-
public LinkedBlockingQueue<CounterValueResponse> getMultiple(List<String> subjects) {
187-
LinkedBlockingQueue<CounterValueResponse> queue = new LinkedBlockingQueue<>();
188-
MessageBatchGetRequest mbgr = MessageBatchGetRequest.multiLastForSubjects(subjects);
189-
dbCtx.requestMessageBatch(mbgr, mi -> queue.add(new CounterValueResponse(mi)));
190-
return queue;
191-
}
192-
193158
public CounterEntry getEntry(String subject) throws JetStreamApiException, IOException {
194159
validateSingleSubject(subject);
195160
MessageInfo mi = jsm.getLastMessage(streamName, subject);

counter/src/main/java/io/synadia/counter/CounterEntryResponse.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
import io.nats.client.api.MessageInfo;
77
import org.jspecify.annotations.Nullable;
88

9+
import java.math.BigInteger;
10+
11+
import static io.synadia.counter.CounterUtils.extractVal;
12+
913
public class CounterEntryResponse extends CounterResponse {
1014

1115
CounterEntryResponse(MessageInfo mi) {
@@ -20,6 +24,11 @@ public boolean isEntry() {
2024
return mi.isMessage();
2125
}
2226

27+
@Nullable
28+
public BigInteger getValue() {
29+
return mi.isMessage() ? extractVal(mi.getData()) : null;
30+
}
31+
2332
@Nullable
2433
public CounterEntry getEntry() {
2534
return mi.isMessage() ? new CounterEntry(mi) : null;

counter/src/main/java/io/synadia/counter/CounterValueResponse.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)