16
16
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
20
#include <drizzled/field.h>
21
21
#include <drizzled/field/blob.h>
22
#include <drizzled/field/timestamp.h>
22
23
#include <drizzled/error.h>
23
24
#include <drizzled/table.h>
24
25
#include <drizzled/session.h>
25
#include <drizzled/internal/my_sys.h>
26
#include "drizzled/internal/my_sys.h"
26
27
#include <google/protobuf/io/zero_copy_stream.h>
27
28
#include <google/protobuf/io/zero_copy_stream_impl.h>
73
74
pthread_mutex_destroy(&filesystem_mutex);
76
virtual Cursor *create(Table &table)
77
virtual Cursor *create(TableShare &table)
78
79
return new FilesystemCursor(*this, table);
87
88
int doCreateTable(Session &,
89
const drizzled::identifier::Table &identifier,
90
const drizzled::TableIdentifier &identifier,
90
91
drizzled::message::Table&);
92
93
int doGetTableDefinition(Session& ,
93
const drizzled::identifier::Table &,
94
const drizzled::TableIdentifier &,
94
95
drizzled::message::Table &);
96
int doDropTable(Session&, const identifier::Table &);
97
/* Temp only engine, so do not return values. */
98
void doGetTableNames(drizzled::CachedDirectory &, const drizzled::SchemaIdentifier &, drizzled::plugin::TableNameList &);
100
int doDropTable(Session&, const TableIdentifier &);
98
102
/* operations on FilesystemTableShare */
99
103
FilesystemTableShare *findOpenTable(const string table_name);
103
107
uint32_t max_keys() const { return 0; }
104
108
uint32_t max_key_parts() const { return 0; }
105
109
uint32_t max_key_length() const { return 0; }
106
bool doDoesTableExist(Session& , const identifier::Table &);
107
int doRenameTable(Session&, const identifier::Table &, const identifier::Table &);
110
bool doDoesTableExist(Session& , const TableIdentifier &);
111
int doRenameTable(Session&, const TableIdentifier &, const TableIdentifier &);
108
112
void doGetTableIdentifiers(drizzled::CachedDirectory &directory,
109
const drizzled::identifier::Schema &schema_identifier,
110
drizzled::identifier::Table::vector &set_of_identifiers);
113
const drizzled::SchemaIdentifier &schema_identifier,
114
drizzled::TableIdentifiers &set_of_identifiers);
112
116
void getTableNamesFromFilesystem(drizzled::CachedDirectory &directory,
113
const drizzled::identifier::Schema &schema_identifier,
117
const drizzled::SchemaIdentifier &schema_identifier,
114
118
drizzled::plugin::TableNameList *set_of_names,
115
drizzled::identifier::Table::vector *set_of_identifiers);
119
drizzled::TableIdentifiers *set_of_identifiers);
118
122
void FilesystemEngine::getTableNamesFromFilesystem(drizzled::CachedDirectory &directory,
119
const drizzled::identifier::Schema &schema_identifier,
123
const drizzled::SchemaIdentifier &schema_identifier,
120
124
drizzled::plugin::TableNameList *set_of_names,
121
drizzled::identifier::Table::vector *set_of_identifiers)
125
drizzled::TableIdentifiers *set_of_identifiers)
123
127
drizzled::CachedDirectory::Entries entries= directory.getEntries();
140
144
char uname[NAME_LEN + 1];
141
145
uint32_t file_name_len;
143
file_name_len= identifier::Table::filename_to_tablename(filename->c_str(), uname, sizeof(uname));
147
file_name_len= TableIdentifier::filename_to_tablename(filename->c_str(), uname, sizeof(uname));
144
148
uname[file_name_len - sizeof(FILESYSTEM_EXT) + 1]= '\0';
145
149
if (set_of_names)
146
150
set_of_names->insert(uname);
147
151
if (set_of_identifiers)
148
set_of_identifiers->push_back(identifier::Table(schema_identifier, uname));
152
set_of_identifiers->push_back(TableIdentifier(schema_identifier, uname));
157
void FilesystemEngine::doGetTableNames(drizzled::CachedDirectory &directory,
158
const drizzled::SchemaIdentifier &schema_identifier,
159
drizzled::plugin::TableNameList &set_of_names)
161
getTableNamesFromFilesystem(directory, schema_identifier, &set_of_names, NULL);
153
164
void FilesystemEngine::doGetTableIdentifiers(drizzled::CachedDirectory &directory,
154
const drizzled::identifier::Schema &schema_identifier,
155
drizzled::identifier::Table::vector &set_of_identifiers)
165
const drizzled::SchemaIdentifier &schema_identifier,
166
drizzled::TableIdentifiers &set_of_identifiers)
157
168
getTableNamesFromFilesystem(directory, schema_identifier, NULL, &set_of_identifiers);
160
int FilesystemEngine::doDropTable(Session &, const identifier::Table &identifier)
171
int FilesystemEngine::doDropTable(Session &, const TableIdentifier &identifier)
162
173
string new_path(identifier.getPath());
163
174
new_path+= FILESYSTEM_EXT;
172
bool FilesystemEngine::doDoesTableExist(Session &, const identifier::Table &identifier)
183
bool FilesystemEngine::doDoesTableExist(Session &, const TableIdentifier &identifier)
174
185
string proto_path(identifier.getPath());
175
186
proto_path.append(FILESYSTEM_EXT);
283
294
int FilesystemEngine::doGetTableDefinition(Session &,
284
const drizzled::identifier::Table &identifier,
295
const drizzled::TableIdentifier &identifier,
285
296
drizzled::message::Table &table_proto)
287
298
string new_path(identifier.getPath());
304
315
if (not table_proto.IsInitialized())
306
317
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
307
table_proto.name().empty() ? " " : table_proto.name().c_str(),
308
318
table_proto.InitializationErrorString().c_str());
310
319
return ER_CORRUPT_TABLE_DEFINITION;
318
327
// then columns of this table are added dynamically here.
319
328
FormatInfo format;
320
329
format.parseFromTable(&table_proto);
321
if (not format.isTagFormat() || not format.isFileGiven())
330
if (!format.isTagFormat() || !format.isFileGiven())
327
std::vector< std::map<std::string, std::string> > vm;
333
vector< map<string, string> > vm;
328
334
if (parseTaggedFile(format, vm) != 0)
334
if (vm.size() == 0) {
339
339
// we don't care what user provides, just clear them all
340
340
table_proto.clear_field();
341
341
// we take the first section as sample
342
std::map<string, string> kv= vm[0];
343
for (std::map<string, string>::iterator iter= kv.begin();
342
map<string, string> kv= vm[0];
343
for (map<string, string>::iterator iter= kv.begin();
344
344
iter != kv.end();
373
371
Guard g(filesystem_mutex);
375
FilesystemEngine *a_engine= static_cast<FilesystemEngine *>(getEngine());
373
FilesystemEngine *a_engine= static_cast<FilesystemEngine *>(engine);
376
374
share= a_engine->findOpenTable(table_name);
417
415
Guard g(filesystem_mutex);
419
417
if (!--share->use_count){
420
FilesystemEngine *a_engine= static_cast<FilesystemEngine *>(getEngine());
418
FilesystemEngine *a_engine= static_cast<FilesystemEngine *>(engine);
421
419
a_engine->deleteOpenTable(share->table_name);
422
420
pthread_mutex_destroy(&share->mutex);
456
454
thread_locked = false;
459
FilesystemCursor::FilesystemCursor(drizzled::plugin::StorageEngine &engine_arg, Table &table_arg)
457
FilesystemCursor::FilesystemCursor(drizzled::plugin::StorageEngine &engine_arg, TableShare &table_arg)
460
458
: Cursor(engine_arg, table_arg),
461
459
file_buff(new TransparentFile),
462
460
thread_locked(false)
466
int FilesystemCursor::doOpen(const drizzled::identifier::Table &identifier, int, uint32_t)
464
int FilesystemCursor::doOpen(const drizzled::TableIdentifier &identifier, int, uint32_t)
468
466
if (!(share= get_share(identifier.getPath().c_str())))
519
517
int FilesystemCursor::find_current_row(unsigned char *buf)
521
ptrdiff_t row_offset= buf - getTable()->record[0];
519
ptrdiff_t row_offset= buf - table->record[0];
523
521
next_position= current_position;
526
524
bool line_done= false;
527
525
bool line_blank= true;
528
Field **field= getTable()->getFields();
526
Field **field= table->getFields();
529
527
for (; !line_done && *field; ++next_position)
531
529
char ch= file_buff->get_value(next_position);
564
562
if ((*field)->isReadSet() || (*field)->isWriteSet())
566
564
(*field)->setWriteSet();
567
(*field)->store_and_check(CHECK_FIELD_WARN,
569
(uint32_t)content.length(),
565
(*field)->store(content.c_str(),
566
(uint32_t)content.length(),
574
571
(*field)->set_default();
578
574
(*field)->set_null();
621
617
if (tag_depth >= share->vm.size())
622
618
return HA_ERR_END_OF_FILE;
624
ptrdiff_t row_offset= buf - getTable()->record[0];
625
for (Field **field= getTable()->getFields(); *field; field++)
620
ptrdiff_t row_offset= buf - table->record[0];
621
for (Field **field= table->getFields(); *field; field++)
627
623
string key((*field)->field_name);
628
624
string content= share->vm[tag_depth][key];
634
630
if ((*field)->isReadSet() || (*field)->isWriteSet())
636
632
(*field)->setWriteSet();
637
(*field)->store_and_check(CHECK_FIELD_WARN,
639
(uint32_t)content.length(),
633
(*field)->store(content.c_str(),
634
(uint32_t)content.length(),
762
758
file_buffer_start= file_buff->read_next();
764
760
// close update file
765
if (::fsync(update_file_desc) ||
761
if (::fsync(update_file_desc) ||
766
762
::close(update_file_desc))
768
764
share->update_file_opened= false;
773
769
if (::rename(update_file_name.c_str(), share->format.getFileName().c_str()))
772
// reopen the data file
773
file_desc= ::open(share->format.getFileName().c_str(), O_RDONLY);
776
774
share->needs_reopen= true;
780
780
pthread_mutex_unlock(&share->mutex);
782
782
if (thread_locked)
783
783
critical_section_exit();
896
int FilesystemEngine::doRenameTable(Session&, const identifier::Table &from, const identifier::Table &to)
895
int FilesystemEngine::doRenameTable(Session&, const TableIdentifier &from, const TableIdentifier &to)
898
897
if (rename_file_ext(from.getPath().c_str(), to.getPath().c_str(), FILESYSTEM_EXT))
909
908
int FilesystemEngine::doCreateTable(Session &,
911
const drizzled::identifier::Table &identifier,
912
drizzled::message::Table &proto)
910
const drizzled::TableIdentifier &identifier,
911
drizzled::message::Table &proto)
914
913
FormatInfo format;
915
914
format.parseFromTable(&proto);
956
955
"Filesystem Engine",
957
956
PLUGIN_LICENSE_GPL,
958
957
filesystem_init_func, /* Plugin Init */
958
NULL, /* system variables */
960
959
NULL /* config options */
962
961
DRIZZLE_DECLARE_PLUGIN_END;