~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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2009 Sun Microsystems, Inc.
 *
 *  Authors:
 *
 *  Jay Pipes <joinfu@sun.com>
 *  Joseph Daly <skinny.moey@gmail.com>
 *
 *  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
 *
 * Implements the DATA_DICTIONARY views which allows querying the
 * state of the transaction log and its entries.
 *
 * There are three views defined for the transaction log:
 *
 * CREATE TABLE DATA_DICTIONARY.TRANSACTION_LOG (
 *   FILE_NAME VARCHAR NOT NULL
 * , FILE_LENGTH BIGINT NOT NULL
 * , NUM_LOG_ENTRIES BIGINT NOT NULL
 * , NUM_TRANSACTIONS BIGINT NOT NULL
 * , MIN_TRANSACTION_ID BIGINT NOT NULL
 * , MAX_TRANSACTION_ID BIGINT NOT NULL
 * , MIN_END_TIMESTAMP BIGINT NOT NULL
 * , MAX_END_TIMESTAMP BIGINT NOT NULL
 * , INDEX_SIZE_IN_BYTES BIGINT NOT NULL
 * );
 *
 * CREATE TABLE DATA_DICTIONARY.TRANSACTION_LOG_ENTRIES (
 *   ENTRY_OFFSET BIGINT NOT NULL
 * , ENTRY_TYPE VARCHAR NOT NULL
 * , ENTRY_LENGTH BIGINT NOT NULL
 * );
 *
 * CREATE TABLE DATA_DICTIONARY.TRANSACTION_LOG_TRANSACTIONS (
 *   ENTRY_OFFSET BIGINT NOT NULL
 * , TRANSACTION_ID BIGINT NOT NULL
 * , SERVER_ID BIGINT NOT NULL
 * , START_TIMESTAMP BIGINT NOT NULL
 * , END_TIMESTAMP BIGINT NOT NULL
 * , NUM_STATEMENTS BIGINT NOT NULL
 * , CHECKSUM BIGINT NOT NULL 
 * );
 */

#include <config.h>

#include "data_dictionary_schema.h"
#include "transaction_log_index.h"

#include <fcntl.h>
#include <sys/stat.h>

using namespace std;
using namespace drizzled;

extern TransactionLog *transaction_log; /* the singleton transaction log */
extern TransactionLogIndex *transaction_log_index; /* the singleton transaction log index */

/*
 *
 * TRANSACTION_LOG_INFO
 *
 */

TransactionLogTool::TransactionLogTool() :
  plugin::TableFunction("DATA_DICTIONARY", "TRANSACTION_LOG")
{
  add_field("FILE_NAME");
  add_field("FILE_LENGTH", plugin::TableFunction::NUMBER);
  add_field("NUM_LOG_ENTRIES", plugin::TableFunction::NUMBER);
  add_field("NUM_TRANSACTIONS", plugin::TableFunction::NUMBER);
  add_field("MIN_TRANSACTION_ID", plugin::TableFunction::NUMBER);
  add_field("MAX_TRANSACTION_ID", plugin::TableFunction::NUMBER);
  add_field("MIN_END_TIMESTAMP", plugin::TableFunction::NUMBER);
  add_field("MAX_END_TIMESTAMP", plugin::TableFunction::NUMBER);
  add_field("INDEX_SIZE_IN_BYTES", plugin::TableFunction::NUMBER);
}

TransactionLogTool::Generator::Generator(Field **arg) :
  plugin::TableFunction::Generator(arg)
{
  is_done= false;
}

bool TransactionLogTool::Generator::populate()
{
  if (is_done)
  {
    return false;
  }

  const string &filename= transaction_log->getLogFilename();
  push(filename.c_str());
  
  /* Grab the file size of the log */
  struct stat file_stat;
  (void) stat(filename.c_str(), &file_stat);
  push(file_stat.st_size);

  push(transaction_log_index->getNumLogEntries());
  push(transaction_log_index->getNumTransactionEntries());
  push(transaction_log_index->getMinTransactionId());
  push(transaction_log_index->getMaxTransactionId());
  push(transaction_log_index->getMinEndTimestamp());
  push(transaction_log_index->getMaxEndTimestamp()); 
  push(static_cast<uint64_t>(transaction_log_index->getSizeInBytes()));

  is_done= true;
  return true;
}

/*
 *
 * TRANSACTION_LOG_ENTRIES view
 *
 */

TransactionLogEntriesTool::TransactionLogEntriesTool() :
  plugin::TableFunction("DATA_DICTIONARY", "TRANSACTION_LOG_ENTRIES")
{
  add_field("ENTRY_OFFSET", plugin::TableFunction::NUMBER);
  add_field("ENTRY_TYPE");
  add_field("ENTRY_LENGTH", plugin::TableFunction::NUMBER);
}

TransactionLogEntriesTool::Generator::Generator(Field **arg) :
  plugin::TableFunction::Generator(arg)
{
  it= transaction_log_index->getEntries().begin();
  end= transaction_log_index->getEntries().end(); 
}

bool TransactionLogEntriesTool::Generator::populate()
{
  if (it == end)
  { 
    return false;
  } 

  TransactionLogEntry &entry= *it;

  push(entry.getOffset());
  push(entry.getTypeAsString());
  push(static_cast<uint64_t>(entry.getLengthInBytes()));

  it++;

  return true;
}

/*
 *
 * TRANSACTION_LOG_TRANSACTIONS view
 *
 */

TransactionLogTransactionsTool::TransactionLogTransactionsTool() :
  plugin::TableFunction("DATA_DICTIONARY", "TRANSACTION_LOG_TRANSACTIONS")
{
  add_field("ENTRY_OFFSET", plugin::TableFunction::NUMBER);
  add_field("TRANSACTION_ID", plugin::TableFunction::NUMBER);
  add_field("SERVER_ID", plugin::TableFunction::NUMBER);
  add_field("START_TIMESTAMP", plugin::TableFunction::NUMBER);
  add_field("END_TIMESTAMP", plugin::TableFunction::NUMBER);
  add_field("NUM_STATEMENTS", plugin::TableFunction::NUMBER);
  add_field("CHECKSUM",  plugin::TableFunction::NUMBER);
}

TransactionLogTransactionsTool::Generator::Generator(Field **arg) :
  plugin::TableFunction::Generator(arg)
{
  it= transaction_log_index->getTransactionEntries().begin();
  end= transaction_log_index->getTransactionEntries().end();
}

bool TransactionLogTransactionsTool::Generator::populate()
{
  if (it == end)
  {
    return false;
  }

  TransactionLogTransactionEntry &entry= *it;

  push(entry.getOffset());
  push(entry.getTransactionId());
  push(static_cast<uint64_t>(entry.getServerId()));
  push(entry.getStartTimestamp());
  push(entry.getEndTimestamp());
  push(static_cast<uint64_t>(entry.getNumStatements()));
  push(static_cast<uint64_t>(entry.getChecksum()));

  it++;

  return true;
}