@@ -13,18 +13,17 @@
|
|||||||
*
|
*
|
||||||
* You may elect to redistribute this code under either of these licenses.
|
* You may elect to redistribute this code under either of these licenses.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package io.moquette.persistence.h2;
|
package io.moquette.persistence.h2;
|
||||||
|
|
||||||
import io.moquette.spi.IMatchingCondition;
|
import io.moquette.spi.IMatchingCondition;
|
||||||
import io.moquette.spi.IMessagesStore;
|
import io.moquette.spi.IMessagesStore;
|
||||||
import io.moquette.spi.MessageGUID;
|
|
||||||
import io.moquette.spi.impl.subscriptions.Topic;
|
import io.moquette.spi.impl.subscriptions.Topic;
|
||||||
import org.h2.mvstore.Cursor;
|
import org.h2.mvstore.Cursor;
|
||||||
import org.h2.mvstore.MVMap;
|
import org.h2.mvstore.MVMap;
|
||||||
import org.h2.mvstore.MVStore;
|
import org.h2.mvstore.MVStore;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
class H2MessagesStore implements IMessagesStore {
|
class H2MessagesStore implements IMessagesStore {
|
||||||
@@ -33,12 +32,7 @@ class H2MessagesStore implements IMessagesStore {
|
|||||||
|
|
||||||
private final MVStore mvStore;
|
private final MVStore mvStore;
|
||||||
|
|
||||||
// maps clientID -> guid
|
private MVMap<Topic, StoredMessage> retainedStore;
|
||||||
private MVMap<Topic, MessageGUID> retainedStore;
|
|
||||||
// maps guid to message, it's message store
|
|
||||||
private MVMap<MessageGUID, StoredMessage> persistentMessageStore;
|
|
||||||
|
|
||||||
private MVMap<Topic, StoredMessage> new_retainedStore;
|
|
||||||
|
|
||||||
public H2MessagesStore(MVStore mvStore) {
|
public H2MessagesStore(MVStore mvStore) {
|
||||||
this.mvStore = mvStore;
|
this.mvStore = mvStore;
|
||||||
@@ -47,8 +41,6 @@ class H2MessagesStore implements IMessagesStore {
|
|||||||
@Override
|
@Override
|
||||||
public void initStore() {
|
public void initStore() {
|
||||||
retainedStore = mvStore.openMap("retained");
|
retainedStore = mvStore.openMap("retained");
|
||||||
persistentMessageStore = mvStore.openMap("oldPersistedMessages");
|
|
||||||
new_retainedStore = mvStore.openMap("persistedMessages");
|
|
||||||
LOG.info("Initialized message H2 store");
|
LOG.info("Initialized message H2 store");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,9 +48,9 @@ class H2MessagesStore implements IMessagesStore {
|
|||||||
public void storeRetained(Topic topic, StoredMessage storedMessage) {
|
public void storeRetained(Topic topic, StoredMessage storedMessage) {
|
||||||
LOG.debug("Store retained message for topic={}, CId={}", topic, storedMessage.getClientID());
|
LOG.debug("Store retained message for topic={}, CId={}", topic, storedMessage.getClientID());
|
||||||
if (storedMessage.getClientID() == null) {
|
if (storedMessage.getClientID() == null) {
|
||||||
throw new IllegalArgumentException( "Message to be persisted must have a not null client ID");
|
throw new IllegalArgumentException("Message to be persisted must have a not null client ID");
|
||||||
}
|
}
|
||||||
new_retainedStore.put(topic, storedMessage);
|
retainedStore.put(topic, storedMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -66,19 +58,14 @@ class H2MessagesStore implements IMessagesStore {
|
|||||||
LOG.debug("Scanning retained messages");
|
LOG.debug("Scanning retained messages");
|
||||||
List<StoredMessage> results = new ArrayList<>();
|
List<StoredMessage> results = new ArrayList<>();
|
||||||
|
|
||||||
Cursor<Topic, MessageGUID> mapCursor = retainedStore.cursor(null);
|
Cursor<Topic, StoredMessage> mapCursor = retainedStore.cursor(null);
|
||||||
while (mapCursor.hasNext()) {
|
while (mapCursor.hasNext()) {
|
||||||
final MessageGUID guid = mapCursor.getValue();
|
Topic topic = mapCursor.next();
|
||||||
final Topic topic = mapCursor.getKey();
|
if (topic != null && condition.match(topic))
|
||||||
StoredMessage storedMsg = persistentMessageStore.get(guid);
|
results.add(mapCursor.getValue());
|
||||||
if (condition.match(topic)) {
|
|
||||||
results.add(storedMsg);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (LOG.isTraceEnabled()) {
|
LOG.trace("Retained messages have been scanned matchingMessages={}", results);
|
||||||
LOG.trace("Retained messages have been scanned matchingMessages={}", results);
|
|
||||||
}
|
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|||||||
+2
@@ -4,6 +4,7 @@ import io.moquette.BrokerConstants;
|
|||||||
import org.h2.mvstore.MVStore;
|
import org.h2.mvstore.MVStore;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@@ -54,6 +55,7 @@ public class H2PersistentQueueTest {
|
|||||||
assertTrue("after poll 2 elements inserted before, should be empty", sut.isEmpty());
|
assertTrue("after poll 2 elements inserted before, should be empty", sut.isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Ignore
|
||||||
@Test
|
@Test
|
||||||
public void testPerformance() {
|
public void testPerformance() {
|
||||||
H2PersistentQueue<String> sut = new H2PersistentQueue<>(this.mvStore, "test");
|
H2PersistentQueue<String> sut = new H2PersistentQueue<>(this.mvStore, "test");
|
||||||
Reference in New Issue
Block a user