1
package org.apache.lucene.facet.search;
3
import java.io.IOException;
7
import org.apache.lucene.search.MatchAllDocsQuery;
9
import org.junit.After;
10
import org.junit.Before;
11
import org.junit.Test;
13
import org.apache.lucene.facet.FacetTestBase;
14
import org.apache.lucene.facet.index.params.CategoryListParams;
15
import org.apache.lucene.facet.index.params.FacetIndexingParams;
16
import org.apache.lucene.facet.search.cache.CategoryListCache;
17
import org.apache.lucene.facet.search.cache.CategoryListData;
18
import org.apache.lucene.facet.search.params.CountFacetRequest;
19
import org.apache.lucene.facet.search.params.FacetSearchParams;
20
import org.apache.lucene.facet.search.results.FacetResult;
21
import org.apache.lucene.facet.taxonomy.CategoryPath;
24
* Licensed to the Apache Software Foundation (ASF) under one or more
25
* contributor license agreements. See the NOTICE file distributed with
26
* this work for additional information regarding copyright ownership.
27
* The ASF licenses this file to You under the Apache License, Version 2.0
28
* (the "License"); you may not use this file except in compliance with
29
* the License. You may obtain a copy of the License at
31
* http://www.apache.org/licenses/LICENSE-2.0
33
* Unless required by applicable law or agreed to in writing, software
34
* distributed under the License is distributed on an "AS IS" BASIS,
35
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36
* See the License for the specific language governing permissions and
37
* limitations under the License.
40
public class TestCategoryListCache extends FacetTestBase {
42
public TestCategoryListCache() {
48
public void setUp() throws Exception {
55
public void tearDown() throws Exception {
61
public void testNoClCache() throws Exception {
66
public void testCorrectClCache() throws Exception {
71
public void testWrongClCache() throws Exception {
75
private void doTest(boolean withCache, boolean plantWrongData) throws IOException, Exception {
76
Map<CategoryPath,Integer> truth = facetCountsTruth();
77
CategoryPath cp = (CategoryPath) truth.keySet().toArray()[0]; // any category path will do for this test
78
CountFacetRequest frq = new CountFacetRequest(cp, 10);
79
FacetSearchParams sParams = getFacetedSearchParams();
80
sParams.addFacetRequest(frq);
82
//let's use a cached cl data
83
FacetIndexingParams iparams = sParams.getFacetIndexingParams();
84
CategoryListParams clp = new CategoryListParams(); // default term ok as only single list
85
CategoryListCache clCache = new CategoryListCache();
86
clCache.loadAndRegister(clp, indexReader, taxoReader, iparams);
88
// let's mess up the cached data and then expect a wrong result...
89
messCachedData(clCache, clp);
91
sParams.setClCache(clCache);
93
FacetsCollector fc = new FacetsCollector(sParams, indexReader, taxoReader);
94
searcher.search(new MatchAllDocsQuery(), fc);
95
List<FacetResult> res = fc.getFacetResults();
97
assertCountsAndCardinality(truth, res);
98
assertFalse("Correct results not expected when wrong data was cached", plantWrongData);
99
} catch (Throwable e) {
100
assertTrue("Wrong results not expected unless wrong data was cached", withCache);
101
assertTrue("Wrong results not expected unless wrong data was cached", plantWrongData);
105
/** Mess the cached data for this {@link CategoryListParams} */
106
private void messCachedData(CategoryListCache clCache, CategoryListParams clp) {
107
final CategoryListData cld = clCache.get(clp);
108
CategoryListData badCld = new CategoryListData() {
110
public CategoryListIterator iterator(int partition) throws IOException {
111
final CategoryListIterator it = cld.iterator(partition);
112
return new CategoryListIterator() {
113
public boolean skipTo(int docId) throws IOException {
114
return it.skipTo(docId);
116
public long nextCategory() throws IOException {
117
long res = it.nextCategory();
118
if (res>Integer.MAX_VALUE) {
121
return res>1 ? res-1 : res+1;
123
public boolean init() throws IOException {
129
clCache.register(clp, badCld);