~drizzle-trunk/drizzle/development

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2008-2009 Sun Microsystems
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/**
 * @file
 *
 * Defines the API of the default command log.
 *
 * @see drizzled/plugin/replicator.h
 * @see drizzled/plugin/applier.h
 *
 * @details
 *
 * The CommandLog applies events it receives from the TransactionServices
 * server component to a simple log file on disk.
 * 
 * Events are received in no guaranteed order and the command log
 * is in charge of writing these events to the log as they are received.
 */

#ifndef DRIZZLE_PLUGIN_command_log_H
#define DRIZZLE_PLUGIN_command_log_H

#include <drizzled/server_includes.h>
#include <drizzled/atomics.h>
#include <drizzled/plugin/replicator.h>
#include <drizzled/plugin/applier.h>

#include <vector>
#include <string>

class CommandLog: public drizzled::plugin::Applier 
{
public:
  enum status
  {
    CRASHED= 0,
    OFFLINE, /* Default state, uninited. */
    ONLINE,
    WRITING
  };
private:
  int log_file; /**< Handle for our log file */
  enum status state; /**< The state the log is in */
  drizzled::atomic<bool> is_enabled; /**< Internal toggle. Atomic to support online toggling of command log... */
  drizzled::atomic<bool> is_active; /**< Internal toggle. If true, log was initialized properly... */
  const char *log_file_path; /**< Full path to the log file */
  drizzled::atomic<off_t> log_offset; /**< Offset in log file where log will write next command */
public:
  CommandLog(const char *in_log_file_path);

  /** Destructor */
  ~CommandLog();

  /**
   * Applies a Command to the serial log
   *
   * @note
   *
   * It is important to note that memory allocation for the 
   * supplied pointer is not guaranteed after the completion 
   * of this function -- meaning the caller can dispose of the
   * supplied message.  Therefore, appliers which are
   * implementing an asynchronous replication system must copy
   * the supplied message to their own controlled memory storage
   * area.
   *
   * @param Command message to be replicated
   */
  void apply(drizzled::message::Command *to_apply);
  
  /** 
   * Returns whether the command log is active.
   */
  bool isActive();

  /**
   * Disables the plugin.
   * Disabled just means that the user has done an online set @command_log_enable= false
   */
  inline void disable()
  {
    is_enabled= false;
  }

  /**
   * Enables the plugin.  Enabling is a bit different from isActive().
   * Enabled just means that the user has done an online set global command_log_enable= true
   * or has manually started up the server with --command-log-enable
   */
  inline void enable()
  {
    is_enabled= true;
  }

  /**
   * Returns the state that the log is in
   */
  inline enum status getState()
  {
    return state;
  }

  /**
   * Truncates the existing log file
   *
   * @note 
   *
   * This is only called currently during debugging and testing of the 
   * command log...when the global command_log_truncate variable is 
   * set to anything other than false, this is called.
   */
  void truncate();
};

#endif /* DRIZZLE_PLUGIN_command_log_H */