Implemented H2 based SubscriptionsRepository
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package io.moquette.persistence;
|
||||
|
||||
import io.moquette.BrokerConstants;
|
||||
import io.moquette.broker.ISubscriptionsRepository;
|
||||
import io.moquette.broker.config.IConfig;
|
||||
import org.h2.mvstore.MVStore;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class H2Builder {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(H2Builder.class);
|
||||
|
||||
private final String storePath;
|
||||
private final int autosaveInterval; // in seconds
|
||||
private final ScheduledExecutorService scheduler;
|
||||
private MVStore mvStore;
|
||||
|
||||
public H2Builder(IConfig props, ScheduledExecutorService scheduler) {
|
||||
this.storePath = props.getProperty(BrokerConstants.PERSISTENT_STORE_PROPERTY_NAME, "");
|
||||
final String autosaveProp = props.getProperty(BrokerConstants.AUTOSAVE_INTERVAL_PROPERTY_NAME, "30");
|
||||
this.autosaveInterval = Integer.parseInt(autosaveProp);
|
||||
this.scheduler = scheduler;
|
||||
}
|
||||
|
||||
@SuppressWarnings("FutureReturnValueIgnored")
|
||||
public H2Builder initStore() {
|
||||
LOG.info("Initializing H2 store");
|
||||
if (storePath == null || storePath.isEmpty()) {
|
||||
throw new IllegalArgumentException("H2 store path can't be null or empty");
|
||||
}
|
||||
mvStore = new MVStore.Builder()
|
||||
.fileName(storePath)
|
||||
.autoCommitDisabled()
|
||||
.open();
|
||||
|
||||
LOG.trace("Scheduling H2 commit task");
|
||||
scheduler.scheduleWithFixedDelay(() -> {
|
||||
LOG.trace("Committing to H2");
|
||||
mvStore.commit();
|
||||
}, autosaveInterval, autosaveInterval, TimeUnit.SECONDS);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISubscriptionsRepository subscriptionsRepository() {
|
||||
return new H2SubscriptionsRepository(mvStore);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package io.moquette.persistence;
|
||||
|
||||
import io.moquette.broker.ISubscriptionsRepository;
|
||||
import io.moquette.broker.subscriptions.Subscription;
|
||||
import org.h2.mvstore.Cursor;
|
||||
import org.h2.mvstore.MVMap;
|
||||
import org.h2.mvstore.MVStore;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class H2SubscriptionsRepository implements ISubscriptionsRepository {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(H2SubscriptionsRepository.class);
|
||||
private static final String SUBSCRIPTIONS_MAP = "subscriptions";
|
||||
|
||||
private MVMap<String, Subscription> subscriptions;
|
||||
|
||||
H2SubscriptionsRepository(MVStore mvStore) {
|
||||
this.subscriptions = mvStore.openMap(SUBSCRIPTIONS_MAP);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Subscription> listAllSubscriptions() {
|
||||
LOG.debug("Retrieving existing subscriptions");
|
||||
|
||||
List<Subscription> results = new ArrayList<>();
|
||||
Cursor<String, Subscription> mapCursor = subscriptions.cursor(null);
|
||||
while (mapCursor.hasNext()) {
|
||||
String subscriptionStr = mapCursor.next();
|
||||
results.add(mapCursor.getValue());
|
||||
}
|
||||
LOG.debug("Loaded {} subscriptions", results.size());
|
||||
return results;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addNewSubscription(Subscription subscription) {
|
||||
subscriptions.put(subscription.getTopicFilter() + "-" + subscription.getClientId(), subscription);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeSubscription(String topic, String clientID) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import io.moquette.spi.IStore;
|
||||
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
@Deprecated
|
||||
public class MemoryStorageService implements IStore {
|
||||
|
||||
private MemorySessionStore m_sessionsStore;
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (c) 2012-2018 The original author or authors
|
||||
* ------------------------------------------------------
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Apache License v2.0 which accompanies this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* The Apache License v2.0 is available at
|
||||
* http://www.opensource.org/licenses/apache2.0.php
|
||||
*
|
||||
* You may elect to redistribute this code under either of these licenses.
|
||||
*/
|
||||
package io.moquette.persistence;
|
||||
|
||||
import io.moquette.broker.ISubscriptionsRepository;
|
||||
import io.moquette.broker.subscriptions.Subscription;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class MemorySubscriptionsRepository implements ISubscriptionsRepository {
|
||||
|
||||
private final List<Subscription> subscriptions = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public List<Subscription> listAllSubscriptions() {
|
||||
return Collections.unmodifiableList(subscriptions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addNewSubscription(Subscription subscription) {
|
||||
subscriptions.add(subscription);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeSubscription(String topic, String clientID) {
|
||||
subscriptions.stream()
|
||||
.filter(s -> s.getTopicFilter().toString().equals(topic) && s.getClientId().equals(clientID))
|
||||
.findFirst()
|
||||
.ifPresent(subscriptions::remove);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user