summaryrefslogtreecommitdiff
path: root/qpid/java/broker/src/main/java/org/apache/qpid/server/ack/TxAck.java
blob: 95de0dc8c31b9925cce8fca74cec5a54aad6d702 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.qpid.server.ack;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;

import org.apache.qpid.AMQException;
import org.apache.qpid.server.store.StoreContext;
import org.apache.qpid.server.txn.TxnOp;
import org.apache.qpid.server.queue.QueueEntry;

/**
 * A TxnOp implementation for handling accumulated acks
 */
public class TxAck implements TxnOp
{
    private final UnacknowledgedMessageMap _map;
    private final Map<Long, QueueEntry> _unacked = new HashMap<Long,QueueEntry>();
    private List<Long> _individual;
    private long _deliveryTag;
    private boolean _multiple;

    public TxAck(UnacknowledgedMessageMap map)
    {
        _map = map;
    }

    public void update(long deliveryTag, boolean multiple)
    {
        _unacked.clear();
        if (!multiple)
        {
            if(_individual == null)
            {
                _individual = new ArrayList<Long>();
            }
            //have acked a single message that is not part of
            //the previously acked region so record
            //individually
            _individual.add(deliveryTag);//_multiple && !multiple
        }
        else if (deliveryTag > _deliveryTag)
        {
            //have simply moved the last acked message on a
            //bit
            _deliveryTag = deliveryTag;
            _multiple = true;
        }
    }

    public void consolidate()
    {
        if(_unacked.isEmpty())
        {
            //lookup all the unacked messages that have been acked in this transaction
            if (_multiple)
            {
                //get all the unacked messages for the accumulated
                //multiple acks
                _map.collect(_deliveryTag, true, _unacked);
            }
            if(_individual != null)
            {
                //get any unacked messages for individual acks outside the
                //range covered by multiple acks
                for (long tag : _individual)
                {
                    if(_deliveryTag < tag)
                    {
                        _map.collect(tag, false, _unacked);
                    }
                }
            }
        }
    }

    public boolean checkPersistent() throws AMQException
    {
        consolidate();
        //if any of the messages in unacked are persistent the txn
        //buffer must be marked as persistent:
        for (QueueEntry msg : _unacked.values())
        {
            if (msg.isPersistent())
            {
                return true;
            }
        }
        return false;
    }

    public void prepare(StoreContext storeContext) throws AMQException
    {
        //make persistent changes, i.e. dequeue and decrementReference
        for (QueueEntry msg : _unacked.values())
        {
            //Message has been ack so dequeueAndDelete it.
            // If the message is persistent and this is the last QueueEntry that uses it then the data will be removed
            // from the transaciton log
            msg.dequeueAndDelete(storeContext);
        }
    }

    public void undoPrepare()
    {
        //As this is transaction the above dequeueAndDelete will only request the message be dequeue from the
        // transactionLog. Only when the transaction succesfully completes will it perform any
        // update of the internal transactionLog reference counting and any resulting message data deletion.
        // The success or failure of the data deletion is not important to this transaction only that the ack has been
        // successfully recorded.
    }

    public void commit(StoreContext storeContext)
    {
        //remove the unacked messages from the channels map
        _map.remove(_unacked);        
    }

    public void rollback(StoreContext storeContext)
    {
    }
}