summaryrefslogtreecommitdiffstats
path: root/storage/mroonga/lib/mrn_index_column_name.cpp
blob: 1a19b9d1f01c5f3e06228e08b3a16758f087bc51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* -*- c-basic-offset: 2 -*- */
/*
  Copyright(C) 2011-2013 Kentoku SHIBA
  Copyright(C) 2011-2012 Kouhei Sutou <kou@clear-code.com>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA
*/

#include <mrn_mysql.h>

#include "mrn_index_column_name.hpp"

#define MRN_MIN_INDEX_COLUMN_NAME_LENGTH 65

// for debug
#define MRN_CLASS_NAME "mrn::IndexColumnName"

namespace mrn {
  IndexColumnName::IndexColumnName(const char *table_name,
                                 const char *mysql_column_name)
    : table_name_(table_name),
      mysql_column_name_(mysql_column_name) {
    uchar encoded_mysql_column_name_multibyte[MRN_MAX_KEY_SIZE];
    const uchar *mysql_column_name_multibyte =
      reinterpret_cast<const uchar *>(mysql_column_name_);
    encode(encoded_mysql_column_name_multibyte,
           encoded_mysql_column_name_multibyte + MRN_MAX_KEY_SIZE,
           mysql_column_name_multibyte,
           mysql_column_name_multibyte + strlen(mysql_column_name_));
    snprintf(name_, MRN_MAX_KEY_SIZE,
             "%s-%s", table_name_, encoded_mysql_column_name_multibyte);
    length_ = strlen(name_);
    if (length_ < MRN_MIN_INDEX_COLUMN_NAME_LENGTH) {
      memset(name_ + length_, '-', MRN_MIN_INDEX_COLUMN_NAME_LENGTH - length_);
      length_ = MRN_MIN_INDEX_COLUMN_NAME_LENGTH;
      name_[length_] = '\0';
    }
  }

  const char *IndexColumnName::c_str() {
    return name_;
  }

  size_t IndexColumnName::length() {
    return length_;
  }

  uint IndexColumnName::encode(uchar *encoded_start,
                              uchar *encoded_end,
                              const uchar *mysql_string_start,
                              const uchar *mysql_string_end) {
    MRN_DBUG_ENTER_METHOD();
    my_charset_conv_mb_wc mb_wc = system_charset_info->cset->mb_wc;
    my_charset_conv_wc_mb wc_mb = my_charset_filename.cset->wc_mb;
    DBUG_PRINT("info", ("mroonga: in=%s", mysql_string_start));
    encoded_end--;
    uchar *encoded = encoded_start;
    const uchar *mysql_string = mysql_string_start;
    while (mysql_string < mysql_string_end && encoded < encoded_end) {
      my_wc_t wc;
      int mb_wc_converted_length;
      int wc_mb_converted_length;
      mb_wc_converted_length =
        (*mb_wc)(NULL, &wc, mysql_string, mysql_string_end);
      if (mb_wc_converted_length > 0) {
        wc_mb_converted_length = (*wc_mb)(NULL, wc, encoded, encoded_end);
        if (wc_mb_converted_length <= 0) {
          break;
        }
      } else if (mb_wc_converted_length == MY_CS_ILSEQ) {
        *encoded = *mysql_string;
        mb_wc_converted_length = 1;
        wc_mb_converted_length = 1;
      } else {
        break;
      }
      mysql_string += mb_wc_converted_length;
      encoded += wc_mb_converted_length;
    }
    *encoded = '\0';
    DBUG_PRINT("info", ("mroonga: out=%s", encoded_start));
    DBUG_RETURN(encoded - encoded_start);
  }
}