summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/qpid/broker/PagedQueue.h
blob: 7a28ac545a62729a4f5979b4596999ad713b7e49 (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
#ifndef QPID_BROKER_PAGEDQUEUE_H
#define QPID_BROKER_PAGEDQUEUE_H

/*
 *
 * 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.
 *
 */
#include "qpid/broker/Messages.h"
#include "qpid/broker/Message.h"
#include "qpid/framing/SequenceSet.h"
#include "qpid/sys/MemoryMappedFile.h"
#include <deque>
#include <list>
#include <map>

namespace qpid {
namespace broker {
class ProtocolRegistry;
/**
 *
 */
class PagedQueue : public Messages {
  public:
    PagedQueue(const std::string& name, const std::string& directory, uint maxLoaded, uint pageFactor, ProtocolRegistry& protocols);
    ~PagedQueue();
    size_t size();
    bool deleted(const QueueCursor&);
    void publish(const Message& added);
    Message* next(QueueCursor& cursor);
    Message* release(const QueueCursor& cursor);
    Message* find(const framing::SequenceNumber&, QueueCursor*);
    Message* find(const QueueCursor&);
    void foreach(Functor);
  private:
    class Page {
      public:
        Page(size_t size, size_t offset);
        void read();//decode messages from memory mapped file
        void write();//encode messages into memory mapped file
        bool isLoaded() const;
        bool empty() const;
        void deleted(qpid::framing::SequenceNumber);
        Message* release(qpid::framing::SequenceNumber);
        bool add(const Message&);
        Message* next(uint32_t version, QueueCursor&);
        Message* find(qpid::framing::SequenceNumber);
        void load(qpid::sys::MemoryMappedFile&,ProtocolRegistry&);
        void unload(qpid::sys::MemoryMappedFile&);
        void clear(qpid::sys::MemoryMappedFile&);
        size_t available() const;
      private:
        size_t size;
        size_t offset;

        char* region;//0 implies not mapped
        qpid::framing::SequenceSet contents;
        qpid::framing::SequenceSet acquired;
        std::deque<Message> messages;//decoded representation
        size_t used;//amount of data used to encode current set of messages held
    };

    qpid::sys::MemoryMappedFile file;
    std::string name;
    std::string path;
    const size_t pageSize;
    const uint maxLoaded;
    ProtocolRegistry& protocols;
    size_t offset;
    typedef std::map<qpid::framing::SequenceNumber, Page> Used;
    Used used;
    std::list<Page> free;
    uint loaded;
    uint32_t version;

    void addPages(size_t count);
    Page& newPage(qpid::framing::SequenceNumber);
    Used::iterator findPage(const QueueCursor& cursor);
    Used::iterator findPage(qpid::framing::SequenceNumber n, bool loadIfRequired);
    void load(Page&);
    void unload(Page&);
    bool deleted(qpid::framing::SequenceNumber);
};
}} // namespace qpid::broker

#endif  /*!QPID_BROKER_PAGEDQUEUE_H*/