1 /* -*- c-basic-offset: 2 -*- */
2 /*
3   Copyright(C) 2012-2013 Kouhei Sutou <kou@clear-code.com>
4 
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9 
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14 
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA
18 */
19 
20 #ifndef MRN_MULTIPLE_COLUMN_KEY_CODEC_HPP_
21 #define MRN_MULTIPLE_COLUMN_KEY_CODEC_HPP_
22 
23 #include <mrn_mysql.h>
24 #include <mrn_mysql_compat.h>
25 
26 #include <groonga.h>
27 
28 namespace mrn {
29   class MultipleColumnKeyCodec {
30   public:
31     MultipleColumnKeyCodec(grn_ctx *ctx, THD *thread, KEY *key_info);
32     ~MultipleColumnKeyCodec();
33 
34     int encode(const uchar *mysql_key, uint mysql_key_length,
35                uchar *grn_key, uint *grn_key_length);
36     int decode(const uchar *grn_key, uint grn_key_length,
37                uchar *mysql_key, uint *mysql_key_length);
38     uint size();
39 
40   private:
41     enum DataType {
42       TYPE_UNKNOWN,
43       TYPE_LONG_LONG_NUMBER,
44       TYPE_NUMBER,
45       TYPE_FLOAT,
46       TYPE_DOUBLE,
47       TYPE_DATETIME,
48 #ifdef MRN_HAVE_MYSQL_TYPE_DATETIME2
49       TYPE_DATETIME2,
50 #endif
51       TYPE_BYTE_SEQUENCE,
52       TYPE_BYTE_REVERSE,
53       TYPE_BYTE_BLOB
54     };
55 
56     grn_ctx *ctx_;
57     THD *thread_;
58     KEY *key_info_;
59 
60     void get_key_info(KEY_PART_INFO *key_part,
61                       DataType *data_type, uint *data_size);
62 
63     void encode_number(const uchar *mysql_key,
64                        uint mysql_key_size,
65                        bool is_signed,
66                        uchar *grn_key);
67     void decode_number(const uchar *grn_key,
68                        uint grn_key_size,
69                        bool is_signed,
70                        uchar *mysql_key);
71     void encode_long_long_int(volatile long long int value,
72                               uchar *grn_key);
73     void decode_long_long_int(const uchar *grn_key,
74                               long long int *value);
75     void encode_float(volatile float value,
76                       uint value_size,
77                       uchar *grn_key);
78     void decode_float(const uchar *grn_key,
79                       uint grn_key_size,
80                       uchar *mysql_key);
81     void encode_double(volatile double value,
82                        uint value_size,
83                        uchar *grn_key);
84     void decode_double(const uchar *grn_key,
85                        uint grn_key_size,
86                        uchar *mysql_key);
87     void encode_reverse(const uchar *mysql_key,
88                         uint mysql_key_size,
89                         uchar *grn_key);
90     void decode_reverse(const uchar *grn_key,
91                         uint grn_key_size,
92                         uchar *mysql_key);
93     void encode_blob(const uchar *mysql_key,
94                      uint *mysql_key_size,
95                      Field *field,
96                      uchar *grn_key);
97   };
98 }
99 
100 #endif // MRN_MULTIPLE_COLUMN_KEY_CODEC_HPP_
101