~drizzle-trunk/drizzle/development

1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
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) 2009 Sun Microsystems, Inc.
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
5
 *
6
 *  Authors:
7
 *
8
 *  Jay Pipes <joinfu@sun.com>
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  (at your option) any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program; if not, write to the Free Software
22
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23
 */
24
25
/**
26
 * @file
27
 *
28
 * Defines the implementation of a simple index into a transaction log.
29
 */
30
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
31
#include <config.h>
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
32
#include <drizzled/message/transaction.pb.h>
33
34
#include "transaction_log_index.h"
35
36
#include <pthread.h>
37
38
using namespace std;
39
using namespace drizzled;
40
41
TransactionLogIndex *transaction_log_index= NULL; /* The singleton transaction log index */
42
43
TransactionLogIndex::TransactionLogIndex(TransactionLog &in_log) :
44
  log(in_log),
45
  index_file(-1),
46
  index_file_path(),
47
  has_error(false),
48
  error_message(),
49
  min_end_timestamp(0),
50
  max_end_timestamp(0),
51
  min_transaction_id(0),
52
  max_transaction_id(0),
1273.1.33 by Jay Pipes
Adds concurrency tests for transaction log and its data dictionary table. Adds ability to see size of the in-memory index of the transaction log in bytes to the transaction_log data dictionary table.
53
  entries(),
54
  transaction_entries()
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
55
{
56
  (void) pthread_mutex_init(&index_lock, NULL);
1273.1.33 by Jay Pipes
Adds concurrency tests for transaction log and its data dictionary table. Adds ability to see size of the in-memory index of the transaction log in bytes to the transaction_log data dictionary table.
57
  entries.reserve(1024);
58
  transaction_entries.reserve(1024);
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
59
  open();
60
}
61
62
TransactionLogIndex::~TransactionLogIndex()
63
{
64
  entries.clear();
65
  transaction_entries.clear();
66
  pthread_mutex_destroy(&index_lock);
67
}
68
1273.1.33 by Jay Pipes
Adds concurrency tests for transaction log and its data dictionary table. Adds ability to see size of the in-memory index of the transaction log in bytes to the transaction_log data dictionary table.
69
void TransactionLogIndex::clear()
70
{
71
  pthread_mutex_lock(&index_lock);
72
  min_end_timestamp= 0;
73
  max_end_timestamp= 0;
74
  min_transaction_id= 0;
75
  max_transaction_id= 0;
76
  entries.clear();
77
  transaction_entries.clear();
78
  clearError();
79
  pthread_mutex_unlock(&index_lock);
80
}
81
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
82
void TransactionLogIndex::open()
83
{
84
85
}
86
87
bool TransactionLogIndex::hasError() const
88
{
89
  return has_error;
90
}
91
92
void TransactionLogIndex::clearError()
93
{
94
  has_error= false;
95
  error_message.clear();
96
}
97
98
const std::string &TransactionLogIndex::getErrorMessage() const
99
{
100
  return error_message;
101
}
102
103
uint64_t TransactionLogIndex::getMinEndTimestamp() const
104
{
105
  return min_end_timestamp;
106
}
107
108
uint64_t TransactionLogIndex::getMaxEndTimestamp() const
109
{
110
  return max_end_timestamp;
111
}
112
113
uint64_t TransactionLogIndex::getMinTransactionId() const
114
{
115
  return min_transaction_id;
116
}
117
118
uint64_t TransactionLogIndex::getMaxTransactionId() const
119
{
120
  return max_transaction_id;
121
}
122
123
uint64_t TransactionLogIndex::getNumLogEntries() const
124
{
1273.1.33 by Jay Pipes
Adds concurrency tests for transaction log and its data dictionary table. Adds ability to see size of the in-memory index of the transaction log in bytes to the transaction_log data dictionary table.
125
  return entries.size();
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
126
}
127
128
uint64_t TransactionLogIndex::getNumTransactionEntries() const
129
{
1237.10.8 by Monty Taylor
Removed num_transaction_entries since it's duplicated in transaction_entries.size()
130
  return transaction_entries.size();
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
131
}
132
133
TransactionLog::Entries &TransactionLogIndex::getEntries()
134
{
135
  return entries;
136
}
137
138
TransactionLog::TransactionEntries &TransactionLogIndex::getTransactionEntries()
139
{
140
  return transaction_entries;
141
}
142
1273.1.33 by Jay Pipes
Adds concurrency tests for transaction log and its data dictionary table. Adds ability to see size of the in-memory index of the transaction log in bytes to the transaction_log data dictionary table.
143
size_t TransactionLogIndex::getTransactionEntriesSizeInBytes()
144
{
145
  return transaction_entries.capacity() * sizeof(TransactionLog::TransactionEntries::value_type);
146
}
147
148
size_t TransactionLogIndex::getEntriesSizeInBytes()
149
{
150
  return entries.capacity() * sizeof(TransactionLog::Entries::value_type);
151
}
152
153
154
size_t TransactionLogIndex::getSizeInBytes()
155
{
156
  return sizeof(this) + getEntriesSizeInBytes() + getTransactionEntriesSizeInBytes();
157
}
158
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
159
void TransactionLogIndex::addEntry(const TransactionLogEntry &entry,
160
                                   const message::Transaction &transaction,
161
                                   uint32_t checksum)
162
{
163
  pthread_mutex_lock(&index_lock);
164
  if (entries.empty())
165
  {
166
    /* First entry...set the minimums */
167
    min_transaction_id= transaction.transaction_context().transaction_id();
168
    min_end_timestamp= transaction.transaction_context().end_timestamp();
169
  }
170
  max_transaction_id= transaction.transaction_context().transaction_id();
171
  max_end_timestamp= transaction.transaction_context().end_timestamp();
172
  entries.push_back(entry);
173
  transaction_entries.push_back(TransactionLogTransactionEntry(entry.getOffset(),
174
                                                               transaction,
175
                                                               checksum));
176
  pthread_mutex_unlock(&index_lock);
177
}