~drizzle-trunk/drizzle/development

1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2010 Brian Aker
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 */
20
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
21
#include <config.h>
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
22
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
23
#include <drizzled/plugin/catalog.h>
24
#include <drizzled/catalog/cache.h>
25
#include <drizzled/catalog/local.h>
26
#include <drizzled/error.h>
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
27
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
28
#include <boost/foreach.hpp>
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
29
30
namespace drizzled
31
{
32
namespace plugin
33
{
34
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
35
// Private container we use for holding the instances of engines passed to
36
// use from the catalog plugins.
37
class Engines {
38
  catalog::Engine::vector _catalogs;
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
39
40
public:
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
41
  static Engines& singleton()
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
42
  {
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
43
    static Engines ptr;
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
44
    return ptr;
45
  }
46
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
47
  catalog::Engine::vector &catalogs()
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
48
  {
49
    return _catalogs;
50
  }
51
};
52
2246.4.8 by Olaf van der Spek
Remove const_reference and reference from identifier::Catalog
53
bool Catalog::create(const identifier::Catalog& identifier)
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
54
{
1960.1.12 by Brian Aker
Add in the schema and table make_shared methods.
55
  message::catalog::shared_ptr message= message::catalog::make_shared(identifier);
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
56
  return create(identifier, message);
57
}
58
2246.4.8 by Olaf van der Spek
Remove const_reference and reference from identifier::Catalog
59
bool Catalog::create(const identifier::Catalog& identifier, message::catalog::shared_ptr &message)
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
60
{
61
  assert(message);
62
2039.6.3 by Brian Aker
Update for session to have a catalog object.
63
  catalog::lock::Create lock(identifier);
2017.3.1 by Brian Aker
Merge catalog with current trunk.
64
65
  if (not lock.locked())
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
66
  {
2017.3.1 by Brian Aker
Merge catalog with current trunk.
67
    my_error(ER_CATALOG_NO_LOCK, MYF(0), identifier.getName().c_str());
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
68
    return false;
69
  }
70
2039.6.2 by Brian Aker
Update code to test/handle conflicting create/drop schema.
71
  size_t create_count= 0;
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
72
  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
73
  {
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
74
    if (ref->create(identifier, message))
2039.6.2 by Brian Aker
Update code to test/handle conflicting create/drop schema.
75
      create_count++;
76
  }
77
  assert(create_count < 2);
78
79
  if (not create_count)
80
  {
81
    my_error(ER_CATALOG_CANNOT_CREATE, MYF(0), identifier.getName().c_str());
82
    return false;
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
83
  }
84
2017.3.1 by Brian Aker
Merge catalog with current trunk.
85
  return true;
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
86
}
87
2246.4.8 by Olaf van der Spek
Remove const_reference and reference from identifier::Catalog
88
bool Catalog::drop(const identifier::Catalog& identifier)
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
89
{
2039.6.6 by Brian Aker
Update so that CATALOG is correctly being displayed.
90
  if (identifier == drizzled::catalog::local_identifier())
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
91
  {
92
    my_error(drizzled::ER_CATALOG_NO_DROP_LOCAL, MYF(0));
93
    return false;
94
  }
95
2039.6.3 by Brian Aker
Update for session to have a catalog object.
96
  catalog::lock::Erase lock(identifier);
2017.3.1 by Brian Aker
Merge catalog with current trunk.
97
  if (not lock.locked())
98
  {
99
    my_error(ER_CATALOG_NO_LOCK, MYF(0), identifier.getName().c_str());
100
    return false; 
101
  }
102
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
103
  
2039.6.2 by Brian Aker
Update code to test/handle conflicting create/drop schema.
104
  size_t drop_count= 0;
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
105
  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
106
  {
2039.6.2 by Brian Aker
Update code to test/handle conflicting create/drop schema.
107
    if (ref->drop(identifier))
108
      drop_count++;
109
  }
110
  assert(drop_count < 2);
111
112
  if (not drop_count)
113
  {
114
    my_error(ER_CATALOG_DOES_NOT_EXIST, MYF(0), identifier.getName().c_str());
115
    return false;
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
116
  }
117
2017.3.1 by Brian Aker
Merge catalog with current trunk.
118
  return true;
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
119
}
120
2246.4.8 by Olaf van der Spek
Remove const_reference and reference from identifier::Catalog
121
bool Catalog::lock(const identifier::Catalog& identifier)
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
122
{
2073.2.1 by Brian Aker
Remove custom error.
123
  drizzled::error_t error;
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
124
  
125
  // We insert a lock into the cache, if this fails we bail.
2278.2.1 by Olaf van der Spek
Catalog Cache
126
  if (not catalog::Cache::lock(identifier, error))
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
127
  {
2073.2.1 by Brian Aker
Remove custom error.
128
    my_error(error, identifier);
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
129
130
    return false;
131
  }
132
133
  return true;
134
}
135
136
2246.4.8 by Olaf van der Spek
Remove const_reference and reference from identifier::Catalog
137
bool Catalog::unlock(const identifier::Catalog& identifier)
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
138
{
2073.2.1 by Brian Aker
Remove custom error.
139
  drizzled::error_t error;
2278.2.1 by Olaf van der Spek
Catalog Cache
140
  if (not catalog::Cache::unlock(identifier, error))
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
141
  {
2073.2.1 by Brian Aker
Remove custom error.
142
    my_error(error, identifier);
1960.1.9 by Brian Aker
Merge in lock testing code/additional fix for tests.
143
  }
144
145
  return false;
146
}
147
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
148
bool plugin::Catalog::addPlugin(plugin::Catalog *arg)
149
{
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
150
  Engines::singleton().catalogs().push_back(arg->engine());
151
152
  return false;
153
}
154
2246.4.8 by Olaf van der Spek
Remove const_reference and reference from identifier::Catalog
155
bool plugin::Catalog::exist(const identifier::Catalog& identifier)
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
156
{
2278.2.1 by Olaf van der Spek
Catalog Cache
157
  if (catalog::Cache::exist(identifier))
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
158
    return true;
159
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
160
  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
161
  {
162
    if (ref->exist(identifier))
163
      return true;
164
  }
165
166
  return false;
167
}
168
2252.1.9 by Olaf van der Spek
Common fwd
169
void plugin::Catalog::getIdentifiers(identifier::catalog::vector &identifiers)
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
170
{
171
  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
172
  {
173
    ref->getIdentifiers(identifiers);
174
  }
175
}
176
177
void plugin::Catalog::getMessages(message::catalog::vector &messages)
178
{
179
  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
180
  {
181
    ref->getMessages(messages);
182
  }
183
}
184
2246.4.8 by Olaf van der Spek
Remove const_reference and reference from identifier::Catalog
185
message::catalog::shared_ptr plugin::Catalog::getMessage(const identifier::Catalog& identifier)
1960.1.6 by Brian Aker
Adding in the engine interface. The filesystem catalog will now handle
186
{
2073.2.1 by Brian Aker
Remove custom error.
187
  drizzled::error_t error;
2278.2.1 by Olaf van der Spek
Catalog Cache
188
  catalog::Instance::shared_ptr instance= catalog::Cache::find(identifier, error);
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
189
  message::catalog::shared_ptr message;
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
190
191
  if (instance and instance->message())
192
  {
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
193
    return instance->message();
194
  }
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
195
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
196
  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
197
  {
198
    if ((message= ref->getMessage(identifier)))
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
199
      return message;
200
  }
201
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
202
  return message;
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
203
}
204
2246.4.8 by Olaf van der Spek
Remove const_reference and reference from identifier::Catalog
205
catalog::Instance::shared_ptr plugin::Catalog::getInstance(const identifier::Catalog& identifier)
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
206
{
2073.2.1 by Brian Aker
Remove custom error.
207
  drizzled::error_t error;
2278.2.1 by Olaf van der Spek
Catalog Cache
208
  catalog::Instance::shared_ptr instance= catalog::Cache::find(identifier, error);
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
209
210
  if (instance)
2039.6.3 by Brian Aker
Update for session to have a catalog object.
211
    return instance;
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
212
213
  BOOST_FOREACH(catalog::Engine::vector::const_reference ref, Engines::singleton().catalogs())
214
  {
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
215
    message::catalog::shared_ptr message;
216
    if (message= ref->getMessage(identifier))
2039.6.3 by Brian Aker
Update for session to have a catalog object.
217
    {
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
218
      instance= catalog::Instance::make_shared(message);
2039.6.3 by Brian Aker
Update for session to have a catalog object.
219
      // If this should fail inserting into the cache, we are in a world of
220
      // pain.
2278.2.1 by Olaf van der Spek
Catalog Cache
221
      catalog::Cache::insert(identifier, instance, error);
2039.6.3 by Brian Aker
Update for session to have a catalog object.
222
223
      return instance;
224
    }
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
225
  }
226
2039.6.3 by Brian Aker
Update for session to have a catalog object.
227
  return catalog::Instance::shared_ptr();
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
228
}
229
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
230
2068.5.6 by Brian Aker
Remove dead reference call for deleting plugin (we don't need cleanup).
231
void plugin::Catalog::removePlugin(plugin::Catalog *)
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
232
{
233
}
234
235
} /* namespace plugin */
236
} /* namespace drizzled */