Removed the constraint on client queues to be BlockingQueue but simple Queue

This commit is contained in:
andsel
2017-06-20 21:55:59 +02:00
parent 1f3d39f4b1
commit 53ec8e5b60
10 changed files with 38 additions and 78 deletions
@@ -9,7 +9,7 @@ import java.util.concurrent.atomic.AtomicLong;
class H2PersistentQueue<T> extends AbstractQueue<T> {
private final MVMap<Long, T> mvMap;
private final MVMap<Long, T> queueMap;
private final MVMap<String, Long> metadataMap;
private final AtomicLong head;
private final AtomicLong tail;
@@ -18,7 +18,7 @@ class H2PersistentQueue<T> extends AbstractQueue<T> {
if (queueName == null || queueName.isEmpty()) {
throw new IllegalArgumentException("queueName parameter can't be empty or null");
}
this.mvMap = store.openMap("queue_" + queueName);
this.queueMap = store.openMap("queue_" + queueName);
this.metadataMap = store.openMap("queue_" + queueName + "_meta");
//setup head index
@@ -40,6 +40,11 @@ class H2PersistentQueue<T> extends AbstractQueue<T> {
this.tail = new AtomicLong(tailIdx);
}
static void dropQueue(MVStore store, String queueName) {
store.removeMap(store.openMap("queue_" + queueName));
store.removeMap(store.openMap("queue_" + queueName + "_meta"));
}
@Override
public Iterator<T> iterator() {
return null;
@@ -56,7 +61,7 @@ class H2PersistentQueue<T> extends AbstractQueue<T> {
throw new NullPointerException("Inserted element can't be null");
}
final long nextHead = head.getAndIncrement();
this.mvMap.put(nextHead, t);
this.queueMap.put(nextHead, t);
this.metadataMap.put("head", nextHead + 1);
return true;
}
@@ -67,7 +72,7 @@ class H2PersistentQueue<T> extends AbstractQueue<T> {
return null;
}
final long nextTail = tail.getAndIncrement();
final T tail = this.mvMap.get(nextTail);
final T tail = this.queueMap.get(nextTail);
this.metadataMap.put("tail", nextTail + 1);
return tail;
}
@@ -77,7 +82,7 @@ class H2PersistentQueue<T> extends AbstractQueue<T> {
if (head.equals(tail)) {
return null;
}
return this.mvMap.get(tail.get());
return this.queueMap.get(tail.get());
}
}
@@ -23,14 +23,12 @@ import io.moquette.spi.ISessionsStore;
import io.moquette.spi.ISubscriptionsStore;
import io.moquette.spi.impl.subscriptions.Subscription;
import io.moquette.spi.impl.subscriptions.Topic;
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.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@@ -219,7 +217,7 @@ public class H2SessionsStore implements ISessionsStore, ISubscriptionsStore {
Set<Integer> inFlightForClient = this.inFlightIds.get(clientID);
if (inFlightForClient == null) {
int nextPacketId = 1;
inFlightForClient = Collections.newSetFromMap(new ConcurrentHashMap<Integer, Boolean>());
inFlightForClient = Collections.newSetFromMap(new ConcurrentHashMap<>());
inFlightForClient.add(nextPacketId);
this.inFlightIds.put(clientID, inFlightForClient);
return nextPacketId;
@@ -232,53 +230,14 @@ public class H2SessionsStore implements ISessionsStore, ISubscriptionsStore {
return nextPacketId;
}
class H2BackedQueue<T> extends AbstractQueue<T> {
private final MVMap<T, ?> map;
private final Cursor<T, ?> cursor;
H2BackedQueue(MVMap<T, ?> map) {
this.cursor = map.cursor(null);
this.map = map;
}
@Override
public Iterator<T> iterator() {
return cursor;
}
@Override
public int size() {
return map.size();
}
@Override
public boolean offer(T t) {
return false;
}
@Override
public T poll() {
return null;
}
@Override
public T peek() {
return null;
}
}
//TODO
@Override
public BlockingQueue<StoredMessage> queue(String clientID) {
throw new RuntimeException("Not yet implemented");
public Queue<StoredMessage> queue(String clientID) {
return new H2PersistentQueue<>(this.mvStore, clientID);
}
//TODO
@Override
public void dropQueue(String clientID) {
throw new RuntimeException("Not yet implemented");
H2PersistentQueue.dropQueue(this.mvStore, clientID);
}
@Override