~drizzle-trunk/drizzle/development

1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
5
 *  Copyright (C) 2010 Jay Pipes <jaypipes@gmail.com>
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
6
 *
7
 *  Authors:
8
 *
1405.4.1 by Jay Pipes
Adds a vector of write buffers to the transaction log applier
9
 *    Jay Pipes <jaypipes@gmail.com>
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
10
 *
11
 *  This program is free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  This program is distributed in the hope that it will be useful,
17
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 *  GNU General Public License for more details.
20
 *
21
 *  You should have received a copy of the GNU General Public License
22
 *  along with this program; if not, write to the Free Software
23
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24
 */
25
26
/**
27
 * @file
28
 *
29
 * Defines the implementation of the transaction applier plugin
30
 * for the transaction log.
31
 *
32
 * @see drizzled/plugin/transaction_replicator.h
33
 * @see drizzled/plugin/transaction_applier.h
34
 *
35
 * @details
36
 *
37
 * The TransactionLogApplier::apply() method constructs the entry
38
 * in the transaction log from the supplied Transaction message and
39
 * asks its associated TransactionLog object to write this entry.
40
 *
41
 * Upon a successful write, the applier adds some information about
42
 * the written transaction to the transaction log index.
43
 */
44
45
#include "config.h"
1405.4.1 by Jay Pipes
Adds a vector of write buffers to the transaction log applier
46
#include "write_buffer.h"
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
47
#include "transaction_log.h"
48
#include "transaction_log_applier.h"
49
#include "transaction_log_index.h"
50
1405.4.1 by Jay Pipes
Adds a vector of write buffers to the transaction log applier
51
#include <vector>
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
52
53
#include <drizzled/message/transaction.pb.h>
1405.4.1 by Jay Pipes
Adds a vector of write buffers to the transaction log applier
54
#include <drizzled/util/functors.h>
55
#include <drizzled/session.h>
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
56
57
using namespace std;
58
using namespace drizzled;
59
60
TransactionLogApplier *transaction_log_applier= NULL; /* The singleton transaction log applier */
61
62
TransactionLogApplier::TransactionLogApplier(const string name_arg,
1452.1.2 by mordred
Made the TransactionLogApplier own the memory of the TransactionLog.
63
                                             TransactionLog *in_transaction_log,
1455.5.1 by Jay Pipes
TransactionLogApplier now manages (frees) memory for TransactionLog and TransactionLogIndex
64
                                             TransactionLogIndex *in_transaction_log_index,
1405.4.1 by Jay Pipes
Adds a vector of write buffers to the transaction log applier
65
                                             uint32_t in_num_write_buffers) :
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
66
  plugin::TransactionApplier(name_arg),
1455.5.1 by Jay Pipes
TransactionLogApplier now manages (frees) memory for TransactionLog and TransactionLogIndex
67
  transaction_log(in_transaction_log),
68
  transaction_log_index(in_transaction_log_index),
1405.4.1 by Jay Pipes
Adds a vector of write buffers to the transaction log applier
69
  num_write_buffers(in_num_write_buffers),
70
  write_buffers()
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
71
{
1405.4.1 by Jay Pipes
Adds a vector of write buffers to the transaction log applier
72
  /* 
73
   * Create each of the buffers we need for undo log entries 
74
   */
75
  write_buffers.reserve(num_write_buffers);
76
  for (size_t x= 0; x < num_write_buffers; ++x)
77
  {
78
    write_buffers.push_back(new WriteBuffer());
79
  }
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
80
}
81
82
TransactionLogApplier::~TransactionLogApplier()
83
{
1405.4.1 by Jay Pipes
Adds a vector of write buffers to the transaction log applier
84
  for_each(write_buffers.begin(),
85
           write_buffers.end(),
86
           DeletePtr());
87
  write_buffers.clear();
1455.5.1 by Jay Pipes
TransactionLogApplier now manages (frees) memory for TransactionLog and TransactionLogIndex
88
  delete transaction_log;
89
  delete transaction_log_index;
1405.4.1 by Jay Pipes
Adds a vector of write buffers to the transaction log applier
90
}
91
92
WriteBuffer *TransactionLogApplier::getWriteBuffer(const Session &session)
93
{
94
  return write_buffers[session.getSessionId() % num_write_buffers];
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
95
}
96
1405.3.1 by Jay Pipes
Adapts the plugin::TransactionReplicator and plugin::TransactionApplier
97
plugin::ReplicationReturnCode
1405.3.7 by Jay Pipes
Change to non-const Session reference in replication API.
98
TransactionLogApplier::apply(Session &in_session,
1405.3.3 by Jay Pipes
Adds Session reference to replication API
99
                             const message::Transaction &to_apply)
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
100
{
1405.4.1 by Jay Pipes
Adds a vector of write buffers to the transaction log applier
101
  size_t entry_size= TransactionLog::getLogEntrySize(to_apply);
102
  WriteBuffer *write_buffer= getWriteBuffer(in_session);
103
104
  uint32_t checksum;
105
106
  write_buffer->lock();
107
  write_buffer->resize(entry_size);
108
  uint8_t *bytes= write_buffer->getRawBytes();
1455.5.1 by Jay Pipes
TransactionLogApplier now manages (frees) memory for TransactionLog and TransactionLogIndex
109
  bytes= transaction_log->packTransactionIntoLogEntry(to_apply,
1405.4.1 by Jay Pipes
Adds a vector of write buffers to the transaction log applier
110
                                                     bytes,
111
                                                     &checksum);
112
1455.5.1 by Jay Pipes
TransactionLogApplier now manages (frees) memory for TransactionLog and TransactionLogIndex
113
  off_t written_to= transaction_log->writeEntry(bytes, entry_size);
1405.4.1 by Jay Pipes
Adds a vector of write buffers to the transaction log applier
114
  write_buffer->unlock();
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
115
116
  /* Add an entry to the index describing what was just applied */
117
  transaction_log_index->addEntry(TransactionLogEntry(ReplicationServices::TRANSACTION,
118
                                                      written_to,
1405.4.1 by Jay Pipes
Adds a vector of write buffers to the transaction log applier
119
                                                      entry_size),
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
120
                                  to_apply,
121
                                  checksum);
1405.3.1 by Jay Pipes
Adapts the plugin::TransactionReplicator and plugin::TransactionApplier
122
  return plugin::SUCCESS;
1273.22.1 by Jay Pipes
Completes the blueprint for refactoring applier out of log descriptor.
123
}