Commit 6f9147f3 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

Makefile-5.1.am

0 → 100755
+44 −0
Changes for Makefile-5.1.am: 44 added lines, 0 removed lines.
Original line number Diff line number Diff line
# Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#called from the top level Makefile

MYSQLDATAdir =          $(localstatedir)
MYSQLSHAREdir =         $(pkgdatadir)
MYSQLBASEdir=           $(prefix)
MYSQLLIBdir=            $(pkglibdir)
INCLUDES =              -I$(top_srcdir)/include -I$(top_builddir)/include \
			-I$(top_srcdir)/regex \
			-I$(top_srcdir)/sql \
                        -I$(srcdir)
WRAPLIBS=

LDADD =

DEFS =                  @DEFS@

noinst_HEADERS =	ha_csv.h


EXTRA_LIBRARIES =	liboreillycsv.a
noinst_LIBRARIES =	@plugin_oreilly_csv_static_target@
liboreillycsv_a_CXXFLAGS =	$(AM_CFLAGS)
liboreillycsv_a_CFLAGS =	$(AM_CFLAGS)
liboreillycsv_a_SOURCES=	ha_csv.cc


# Don't update the files from bitkeeper
%::SCCS/s.%

README.md

0 → 100644
+15 −0
Changes for README.md: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
## Example files for the title:  
  
# Understanding MySQL Internals, by Sasha Pachev
  
