~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/archive/ha_archive.cc

Merged build.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* Copyright (C) 2003 MySQL AB
 
2
   Copyright (C) 2010 Brian Aker
2
3
 
3
4
  This program is free software; you can redistribute it and/or modify
4
5
  it under the terms of the GNU General Public License as published by
15
16
 
16
17
 
17
18
#include "config.h"
18
 
#include "drizzled/field.h"
19
 
#include "drizzled/field/blob.h"
20
 
#include "drizzled/field/timestamp.h"
21
 
#include "plugin/myisam/myisam.h"
22
 
#include "drizzled/table.h"
23
 
#include "drizzled/session.h"
24
 
 
25
 
#include "ha_archive.h"
26
 
 
27
 
#include <unistd.h>
28
 
#include <fcntl.h>
29
 
#include <sys/stat.h>
30
 
 
31
 
#include <cstdio>
32
 
#include <string>
33
 
#include <map>
 
19
 
 
20
#include "plugin/archive/archive_engine.h"
34
21
 
35
22
using namespace std;
 
23
using namespace drizzled;
 
24
 
36
25
 
37
26
/*
38
27
  First, if you want to understand storage engines you should look at
105
94
/* Variables for archive share methods */
106
95
pthread_mutex_t archive_mutex= PTHREAD_MUTEX_INITIALIZER;
107
96
 
108
 
static unsigned int global_version;
109
 
 
110
 
/* The file extension */
111
 
#define ARZ ".arz"               // The data file
112
 
#define ARN ".ARN"               // Files used during an optimize call
113
 
 
114
 
 
115
 
 
116
 
static bool archive_use_aio= false;
 
97
/* When the engine starts up set the first version */
 
98
static uint64_t global_version= 1;
 
99
 
 
100
// We use this to find out the state of the archive aio option.
 
101
extern bool archive_aio_state(void);
117
102
 
118
103
/*
119
104
  Number of rows that will force a bulk insert.
125
110
*/
126
111
#define ARCHIVE_ROW_HEADER_SIZE 4
127
112
 
128
 
/*
129
 
  We just implement one additional file extension.
130
 
*/
131
 
static const char *ha_archive_exts[] = {
132
 
  ARZ,
133
 
  NULL
134
 
};
135
 
 
136
 
class ArchiveEngine : public drizzled::plugin::StorageEngine
137
 
{
138
 
  typedef std::map<string, ArchiveShare*> ArchiveMap;
139
 
  ArchiveMap archive_open_tables;
140
 
 
141
 
public:
142
 
  ArchiveEngine(const string &name_arg)
143
 
   : drizzled::plugin::StorageEngine(name_arg,
144
 
                                     HTON_FILE_BASED |
145
 
                                     HTON_STATS_RECORDS_IS_EXACT |
146
 
                                     HTON_HAS_RECORDS |
147
 
                                     HTON_HAS_DATA_DICTIONARY),
148
 
     archive_open_tables()
149
 
  {
150
 
    table_definition_ext= ARZ;
151
 
  }
152
 
 
153
 
  virtual Cursor *create(TableShare &table,
154
 
                         drizzled::memory::Root *mem_root)
155
 
  {
156
 
    return new (mem_root) ha_archive(*this, table);
157
 
  }
158
 
 
159
 
  const char **bas_ext() const {
160
 
    return ha_archive_exts;
161
 
  }
162
 
 
163
 
  int doCreateTable(Session *session, const char *table_name,
164
 
                    Table& table_arg,
165
 
                    drizzled::message::Table& proto);
166
 
 
167
 
  int doGetTableDefinition(Session& session,
168
 
                           const char* path,
169
 
                           const char *db,
170
 
                           const char *table_name,
171
 
                           const bool is_tmp,
172
 
                           drizzled::message::Table *table_proto);
173
 
 
174
 
  void doGetTableNames(drizzled::CachedDirectory &directory, string& , set<string>& set_of_names);
175
 
 
176
 
  int doDropTable(Session&, const string table_path);
177
 
  ArchiveShare *findOpenTable(const string table_name);
178
 
  void addOpenTable(const string &table_name, ArchiveShare *);
179
 
  void deleteOpenTable(const string &table_name);
180
 
 
181
 
  uint32_t max_supported_keys()          const { return 1; }
182
 
  uint32_t max_supported_key_length()    const { return sizeof(uint64_t); }
183
 
  uint32_t max_supported_key_part_length() const { return sizeof(uint64_t); }
184
 
 
185
 
  uint32_t index_flags(enum  ha_key_alg) const
186
 
  {
187
 
    return HA_ONLY_WHOLE_INDEX;
188
 
  }
189
 
};
190
 
 
191
113
ArchiveShare *ArchiveEngine::findOpenTable(const string table_name)
192
114
{
193
115
  ArchiveMap::iterator find_iter=
307
229
  return error;
308
230
}
309
231
 
310
 
static ArchiveEngine *archive_engine= NULL;
311
 
 
312
 
/*
313
 
  Initialize the archive Cursor.
314
 
 
315
 
  SYNOPSIS
316
 
    archive_db_init()
317
 
    void *
318
 
 
319
 
  RETURN
320
 
    false       OK
321
 
    true        Error
322
 
*/
323
 
 
324
 
static int archive_db_init(drizzled::plugin::Registry &registry)
325
 
