~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to drizzled/transaction_services.h

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-10-02 14:17:48 UTC
  • mfrom: (1.1.1 upstream)
  • mto: (2.1.17 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20101002141748-m6vbfbfjhrw1153e
Tags: 2010.09.1802-1
* New upstream release.
* Removed pid-file argument hack.
* Updated GPL-2 address to be new address.
* Directly copy in drizzledump.1 since debian doesn't have sphinx 1.0 yet.
* Link to jquery from libjs-jquery. Add it as a depend.
* Add drizzled.8 symlink to the install files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#ifndef DRIZZLED_TRANSACTION_SERVICES_H
26
26
#define DRIZZLED_TRANSACTION_SERVICES_H
27
27
 
 
28
#include "drizzled/atomics.h"
 
29
#include "drizzled/message/transaction.pb.h"
 
30
 
28
31
namespace drizzled
29
32
{
30
33
 
38
41
 
39
42
class Session;
40
43
class NamedSavepoint;
41
 
 
 
44
class Field;
 
45
 
42
46
/**
43
47
 * This is a class which manages the XA transaction processing
44
48
 * in the server
46
50
class TransactionServices
47
51
{
48
52
public:
 
53
  static const size_t DEFAULT_RECORD_SIZE= 100;
 
54
  typedef uint64_t TransactionId;
49
55
  /**
50
56
   * Constructor
51
57
   */
52
 
  TransactionServices() {}
 
58
  TransactionServices()
 
59
  {
 
60
    /**
 
61
     * @todo set transaction ID to the last one from an applier...
 
62
     */
 
63
    current_transaction_id= 0;
 
64
  }
53
65
 
54
66
  /**
55
67
   * Singleton method
60
72
    static TransactionServices transaction_services;
61
73
    return transaction_services;
62
74
  }
 
75
 
 
76
  /**
 
77
   * Returns true if the transaction manager should construct
 
78
   * Transaction and Statement messages, false otherwise.
 
79
   */
 
80
  bool shouldConstructMessages();
 
81
  /**
 
82
   * Method which returns the active Transaction message
 
83
   * for the supplied Session.  If one is not found, a new Transaction
 
84
   * message is allocated, initialized, and returned. It is possible that
 
85
   * we may want to NOT increment the transaction id for a new Transaction
 
86
   * object (e.g., splitting up Transactions into smaller chunks). The
 
87
   * should_inc_trx_id flag controls if we do this.
 
88
   *
 
89
   * @param in_session The session processing the transaction
 
90
   * @param should_inc_trx_id If true, increments the transaction id for a new trx
 
91
   */
 
92
  message::Transaction *getActiveTransactionMessage(Session *in_session,
 
93
                                                    bool should_inc_trx_id= true);
 
94
  /** 
 
95
   * Method which attaches a transaction context
 
96
   * the supplied transaction based on the supplied Session's
 
97
   * transaction information.  This method also ensure the
 
98
   * transaction message is attached properly to the Session object
 
99
   *
 
100
   * @param in_transaction The transaction message to initialize
 
101
   * @param in_session The Session processing this transaction
 
102
   * @param should_inc_trx_id If true, increments the transaction id for a new trx
 
103
   */
 
104
  void initTransactionMessage(message::Transaction &in_transaction,
 
105
                              Session *in_session,
 
106
                              bool should_inc_trx_id);
 
107
  /** 
 
108
   * Helper method which finalizes data members for the 
 
109
   * supplied transaction's context.
 
110
   *
 
111
   * @param in_transaction The transaction message to finalize 
 
112
   * @param in_session The Session processing this transaction
 
113
   */
 
114
  void finalizeTransactionMessage(message::Transaction &in_transaction, Session *in_session);
 
115
  /**
 
116
   * Helper method which deletes transaction memory and
 
117
   * unsets Session's transaction and statement messages.
 
118
   */
 
119
  void cleanupTransactionMessage(message::Transaction *in_transaction,
 
120
                                 Session *in_session);
 
121
  /**
 
122
   * Helper method which initializes a Statement message
 
123
   *
 
124
   * @param statement The statement to initialize
 
125
   * @param in_type The type of the statement
 
126
   * @param in_session The session processing this statement
 
127
   */
 
128
  void initStatementMessage(message::Statement &statement,
 
129
                            message::Statement::Type in_type,
 
130
                            Session *in_session);
 
131
  /**
 
132
   * Finalizes a Statement message and sets the Session's statement
 
133
   * message to NULL.
 
134
   *
 
135
   * @param statement The statement to initialize
 
136
   * @param in_session The session processing this statement
 
137
   */
 
138
  void finalizeStatementMessage(message::Statement &statement,
 
139
                                Session *in_session);
 
140
  /** Helper method which returns an initialized Statement message for methods
 
141
   * doing insertion of data.
 
142
   *
 
143
   * @param[in] in_session Pointer to the Session doing the processing
 
144
   * @param[in] in_table Pointer to the Table object being inserted into
 
145
   * @param[out] next_segment_id The next Statement segment id to be used
 
146
   */
 
147
  message::Statement &getInsertStatement(Session *in_session,
 
148
                                         Table *in_table,
 
149
                                         uint32_t *next_segment_id);
 
150
 
 
151
  /**
 
152
   * Helper method which initializes the header message for
 
153
   * insert operations.
 
154
   *
 
155
   * @param[in,out] statement Statement message container to modify
 
156
   * @param[in] in_session Pointer to the Session doing the processing
 
157
   * @param[in] in_table Pointer to the Table being inserted into
 
158
   */
 
159
  void setInsertHeader(message::Statement &statement,
 
160
                       Session *in_session,
 
161
                       Table *in_table);
 
162
  /**
 
163
   * Helper method which returns an initialized Statement
 
164
   * message for methods doing updates of data.
 
165
   *
 
166
   * @param[in] in_session Pointer to the Session doing the processing
 
167
   * @param[in] in_table Pointer to the Table object being updated
 
168
   * @param[in] old_record Pointer to the old data in the record
 
169
   * @param[in] new_record Pointer to the new data in the record
 
170
   * @param[out] next_segment_id The next Statement segment id to be used
 
171
   */
 
172
  message::Statement &getUpdateStatement(Session *in_session,
 
173
                                         Table *in_table,
 
174
                                         const unsigned char *old_record, 
 
175
                                         const unsigned char *new_record,
 
176
                                         uint32_t *next_segment_id);
 
177
  /**
 
178
   * Helper method which initializes the header message for
 
179
   * update operations.
 
180
   *
 
181
   * @param[in,out] statement Statement message container to modify
 
182
   * @param[in] in_session Pointer to the Session doing the processing
 
183
   * @param[in] in_table Pointer to the Table being updated
 
184
   * @param[in] old_record Pointer to the old data in the record
 
185
   * @param[in] new_record Pointer to the new data in the record
 
186
   */
 
187
  void setUpdateHeader(message::Statement &statement,
 
188
                       Session *in_session,
 
189
                       Table *in_table,
 
190
                       const unsigned char *old_record, 
 
191
                       const unsigned char *new_record);
 
192
  /**
 
193
   * Helper method which returns an initialized Statement
 
194
   * message for methods doing deletion of data.
 
195
   *
 
196
   * @param[in] in_session Pointer to the Session doing the processing
 
197
   * @param[in] in_table Pointer to the Table object being deleted from
 
198
   * @param[out] next_segment_id The next Statement segment id to be used
 
199
   */
 
200
  message::Statement &getDeleteStatement(Session *in_session,
 
201
                                         Table *in_table,
 
202
                                         uint32_t *next_segment_id);
 
203
 
 
204
  /**
 
205
   * Helper method which initializes the header message for
 
206
   * insert operations.
 
207
   *
 
208
   * @param[in,out] statement Statement message container to modify
 
209
   * @param[in] in_session Pointer to the Session doing the processing
 
210
   * @param[in] in_table Pointer to the Table being deleted from
 
211
   */
 
212
  void setDeleteHeader(message::Statement &statement,
 
213
                       Session *in_session,
 
214
                       Table *in_table);
 
215
  /** 
 
216
   * Commits a normal transaction (see above) and pushes the transaction
 
217
   * message out to the replicators.
 
218
   *
 
219
   * @param in_session Pointer to the Session committing the transaction
 
220
   */
 
221
  int commitTransactionMessage(Session *in_session);
 
222
  /** 
 
223
   * Marks the current active transaction message as being rolled back and
 
224
   * pushes the transaction message out to replicators.
 
225
   *
 
226
   * @param in_session Pointer to the Session committing the transaction
 
227
   */
 
228
  void rollbackTransactionMessage(Session *in_session);
 
229
  /**
 
230
   * Creates a new InsertRecord GPB message and pushes it to
 
231
   * replicators.
 
232
   *
 
233
   * @param in_session Pointer to the Session which has inserted a record
 
234
   * @param in_table Pointer to the Table containing insert information
 
235
   *
 
236
   * Grr, returning "true" here on error because of the cursor
 
237
   * reversed bool return crap...fix that.
 
238
   */
 
239
  bool insertRecord(Session *in_session, Table *in_table);
 
240
  /**
 
241
   * Creates a new UpdateRecord GPB message and pushes it to
 
242
   * replicators.
 
243
   *
 
244
   * @param in_session Pointer to the Session which has updated a record
 
245
   * @param in_table Pointer to the Table containing update information
 
246
   * @param old_record Pointer to the raw bytes representing the old record/row
 
247
   * @param new_record Pointer to the raw bytes representing the new record/row 
 
248
   */
 
249
  void updateRecord(Session *in_session, 
 
250
                    Table *in_table, 
 
251
                    const unsigned char *old_record, 
 
252
                    const unsigned char *new_record);
 
253
  /**
 
254
   * Creates a new DeleteRecord GPB message and pushes it to
 
255
   * replicators.
 
256
   *
 
257
   * @param in_session Pointer to the Session which has deleted a record
 
258
   * @param in_table Pointer to the Table containing delete information
 
259
   * @param use_update_record If true, uses the values from the update row instead
 
260
   */
 
261
  void deleteRecord(Session *in_session, Table *in_table, bool use_update_record= false);
 
262
  /**
 
263
   * Creates a CreateSchema Statement GPB message and adds it
 
264
   * to the Session's active Transaction GPB message for pushing
 
265
   * out to the replicator streams.
 
266
   *
 
267
   * @param[in] in_session Pointer to the Session which issued the statement
 
268
   * @param[in] schema message::Schema message describing new schema
 
269
   */
 
270
  void createSchema(Session *in_session, const message::Schema &schema);
 
271
  /**
 
272
   * Creates a DropSchema Statement GPB message and adds it
 
273
   * to the Session's active Transaction GPB message for pushing
 
274
   * out to the replicator streams.
 
275
   *
 
276
   * @param[in] in_session Pointer to the Session which issued the statement
 
277
   * @param[in] schema_name message::Schema message describing new schema
 
278
   */
 
279
  void dropSchema(Session *in_session, const std::string &schema_name);
 
280
  /**
 
281
   * Creates a CreateTable Statement GPB message and adds it
 
282
   * to the Session's active Transaction GPB message for pushing
 
283
   * out to the replicator streams.
 
284
   *
 
285
   * @param[in] in_session Pointer to the Session which issued the statement
 
286
   * @param[in] table message::Table message describing new schema
 
287
   */
 
288
  void createTable(Session *in_session, const message::Table &table);
 
289
  /**
 
290
   * Creates a DropTable Statement GPB message and adds it
 
291
   * to the Session's active Transaction GPB message for pushing
 
292
   * out to the replicator streams.
 
293
   *
 
294
   * @param[in] in_session Pointer to the Session which issued the statement
 
295
   * @param[in] schema_name The schema of the table being dropped
 
296
   * @param[in] table_name The table name of the table being dropped
 
297
   * @param[in] if_exists Did the user specify an IF EXISTS clause?
 
298
   */
 
299
  void dropTable(Session *in_session,
 
300
                     const std::string &schema_name,
 
301
                     const std::string &table_name,
 
302
                     bool if_exists);
 
303
  /**
 
304
   * Creates a TruncateTable Statement GPB message and adds it
 
305
   * to the Session's active Transaction GPB message for pushing
 
306
   * out to the replicator streams.
 
307
   *
 
308
   * @param[in] in_session Pointer to the Session which issued the statement
 
309
   * @param[in] in_table The Table being truncated
 
310
   */
 
311
  void truncateTable(Session *in_session, Table *in_table);
 
312
  /**
 
313
   * Creates a new RawSql GPB message and pushes it to 
 
314
   * replicators.
 
315
   *
 
316
   * @TODO With a real data dictionary, this really shouldn't
 
317
   * be needed.  CREATE TABLE would map to insertRecord call
 
318
   * on the I_S, etc.  Not sure what to do with administrative
 
319
   * commands like CHECK TABLE, though..
 
320
   *
 
321
   * @param in_session Pointer to the Session which issued the statement
 
322
   * @param query Query string
 
323
   */
 
324
  void rawStatement(Session *in_session, const std::string &query);
63
325
  /* transactions: interface to plugin::StorageEngine functions */
64
 
  int ha_commit_one_phase(Session *session, bool all);
65
 
  int ha_rollback_trans(Session *session, bool all);
 
326
  int commitPhaseOne(Session *session, bool all);
 
327
  int rollbackTransaction(Session *session, bool all);
66
328
 
67
329
  /* transactions: these functions never call plugin::StorageEngine functions directly */
68
 
  int ha_commit_trans(Session *session, bool all);
69
 
  int ha_autocommit_or_rollback(Session *session, int error);
 
330
  int commitTransaction(Session *session, bool all);
 
331
  int autocommitOrRollback(Session *session, int error);
70
332
 
71
333
  /* savepoints */
72
 
  int ha_rollback_to_savepoint(Session *session, NamedSavepoint &sv);
73
 
  int ha_savepoint(Session *session, NamedSavepoint &sv);
74
 
  int ha_release_savepoint(Session *session, NamedSavepoint &sv);
75
 
  bool mysql_xa_recover(Session *session);
 
334
  int rollbackToSavepoint(Session *session, NamedSavepoint &sv);
 
335
  int setSavepoint(Session *session, NamedSavepoint &sv);
 
336
  int releaseSavepoint(Session *session, NamedSavepoint &sv);
76
337
 
77
338
  /**
78
339
   * Marks a storage engine as participating in a statement
88
349
   * per statement, and therefore should not need to be idempotent.
89
350
   * Put in assert()s to test this.
90
351
   *
91
 
   * @param[in] Session pointer
92
 
   * @param[in] Descriptor for the resource which will be participating
93
 
   * @param[in] Pointer to the TransactionalStorageEngine resource
 
352
   * @param[in] session Session pointer
 
353
   * @param[in] monitored Descriptor for the resource which will be participating
 
354
   * @param[in] engine Pointer to the TransactionalStorageEngine resource
94
355
   */
95
356
  void registerResourceForStatement(Session *session,
96
357
                                    plugin::MonitoredInTransaction *monitored,
110
371
   * per statement, and therefore should not need to be idempotent.
111
372
   * Put in assert()s to test this.
112
373
   *
113
 
   * @param[in] Session pointer
114
 
   * @param[in] Descriptor for the resource which will be participating
115
 
   * @param[in] Pointer to the TransactionalStorageEngine resource
116
 
   * @param[in] Pointer to the XaResourceManager resource manager
 
374
   * @param[in] session Session pointer
 
375
   * @param[in] monitored Descriptor for the resource which will be participating
 
376
   * @param[in] engine Pointer to the TransactionalStorageEngine resource
 
377
   * @param[in] resource_manager Pointer to the XaResourceManager resource manager
117
378
   */
118
379
  void registerResourceForStatement(Session *session,
119
380
                                    plugin::MonitoredInTransaction *monitored,
151
412
                                      plugin::MonitoredInTransaction *monitored,
152
413
                                      plugin::TransactionalStorageEngine *engine,
153
414
                                      plugin::XaResourceManager *resource_manager);
 
415
  TransactionId getNextTransactionId()
 
416
  {
 
417
    return current_transaction_id.increment();
 
418
  }
 
419
  TransactionId getCurrentTransactionId()
 
420
  {
 
421
    return current_transaction_id;
 
422
  }
 
423
  /**
 
424
   * DEBUG ONLY.  See plugin::TransactionLog::truncate()
 
425
   */
 
426
  void resetTransactionId()
 
427
  {
 
428
    current_transaction_id= 0;
 
429
  }
 
430
private:
 
431
  atomic<TransactionId> current_transaction_id;
 
432
 
 
433
  /**
 
434
   * Checks if a field has been updated 
 
435
   *
 
436
   * @param current_field Pointer to the field to check if it is updated 
 
437
   * @in_table Pointer to the Table containing update information
 
438
   * @param old_record Pointer to the raw bytes representing the old record/row
 
439
   * @param new_record Pointer to the raw bytes representing the new record/row
 
440
   */
 
441
  bool isFieldUpdated(Field *current_field,
 
442
                      Table *in_table,
 
443
                      const unsigned char *old_record,
 
444
                      const unsigned char *new_record);
154
445
};
155
446
 
156
447
} /* namespace drizzled */