Log in

No account? Create an account

Building a Storage Engine: Updating Data

« previous entry | next entry »
Jun. 7th, 2007 | 02:11 pm

In our last installment, we looked at how to write data into a storage engine, now lets look at updating it.

To update data you need to first know that it exists. To do this MySQL reads the rows that need to be updated, either by index or scan read. Since we have already spoken on scan reads, we are going to look at updates that occur that need to scan the entire table. MySQL will start up a read and do the folllowing:


In the above example, MySQL find four rows in the table. The third row is matched for an update. The engine writer doesn't have to do to the predicate test themselves (aka ... UPDAT E A SET b=5 WHERE a=3, the engine writer doesn't need to worry about matching the a=3). The update_row() code looks like this:

int ha_skeleton::update_row(const byte * old_data, byte * new_data)
   char content_buffer[1024];
   String content(content_buffer, sizeof(content_buffer),
   char utf8_buffer[1024];
   String utf8(utf8_buffer, sizeof(utf8_buffer),


   if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_UPDATE)

   for (Field **field=table->field ; *field ; field++)
     if ((*field)->is_null())
       xml_engine_row_add(engine_clone, (char *)(*field)->field_name, NULL, 0, 1);
       utf8.set(content->ptr(), content->length(), content->charset(),
                &my_charset_utf8_general_ci, &error_count);

       xml_engine_row_add(engine_clone, (char *)(*field)->field_name,
                          content.c_ptr_safe(), content.length(), 1);


The two arguments passed into update_row is the old image, and the new image of the row. The Field objects points to the data in old row. We loop through the new row and push the data into the storage engine. Notice that we differentiate between NULL and no content. While some engines in MySQL don't store NULL as NULL in MySQL, CSV for example, almost all engines store NULL.

Notice that there is no update_end() call. If you require this sort of call it is best to flag that an update occurred and then use the index_end() or rnd_end() methods to do any cleanup required.

Since the xml database only operates in UTF8, notice that we used the internal mysql String class to convert the content to UTF8 before storage. You can use this to convert character sets MySQL supports.

You can find the code for this in Chapter 4 of:

Previous articles in this series:
The previous entries in this series:
Getting The Skeleton to Compile
Reading Data
Writing Data

Link | Leave a comment |

Comments {0}