pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / test / org / apache / lucene / facet / search / TestTotalFacetCounts.java
1 package org.apache.lucene.facet.search;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.Arrays;
6
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;
11
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;
17
18 /**
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
25  *
26  *     http://www.apache.org/licenses/LICENSE-2.0
27  *
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.
33  */
34
35 public class TestTotalFacetCounts extends LuceneTestCase {
36
37   private static void initCache(int numEntries) {
38     TotalFacetCountsCache.getSingleton().clear();
39     TotalFacetCountsCache.getSingleton().setCacheSize(numEntries); // Set to keep one in mem
40   }
41
42   @Test
43   public void testWriteRead() throws IOException {
44     doTestWriteRead(14);
45     doTestWriteRead(100);
46     doTestWriteRead(7);
47     doTestWriteRead(3);
48     doTestWriteRead(1);
49   }
50
51   private void doTestWriteRead(final int partitionSize) throws IOException {
52     initCache(1);
53
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() {
60       @Override
61       protected int fixedPartitionSize() {
62         return partitionSize;
63       }
64     };
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 };
68     
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");
79
80     // Commit Changes
81     writers[0].commit();
82     writers[0].close();
83
84     IndexTaxonomyReaderPair[] readers = 
85       FacetTestUtils.createIndexTaxonomyReaderPair(dirs);
86     
87     int[] intArray = new int[iParams.getPartitionSize()];
88
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);
94     
95     // now retrieve the one just loaded
96     TotalFacetCounts totalCounts = 
97       tfcc.getTotalCounts(readers[0].indexReader, readers[0].taxReader, iParams, null);
98
99     int partition = 0;
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));
109       ++partition;
110     }
111     readers[0].close();
112     IOUtils.close(dirs[0]);
113     tmpFile.delete();
114   }
115
116 }