/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * 
 **/
package org.codehaus.activemq.store.vm;

import org.codehaus.activemq.message.ConsumerInfo;
import org.codehaus.activemq.message.MessageAck;
import org.codehaus.activemq.service.MessageContainer;
import org.codehaus.activemq.service.MessageIdentity;
import org.codehaus.activemq.service.SubscriberEntry;
import org.codehaus.activemq.service.Subscription;
import org.codehaus.activemq.store.TopicMessageStore;

import javax.jms.JMSException;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @version $Revision: 1.1 $
 */
public class VMTopicMessageStore extends VMMessageStore implements TopicMessageStore {
    private static final Integer ONE = new Integer(1);

    private Map ackDatabase;
    private Map messageCounts;
    private Map subscriberDatabase;

    public VMTopicMessageStore() {
        this(new LinkedHashMap(), makeMap(), makeMap(), makeMap());
    }

    public VMTopicMessageStore(LinkedHashMap messageTable, Map subscriberDatabase, Map ackDatabase, Map messageCounts) {
        super(messageTable);
        this.subscriberDatabase = subscriberDatabase;
        this.ackDatabase = ackDatabase;
        this.messageCounts = messageCounts;
    }

    public void setMessageContainer(MessageContainer container) {
    }

    public synchronized void incrementMessageCount(MessageIdentity messageId) throws JMSException {
        Integer number = (Integer) messageCounts.get(messageId);
        if (number == null) {
            number = ONE;
        }
        else {
            number = new Integer(number.intValue() + 1);
        }
        messageCounts.put(messageId, number);
    }

    public synchronized void decrementMessageCountAndMaybeDelete(MessageIdentity messageIdentity, MessageAck ack) throws JMSException {
        Integer number = (Integer) messageCounts.get(messageIdentity);
        if (number == null || number.intValue() <= 1) {
            removeMessage(messageIdentity, ack);
            if (number != null) {
                messageCounts.remove(messageIdentity);
            }
        }
        else {
            messageCounts.put(messageIdentity, new Integer(number.intValue() - 1));
            number = ONE;
        }
    }

    public void setLastAcknowledgedMessageIdentity(Subscription subscription, MessageIdentity messageIdentity) throws JMSException {
        String consumerId = subscription.getConsumerId();
        ackDatabase.put(consumerId, messageIdentity);
    }

    public void recoverSubscription(Subscription subscription, MessageIdentity lastDispatchedMessage) {
    }

    public MessageIdentity getLastestMessageIdentity() throws JMSException {
        return null;  /** TODO */
    }

    public SubscriberEntry getSubscriberEntry(ConsumerInfo info) throws JMSException {
        Object key = info.getConsumerKey();
        return (SubscriberEntry) subscriberDatabase.get(key);
    }

    public void setSubscriberEntry(ConsumerInfo info, SubscriberEntry subscriberEntry) throws JMSException {
        subscriberDatabase.put(info.getConsumerKey(), subscriberEntry);
    }

    public void stop() throws JMSException {
        ackDatabase.clear();
        messageCounts.clear();
        super.stop();
    }

    protected static Map makeMap() {
        return Collections.synchronizedMap(new HashMap());
    }
}