[![Understanding MySQL Internals, by Sasha Pachev](http://akamaicovers.oreilly.com/images/9780596009571/cat.gif)](https://www.safaribooksonline.com/library/view/title/0596009577//)
  
The following applies to example files from material published by O’Reilly Media, Inc. Content from other publishers may include different rules of usage. Please refer to any additional usage rights explained in the actual example files or refer to the publisher’s website.
  
O'Reilly books are here to help you get your job done. In general, you may use the code in O'Reilly books in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from our books does not require permission. Answering a question by citing our books and quoting example code does not require permission. On the other hand, selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Incorporating a significant amount of example code from our books into your product's documentation does require permission.
  
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN.
  
If you think your use of code examples falls outside fair use or the permission given here, feel free to contact us at <permissions@oreilly.com>.
  
Please note that the examples are not production code and have not been carefully testing. They are provided "as-is" and come with no warranty of any kind.

examples.tar.gz

0 → 100755
+7.11 KiB

File added.

No diff preview for this file type.

ha_csv-4.1.cc

0 → 100755
+437 −0
Changes for ha_csv-4.1.cc: 437 added lines, 0 removed lines.
Original line number Diff line number Diff line
/* Tell GCC we are in the impelmentation source file. */
#ifdef __GNUC__
#pragma implementation				
#endif

/* Main include file in the sql/ directory. */
#include "mysql_priv.h"

/* Our own header file for this storage engine. */
#include "ha_csv.h"

/* Used by ha_csv::bas_ext(). */
static const char* csv_ext[]= {".csv",0};

/* Called when the table is opened. */
int ha_csv::open(const char *name, int mode, uint test_if_locked)
{
  /* Initialize the lock structures used by the lock manager. */
  thr_lock_init(&thr_lock);
  thr_lock_data_init(&thr_lock,&lock,NULL);
  
  /* Allocate memory for the data file descriptor. */
  file= (CSV_INFO*)my_malloc(sizeof(CSV_INFO),MYF(MY_WME));
  if (!file)
    return 1;
  
  /* Translate the name of the name into the data file name. */   
  fn_format(file->fname, name, "", ".csv", 
     MY_REPLACE_EXT|MY_UNPACK_FILENAME);
  
  /* 
     Open the file, and save the file handle id in the data
     file descriptor structure.  
   */      
  if ((file->fd = my_open(file->fname,mode,MYF(0))) < 0) 
  {
    int error = my_errno;
    close();
    return error;
  } 
  
  /* Read operations start from the beginning of the file. */
  pos = 0;
  return 0;
}

/* Called when the table is closed. */
int ha_csv::close(void)
{
  /* 
     Clean up the lock structures, close the file handle, and 
     deallocate the data file descriptor memory. 
   */
  thr_lock_delete(&thr_lock);
  if (file)
  {
    if (file->fd >= 0)
      my_close(file->fd, MYF(0));
    my_free((gptr)file,MYF(0));
    file = 0;  
  }  
  return 0;
}

/* 
   Read the line from the current position into the 
   caller-provided record buffer. 
 */
int ha_csv::fetch_line(byte* buf)
{
  /* 
     Keep track of the current offset in the file as we read
     portions of the line into a buffer.
     Start at the current read cursor position.
   */  
  my_off_t cur_pos = pos;
  
  /* 
     We will use this to iterate through the array of 
     table field pointers to store the parsed data in the right
     place and the right format.
   */  
  Field** field = table->field;
  
  /* 
    Used in parsing to remember the previous character. The impossible
    value of 256 indicates that the last character either did not exist
    (we are on the first one), or its value is irrelevant.
  */  
  int last_c = 256;
  
  /* Set to 1 if we are inside a quoted string. */
  int in_quote = 0;
  
  /* How many bytes we have seen so far in this line. */
  uint bytes_parsed = 0;
  
  /* Loop breaker flag. */
  int line_read_done = 0;
  
  /* Truncate the field value buffer. */
  field_buf.length(0);
    
  /* Attempt to read a whole line. */
  for (;!line_read_done;)
  {
    /* Read a block into a local buffer and deal with errors. */
    char buf[CSV_READ_BLOCK_SIZE];
    uint bytes_read = my_pread(file->fd,buf,sizeof(buf),cur_pos,MYF(MY_WME));
    if (bytes_read == MY_FILE_ERROR) 
      return HA_ERR_END_OF_FILE;
    if (!bytes_read)
      return HA_ERR_END_OF_FILE;
    
    /* 
      If we reach this point, the read was successful. Start parsing the
      data we have read.
    */
    char* p = buf;
    char* buf_end = buf + bytes_read;

    /* For each byte in the buffer. */        
    for (;p < buf_end;)
    {
      char c = *p;
      int end_of_line = 0;
      int end_of_field = 0;
      int char_escaped = 0;
      
      switch (c)
      {
        /* 
          A double-quote marks the start or the end of a quoted string
          unless it has been escaped.
        */        
        case '"':
          if (last_c == '"' || last_c == '\\')
          {
            field_buf.append(c);
            char_escaped = 1;
            
            /* 
              When we see the first quote, in_quote will get flipped.
              A subsequent quote, however, tells us we are still inside the
              quoted string.
            */  
            if (last_c == '"')
              in_quote = 1;
          }  
          else 
            in_quote = !in_quote;
          break;
        /*
          Treat the backward slash as an escape character.
        */  
        case '\\':
          if (last_c == '\\')
          {
             field_buf.append(c);
             char_escaped = 1;
          }   
          break;       
          
        /*
          Set the termination flags on end-of-line unless it is quoted. 
        */  
        case '\r':
        case '\n':
          if (in_quote)
          {
            field_buf.append(c);
          }        
          else
          {
            end_of_line = 1; 
            end_of_field = 1;
          }  
          break;
        
        /* Comma signifies end-of-field unless quoted. */      
        case ',': 
          if (in_quote)
          {
            field_buf.append(c);
          }
          else
            end_of_field = 1;
          break;
        
        /* 
          Regular charcters just get appended to the field 
          value buffer. 
        */    
        default: 
          field_buf.append(c);
          break;     
      }
      
      /*
        If at the end a field, and a matching field exists in the table
        (it may not if the CSV file has extra fields), transfer the field
        value buffer contents into the corresponding Field object. This 
        actually takes care of initializing the correct parts of the buffer
        argument passed to us by the caller. The internal convention of the
        optimizer dictates that the buffer pointers of the Field objects
        must already be set up to point at the correct areas of the buffer
        argument prior to calls to the data-retrieval methods of the handler
        class.
      */
      if (end_of_field && *field) 
      {      
         (*field)->store(field_buf.ptr(),field_buf.length(),
            system_charset_info);
         field++;
         field_buf.length(0);  
      }  
      
      /* 
        Special case - a character that was escaped itself should not be
        regared as an escape character.
      */  
      if (char_escaped)
        last_c = 256;
      else  
        last_c = c;
      p++;
      
      /* Prepare for loop exit on end-of-line. */
      if (end_of_line)
      {
        if (c == '\r')
          p++;
        line_read_done = 1;
        in_quote = 0;
        break;  
      }
    }             
    
    /* Block read/parse cycle is complete - update the counters. */
    bytes_parsed += (p - buf);
    cur_pos += bytes_read;
  }
  
  /* 
    Now we are done with the line read/parsing. We still have a number
    of small tasks left to complete the job.
  */
  
  /* Initialize the NULL indicator flags in the record. */
  memset(buf,0,table->null_bytes); 
  
  /* 
    The parsed line may not have had the values of all of the fields.
    Set the remaining fields to their default values.
   */ 
  for (;*field;field++)
  {
    (*field)->set_default();
  }
  
  /* Move the cursor to the next record. */ 
  pos += bytes_parsed;
  
  /* Report success. */
  return 0;
}

/* Called once for each record during a sequential table scan. */
int ha_csv::rnd_next(byte *buf)
{
  /* 
    Increment the global statistics counter displayed by SHOW STATUS
    under Handler_read_rnd_next.
  */  
  statistic_increment(ha_read_rnd_next_count,&LOCK_status);
  
  /* fetch_line() does the actual work. */
  int error = fetch_line(buf);
  
  /* 
    On success, update our estimate for the total number of records in the
    table.
  */  
  if (!error)
    records++;

  /* Return whatever code we got from fetch_line() to the caller. */      
  return error;
}

/* 
  Positions the scan cursor at the position specified by set_pos and
  read the record at that position. Used in GROUP BY and ORDER BY 
  optimization when the "filesort" technique is applied.
*/  
int ha_csv::rnd_pos(byte * buf, byte *set_pos)
{
  statistic_increment(ha_read_rnd_count,&LOCK_status);
  pos = ha_get_ptr(set_pos,ref_length);
  return fetch_line(buf); 
}

/* 
  Stores the "position" reference to the current record in the ref
  variable. At this point, this method is called in situation that are
  impossible for this storage engine, but this could change in the future.
 */
void ha_csv::position(const byte *record)
{
  ha_store_ptr(ref,ref_length,pos);
}

/* Updates the statistical variables in the handler object */
void ha_csv::info(uint flags)
{
  /* 
    The optimizer must never think that the table has fewer than
    two records unless this is indeed the case. Reporting a smaller number
    makes the optimizer assume it needs to read no more than one record from
    the table. Our storage engine does always know the number of records, 
    and in many cases cannot even make a good guess. To be safe and to keep
    things simple, we always report that we have at least 2 records.
  */  
  if (records < 2)
    records = 2; 
 
 /*
   The rest of the variables merely appear in SHOW TABLE STATUS output and
   do not affect the optimizer. For the purpose of this example they can
   be set to 0.
  */      
    
  deleted = 0;
  errkey = 0;
  mean_rec_length = 0;
  data_file_length = 0;
  index_file_length = 0;
  max_data_file_length = 0;
  delete_length = 0;
  if (flags & HA_STATUS_AUTO)
    auto_increment_value = 1;
}

/* 
  This is essentially a callback for the table lock manager saying:
  "I am locking this table internally, please take care of the things
  specific to the storage engine that need to be done in conjunction with
  this lock." MyISAM needed to lock the files in this case in some
  configurations, thus the name - external_lock(). In our case, there is
  nothing to do - we just report success.
*/  
int ha_csv::external_lock(THD *thd, int lock_type)
{
  return 0;
}

/* 
   Returns an array of all possible file extensions used by the storage
   engine.
*/   
const char ** ha_csv::bas_ext() const
{
  return csv_ext;
}

/* 
   We need this function to report that the records member cannot be used
   to optimize SELECT COUNT(*) without a WHERE clause. Note that the value
   records actually shows the correct count after a full scan, and can 
   indeed be used to optimize SELECT COUNT(*). This is left as an exercise
   for the reader.
 */  
ulong ha_csv::table_flags(void) const
{
  return HA_NOT_EXACT_COUNT;
}

/* 
  Our storage engine does not support keys, so we report no special
  key capabilities.
*/  
ulong ha_csv::index_flags(uint idx, uint part, bool all_parts) const
{
  return 0;
}

/* Nothing special to do on the storage engine level when the table 
   is created. The .CSV file is placed externally into the data directory.
*/   
int ha_csv::create(const char *name, TABLE *form, HA_CREATE_INFO *info)
{
  return 0;
}

/* This method is needed for the table lock manager to work right. */
THR_LOCK_DATA ** ha_csv::store_lock(THD *thd,
            THR_LOCK_DATA **to,
            enum thr_lock_type lock_type)
{
  if (lock_type != TL_IGNORE && lock.type == TL_UNLOCK)
    lock.type=lock_type;

  *to++ = &lock;
  return to;
}          

#include "sql_select.h"

void dump_join_tab(JOIN_TAB* jt)
{
  fprintf(stderr,"table=%s\n", jt->table->real_name);
  fprintf(stderr,"access method = %s\n", join_type_str[jt->type]);  
}

void dump_join(JOIN* join)
{
  uint i;

  for (i = 0; i < join->tables; i++)
  {
    dump_join_tab(join->join_tab + i);
  }
}

void dump_explain()
{
  THD* thd = current_thd;
  LEX* lex = thd->lex;
  Item *where = lex->select_lex.where;
  JOIN* join = lex->select_lex.join;
  
  fprintf(stderr, "query = %s\n", thd->query);  
  if (where)
    fprintf(stderr,"WHERE clause is present\n");
  if (join)
    dump_join(join);
}

ha_csv-4.1.h

0 → 100755
+87 −0
Changes for ha_csv-4.1.h: 87 added lines, 0 removed lines.
Original line number Diff line number Diff line
/* Tell GCC  this is a header file */
#ifdef __GNUCC__
#pragma interface
#endif

/* The CSV lines could be big. Read them in blocks of 512. */
#define CSV_READ_BLOCK_SIZE   512

/* 
  Following the tradition of other storage engines, we put all of the 
  low-level information under a separate structure.
 */ 
struct CSV_INFO
{
  char fname[FN_REFLEN+1];
  int fd;
} ;

void dump_explain();

/* Now define the handler class. */
class ha_csv: public handler
{
protected:
  /* Low-level storage engine data. */
  CSV_INFO* file;
  
  /* Lock structures for the table lock manager. */
  THR_LOCK_DATA lock;
  THR_LOCK thr_lock;
  
  /* Table scan cursor.*/
  my_off_t pos;
  
  /* Buffer for reading CSV line blocks. */
  char read_buf[CSV_READ_BLOCK_SIZE];
  
  /* Buffer for parsing the field values. */
  String field_buf;
  
  /* See the comment in the implementation file. */
  int fetch_line(byte* buf);
  
  /* Initializes the storage engine object for a sequential scan. */
  int rnd_init(bool scan) 
  {
    pos = 0; 
    records = 0;
    dump_explain();
    return 0;
  }
  int index_init(uint idx)
  {
    active_index=idx;
    dump_explain();
    return 0;
  }
public:
 /* Constructor. */
 ha_csv(TABLE* table): handler(table), file(0) {}
 
 /* Destructor. */
 ~ha_csv() {}
 
 /* See the comments in the implementation file for the methods below. */
 int open(const char *name, int mode, uint test_if_locked);
 int close(void);
 int rnd_next(byte *buf);
 int rnd_pos(byte * buf, byte *pos);
 void position(const byte *record);
 void info(uint flags);
 int external_lock(THD *thd, int lock_type);
 const char **bas_ext() const;
 ulong table_flags(void) const;
 ulong index_flags(uint idx, uint part, bool all_parts) const;
 int create(const char *name, TABLE *form, HA_CREATE_INFO *info);
 THR_LOCK_DATA **store_lock(THD *thd,
				     THR_LOCK_DATA **to,
				     enum thr_lock_type lock_type);
 
 /* 
   Returns the storage engine type string used in the output of
   SHOW TABLE STATUS
 */                          
 const char *table_type() const { return "OREILLY_CSV"; } 
  
};
Loading