MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
QueryMultiColumnIndexInTest.java
1 /*
2 Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 package testsuite.clusterj;
19 
20 import com.mysql.clusterj.ClusterJUserException;
21 
22 import java.util.Arrays;
23 
24 import testsuite.clusterj.model.LongIntStringIndex;
25 
33  /*
34 drop table if exists longintstringix;
35 create table longintstringix (
36  id int(11) not null,
37  longix bigint(20) not null,
38  stringix varchar(10) not null,
39  intix int(11) not null,
40  stringvalue varchar(10) default null,
41  PRIMARY KEY (id),
42  KEY idx_long_int_string (longix, intix, stringix)
43 ) ENGINE=ndbcluster DEFAULT CHARSET=latin1;
44 
45  */
46 
47  @Override
48  public Class<?> getInstanceType() {
49  return LongIntStringIndex.class;
50  }
51 
52  protected int PK_MODULUS = 3;
53  protected long PRETTY_BIG_NUMBER = 1000000000000000L;
54 
55  @Override
56  protected boolean getCleanupAfterTest() {
57  return true;
58  }
59 
60  public void testInAndBetween() {
61  inAndBetweenQuery("longix", new Object[] {1000000000000000L, 0L}, "intix", 1, 2, "idx_long_int_string", 3, 4, 5, 6, 7, 8);
62  inAndBetweenQuery("longix", Arrays.asList(new Object[] {1000000000000000L, 0L}), "stringix", "1", "4", "idx_long_int_string", 1, 2, 4, 5, 7, 8);
63  failOnError();
64  }
65 
66  public void testBetweenAndIn() {
67  betweenAndInQuery("longix", 0L, 3000000000000000L, "intix", new Object[] {2, 0}, "idx_long_int_string", 0, 1, 2, 6, 7, 8, 9);
68  betweenAndInQuery("longix", 0L, 1000000000000000L, "intix", Arrays.asList(new Object[] {2, 1}), "idx_long_int_string", 3, 4, 5, 6, 7, 8);
69  failOnError();
70  }
71 
72  public void testPartialBoundsAndEqual() {
73  greaterThanAnd1ExtraQuery("longix", 0, "intix", extraEqualPredicateProvider, 0, "idx_long_int_string", 9);
74  greaterEqualAnd1ExtraQuery("longix", 0, "intix", extraEqualPredicateProvider, 0, "idx_long_int_string", 0, 1, 2, 9);
75  lessThanAnd1ExtraQuery("longix", 1000000000000000L, "intix", extraEqualPredicateProvider, 0, "idx_long_int_string", 0, 1, 2);
76  lessEqualAnd1ExtraQuery("longix", 1000000000000000L, "intix", extraEqualPredicateProvider, 0, "idx_long_int_string", 0, 1, 2, 9);
77  failOnError();
78  }
79 
80  public void testGapBoundsAndEqual() {
81  greaterThanAnd1ExtraQuery("longix", 0, "stringix", extraEqualPredicateProvider, "0", "idx_long_int_string", 9);
82  greaterEqualAnd1ExtraQuery("longix", 0, "stringix", extraEqualPredicateProvider, "0", "idx_long_int_string", 0, 3, 6, 9);
83  lessThanAnd1ExtraQuery("longix", 1000000000000000L, "stringix", extraEqualPredicateProvider, "0", "idx_long_int_string", 0, 3, 6);
84  lessEqualAnd1ExtraQuery("longix", 1000000000000000L, "stringix", extraEqualPredicateProvider, "0", "idx_long_int_string", 0, 3, 6, 9);
85  failOnError();
86  }
87 
88  public void testNegativeInParameter() {
89  int keys[] = new int[0];
90  try {
91  inQuery("id", keys, "PRIMARY", keys);
92  error("Query with ''in'' parameter of int[] type should fail.");
93  } catch (ClusterJUserException e) {
94  if (getDebug()) {
95  e.printStackTrace();
96  }
97  String message = e.getMessage();
98  if (!message.contains("id")) {
99  error("Query with ''in'' parameter of int[] type should fail.");
100  }
101  }
102  failOnError();
103  }
104 
105  public void testPrettyBigIn() {
106  int arraySize = 4096;
107  Integer[] keys = new Integer[arraySize];
108  for (int i = 0; i < arraySize; ++i) {
109  keys[i] = i;
110  }
111  int[] expectedKeys = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
112  inQuery("id", keys, "PRIMARY", expectedKeys);
113  failOnError();
114  }
115 
116  public void testNegativeInTooBig() {
117  int arraySize = 4097;
118  Integer[] keys = new Integer[arraySize];
119  for (int i = 0; i < arraySize; ++i) {
120  keys[i] = i;
121  }
122  int[] expectedKeys = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
123  try {
124  inQuery("id", keys, "PRIMARY", expectedKeys);
125  error("Query with more than 4096 elements should fail.");
126  } catch (ClusterJUserException e) {
127  if (getDebug()) {
128  e.printStackTrace();
129  }
130  String message = e.getMessage();
131  if (!message.contains("4097")) {
132  error("Query with more than 4096 elements should fail.");
133  }
134  }
135  failOnError();
136  }
137 
143  protected void createInstances(int number) {
144  for (int i = 0; i < number; ++i) {
145  LongIntStringIndex instance = createInstance(i);
146  //System.out.println(toString(instance));
147  instances.add(instance);
148  }
149  }
150 
156  LongIntStringIndex instance = session.newInstance(LongIntStringIndex.class);
157  instance.setId(index);
158  instance.setLongix(getPK1(index));
159  instance.setIntix(getPK2(index));
160  instance.setStringix(getPK3(index));
161  instance.setStringvalue(getValue(index));
162  return instance;
163  }
164 
165  protected long getPK1(int index) {
166  return PRETTY_BIG_NUMBER * ((index / PK_MODULUS / PK_MODULUS) % PK_MODULUS);
167  }
168 
169  protected int getPK2(int index) {
170  return ((index / PK_MODULUS) % PK_MODULUS);
171  }
172 
173  protected String getPK3(int index) {
174  return "" + (index % PK_MODULUS);
175  }
176 
177  protected String getValue(int index) {
178  return "Value " + index;
179  }
180 
181  protected String toString(LongIntStringIndex instance) {
182  StringBuffer result = new StringBuffer();
183  result.append("LongIntStringIndex[");
184  result.append(instance.getId());
185  result.append("]: ");
186  result.append(instance.getLongix());
187  result.append(", ");
188  result.append(instance.getIntix());
189  result.append(", \"");
190  result.append(instance.getStringix());
191  result.append("\", \"");
192  result.append(instance.getStringvalue());
193  result.append("\".");
194  return result.toString();
195  }
196 
197 }