/
FreshReadersProvidedTest.java
238 lines (201 loc) · 8.15 KB
/
FreshReadersProvidedTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.test.filters;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.lucene.index.CompositeReaderContext;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.MultiReader;
import org.apache.lucene.index.Terms;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.search.BitsFilteredDocIdSet;
import org.apache.lucene.search.DocIdSet;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.BitDocIdSet;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.FixedBitSet;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.backend.spi.Work;
import org.hibernate.search.backend.spi.WorkType;
import org.hibernate.search.engine.integration.impl.ExtendedSearchIntegrator;
import org.hibernate.search.query.dsl.QueryBuilder;
import org.hibernate.search.query.engine.spi.EntityInfo;
import org.hibernate.search.testsupport.TestForIssue;
import org.hibernate.search.testsupport.junit.SearchFactoryHolder;
import org.hibernate.search.testsupport.junit.SkipOnElasticsearch;
import org.hibernate.search.testsupport.setup.TransactionContextForTest;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
/**
* Verified filters don't get stale IndexReader instances after a change is applied.
* Note that filters operate on per-segment sub-readers, while we usually expose
* top level (recursive) global IndexReader views: this usually should not affect
* their usage but is relevant to how we test them.
*
* @author Sanne Grinovero (C) 2013 Red Hat Inc.
* @since 4.2
*/
@TestForIssue(jiraKey = "HSEARCH-1230")
@Category(SkipOnElasticsearch.class) // IndexReaders are specific to Lucene
public class FreshReadersProvidedTest {
@Rule
public SearchFactoryHolder sfHolder = new SearchFactoryHolder( Guest.class );
@Test
public void filtersTest() {
ExtendedSearchIntegrator searchFactory = sfHolder.getSearchFactory();
Assert.assertNotNull( searchFactory.getIndexManagerHolder().getIndexManager( "guests" ) );
{ // Store guest "Thorin Oakenshield" in the index
Guest lastDwarf = new Guest();
lastDwarf.id = 13l;
lastDwarf.name = "Thorin Oakenshield";
Work work = new Work( lastDwarf, lastDwarf.id, WorkType.ADD, false );
TransactionContextForTest tc = new TransactionContextForTest();
searchFactory.getWorker().performWork( work, tc );
tc.end();
}
QueryBuilder guestQueryBuilder = searchFactory.buildQueryBuilder()
.forEntity( Guest.class )
.get();
Query queryAllGuests = guestQueryBuilder.all().createQuery();
List<EntityInfo> queryEntityInfos = searchFactory.createHSQuery( queryAllGuests, Guest.class )
.queryEntityInfos();
Assert.assertEquals( 1, queryEntityInfos.size() );
Assert.assertEquals( 13L, queryEntityInfos.get( 0 ).getId() );
RecordingFilter filter = new RecordingFilter( "name" );
List<EntityInfo> filteredQueryEntityInfos = searchFactory.createHSQuery( queryAllGuests, Guest.class )
.filter( filter )
.queryEntityInfos();
checkFilterInspectedAllSegments( filter );
expectedTermsForFilter( filter, "thorin", "oakenshield" );
Assert.assertEquals( 1, filteredQueryEntityInfos.size() );
Assert.assertEquals( 13L, filteredQueryEntityInfos.get( 0 ).getId() );
{ // Store guest "Balin"
Guest balin = new Guest();
balin.id = 7l;
balin.name = "Balin";
Work work = new Work( balin, balin.id, WorkType.ADD, false );
TransactionContextForTest tc = new TransactionContextForTest();
searchFactory.getWorker().performWork( work, tc );
tc.end();
}
List<EntityInfo> queryEntityInfosAgain = searchFactory.createHSQuery( queryAllGuests, Guest.class )
.queryEntityInfos();
Assert.assertEquals( 2, queryEntityInfosAgain.size() );
Assert.assertEquals( 13L, queryEntityInfosAgain.get( 0 ).getId() );
Assert.assertEquals( 7L, queryEntityInfosAgain.get( 1 ).getId() );
RecordingFilter secondFilter = new RecordingFilter( "name" );
List<EntityInfo> secondFilteredQueryEntityInfos = searchFactory.createHSQuery( queryAllGuests, Guest.class )
.filter( secondFilter )
.queryEntityInfos();
checkFilterInspectedAllSegments( secondFilter );
expectedTermsForFilter( secondFilter, "thorin", "oakenshield", "balin" );
Assert.assertEquals( 2, secondFilteredQueryEntityInfos.size() );
Assert.assertEquals( 13L, secondFilteredQueryEntityInfos.get( 0 ).getId() );
Assert.assertEquals( 7L, secondFilteredQueryEntityInfos.get( 1 ).getId() );
}
private void expectedTermsForFilter(RecordingFilter filter, String... term) {
Assert.assertEquals( term.length, filter.seenTerms.size() );
Assert.assertTrue( filter.seenTerms.containsAll( Arrays.asList( term ) ) );
}
/**
* Verifies that the current RecordingFilter has been fed all the same sub-readers
* which would be obtained from a freshly checked out IndexReader.
*
* @param filter test filter instance
*/
private void checkFilterInspectedAllSegments(RecordingFilter filter) {
ExtendedSearchIntegrator searchFactory = sfHolder.getSearchFactory();
IndexReader currentIndexReader = searchFactory.getIndexReaderAccessor().open( Guest.class );
try {
List<IndexReader> allSubReaders = getSubIndexReaders( (MultiReader) currentIndexReader );
for ( IndexReader ir : allSubReaders ) {
Assert.assertTrue( filter.visitedReaders.contains( ir ) );
}
for ( IndexReader ir : filter.visitedReaders ) {
Assert.assertTrue( allSubReaders.contains( ir ) );
}
}
finally {
searchFactory.getIndexReaderAccessor().close( currentIndexReader );
}
}
public static List<IndexReader> getSubIndexReaders(MultiReader compositeReader) {
CompositeReaderContext compositeReaderContext = compositeReader.getContext();
ArrayList<IndexReader> segmentReaders = new ArrayList<IndexReader>( 20 );
for ( LeafReaderContext readerContext : compositeReaderContext.leaves() ) {
segmentReaders.add( readerContext.reader() );
}
return segmentReaders;
}
/**
* Filters are invoked once for each segment, so they are invoked multiple times
* once for each segment, each time being passed a different IndexReader.
* These IndexReader instances are "subreaders", not the global kind representing
* the whole index.
*/
private static class RecordingFilter extends Filter {
final List<IndexReader> visitedReaders = new ArrayList<IndexReader>();
final List<String> seenTerms = new ArrayList<String>();
final String fieldName;
public RecordingFilter(String fieldName) {
this.fieldName = fieldName;
}
@Override
public DocIdSet getDocIdSet(LeafReaderContext context, Bits acceptDocs) throws IOException {
final LeafReader reader = context.reader();
this.visitedReaders.add( reader );
FixedBitSet bitSet = new FixedBitSet( reader.maxDoc() );
for ( int i = 0; i < reader.maxDoc(); i++ ) {
bitSet.set( i );
}
Terms terms = reader.terms( fieldName );
TermsEnum iterator = terms.iterator();
BytesRef next = iterator.next();
while ( next != null ) {
seenTerms.add( next.utf8ToString() );
next = iterator.next();
}
return BitsFilteredDocIdSet.wrap( new BitDocIdSet( bitSet ), acceptDocs );
}
@Override
public String toString(String fieldName) {
return new StringBuilder( "RecordingFilter(" )
.append( this.fieldName )
.append( ")" )
.toString();
}
}
@Indexed(index = "guests")
public static final class Guest {
private long id;
private String name;
@DocumentId
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Field
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}