View Javadoc
1   /*
2    * Copyright 2021 TiKV Project Authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  
18  package org.tikv.common.meta;
19  
20  import com.google.common.collect.ImmutableMap;
21  import java.util.Map;
22  
23  public class Collation {
24    public static final int DEF_COLLATION_CODE = 83;
25    private static final Map<String, Integer> collationMap;
26    private static final Map<Integer, String> collationCodeMap;
27  
28    static {
29      collationMap =
30          ImmutableMap.<String, Integer>builder()
31              .put("big5_chinese_ci", 1)
32              .put("latin2_czech_cs", 2)
33              .put("dec8_swedish_ci", 3)
34              .put("cp850_general_ci", 4)
35              .put("latin1_german1_ci", 5)
36              .put("hp8_english_ci", 6)
37              .put("koi8r_general_ci", 7)
38              .put("latin1_swedish_ci", 8)
39              .put("latin2_general_ci", 9)
40              .put("swe7_swedish_ci", 10)
41              .put("ascii_general_ci", 11)
42              .put("ujis_japanese_ci", 12)
43              .put("sjis_japanese_ci", 13)
44              .put("cp1251_bulgarian_ci", 14)
45              .put("latin1_danish_ci", 15)
46              .put("hebrew_general_ci", 16)
47              .put("tis620_thai_ci", 18)
48              .put("euckr_korean_ci", 19)
49              .put("latin7_estonian_cs", 20)
50              .put("latin2_hungarian_ci", 21)
51              .put("koi8u_general_ci", 22)
52              .put("cp1251_ukrainian_ci", 23)
53              .put("gb2312_chinese_ci", 24)
54              .put("greek_general_ci", 25)
55              .put("cp1250_general_ci", 26)
56              .put("latin2_croatian_ci", 27)
57              .put("gbk_chinese_ci", 28)
58              .put("cp1257_lithuanian_ci", 29)
59              .put("latin5_turkish_ci", 30)
60              .put("latin1_german2_ci", 31)
61              .put("armscii8_general_ci", 32)
62              .put("utf8_general_ci", 33)
63              .put("cp1250_czech_cs", 34)
64              .put("ucs2_general_ci", 35)
65              .put("cp866_general_ci", 36)
66              .put("keybcs2_general_ci", 37)
67              .put("macce_general_ci", 38)
68              .put("macroman_general_ci", 39)
69              .put("cp852_general_ci", 40)
70              .put("latin7_general_ci", 41)
71              .put("latin7_general_cs", 42)
72              .put("macce_bin", 43)
73              .put("cp1250_croatian_ci", 44)
74              .put("utf8mb4_general_ci", 45)
75              .put("utf8mb4_bin", 46)
76              .put("latin1_bin", 47)
77              .put("latin1_general_ci", 48)
78              .put("latin1_general_cs", 49)
79              .put("cp1251_bin", 50)
80              .put("cp1251_general_ci", 51)
81              .put("cp1251_general_cs", 52)
82              .put("macroman_bin", 53)
83              .put("utf16_general_ci", 54)
84              .put("utf16_bin", 55)
85              .put("utf16le_general_ci", 56)
86              .put("cp1256_general_ci", 57)
87              .put("cp1257_bin", 58)
88              .put("cp1257_general_ci", 59)
89              .put("utf32_general_ci", 60)
90              .put("utf32_bin", 61)
91              .put("utf16le_bin", 62)
92              .put("binary", 63)
93              .put("armscii8_bin", 64)
94              .put("ascii_bin", 65)
95              .put("cp1250_bin", 66)
96              .put("cp1256_bin", 67)
97              .put("cp866_bin", 68)
98              .put("dec8_bin", 69)
99              .put("greek_bin", 70)
100             .put("hebrew_bin", 71)
101             .put("hp8_bin", 72)
102             .put("keybcs2_bin", 73)
103             .put("koi8r_bin", 74)
104             .put("koi8u_bin", 75)
105             .put("latin2_bin", 77)
106             .put("latin5_bin", 78)
107             .put("latin7_bin", 79)
108             .put("cp850_bin", 80)
109             .put("cp852_bin", 81)
110             .put("swe7_bin", 82)
111             .put("utf8_bin", 83)
112             .put("big5_bin", 84)
113             .put("euckr_bin", 85)
114             .put("gb2312_bin", 86)
115             .put("gbk_bin", 87)
116             .put("sjis_bin", 88)
117             .put("tis620_bin", 89)
118             .put("ucs2_bin", 90)
119             .put("ujis_bin", 91)
120             .put("geostd8_general_ci", 92)
121             .put("geostd8_bin", 93)
122             .put("latin1_spanish_ci", 94)
123             .put("cp932_japanese_ci", 95)
124             .put("cp932_bin", 96)
125             .put("eucjpms_japanese_ci", 97)
126             .put("eucjpms_bin", 98)
127             .put("cp1250_polish_ci", 99)
128             .put("utf16_unicode_ci", 101)
129             .put("utf16_icelandic_ci", 102)
130             .put("utf16_latvian_ci", 103)
131             .put("utf16_romanian_ci", 104)
132             .put("utf16_slovenian_ci", 105)
133             .put("utf16_polish_ci", 106)
134             .put("utf16_estonian_ci", 107)
135             .put("utf16_spanish_ci", 108)
136             .put("utf16_swedish_ci", 109)
137             .put("utf16_turkish_ci", 110)
138             .put("utf16_czech_ci", 111)
139             .put("utf16_danish_ci", 112)
140             .put("utf16_lithuanian_ci", 113)
141             .put("utf16_slovak_ci", 114)
142             .put("utf16_spanish2_ci", 115)
143             .put("utf16_roman_ci", 116)
144             .put("utf16_persian_ci", 117)
145             .put("utf16_esperanto_ci", 118)
146             .put("utf16_hungarian_ci", 119)
147             .put("utf16_sinhala_ci", 120)
148             .put("utf16_german2_ci", 121)
149             .put("utf16_croatian_ci", 122)
150             .put("utf16_unicode_520_ci", 123)
151             .put("utf16_vietnamese_ci", 124)
152             .put("ucs2_unicode_ci", 128)
153             .put("ucs2_icelandic_ci", 129)
154             .put("ucs2_latvian_ci", 130)
155             .put("ucs2_romanian_ci", 131)
156             .put("ucs2_slovenian_ci", 132)
157             .put("ucs2_polish_ci", 133)
158             .put("ucs2_estonian_ci", 134)
159             .put("ucs2_spanish_ci", 135)
160             .put("ucs2_swedish_ci", 136)
161             .put("ucs2_turkish_ci", 137)
162             .put("ucs2_czech_ci", 138)
163             .put("ucs2_danish_ci", 139)
164             .put("ucs2_lithuanian_ci", 140)
165             .put("ucs2_slovak_ci", 141)
166             .put("ucs2_spanish2_ci", 142)
167             .put("ucs2_roman_ci", 143)
168             .put("ucs2_persian_ci", 144)
169             .put("ucs2_esperanto_ci", 145)
170             .put("ucs2_hungarian_ci", 146)
171             .put("ucs2_sinhala_ci", 147)
172             .put("ucs2_german2_ci", 148)
173             .put("ucs2_croatian_ci", 149)
174             .put("ucs2_unicode_520_ci", 150)
175             .put("ucs2_vietnamese_ci", 151)
176             .put("ucs2_general_mysql500_ci", 159)
177             .put("utf32_unicode_ci", 160)
178             .put("utf32_icelandic_ci", 161)
179             .put("utf32_latvian_ci", 162)
180             .put("utf32_romanian_ci", 163)
181             .put("utf32_slovenian_ci", 164)
182             .put("utf32_polish_ci", 165)
183             .put("utf32_estonian_ci", 166)
184             .put("utf32_spanish_ci", 167)
185             .put("utf32_swedish_ci", 168)
186             .put("utf32_turkish_ci", 169)
187             .put("utf32_czech_ci", 170)
188             .put("utf32_danish_ci", 171)
189             .put("utf32_lithuanian_ci", 172)
190             .put("utf32_slovak_ci", 173)
191             .put("utf32_spanish2_ci", 174)
192             .put("utf32_roman_ci", 175)
193             .put("utf32_persian_ci", 176)
194             .put("utf32_esperanto_ci", 177)
195             .put("utf32_hungarian_ci", 178)
196             .put("utf32_sinhala_ci", 179)
197             .put("utf32_german2_ci", 180)
198             .put("utf32_croatian_ci", 181)
199             .put("utf32_unicode_520_ci", 182)
200             .put("utf32_vietnamese_ci", 183)
201             .put("utf8_unicode_ci", 192)
202             .put("utf8_icelandic_ci", 193)
203             .put("utf8_latvian_ci", 194)
204             .put("utf8_romanian_ci", 195)
205             .put("utf8_slovenian_ci", 196)
206             .put("utf8_polish_ci", 197)
207             .put("utf8_estonian_ci", 198)
208             .put("utf8_spanish_ci", 199)
209             .put("utf8_swedish_ci", 200)
210             .put("utf8_turkish_ci", 201)
211             .put("utf8_czech_ci", 202)
212             .put("utf8_danish_ci", 203)
213             .put("utf8_lithuanian_ci", 204)
214             .put("utf8_slovak_ci", 205)
215             .put("utf8_spanish2_ci", 206)
216             .put("utf8_roman_ci", 207)
217             .put("utf8_persian_ci", 208)
218             .put("utf8_esperanto_ci", 209)
219             .put("utf8_hungarian_ci", 210)
220             .put("utf8_sinhala_ci", 211)
221             .put("utf8_german2_ci", 212)
222             .put("utf8_croatian_ci", 213)
223             .put("utf8_unicode_520_ci", 214)
224             .put("utf8_vietnamese_ci", 215)
225             .put("utf8_general_mysql500_ci", 223)
226             .put("utf8mb4_unicode_ci", 224)
227             .put("utf8mb4_icelandic_ci", 225)
228             .put("utf8mb4_latvian_ci", 226)
229             .put("utf8mb4_romanian_ci", 227)
230             .put("utf8mb4_slovenian_ci", 228)
231             .put("utf8mb4_polish_ci", 229)
232             .put("utf8mb4_estonian_ci", 230)
233             .put("utf8mb4_spanish_ci", 231)
234             .put("utf8mb4_swedish_ci", 232)
235             .put("utf8mb4_turkish_ci", 233)
236             .put("utf8mb4_czech_ci", 234)
237             .put("utf8mb4_danish_ci", 235)
238             .put("utf8mb4_lithuanian_ci", 236)
239             .put("utf8mb4_slovak_ci", 237)
240             .put("utf8mb4_spanish2_ci", 238)
241             .put("utf8mb4_roman_ci", 239)
242             .put("utf8mb4_persian_ci", 240)
243             .put("utf8mb4_esperanto_ci", 241)
244             .put("utf8mb4_hungarian_ci", 242)
245             .put("utf8mb4_sinhala_ci", 243)
246             .put("utf8mb4_german2_ci", 244)
247             .put("utf8mb4_croatian_ci", 245)
248             .put("utf8mb4_unicode_520_ci", 246)
249             .put("utf8mb4_vietnamese_ci", 247)
250             .build();
251 
252     ImmutableMap.Builder<Integer, String> builder = ImmutableMap.builder();
253     for (String collation : collationMap.keySet()) {
254       builder.put(collationMap.get(collation), collation);
255     }
256     collationCodeMap = builder.build();
257   }
258 
259   public static int translate(String collation) {
260     Integer code = collationMap.get(collation);
261     if (code == null) {
262       return DEF_COLLATION_CODE;
263     }
264     return code;
265   }
266 
267   public static String translate(int code) {
268     String collation = collationCodeMap.get(code);
269     if (collation == null) {
270       return "";
271     }
272     return collation;
273   }
274 }