pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / test / org / apache / lucene / facet / taxonomy / writercache / cl2o / TestCompactLabelToOrdinal.java
1 package org.apache.lucene.facet.taxonomy.writercache.cl2o;
2
3 import java.io.File;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import org.junit.Test;
8
9 import org.apache.lucene.util.LuceneTestCase;
10 import org.apache.lucene.facet.taxonomy.CategoryPath;
11 import org.apache.lucene.facet.taxonomy.writercache.cl2o.CompactLabelToOrdinal;
12 import org.apache.lucene.facet.taxonomy.writercache.cl2o.LabelToOrdinal;
13
14 /**
15  * Licensed to the Apache Software Foundation (ASF) under one or more
16  * contributor license agreements.  See the NOTICE file distributed with
17  * this work for additional information regarding copyright ownership.
18  * The ASF licenses this file to You under the Apache License, Version 2.0
19  * (the "License"); you may not use this file except in compliance with
20  * the License.  You may obtain a copy of the License at
21  *
22  *     http://www.apache.org/licenses/LICENSE-2.0
23  *
24  * Unless required by applicable law or agreed to in writing, software
25  * distributed under the License is distributed on an "AS IS" BASIS,
26  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27  * See the License for the specific language governing permissions and
28  * limitations under the License.
29  */
30
31 public class TestCompactLabelToOrdinal extends LuceneTestCase {
32
33   @Test
34   public void testL2O() throws Exception {
35     LabelToOrdinal map = new LabelToOrdinalMap();
36
37     CompactLabelToOrdinal compact = new CompactLabelToOrdinal(2000000, 0.15f, 3);
38
39     final int n = atLeast(10 * 1000);
40     final int numUniqueValues = 50 * 1000;
41
42     String[] uniqueValues = new String[numUniqueValues];
43     byte[] buffer = new byte[50];
44
45     for (int i = 0; i < numUniqueValues;) {
46       random.nextBytes(buffer);
47       int size = 1 + random.nextInt(50);
48
49       uniqueValues[i] = new String(buffer, 0, size);
50       if (uniqueValues[i].indexOf(CompactLabelToOrdinal.TerminatorChar) == -1) {
51         i++;
52       }
53     }
54
55     TEMP_DIR.mkdirs();
56     File f = new File(TEMP_DIR, "CompactLabelToOrdinalTest.tmp");
57     int flushInterval = 10;
58
59     for (int i = 0; i < n * 10; i++) {
60       if (i > 0 && i % flushInterval == 0) {
61         compact.flush(f);    
62         compact = CompactLabelToOrdinal.open(f, 0.15f, 3);
63         assertTrue(f.delete());
64         if (flushInterval < (n / 10)) {
65           flushInterval *= 10;
66         }
67       }
68
69       int index = random.nextInt(numUniqueValues);
70       CategoryPath label = new CategoryPath(uniqueValues[index], '/');
71
72       int ord1 = map.getOrdinal(label);
73       int ord2 = compact.getOrdinal(label);
74
75       //System.err.println(ord1+" "+ord2);
76
77       assertEquals(ord1, ord2);
78
79       if (ord1 == LabelToOrdinal.InvalidOrdinal) {
80         ord1 = compact.getNextOrdinal();
81
82         map.addLabel(label, ord1);
83         compact.addLabel(label, ord1);
84       }
85     }
86
87     for (int i = 0; i < numUniqueValues; i++) {
88       CategoryPath label = new CategoryPath(uniqueValues[i], '/');
89       int ord1 = map.getOrdinal(label);
90       int ord2 = compact.getOrdinal(label);
91       assertEquals(ord1, ord2);
92     }
93   }
94
95   private static class LabelToOrdinalMap extends LabelToOrdinal {
96     private Map<CategoryPath, Integer> map = new HashMap<CategoryPath, Integer>();
97
98     LabelToOrdinalMap() { }
99     
100     @Override
101     public void addLabel(CategoryPath label, int ordinal) {
102       map.put(new CategoryPath(label), ordinal);
103     }
104
105     @Override
106     public void addLabel(CategoryPath label, int prefixLen, int ordinal) {
107       map.put(new CategoryPath(label, prefixLen), ordinal);
108     }
109
110     @Override
111     public int getOrdinal(CategoryPath label) {
112       Integer value = map.get(label);
113       return (value != null) ? value.intValue() : LabelToOrdinal.InvalidOrdinal;
114     }
115
116     @Override
117     public int getOrdinal(CategoryPath label, int prefixLen) {
118       Integer value = map.get(new CategoryPath(label, prefixLen));
119       return (value != null) ? value.intValue() : LabelToOrdinal.InvalidOrdinal;
120     }
121
122   }
123 }