pl.allegro.tech.hermes.consumers.consumer.rate.maxrate.ConsumerMaxRatesEncoder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hermes-consumers Show documentation
Show all versions of hermes-consumers Show documentation
Fast and reliable message broker built on top of Kafka.
package pl.allegro.tech.hermes.consumers.consumer.rate.maxrate;
import org.agrona.ExpandableDirectByteBuffer;
import org.agrona.MutableDirectBuffer;
import pl.allegro.tech.hermes.consumers.consumer.rate.sbe.stubs.MaxRateEncoder;
import pl.allegro.tech.hermes.consumers.consumer.rate.sbe.stubs.MessageHeaderEncoder;
import pl.allegro.tech.hermes.consumers.subscription.id.SubscriptionId;
import pl.allegro.tech.hermes.consumers.subscription.id.SubscriptionIds;
import java.util.Map;
class ConsumerMaxRatesEncoder {
private final SubscriptionIds subscriptionIds;
private final MutableDirectBuffer buffer;
ConsumerMaxRatesEncoder(SubscriptionIds subscriptionIds, int bufferSize) {
this.subscriptionIds = subscriptionIds;
this.buffer = new ExpandableDirectByteBuffer(bufferSize);
}
byte[] encode(ConsumerMaxRates consumerMaxRates) {
MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder();
MaxRateEncoder body = new MaxRateEncoder();
Map filteredRates = consumerMaxRates.toSubscriptionsIdsMap(subscriptionIds::getSubscriptionId);
MaxRateEncoder.SubscriptionsEncoder subscriptionsEncoder = body.wrapAndApplyHeader(buffer, 0, headerEncoder)
.subscriptionsCount(filteredRates.size());
filteredRates.forEach((id, maxRate) -> {
subscriptionsEncoder.next()
.id(id.getValue())
.maxRate(maxRate.getMaxRate());
});
int len = headerEncoder.encodedLength() + body.encodedLength();
byte[] dst = new byte[len];
buffer.getBytes(0, dst);
return dst;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy