12
12
You should have received a copy of the GNU General Public License
13
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
16
#include "config.h"
17
#include <drizzled/table.h>
18
18
#include <drizzled/error.h>
19
#include <drizzled/global_charset_info.h>
20
#include <drizzled/internal/m_string.h>
21
#include <drizzled/internal/my_pthread.h>
22
#include <drizzled/message/table.h>
23
#include <drizzled/plugin/storage_engine.h>
24
#include <drizzled/table.h>
19
#include "drizzled/internal/my_pthread.h"
27
21
#include "ha_blackhole.h"
28
#include <drizzled/message/table.pb.h>
29
#include "drizzled/internal/m_string.h"
35
30
#include <google/protobuf/io/zero_copy_stream.h>
36
31
#include <google/protobuf/io/zero_copy_stream_impl.h>
32
#include "drizzled/global_charset_info.h"
39
35
using namespace std;
43
39
#define BLACKHOLE_EXT ".blk"
45
static pthread_mutex_t blackhole_mutex;
48
41
static const char *ha_blackhole_exts[] = {
53
45
class BlackholeEngine : public drizzled::plugin::StorageEngine
55
typedef std::map<std::string, BlackholeShare*> BlackholeMap;
47
typedef map<string, BlackholeShare*> BlackholeMap;
56
48
BlackholeMap blackhole_open_tables;
59
51
BlackholeEngine(const string &name_arg)
60
: drizzled::plugin::StorageEngine(name_arg,
52
: drizzled::plugin::StorageEngine(name_arg, HTON_FILE_BASED |
62
54
HTON_CAN_INDEX_BLOBS |
63
55
HTON_SKIP_STORE_LOCK |
57
HTON_HAS_DATA_DICTIONARY),
65
58
blackhole_open_tables()
67
60
table_definition_ext= BLACKHOLE_EXT;
70
virtual ~BlackholeEngine()
72
pthread_mutex_destroy(&blackhole_mutex);
75
virtual Cursor *create(Table &table)
77
return new ha_blackhole(*this, table);
63
virtual Cursor *create(TableShare &table,
64
drizzled::memory::Root *mem_root)
66
return new (mem_root) ha_blackhole(*this, table);
80
69
const char **bas_ext() const {
81
70
return ha_blackhole_exts;
84
int doCreateTable(Session&,
73
int doCreateTable(Session*,
86
const drizzled::identifier::Table &identifier,
87
76
drizzled::message::Table&);
89
int doDropTable(Session&, const drizzled::identifier::Table &identifier);
78
int doDropTable(Session&, const string &table_name);
91
80
BlackholeShare *findOpenTable(const string table_name);
92
81
void addOpenTable(const string &table_name, BlackholeShare *);
93
82
void deleteOpenTable(const string &table_name);
95
84
int doGetTableDefinition(Session& session,
96
const drizzled::identifier::Table &identifier,
97
drizzled::message::Table &table_message);
87
const char *table_name,
89
drizzled::message::Table *table_proto);
91
void doGetTableNames(drizzled::CachedDirectory &directory,
92
string&, set<string>& set_of_names)
94
drizzled::CachedDirectory::Entries entries= directory.getEntries();
96
for (drizzled::CachedDirectory::Entries::iterator entry_iter= entries.begin();
97
entry_iter != entries.end(); ++entry_iter)
99
drizzled::CachedDirectory::Entry *entry= *entry_iter;
100
const string *filename= &entry->filename;
102
assert(filename->size());
104
const char *ext= strchr(filename->c_str(), '.');
106
if (ext == NULL || my_strcasecmp(system_charset_info, ext, BLACKHOLE_EXT) ||
107
(filename->compare(0, strlen(TMP_FILE_PREFIX), TMP_FILE_PREFIX) == 0))
111
char uname[NAME_LEN + 1];
112
uint32_t file_name_len;
114
file_name_len= filename_to_tablename(filename->c_str(), uname, sizeof(uname));
115
// TODO: Remove need for memory copy here
116
uname[file_name_len - sizeof(BLACKHOLE_EXT) + 1]= '\0'; // Subtract ending, place NULL
117
set_of_names.insert(uname);
99
122
/* The following defines can be increased if necessary */
100
123
uint32_t max_supported_keys() const { return BLACKHOLE_MAX_KEY; }
110
133
HA_KEYREAD_ONLY);
113
bool doDoesTableExist(Session& session, const drizzled::identifier::Table &identifier);
114
int doRenameTable(Session&, const drizzled::identifier::Table &from, const drizzled::identifier::Table &to);
115
void doGetTableIdentifiers(drizzled::CachedDirectory &directory,
116
const drizzled::identifier::Schema &schema_identifier,
117
drizzled::identifier::Table::vector &set_of_identifiers);
121
void BlackholeEngine::doGetTableIdentifiers(drizzled::CachedDirectory &directory,
122
const drizzled::identifier::Schema &schema_identifier,
123
drizzled::identifier::Table::vector &set_of_identifiers)
125
drizzled::CachedDirectory::Entries entries= directory.getEntries();
127
for (drizzled::CachedDirectory::Entries::iterator entry_iter= entries.begin();
128
entry_iter != entries.end(); ++entry_iter)
130
drizzled::CachedDirectory::Entry *entry= *entry_iter;
131
const string *filename= &entry->filename;
133
assert(filename->size());
135
const char *ext= strchr(filename->c_str(), '.');
137
if (ext == NULL || my_strcasecmp(system_charset_info, ext, BLACKHOLE_EXT) ||
138
(filename->compare(0, strlen(TMP_FILE_PREFIX), TMP_FILE_PREFIX) == 0))
142
char uname[NAME_LEN + 1];
143
uint32_t file_name_len;
145
file_name_len= identifier::Table::filename_to_tablename(filename->c_str(), uname, sizeof(uname));
146
// TODO: Remove need for memory copy here
147
uname[file_name_len - sizeof(BLACKHOLE_EXT) + 1]= '\0'; // Subtract ending, place NULL
149
set_of_identifiers.push_back(identifier::Table(schema_identifier, uname));
154
int BlackholeEngine::doRenameTable(Session&, const drizzled::identifier::Table &from, const drizzled::identifier::Table &to)
158
for (const char **ext= bas_ext(); *ext ; ext++)
160
if (rename_file_ext(from.getPath().c_str(), to.getPath().c_str(), *ext))
162
if ((error=errno) != ENOENT)
170
139
BlackholeShare *BlackholeEngine::findOpenTable(const string table_name)
172
141
BlackholeMap::iterator find_iter=
262
bool BlackholeEngine::doDoesTableExist(Session&,
263
const drizzled::identifier::Table &identifier)
265
string proto_path(identifier.getPath());
266
proto_path.append(BLACKHOLE_EXT);
268
if (access(proto_path.c_str(), F_OK))
277
233
int BlackholeEngine::doGetTableDefinition(Session&,
278
const drizzled::identifier::Table &identifier,
279
drizzled::message::Table &table_proto)
238
drizzled::message::Table *table_proto)
283
new_path= identifier.getPath();
284
243
new_path+= BLACKHOLE_EXT;
286
245
int fd= open(new_path.c_str(), O_RDONLY);
293
252
google::protobuf::io::ZeroCopyInputStream* input=
294
253
new google::protobuf::io::FileInputStream(fd);
297
256
return HA_ERR_CRASHED_ON_USAGE;
299
if (not table_proto.ParseFromZeroCopyStream(input))
258
if (table_proto && ! table_proto->ParseFromZeroCopyStream(input))
303
if (not table_proto.IsInitialized())
262
if (! table_proto->IsInitialized())
305
264
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
306
table_proto.name().empty() ? " " : table_proto.name().c_str(),
307
table_proto.InitializationErrorString().c_str());
265
table_proto->InitializationErrorString().c_str());
309
266
return ER_CORRUPT_TABLE_DEFINITION;
451
407
BlackholeShare::~BlackholeShare()
409
thr_lock_delete(&lock);
457
413
static drizzled::plugin::StorageEngine *blackhole_engine= NULL;
459
static int blackhole_init(drizzled::module::Context &context)
415
static int blackhole_init(drizzled::plugin::Registry ®istry)
462
418
blackhole_engine= new BlackholeEngine("BLACKHOLE");
463
context.add(blackhole_engine);
419
registry.add(blackhole_engine);
465
421
pthread_mutex_init(&blackhole_mutex, MY_MUTEX_INIT_FAST);
426
static int blackhole_fini(drizzled::plugin::Registry ®istry)
428
registry.remove(blackhole_engine);
429
delete blackhole_engine;
431
pthread_mutex_destroy(&blackhole_mutex);
471
436
DRIZZLE_DECLARE_PLUGIN