1 package org.apache.lucene.facet.search;
4 import java.io.IOException;
5 import java.util.Arrays;
7 import org.apache.lucene.store.Directory;
8 import org.apache.lucene.util.IOUtils;
9 import org.apache.lucene.util._TestUtil;
10 import org.junit.Test;
12 import org.apache.lucene.util.LuceneTestCase;
13 import org.apache.lucene.facet.FacetTestUtils;
14 import org.apache.lucene.facet.FacetTestUtils.IndexTaxonomyReaderPair;
15 import org.apache.lucene.facet.FacetTestUtils.IndexTaxonomyWriterPair;
16 import org.apache.lucene.facet.index.params.DefaultFacetIndexingParams;
19 * Licensed to the Apache Software Foundation (ASF) under one or more
20 * contributor license agreements. See the NOTICE file distributed with
21 * this work for additional information regarding copyright ownership.
22 * The ASF licenses this file to You under the Apache License, Version 2.0
23 * (the "License"); you may not use this file except in compliance with
24 * the License. You may obtain a copy of the License at
26 * http://www.apache.org/licenses/LICENSE-2.0
28 * Unless required by applicable law or agreed to in writing, software
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
35 public class TestTotalFacetCounts extends LuceneTestCase {
37 private static void initCache(int numEntries) {
38 TotalFacetCountsCache.getSingleton().clear();
39 TotalFacetCountsCache.getSingleton().setCacheSize(numEntries); // Set to keep one in mem
43 public void testWriteRead() throws IOException {
51 private void doTestWriteRead(final int partitionSize) throws IOException {
54 // Create temporary RAMDirectories
55 Directory[][] dirs = FacetTestUtils.createIndexTaxonomyDirs(1);
56 // Create our index/taxonomy writers
57 IndexTaxonomyWriterPair[] writers = FacetTestUtils
58 .createIndexTaxonomyWriterPair(dirs);
59 DefaultFacetIndexingParams iParams = new DefaultFacetIndexingParams() {
61 protected int fixedPartitionSize() {
65 // The counts that the TotalFacetCountsArray should have after adding
66 // the below facets to the index.
67 int[] expectedCounts = new int[] { 0, 3, 1, 3, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1 };
69 // Add a facet to the index
70 TestTotalFacetCountsCache.addFacets(iParams, writers[0].indexWriter, writers[0].taxWriter, "a", "b");
71 TestTotalFacetCountsCache.addFacets(iParams, writers[0].indexWriter, writers[0].taxWriter, "c", "d");
72 TestTotalFacetCountsCache.addFacets(iParams, writers[0].indexWriter, writers[0].taxWriter, "a", "e");
73 TestTotalFacetCountsCache.addFacets(iParams, writers[0].indexWriter, writers[0].taxWriter, "a", "d");
74 TestTotalFacetCountsCache.addFacets(iParams, writers[0].indexWriter, writers[0].taxWriter, "c", "g");
75 TestTotalFacetCountsCache.addFacets(iParams, writers[0].indexWriter, writers[0].taxWriter, "c", "z");
76 TestTotalFacetCountsCache.addFacets(iParams, writers[0].indexWriter, writers[0].taxWriter, "b", "a");
77 TestTotalFacetCountsCache.addFacets(iParams, writers[0].indexWriter, writers[0].taxWriter, "1", "2");
78 TestTotalFacetCountsCache.addFacets(iParams, writers[0].indexWriter, writers[0].taxWriter, "b", "c");
84 IndexTaxonomyReaderPair[] readers =
85 FacetTestUtils.createIndexTaxonomyReaderPair(dirs);
87 int[] intArray = new int[iParams.getPartitionSize()];
89 TotalFacetCountsCache tfcc = TotalFacetCountsCache.getSingleton();
90 File tmpFile = _TestUtil.createTempFile("test", "tmp", TEMP_DIR);
91 tfcc.store(tmpFile, readers[0].indexReader, readers[0].taxReader, iParams, null);
92 tfcc.clear(); // not really required because TFCC overrides on load(), but in the test we need not rely on this.
93 tfcc.load(tmpFile, readers[0].indexReader, readers[0].taxReader, iParams);
95 // now retrieve the one just loaded
96 TotalFacetCounts totalCounts =
97 tfcc.getTotalCounts(readers[0].indexReader, readers[0].taxReader, iParams, null);
100 for (int i=0; i<expectedCounts.length; i+=partitionSize) {
101 totalCounts.fillTotalCountsForPartition(intArray, partition);
102 int[] partitionExpectedCounts = new int[partitionSize];
103 int nToCopy = Math.min(partitionSize,expectedCounts.length-i);
104 System.arraycopy(expectedCounts, i, partitionExpectedCounts, 0, nToCopy);
105 assertTrue("Wrong counts! for partition "+partition+
106 "\nExpected:\n" + Arrays.toString(partitionExpectedCounts)+
107 "\nActual:\n" + Arrays.toString(intArray),
108 Arrays.equals(partitionExpectedCounts, intArray));
112 IOUtils.close(dirs[0]);