{
326
 
 
327
 
  pthread_mutex_init(&archive_mutex, MY_MUTEX_INIT_FAST);
328
 
  archive_engine= new ArchiveEngine("ARCHIVE");
329
 
  registry.add(archive_engine);
330
 
 
331
 
  /* When the engine starts up set the first version */
332
 
  global_version= 1;
333
 
 
334
 
  return false;
335
 
}
336
 
 
337
 
/*
338
 
  Release the archive Cursor.
339
 
 
340
 
  SYNOPSIS
341
 
    archive_db_done()
342
 
    void
343
 
 
344
 
  RETURN
345
 
    false       OK
346
 
*/
347
 
 
348
 
static int archive_db_done(drizzled::plugin::Registry &registry)
349
 
{
350
 
  registry.remove(archive_engine);
351
 
  delete archive_engine;
352
 
 
353
 
  pthread_mutex_destroy(&archive_mutex);
354
 
 
355
 
  return 0;
356
 
}
357
 
 
358
232
 
359
233
ha_archive::ha_archive(drizzled::plugin::StorageEngine &engine_arg,
360
234
                       TableShare &table_arg)
364
238
  buffer.set((char *)byte_buffer, IO_SIZE, system_charset_info);
365
239
 
366
240
  /* The size of the offset value we will use for position() */
367
 
  ref_length= sizeof(my_off_t);
 
241
  ref_length= sizeof(internal::my_off_t);
368
242
  archive_reader_open= false;
369
243
}
370
244
 
395
269
{
396
270
  memset(&archive_write, 0, sizeof(azio_stream));     /* Archive file we are working with */
397
271
  table_name.append(name);
398
 
  fn_format(data_file_name, table_name.c_str(), "",
 
272
  internal::fn_format(data_file_name, table_name.c_str(), "",
399
273
            ARZ, MY_REPLACE_EXT | MY_UNPACK_FILENAME);
400
274
  /*
401
275
    We will use this lock for rows.
544
418
  {
545
419
    az_method method;
546
420
 
547
 
    switch (archive_use_aio)
 
421
    switch (archive_aio_state())
548
422
    {
549
423
    case false:
550
424
      method= AZ_METHOD_BLOCK;
691
565
  /*
692
566
    We reuse name_buff since it is available.
693
567
  */
694
 
  fn_format(name_buff, table_name, "", ARZ,
 
568
  internal::fn_format(name_buff, table_name, "", ARZ,
695
569
            MY_REPLACE_EXT | MY_UNPACK_FILENAME);
696
570
 
697
571
  errno= 0;
1084
958
 
1085
959
void ha_archive::position(const unsigned char *)
1086
960
{
1087
 
  my_store_ptr(ref, ref_length, current_position);
 
961
  internal::my_store_ptr(ref, ref_length, current_position);
1088
962
  return;
1089
963
}
1090
964
 
1099
973
int ha_archive::rnd_pos(unsigned char * buf, unsigned char *pos)
1100
974
{
1101
975
  ha_statistic_increment(&SSV::ha_read_rnd_next_count);
1102
 
  current_position= (my_off_t)my_get_ptr(pos, ref_length);
 
976
  current_position= (internal::my_off_t)internal::my_get_ptr(pos, ref_length);
1103
977
  if (azseek(&archive, (size_t)current_position, SEEK_SET) == (size_t)(-1L))
1104
978
    return(HA_ERR_CRASHED_ON_USAGE);
1105
979
  return(get_row(&archive, buf));
1149
1023
  azread_frm(&archive, proto_string);
1150
1024
 
1151
1025
  /* Lets create a file to contain the new data */
1152
 
  fn_format(writer_filename, share->table_name.c_str(), "", ARN,
 
1026
  internal::fn_format(writer_filename, share->table_name.c_str(), "", ARN,
1153
1027
            MY_REPLACE_EXT | MY_UNPACK_FILENAME);
1154
1028
 
1155
1029
  if (!(azopen(&writer, writer_filename, O_CREAT|O_RDWR, AZ_METHOD_BLOCK)))
1232
1106
  azclose(&archive);
1233
1107
 
1234
1108
  // make the file we just wrote be our data file
1235
 
  rc = my_rename(writer_filename,share->data_file_name,MYF(0));
 
1109
  rc = internal::my_rename(writer_filename,share->data_file_name,MYF(0));
1236
1110
 
1237
1111
  free(proto_string);
1238
1112
  return(rc);
1451
1325
  free((char*) r);
1452
1326
  return;
1453
1327
}
1454
 
 
1455
 
static DRIZZLE_SYSVAR_BOOL(aio, archive_use_aio,
1456
 
  PLUGIN_VAR_NOCMDOPT,
1457
 
  "Whether or not to use asynchronous IO.",
1458
 
  NULL, NULL, true);
1459
 
 
1460
 
static drizzle_sys_var* archive_system_variables[]= {
1461
 
  DRIZZLE_SYSVAR(aio),
1462
 
  NULL
1463
 
};
1464
 
 
1465
 
DRIZZLE_DECLARE_PLUGIN
1466
 
{
1467
 
  DRIZZLE_VERSION_ID,
1468
 
  "ARCHIVE",
1469
 
  "3.5",
1470
 
  "Brian Aker, MySQL AB",
1471
 
  "Archive storage engine",
1472
 
  PLUGIN_LICENSE_GPL,
1473
 
  archive_db_init, /* Plugin Init */
1474
 
  archive_db_done, /* Plugin Deinit */
1475
 
  NULL,                       /* status variables                */
1476
 
  archive_system_variables,   /* system variables                */
1477
 
  NULL                        /* config options                  */
1478
 
}
1479
 
DRIZZLE_DECLARE_PLUGIN_END;
1480