[Groonga-mysql-commit] mroonga/mroonga [master] [wrapper][update] extrace groonga related method.

Back to archive index

null+****@clear***** null+****@clear*****
2011年 6月 20日 (月) 16:54:42 JST


Kouhei Sutou	2011-06-20 07:54:42 +0000 (Mon, 20 Jun 2011)

  New Revision: cf3cfb56d1cd3cf6cdce591bd60b6f8eb278e9b1

  Log:
    [wrapper][update] extrace groonga related method.

  Modified files:
    ha_mroonga.cc
    ha_mroonga.h

  Modified: ha_mroonga.cc (+71 -45)
===================================================================
--- ha_mroonga.cc    2011-06-20 07:27:38 +0000 (d4498bd)
+++ ha_mroonga.cc    2011-06-20 07:54:42 +0000 (57b86c7)
@@ -2615,7 +2615,7 @@ int ha_mroonga::wrapper_write_row_index(uchar *buf)
   if (record_id == GRN_ID_NIL) {
     char error_message[MRN_MESSAGE_BUFFER_SIZE];
     snprintf(error_message, MRN_MESSAGE_BUFFER_SIZE,
-             "failed to add a new row into groonga: key=<%.*s>",
+             "failed to add a new record into groonga: key=<%.*s>",
              GRN_TEXT_LEN(&key), GRN_TEXT_VALUE(&key));
     error = ER_ERROR_ON_WRITE;
     my_message(error, error_message, MYF(0));
@@ -2830,67 +2830,93 @@ int ha_mroonga::wrapper_update_row(const uchar *old_data, uchar *new_data)
   MRN_SET_BASE_SHARE_KEY(share, table->s);
   MRN_SET_BASE_TABLE_KEY(this, table);
 
-  if (!error) {
-    my_ptrdiff_t ptr_diff = PTR_BYTE_DIFF(old_data, table->record[0]);
+  if (!error && wrapper_have_fulltext_index()) {
+    error = wrapper_update_row_index(old_data, new_data);
+  }
 
-    grn_obj key;
-    GRN_TEXT_INIT(&key, 0);
+  DBUG_RETURN(error);
+}
 
-    grn_bulk_space(ctx, &key, table->key_info->key_length);
-    key_copy((uchar *)(GRN_TEXT_VALUE(&key)),
-             (uchar *)old_data,
-             &(table->key_info[table_share->primary_key]),
-             table->key_info[table_share->primary_key].key_length);
+int ha_mroonga::wrapper_update_row_index(const uchar *old_data, uchar *new_data)
+{
+  MRN_DBUG_ENTER_METHOD();
 
-    grn_id record_id;
-    record_id = grn_table_get(ctx, grn_table,
-                              GRN_TEXT_VALUE(&key), GRN_TEXT_LEN(&key));
-    // TODO: check record_id == GRN_ID_NIL
-    grn_obj_unlink(ctx, &key);
+  int error = 0;
 
-    grn_obj old_value, new_value;
-    GRN_TEXT_INIT(&old_value, 0);
-    GRN_TEXT_INIT(&new_value, 0);
+  grn_obj key;
+  GRN_TEXT_INIT(&key, 0);
+
+  grn_bulk_space(ctx, &key, table->key_info->key_length);
+  key_copy((uchar *)(GRN_TEXT_VALUE(&key)),
+           (uchar *)old_data,
+           &(table->key_info[table_share->primary_key]),
+           table->key_info[table_share->primary_key].key_length);
+
+  grn_id record_id;
+  record_id = grn_table_get(ctx, grn_table,
+                            GRN_TEXT_VALUE(&key), GRN_TEXT_LEN(&key));
+  if (record_id == GRN_ID_NIL) {
+    char error_message[MRN_MESSAGE_BUFFER_SIZE];
+    snprintf(error_message, MRN_MESSAGE_BUFFER_SIZE,
+             "failed to get record ID for updating from groonga: key=<%.*s>",
+             GRN_TEXT_LEN(&key), GRN_TEXT_VALUE(&key));
+    error = ER_ERROR_ON_WRITE;
+    my_message(error, error_message, MYF(0));
+  }
+  grn_obj_unlink(ctx, &key);
+  if (error) {
+    DBUG_RETURN(error);
+  }
+
+  grn_obj old_value, new_value;
+  GRN_TEXT_INIT(&old_value, 0);
+  GRN_TEXT_INIT(&new_value, 0);
+
+  my_ptrdiff_t ptr_diff = PTR_BYTE_DIFF(old_data, table->record[0]);
 
 #ifndef DBUG_OFF
-    my_bitmap_map *tmp_map = dbug_tmp_use_all_columns(table, table->read_set);
+  my_bitmap_map *tmp_map = dbug_tmp_use_all_columns(table, table->read_set);
 #endif
-    uint i;
-    uint n_keys = table->s->keys;
-    for (i = 0; i < n_keys; i++) {
-      grn_rc rc;
-      KEY key_info = table->key_info[i];
+  uint i;
+  uint n_keys = table->s->keys;
+  for (i = 0; i < n_keys; i++) {
+    grn_rc rc;
+    KEY key_info = table->key_info[i];
 
-      if (key_info.algorithm != HA_KEY_ALG_FULLTEXT) {
-        continue;
-      }
+    if (key_info.algorithm != HA_KEY_ALG_FULLTEXT) {
+      continue;
+    }
 
-      grn_obj *index_column = grn_index_columns[i];
+    grn_obj *index_column = grn_index_columns[i];
 
-      uint j;
-      for (j = 0; j < key_info.key_parts; j++) {
-        Field *field = key_info.key_part[j].field;
-        const char *column_name = field->field_name;
+    uint j;
+    for (j = 0; j < key_info.key_parts; j++) {
+      Field *field = key_info.key_part[j].field;
+      const char *column_name = field->field_name;
 
-        int new_column_size;
-        mrn_set_buf(ctx, field, &new_value, &new_column_size);
+      int new_column_size;
+      mrn_set_buf(ctx, field, &new_value, &new_column_size);
 
-        field->move_field_offset(ptr_diff);
-        int old_column_size;
-        mrn_set_buf(ctx, field, &old_value, &old_column_size);
-        field->move_field_offset(-ptr_diff);
+      field->move_field_offset(ptr_diff);
+      int old_column_size;
+      mrn_set_buf(ctx, field, &old_value, &old_column_size);
+      field->move_field_offset(-ptr_diff);
 
-        rc = grn_column_index_update(ctx, index_column, record_id, 1,
-                                     &old_value, &new_value);
-        // TODO: check rc;
+      rc = grn_column_index_update(ctx, index_column, record_id, 1,
+                                   &old_value, &new_value);
+      if (rc) {
+        error = ER_ERROR_ON_WRITE;
+        my_message(error, ctx->errbuf, MYF(0));
+        goto err;
       }
     }
+  }
+err:
 #ifndef DBUG_OFF
-    dbug_tmp_restore_column_map(table->read_set, tmp_map);
+  dbug_tmp_restore_column_map(table->read_set, tmp_map);
 #endif
-    grn_obj_unlink(ctx, &old_value);
-    grn_obj_unlink(ctx, &new_value);
-  }
+  grn_obj_unlink(ctx, &old_value);
+  grn_obj_unlink(ctx, &new_value);
 
   DBUG_RETURN(error);
 }

  Modified: ha_mroonga.h (+1 -0)
===================================================================
--- ha_mroonga.h    2011-06-20 07:27:38 +0000 (3eea1e1)
+++ ha_mroonga.h    2011-06-20 07:54:42 +0000 (173abbe)
@@ -267,6 +267,7 @@ private:
   int wrapper_write_row_index(uchar *buf);
   int default_write_row(uchar *buf);
   int wrapper_update_row(const uchar *old_data, uchar *new_data);
+  int wrapper_update_row_index(const uchar *old_data, uchar *new_data);
   int default_update_row(const uchar *old_data, uchar *new_data);
   int wrapper_delete_row(const uchar *buf);
   int default_delete_row(const uchar *buf);




Groonga-mysql-commit メーリングリストの案内
Back to archive index