~drizzle-trunk/drizzle/development

1780.3.1 by David Shrewsbury
Add code to manage multi-GPB-message transactions and output them atomically.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (c) 2010 David Shrewsbury <shrewsbury.dave@gmail.com>
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
20
/**
21
 * @file
22
 * Declaration of a class used to manage Transaction messages so that they
23
 * can be retrieved as a collection.
24
 */
25
26
#ifndef DRIZZLED_MESSAGE_TRANSACTION_MANAGER_H
27
#define DRIZZLED_MESSAGE_TRANSACTION_MANAGER_H
28
29
#include "drizzled/message/transaction.pb.h"
30
#include <string>
31
#include <vector>
1780.3.3 by David Shrewsbury
Replace std::map with boost::unordered_map
32
#include <boost/unordered_map.hpp>
1780.3.1 by David Shrewsbury
Add code to manage multi-GPB-message transactions and output them atomically.
33
34
typedef std::vector<std::string> MsgBufferType;
35
36
namespace drizzled
37
{
38
namespace message
39
{
40
41
/**
42
 * Simple (example) Transaction message buffer and content manager.
43
 *
44
 * @details
45
 * This class groups Transaction messages together by transaction ID and
46
 * buffers them together in memory. Obviously, this could eat up a lot of
47
 * memory if you have very large transactions. A more robust implementation
48
 * would buffer larger transactions to disk rather than memory.
49
 *
50
 * @note
51
 * Once you have a complete transaction and have processed it, you should
52
 * call remove() to remove the cache contents for that transaction from memory.
53
 */
54
class TransactionManager
55
{
56
public:
57
  /**
58
   * Store the given Transaction message in a buffer.
59
   *
60
   * @param[in] transaction Pointer to the Transaction message to store.
61
   *
62
   * @retval true Success
63
   * @retval false Failure
64
   */
65
  bool store(const drizzled::message::Transaction &transaction);
66
67
  /**
68
   * Clear the buffer contents for a given transaction ID.
69
   *
70
   * @param[in] trx_id The transaction ID for the transaction to remove
71
   *
72
   * @retval true Success
73
   * @retval false Failure
74
   */
75
  bool remove(uint64_t trx_id);
76
77
  /**
78
   * Check to see if any Transaction messages exist for a given transaction.
79
   *
80
   * @param[in] trx_id The transaction ID to check for.
81
   *
82
   * @retval true Transaction messages exist
83
   * @retval false No Transaction messages found
84
   */
85
  bool contains(uint64_t trx_id);
86
87
  /**
88
   * Return number of cached elements for the given transaction ID.
89
   *
90
   * @param[in] trx_id Transaction ID
91
   *
92
   * @returns The number of cached elements associated with trx_id.
93
   */
94
  uint32_t getTransactionBufferSize(uint64_t trx_id);
95
96
  /**
97
   * Retrieve a Transaction message from the managed cache.
98
   *
99
   * Caller must supply a Transaction message to populate. The Transaction
100
   * message to retrieve is indexed by a combination of transaction ID and
101
   * position.
102
   *
103
   * @param[out] transaction Transaction message to populate
104
   * @param[in] trx_id Transaction ID
105
   * @param[in] position Index into the buffer associated with trx_id
106
   *
107
   * @retval true Success
108
   * @retval false Failure
109
   */
110
  bool getTransactionMessage(drizzled::message::Transaction &transaction,
111
                             uint64_t trx_id,
112
                             uint32_t position);
113
114
private:
115
  /**
116
   * Our message buffer cache, mapped by the transaction ID.
117
   *
118
   * We organize Transactions messages by grouping them by transaction ID,
119
   * then storing the messages in std::vectors in std::string format. The
120
   * string format is convenient because it can be easily copied around
121
   * (GPB messages do not provide deep copying).
122
   */
1780.3.3 by David Shrewsbury
Replace std::map with boost::unordered_map
123
  boost::unordered_map<uint64_t,MsgBufferType> cache;
1780.3.1 by David Shrewsbury
Add code to manage multi-GPB-message transactions and output them atomically.
124
};
125
126
} /* namespace message */
127
} /* namespace drizzled */
128
129
#endif /* DRIZZLED_MESSAGE_TRANSACTION_MANAGER_H */