00001 package de.aitools.aq.invertedindex.core;
00002
00003
00004 import java.io.File;
00005 import java.util.ArrayList;
00006 import java.util.List;
00007
00008 import org.junit.Assert;
00009 import org.junit.Test;
00010
00011 import de.aitools.aq.util.RandomGenerator;
00012 import de.aitools.aq.value.Value;
00013 import de.aitools.aq.value.pair.DoubleDouble;
00014 import de.aitools.aq.value.pair.DoubleInt;
00015 import de.aitools.aq.value.pair.DoubleLong;
00016 import de.aitools.aq.value.pair.DoubleString;
00017 import de.aitools.aq.value.pair.IntDouble;
00018 import de.aitools.aq.value.pair.IntInt;
00019 import de.aitools.aq.value.pair.IntLong;
00020 import de.aitools.aq.value.pair.IntString;
00021 import de.aitools.aq.value.pair.LongDouble;
00022 import de.aitools.aq.value.pair.LongInt;
00023 import de.aitools.aq.value.pair.LongLong;
00024 import de.aitools.aq.value.pair.LongString;
00025 import de.aitools.aq.value.quadruple.DoubleDoubleIntInt;
00026 import de.aitools.aq.value.quadruple.DoubleDoubleLongLong;
00027 import de.aitools.aq.value.quadruple.DoubleLongLongInt;
00028 import de.aitools.aq.value.quadruple.IntIntDoubleDouble;
00029 import de.aitools.aq.value.quadruple.IntIntLongLong;
00030 import de.aitools.aq.value.quadruple.LongDoubleLongInt;
00031 import de.aitools.aq.value.quadruple.LongLongDoubleDouble;
00032 import de.aitools.aq.value.quadruple.LongLongIntInt;
00033 import de.aitools.aq.value.single.SingleBuffer;
00034 import de.aitools.aq.value.single.SingleDouble;
00035 import de.aitools.aq.value.single.SingleInt;
00036 import de.aitools.aq.value.single.SingleLong;
00037 import de.aitools.aq.value.single.SingleString;
00038 import de.aitools.aq.value.triple.DoubleDoubleDouble;
00039 import de.aitools.aq.value.triple.DoubleIntDouble;
00040 import de.aitools.aq.value.triple.DoubleIntInt;
00041 import de.aitools.aq.value.triple.IntDoubleDouble;
00042 import de.aitools.aq.value.triple.IntIntDouble;
00043 import de.aitools.aq.value.triple.IntIntInt;
00044 import de.aitools.aq.value.triple.LongDoubleDouble;
00045 import de.aitools.aq.value.triple.LongIntDouble;
00046 import de.aitools.aq.value.triple.LongIntInt;
00047
00048 public class TestIndexer {
00049
00050 private static final int RECORD_COUNT = 100000;
00051
00052 private static final File tmpIndexDir = new File("testIndexer");
00053
00054 @SuppressWarnings("unchecked")
00055 public static <V extends Value> List<V>
00056 createRandomValues(Class<V> clazz, int count)
00057 throws InstantiationException, IllegalAccessException {
00058 String unicode = "valäöü€łŧ";
00059 List<V> values = new ArrayList<V>();
00060 switch (Value.getNativeTypeId(clazz))
00061 {
00062 case SingleBuffer.NATIVE_TYPE_ID:
00063 for (int i = 0; i != count; ++i) {
00064 SingleBuffer array = new SingleBuffer(20);
00065 array.getBuffer().putDouble(i * 23.42);
00066 array.getBuffer().putLong(0);
00067 array.getBuffer().putInt(i * 23 + 42);
00068 array.getBuffer().rewind();
00069 values.add((V) array);
00070 }
00071 break;
00072 case SingleString.NATIVE_TYPE_ID:
00073 for (int i = 0; i != count; ++i) {
00074 values.add((V) new SingleString(unicode + i));
00075 }
00076 break;
00077 case DoubleString.NATIVE_TYPE_ID:
00078 for (int i = 0; i != count; ++i) {
00079 values.add((V) new DoubleString(i, unicode + i));
00080 }
00081 break;
00082 case IntString.NATIVE_TYPE_ID:
00083 for (int i = 0; i != count; ++i) {
00084 values.add((V) new IntString(i, unicode + i));
00085 }
00086 break;
00087 case LongString.NATIVE_TYPE_ID:
00088 for (int i = 0; i != count; ++i) {
00089 values.add((V) new LongString(i, unicode + i));
00090 }
00091 break;
00092 default:
00093 for (int i = 0; i != count; ++i) {
00094 values.add(RandomGenerator.nextValue(clazz));
00095 }
00096 }
00097 return values;
00098 }
00099
00100 private static void deleteAll(File directory) {
00101 File[] files = directory.listFiles();
00102 if (files != null) {
00103 for (File file : directory.listFiles()) {
00104 deleteAll(file);
00105 }
00106 }
00107 directory.delete();
00108 }
00109
00110 private static <V extends Value> void test(Class<V> clazz, int count) {
00111
00112
00113
00114 Assert.assertTrue(tmpIndexDir.mkdir());
00115 Configuration config = new Configuration();
00116 config.setIndexDirectory(tmpIndexDir);
00117 config.setExpectedNumberOfRecords(RECORD_COUNT);
00118 Indexer<V> indexer = Indexer.open(clazz, config);
00119
00120
00121
00122
00123 String key = "k e y";
00124 List<V> values = null;
00125 try {
00126 values = createRandomValues(clazz, count);
00127 } catch (InstantiationException e) {
00128 e.printStackTrace();
00129 } catch (IllegalAccessException e) {
00130 e.printStackTrace();
00131 }
00132 for (V value : values) {
00133 indexer.put(key, value);
00134 }
00135
00136
00137
00138 StringBuilder sb = new StringBuilder();
00139 for (int i = 0; i != 100000; ++i) sb.append('x');
00140 Assert.assertFalse(indexer.put(sb.toString(), values.get(0)));
00141 indexer.index();
00142
00143
00144
00145
00146 Searcher<V> searcher = Searcher.open(clazz, config);
00147 Postlist<V> postlist = searcher.search(key);
00148 Assert.assertNotNull(postlist);
00149 Assert.assertEquals(count, postlist.size());
00150 for (int i = 0; i != count; ++i) {
00151 V value = postlist.iterator().next();
00152 Assert.assertEquals(values.get(i), value);
00153 }
00154 postlist.deleteNative();
00155
00156
00157
00158
00159 postlist = searcher.search("unknown");
00160 Assert.assertNull(postlist);
00161
00162
00163
00164
00165 searcher.close();
00166 deleteAll(tmpIndexDir);
00167 }
00168
00169
00170
00171
00172
00173 @Test
00174 public final void testSingleDouble() {
00175 test(SingleDouble.class, RECORD_COUNT);
00176 }
00177
00178 @Test
00179 public final void testSingleInt() {
00180 test(SingleInt.class, RECORD_COUNT);
00181 }
00182
00183 @Test
00184 public final void testSingleLong() {
00185 test(SingleLong.class, RECORD_COUNT);
00186 }
00187
00188 @Test
00189 public final void testSingleBuffer() {
00190 test(SingleBuffer.class, RECORD_COUNT);
00191 }
00192
00193 @Test
00194 public final void testSingleString() {
00195 test(SingleString.class, RECORD_COUNT);
00196 }
00197
00198
00199
00200
00201
00202 @Test
00203 public final void testDoubleDouble() {
00204 test(DoubleDouble.class, RECORD_COUNT);
00205 }
00206
00207 @Test
00208 public final void testDoubleInt() {
00209 test(DoubleInt.class, RECORD_COUNT);
00210 }
00211
00212 @Test
00213 public final void testDoubleLong() {
00214 test(DoubleLong.class, RECORD_COUNT);
00215 }
00216
00217 @Test
00218 public final void testIntDouble() {
00219 test(IntDouble.class, RECORD_COUNT);
00220 }
00221
00222 @Test
00223 public final void testIntInt() {
00224 test(IntInt.class, RECORD_COUNT);
00225 }
00226
00227 @Test
00228 public final void testIntLong() {
00229 test(IntLong.class, RECORD_COUNT);
00230 }
00231
00232 @Test
00233 public final void testLongDouble() {
00234 test(LongDouble.class, RECORD_COUNT);
00235 }
00236
00237 @Test
00238 public final void testLongInt() {
00239 test(LongInt.class, RECORD_COUNT);
00240 }
00241
00242 @Test
00243 public final void testLongLong() {
00244 test(LongLong.class, RECORD_COUNT);
00245 }
00246
00247
00248
00249
00250
00251 @Test
00252 public final void testDoubleString() {
00253 test(DoubleString.class, RECORD_COUNT);
00254 }
00255
00256 @Test
00257 public final void testIntString() {
00258 test(IntString.class, RECORD_COUNT);
00259 }
00260
00261 @Test
00262 public final void testLongString() {
00263 test(LongString.class, RECORD_COUNT);
00264 }
00265
00266
00267
00268
00269
00270 @Test
00271 public final void testDoubleDoubleDouble() {
00272 test(DoubleDoubleDouble.class, RECORD_COUNT);
00273 }
00274
00275 @Test
00276 public final void testDoubleIntDouble() {
00277 test(DoubleIntDouble.class, RECORD_COUNT);
00278 }
00279
00280 @Test
00281 public final void testDoubleIntInt() {
00282 test(DoubleIntInt.class, RECORD_COUNT);
00283 }
00284
00285 @Test
00286 public final void testIntDoubleDouble() {
00287 test(IntDoubleDouble.class, RECORD_COUNT);
00288 }
00289
00290 @Test
00291 public final void testIntIntDouble() {
00292 test(IntIntDouble.class, RECORD_COUNT);
00293 }
00294
00295 @Test
00296 public final void testIntIntInt() {
00297 test(IntIntInt.class, RECORD_COUNT);
00298 }
00299
00300 @Test
00301 public final void testLongDoubleDouble() {
00302 test(LongDoubleDouble.class, RECORD_COUNT);
00303 }
00304
00305 @Test
00306 public final void testLongIntDouble() {
00307 test(LongIntDouble.class, RECORD_COUNT);
00308 }
00309
00310 @Test
00311 public final void testLongIntInt() {
00312 test(LongIntInt.class, RECORD_COUNT);
00313 }
00314
00315
00316
00317
00318
00319 @Test
00320 public final void testDoubleDoubleIntInt() {
00321 test(DoubleDoubleIntInt.class, RECORD_COUNT);
00322 }
00323
00324 @Test
00325 public final void testDoubleDoubleLongLong() {
00326 test(DoubleDoubleLongLong.class, RECORD_COUNT);
00327 }
00328
00329 @Test
00330 public final void testIntIntDoubleDouble() {
00331 test(IntIntDoubleDouble.class, RECORD_COUNT);
00332 }
00333
00334 @Test
00335 public final void testIntIntLongLong() {
00336 test(IntIntLongLong.class, RECORD_COUNT);
00337 }
00338
00339 @Test
00340 public final void testLongDoubleLongInt() {
00341 test(LongDoubleLongInt.class, RECORD_COUNT);
00342 }
00343
00344 @Test
00345 public final void testLongLongDoubleDouble() {
00346 test(LongLongDoubleDouble.class, RECORD_COUNT);
00347 }
00348
00349 @Test
00350 public final void testLongLongIntInt() {
00351 test(LongLongIntInt.class, RECORD_COUNT);
00352 }
00353
00354 @Test
00355 public final void testDoubleLongLongInt() {
00356 test(DoubleLongLongInt.class, RECORD_COUNT);
00357 }
00358
00359 }