1 package org.apache.lucene.facet.taxonomy.writercache.cl2o;
4 import java.util.HashMap;
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;
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
22 * http://www.apache.org/licenses/LICENSE-2.0
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.
31 public class TestCompactLabelToOrdinal extends LuceneTestCase {
34 public void testL2O() throws Exception {
35 LabelToOrdinal map = new LabelToOrdinalMap();
37 CompactLabelToOrdinal compact = new CompactLabelToOrdinal(2000000, 0.15f, 3);
39 final int n = atLeast(10 * 1000);
40 final int numUniqueValues = 50 * 1000;
42 String[] uniqueValues = new String[numUniqueValues];
43 byte[] buffer = new byte[50];
45 for (int i = 0; i < numUniqueValues;) {
46 random.nextBytes(buffer);
47 int size = 1 + random.nextInt(50);
49 uniqueValues[i] = new String(buffer, 0, size);
50 if (uniqueValues[i].indexOf(CompactLabelToOrdinal.TerminatorChar) == -1) {
56 File f = new File(TEMP_DIR, "CompactLabelToOrdinalTest.tmp");
57 int flushInterval = 10;
59 for (int i = 0; i < n * 10; i++) {
60 if (i > 0 && i % flushInterval == 0) {
62 compact = CompactLabelToOrdinal.open(f, 0.15f, 3);
63 assertTrue(f.delete());
64 if (flushInterval < (n / 10)) {
69 int index = random.nextInt(numUniqueValues);
70 CategoryPath label = new CategoryPath(uniqueValues[index], '/');
72 int ord1 = map.getOrdinal(label);
73 int ord2 = compact.getOrdinal(label);
75 //System.err.println(ord1+" "+ord2);
77 assertEquals(ord1, ord2);
79 if (ord1 == LabelToOrdinal.InvalidOrdinal) {
80 ord1 = compact.getNextOrdinal();
82 map.addLabel(label, ord1);
83 compact.addLabel(label, ord1);
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);
95 private static class LabelToOrdinalMap extends LabelToOrdinal {
96 private Map<CategoryPath, Integer> map = new HashMap<CategoryPath, Integer>();
98 LabelToOrdinalMap() { }
101 public void addLabel(CategoryPath label, int ordinal) {
102 map.put(new CategoryPath(label), ordinal);
106 public void addLabel(CategoryPath label, int prefixLen, int ordinal) {
107 map.put(new CategoryPath(label, prefixLen), ordinal);
111 public int getOrdinal(CategoryPath label) {
112 Integer value = map.get(label);
113 return (value != null) ? value.intValue() : LabelToOrdinal.InvalidOrdinal;
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;