Initial try to remove buffer copies
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package org.dna.mqtt.moquette.messaging.spi;
|
package org.dna.mqtt.moquette.messaging.spi;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
import org.dna.mqtt.moquette.messaging.spi.impl.subscriptions.Subscription;
|
import org.dna.mqtt.moquette.messaging.spi.impl.subscriptions.Subscription;
|
||||||
import org.dna.mqtt.moquette.messaging.spi.impl.events.PublishEvent;
|
import org.dna.mqtt.moquette.messaging.spi.impl.events.PublishEvent;
|
||||||
import org.dna.mqtt.moquette.proto.messages.AbstractMessage;
|
import org.dna.mqtt.moquette.proto.messages.AbstractMessage;
|
||||||
@@ -18,7 +19,7 @@ public interface IStorageService {
|
|||||||
* */
|
* */
|
||||||
void initStore();
|
void initStore();
|
||||||
|
|
||||||
void storeRetained(String topic, byte[] message, AbstractMessage.QOSType qos);
|
void storeRetained(String topic, ByteBuffer message, AbstractMessage.QOSType qos);
|
||||||
|
|
||||||
Collection<StoredMessage> searchMatching(IMatchingCondition condition);
|
Collection<StoredMessage> searchMatching(IMatchingCondition condition);
|
||||||
|
|
||||||
|
|||||||
+4
-3
@@ -15,6 +15,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
|
||||||
@@ -38,8 +39,8 @@ public class HawtDBStorageService implements IStorageService {
|
|||||||
return m_qos;
|
return m_qos;
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] getPayload() {
|
ByteBuffer getPayload() {
|
||||||
return m_payload;
|
return (ByteBuffer) ByteBuffer.allocate(m_payload.length).put(m_payload).flip();
|
||||||
}
|
}
|
||||||
|
|
||||||
String getTopic() {
|
String getTopic() {
|
||||||
@@ -132,7 +133,7 @@ public class HawtDBStorageService implements IStorageService {
|
|||||||
m_qos2Store = (SortedIndex<String, PublishEvent>) m_multiIndexFactory.openOrCreate("qos2Store", indexFactory);
|
m_qos2Store = (SortedIndex<String, PublishEvent>) m_multiIndexFactory.openOrCreate("qos2Store", indexFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void storeRetained(String topic, byte[] message, AbstractMessage.QOSType qos) {
|
public void storeRetained(String topic, ByteBuffer message, AbstractMessage.QOSType qos) {
|
||||||
if (message.length == 0) {
|
if (message.length == 0) {
|
||||||
//clean the message from topic
|
//clean the message from topic
|
||||||
m_retainedStore.remove(topic);
|
m_retainedStore.remove(topic);
|
||||||
|
|||||||
+12
-11
@@ -4,6 +4,7 @@ import com.lmax.disruptor.BatchEventProcessor;
|
|||||||
import com.lmax.disruptor.EventHandler;
|
import com.lmax.disruptor.EventHandler;
|
||||||
import com.lmax.disruptor.RingBuffer;
|
import com.lmax.disruptor.RingBuffer;
|
||||||
import com.lmax.disruptor.SequenceBarrier;
|
import com.lmax.disruptor.SequenceBarrier;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -123,8 +124,10 @@ class ProtocolProcessor implements EventHandler<ValueEvent> {
|
|||||||
//Handle will flag
|
//Handle will flag
|
||||||
if (msg.isWillFlag()) {
|
if (msg.isWillFlag()) {
|
||||||
AbstractMessage.QOSType willQos = AbstractMessage.QOSType.values()[msg.getWillQos()];
|
AbstractMessage.QOSType willQos = AbstractMessage.QOSType.values()[msg.getWillQos()];
|
||||||
PublishEvent pubEvt = new PublishEvent(msg.getWillTopic(), willQos, msg.getWillMessage().getBytes(),
|
byte[] willPayload = msg.getWillMessage().getBytes();
|
||||||
msg.isWillRetain(), msg.getClientID(), session);
|
ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(willPayload.length).put(willPayload).flip();
|
||||||
|
PublishEvent pubEvt = new PublishEvent(msg.getWillTopic(), willQos,
|
||||||
|
bb, msg.isWillRetain(), msg.getClientID(), session);
|
||||||
processPublish(pubEvt);
|
processPublish(pubEvt);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,7 +191,7 @@ class ProtocolProcessor implements EventHandler<ValueEvent> {
|
|||||||
LOG.debug("processPublish invoked with " + evt);
|
LOG.debug("processPublish invoked with " + evt);
|
||||||
final String topic = evt.getTopic();
|
final String topic = evt.getTopic();
|
||||||
final AbstractMessage.QOSType qos = evt.getQos();
|
final AbstractMessage.QOSType qos = evt.getQos();
|
||||||
final byte[] message = evt.getMessage();
|
final ByteBuffer message = evt.getMessage();
|
||||||
boolean retain = evt.isRetain();
|
boolean retain = evt.isRetain();
|
||||||
|
|
||||||
String publishKey = null;
|
String publishKey = null;
|
||||||
@@ -222,7 +225,7 @@ class ProtocolProcessor implements EventHandler<ValueEvent> {
|
|||||||
/**
|
/**
|
||||||
* Flood the subscribers with the message to notify. MessageID is optional and should only used for QoS 1 and 2
|
* Flood the subscribers with the message to notify. MessageID is optional and should only used for QoS 1 and 2
|
||||||
* */
|
* */
|
||||||
private void publish2Subscribers(String topic, AbstractMessage.QOSType qos, byte[] message, boolean retain, Integer messageID) {
|
private void publish2Subscribers(String topic, AbstractMessage.QOSType qos, ByteBuffer message, boolean retain, Integer messageID) {
|
||||||
LOG.debug("publish2Subscribers republishing to existing subscribers that matches the topic " + topic);
|
LOG.debug("publish2Subscribers republishing to existing subscribers that matches the topic " + topic);
|
||||||
for (final Subscription sub : subscriptions.matches(topic)) {
|
for (final Subscription sub : subscriptions.matches(topic)) {
|
||||||
LOG.debug("found matching subscription on topic " + sub.getTopic());
|
LOG.debug("found matching subscription on topic " + sub.getTopic());
|
||||||
@@ -250,11 +253,11 @@ class ProtocolProcessor implements EventHandler<ValueEvent> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendPublish(String clientId, String topic, AbstractMessage.QOSType qos, byte[] message, boolean retained) {
|
private void sendPublish(String clientId, String topic, AbstractMessage.QOSType qos, ByteBuffer message, boolean retained) {
|
||||||
sendPublish(clientId, topic, qos, message, retained, 0);
|
sendPublish(clientId, topic, qos, message, retained, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendPublish(String clientId, String topic, AbstractMessage.QOSType qos, byte[] message, boolean retained, int messageID) {
|
private void sendPublish(String clientId, String topic, AbstractMessage.QOSType qos, ByteBuffer message, boolean retained, int messageID) {
|
||||||
LOG.debug("notify invoked with event ");
|
LOG.debug("notify invoked with event ");
|
||||||
PublishMessage pubMessage = new PublishMessage();
|
PublishMessage pubMessage = new PublishMessage();
|
||||||
pubMessage.setRetainFlag(retained);
|
pubMessage.setRetainFlag(retained);
|
||||||
@@ -324,15 +327,13 @@ class ProtocolProcessor implements EventHandler<ValueEvent> {
|
|||||||
|
|
||||||
final String topic = evt.getTopic();
|
final String topic = evt.getTopic();
|
||||||
final AbstractMessage.QOSType qos = evt.getQos();
|
final AbstractMessage.QOSType qos = evt.getQos();
|
||||||
final byte[] message = evt.getMessage();
|
|
||||||
boolean retain = evt.isRetain();
|
|
||||||
|
|
||||||
publish2Subscribers(topic, qos, message, retain, evt.getMessageID());
|
publish2Subscribers(topic, qos, evt.getMessage(), evt.isRetain(), evt.getMessageID());
|
||||||
|
|
||||||
m_storageService.removeQoS2Message(publishKey);
|
m_storageService.removeQoS2Message(publishKey);
|
||||||
|
|
||||||
if (retain) {
|
if (evt.isRetain()) {
|
||||||
m_storageService.storeRetained(topic, message, qos);
|
m_storageService.storeRetained(topic, evt.getMessage(), qos);
|
||||||
}
|
}
|
||||||
|
|
||||||
sendPubComp(clientID, messageID);
|
sendPubComp(clientID, messageID);
|
||||||
|
|||||||
@@ -108,17 +108,15 @@ public class SimpleMessaging implements IMessaging, EventHandler<ValueEvent> {
|
|||||||
if (message instanceof ConnectMessage) {
|
if (message instanceof ConnectMessage) {
|
||||||
m_processor.processConnect(session, (ConnectMessage) message);
|
m_processor.processConnect(session, (ConnectMessage) message);
|
||||||
} else if (message instanceof PublishMessage) {
|
} else if (message instanceof PublishMessage) {
|
||||||
PublishMessage pubMsg = (PublishMessage) message;
|
|
||||||
PublishEvent pubEvt;
|
PublishEvent pubEvt;
|
||||||
|
|
||||||
String clientID = (String) session.getAttribute(Constants.ATTR_CLIENTID);
|
String clientID = (String) session.getAttribute(Constants.ATTR_CLIENTID);
|
||||||
|
pubEvt = new PublishEvent((PublishMessage) message, clientID, session);
|
||||||
if (message.getQos() == QOSType.MOST_ONE) {
|
// if (message.getQos() == QOSType.MOST_ONE) {
|
||||||
pubEvt = new PublishEvent(pubMsg.getTopicName(), pubMsg.getQos(), pubMsg.getPayload(), pubMsg.isRetainFlag(), clientID, session);
|
// pubEvt = new PublishEvent(pubMsg.getTopicName(), pubMsg.getQos(), pubMsg.getPayload(), pubMsg.isRetainFlag(), clientID, session);
|
||||||
|
//
|
||||||
} else {
|
// } else {
|
||||||
pubEvt = new PublishEvent(pubMsg.getTopicName(), pubMsg.getQos(), pubMsg.getPayload(), pubMsg.isRetainFlag(), clientID, pubMsg.getMessageID(), session);
|
// pubEvt = new PublishEvent(pubMsg.getTopicName(), pubMsg.getQos(), pubMsg.getPayload(), pubMsg.isRetainFlag(), clientID, pubMsg.getMessageID(), session);
|
||||||
}
|
// }
|
||||||
m_processor.processPublish(pubEvt);
|
m_processor.processPublish(pubEvt);
|
||||||
} else if (message instanceof DisconnectMessage) {
|
} else if (message instanceof DisconnectMessage) {
|
||||||
String clientID = (String) session.getAttribute(Constants.ATTR_CLIENTID);
|
String clientID = (String) session.getAttribute(Constants.ATTR_CLIENTID);
|
||||||
|
|||||||
+19
-4
@@ -3,6 +3,8 @@ package org.dna.mqtt.moquette.messaging.spi.impl.events;
|
|||||||
import org.dna.mqtt.moquette.proto.messages.AbstractMessage.QOSType;
|
import org.dna.mqtt.moquette.proto.messages.AbstractMessage.QOSType;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import org.dna.mqtt.moquette.proto.messages.PublishMessage;
|
||||||
import org.dna.mqtt.moquette.server.ServerChannel;
|
import org.dna.mqtt.moquette.server.ServerChannel;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -12,7 +14,8 @@ import org.dna.mqtt.moquette.server.ServerChannel;
|
|||||||
public class PublishEvent extends MessagingEvent implements Serializable {
|
public class PublishEvent extends MessagingEvent implements Serializable {
|
||||||
String m_topic;
|
String m_topic;
|
||||||
QOSType m_qos;
|
QOSType m_qos;
|
||||||
byte[] m_message;
|
//byte[] m_message;
|
||||||
|
ByteBuffer m_message;
|
||||||
boolean m_retain;
|
boolean m_retain;
|
||||||
String m_clientID;
|
String m_clientID;
|
||||||
//Optional attribute, available only fo QoS 1 and 2
|
//Optional attribute, available only fo QoS 1 and 2
|
||||||
@@ -20,7 +23,19 @@ public class PublishEvent extends MessagingEvent implements Serializable {
|
|||||||
|
|
||||||
transient ServerChannel m_session;
|
transient ServerChannel m_session;
|
||||||
|
|
||||||
public PublishEvent(String topic, QOSType qos, byte[] message, boolean retain,
|
public PublishEvent(PublishMessage pubMsg, String clientID, ServerChannel session) {
|
||||||
|
m_topic = pubMsg.getTopicName();
|
||||||
|
m_qos = pubMsg.getQos();
|
||||||
|
m_message = pubMsg.getPayload();
|
||||||
|
m_retain = pubMsg.isRetainFlag();
|
||||||
|
m_clientID = clientID;
|
||||||
|
m_session = session;
|
||||||
|
if (pubMsg.getQos() != QOSType.MOST_ONE) {
|
||||||
|
m_msgID = pubMsg.getMessageID();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public PublishEvent(String topic, QOSType qos, ByteBuffer message, boolean retain,
|
||||||
String clientID, ServerChannel session) {
|
String clientID, ServerChannel session) {
|
||||||
m_topic = topic;
|
m_topic = topic;
|
||||||
m_qos = qos;
|
m_qos = qos;
|
||||||
@@ -30,7 +45,7 @@ public class PublishEvent extends MessagingEvent implements Serializable {
|
|||||||
m_session = session;
|
m_session = session;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PublishEvent(String topic, QOSType qos, byte[] message, boolean retain,
|
public PublishEvent(String topic, QOSType qos, ByteBuffer message, boolean retain,
|
||||||
String clientID, int msgID, ServerChannel session) {
|
String clientID, int msgID, ServerChannel session) {
|
||||||
this(topic, qos, message, retain, clientID, session);
|
this(topic, qos, message, retain, clientID, session);
|
||||||
m_msgID = msgID;
|
m_msgID = msgID;
|
||||||
@@ -44,7 +59,7 @@ public class PublishEvent extends MessagingEvent implements Serializable {
|
|||||||
return m_qos;
|
return m_qos;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] getMessage() {
|
public ByteBuffer getMessage() {
|
||||||
return m_message;
|
return m_message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+5
-3
@@ -1,5 +1,7 @@
|
|||||||
package org.dna.mqtt.moquette.proto.messages;
|
package org.dna.mqtt.moquette.proto.messages;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author andrea
|
* @author andrea
|
||||||
@@ -8,7 +10,7 @@ public class PublishMessage extends MessageIDMessage {
|
|||||||
|
|
||||||
private String m_topicName;
|
private String m_topicName;
|
||||||
// private Integer m_messageID; //could be null if Qos is == 0
|
// private Integer m_messageID; //could be null if Qos is == 0
|
||||||
private byte[] m_payload;
|
private ByteBuffer m_payload;
|
||||||
|
|
||||||
/*public Integer getMessageID() {
|
/*public Integer getMessageID() {
|
||||||
return m_messageID;
|
return m_messageID;
|
||||||
@@ -30,11 +32,11 @@ public class PublishMessage extends MessageIDMessage {
|
|||||||
this.m_topicName = topicName;
|
this.m_topicName = topicName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] getPayload() {
|
public ByteBuffer getPayload() {
|
||||||
return m_payload;
|
return m_payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPayload(byte[] payload) {
|
public void setPayload(ByteBuffer payload) {
|
||||||
this.m_payload = payload;
|
this.m_payload = payload;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user