1 /*------------------------------------------------------------------------------
  2 Name:      SQLiteQueuePlugin.h
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 ------------------------------------------------------------------------------*/
  6 #ifndef _UTIL_QUEUE_SQLITEQUEUE_H
  7 #define _UTIL_QUEUE_SQLITEQUEUE_H
  8 
  9 #include <util/xmlBlasterDef.h>
 10 #include <util/ReferenceHolder.h>
 11 #include <util/queue/I_Queue.h>
 12 #include <util/queue/MsgQueueEntry.h>
 13 #include <util/qos/ConnectQosFactory.h>
 14 #include <util/qos/StatusQosFactory.h>
 15 #include <util/qos/MsgQosFactory.h>
 16 #include <util/key/MsgKeyFactory.h>
 17 #include <util/thread/ThreadImpl.h>
 18 #include <util/I_Log.h>
 19 
 20 struct I_QueueStruct;
 21 struct ExceptionStruct;
 22 
 23 namespace org { namespace xmlBlaster { namespace util { namespace queue {
 24 
 25 /**
 26  * Implements a persistent queue using SQLite as a base. 
 27  *
 28  * This class wraps the ANSI C based persistent queue implementation 
 29  * <code>xmlBlaster/src/c/util/queue/SQLiteQueue.c</code>.
 30  *
 31  * @see <a href="http://www.sqlite.org">The embedded SQLite SQL database</a>
 32  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/client.c.queue.html">The client.c.queue requirement</a>
 33  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/client.cpp.queue.html">The client.cpp.queue requirement</a>
 34  * @author <a href="mailto:xmlBlaster@marcelruff.info">Marcel Ruff</a>
 35  */
 36 class Dll_Export SQLiteQueuePlugin : public I_Queue
 37 {
 38 private:
 39    SQLiteQueuePlugin(const SQLiteQueuePlugin& queue);
 40    SQLiteQueuePlugin& operator =(const SQLiteQueuePlugin& queue);
 41 
 42 protected:
 43    std::string ME;
 44    org::xmlBlaster::util::Global& global_;
 45    org::xmlBlaster::util::I_Log& log_;
 46    org::xmlBlaster::util::qos::storage::QueuePropertyBase property_;
 47    struct ::I_QueueStruct *queueP_; // The C based xmlBlaster SQLite queue implementation
 48    mutable org::xmlBlaster::util::qos::ConnectQosFactory connectQosFactory_;
 49    mutable org::xmlBlaster::util::qos::StatusQosFactory statusQosFactory_;
 50    mutable org::xmlBlaster::util::key::MsgKeyFactory msgKeyFactory_;
 51    mutable org::xmlBlaster::util::qos::MsgQosFactory msgQosFactory_;
 52    org::xmlBlaster::util::thread::Mutex accessMutex_;
 53 
 54 public:
 55    SQLiteQueuePlugin(org::xmlBlaster::util::Global& global, const org::xmlBlaster::util::qos::storage::ClientQueueProperty& property);
 56    
 57    /**
 58     * Shutdown the queue, keep existing entries. 
 59     */
 60    virtual ~SQLiteQueuePlugin();
 61     
 62    /**
 63     * Access logging framework. 
 64     */
 65    org::xmlBlaster::util::I_Log& getLog() const { return log_; }
 66 
 67    /**
 68     * puts a new entry into the queue. 
 69     * Note that this method takes the entry pointed to by the argument 
 70     * and puts a reference to it into the queue. This means that you can not destroy the entry before the
 71     * reference to it has been removed from the queue (which normally happens on a remove or when destroying
 72     * the queue.
 73     */
 74    void put(const MsgQueueEntry &entry);
 75 
 76    /**
 77     * Returns the entries with the highest priority in the queue. If 'maxNumOfEntries' is positive,
 78     * this is the maximum number of entries to return. If maxNumOfBytes is positive, only the entries
 79     * which fit into the range specified are returned. If there are no such entries, an empty std::vector is
 80     * returned.
 81     */
 82    const std::vector<EntryType> peekWithSamePriority(long maxNumOfEntries=-1, long maxNumOfBytes=-1) const;
 83 
 84    /**
 85     * Deletes the entries specified in the std::vector in the argument list. If this std::vector is empty or if
 86     * the queue is empty, zero (0) is returned, otherwise it returns the number of entries really deleted.
 87     */
 88    long randomRemove(const std::vector<EntryType>::const_iterator &start, const std::vector<EntryType>::const_iterator &end);
 89 
 90    /**
 91     * Access the current number of entries. 
 92     * @return The number of entries in the queue
 93     */                                  
 94    long getNumOfEntries() const;
 95 
 96    /**
 97     * Access the configured maximum number of elements for this queue. 
 98     * @return The maximum number of elements in the queue
 99     */
100    long getMaxNumOfEntries() const;
101 
102    /**
103     * Returns the amount of bytes currently in the queue. 
104     * If the implementation of this interface is not able to return the correct
105     * number of entries (for example if the implementation must make a remote
106     * call to a DB which is temporarly not available) it will return -1.
107     * @return The amount of bytes currently in the queue, returns -1 on error
108     */
109    int64_t getNumOfBytes() const;
110 
111    /**
112     * Access the configured capacity (maximum bytes) for this queue. 
113     * @return The maximum capacity for the queue in bytes
114     */
115    int64_t getMaxNumOfBytes() const;
116 
117    /**
118     * Clears (removes all entries) this queue
119     */
120    void clear();
121 
122    /**
123     * returns true if the queue is empty, false otherwise
124     */                                  
125    bool empty() const;
126 
127    /**
128     * Converts the C ExceptionStruct into our XmlBlasterException class. 
129     */
130    org::xmlBlaster::util::XmlBlasterException convertFromQueueException(const ::ExceptionStruct *ex) const;
131 
132    /**
133     * Parse the embedded type information. 
134     * @param embeddedType The input, for example "MSG_RAW|publish"
135     * @param type Output: "MSG_RAW" (the SOCKET serialization format)
136     * @param methodName Output: "publish" (see MethodName.cpp)
137     */
138    static void parseEmbeddedType(const std::string& embeddedType, std::string &type, std::string &methodName);
139 
140    static std::string usage();
141 
142    /**
143     * Get the name of the plugin. 
144     * @return "SQLite"
145     * @enforcedBy I_Plugin
146     */
147    std::string getType() { static std::string type = "SQLite"; return type; }
148 
149    /**
150     * Get the version of the plugin. 
151     * @return "1.0"
152     * @enforcedBy I_Plugin
153     */
154    std::string getVersion() { static std::string version = "1.0"; return version; }
155 
156    void destroy();
157 };
158 
159 }}}} // namespace
160 
161 #endif


syntax highlighted by Code2HTML, v. 0.9.1