-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
LuceneDataSourceTest.java
276 lines (244 loc) · 12 KB
/
LuceneDataSourceTest.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.index.impl.lucene.legacy;
import org.apache.lucene.index.IndexWriter;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.RuleChain;
import java.io.IOException;
import java.util.Map;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.graphdb.index.IndexManager;
import org.neo4j.helpers.collection.MapUtil;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.index.IndexConfigStore;
import org.neo4j.kernel.impl.index.IndexEntityType;
import org.neo4j.kernel.lifecycle.LifeRule;
import org.neo4j.test.rule.TestDirectory;
import org.neo4j.test.rule.fs.DefaultFileSystemRule;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
public class LuceneDataSourceTest
{
private final LifeRule life = new LifeRule( true );
private final TestDirectory directory = TestDirectory.testDirectory();
private final ExpectedException expectedException = ExpectedException.none();
private final DefaultFileSystemRule fileSystemRule = new DefaultFileSystemRule();
@Rule
public final RuleChain ruleChain = RuleChain.outerRule( directory ).around( fileSystemRule )
.around( life ).around( expectedException );
private IndexConfigStore indexStore;
private LuceneDataSource dataSource;
@Before
public void setUp()
{
indexStore = new IndexConfigStore( directory.directory(), fileSystemRule.get() );
addIndex( "foo" );
}
@Test
public void doNotTryToCommitWritersOnForceInReadOnlyMode() throws IOException
{
IndexIdentifier indexIdentifier = identifier( "foo" );
prepareIndexesByIdentifiers( indexIdentifier );
stopDataSource();
Config readOnlyConfig = new Config( readOnlyConfig(), GraphDatabaseSettings.class );
LuceneDataSource readOnlyDataSource = life.add( new LuceneDataSource( directory.graphDbDir(), readOnlyConfig,
indexStore, fileSystemRule.get() ) );
assertNotNull( readOnlyDataSource.getIndexSearcher( indexIdentifier ) );
readOnlyDataSource.force();
}
@Test
public void notAllowIndexDeletionInReadOnlyMode() throws IOException
{
IndexIdentifier indexIdentifier = identifier( "foo" );
prepareIndexesByIdentifiers( indexIdentifier );
stopDataSource();
Config readOnlyConfig = new Config( readOnlyConfig(), GraphDatabaseSettings.class );
dataSource = life.add( new LuceneDataSource( directory.graphDbDir(), readOnlyConfig, indexStore,
fileSystemRule.get() ) );
expectedException.expect( IllegalStateException.class );
expectedException.expectMessage("Index deletion in read only mode is not supported.");
dataSource.deleteIndex( indexIdentifier, false );
}
@Test
public void useReadOnlyIndexSearcherInReadOnlyMode() throws IOException
{
IndexIdentifier indexIdentifier = identifier( "foo" );
prepareIndexesByIdentifiers( indexIdentifier );
stopDataSource();
Config readOnlyConfig = new Config( readOnlyConfig(), GraphDatabaseSettings.class );
dataSource = life.add( new LuceneDataSource( directory.graphDbDir(), readOnlyConfig, indexStore,
fileSystemRule.get() ) );
IndexReference indexSearcher = dataSource.getIndexSearcher( indexIdentifier );
assertTrue( "Read only index reference should be used in read only mode.",
ReadOnlyIndexReference.class.isInstance( indexSearcher ) );
}
@Test
public void refreshReadOnlyIndexSearcherInReadOnlyMode() throws IOException
{
IndexIdentifier indexIdentifier = identifier( "foo" );
prepareIndexesByIdentifiers( indexIdentifier );
stopDataSource();
Config readOnlyConfig = new Config( readOnlyConfig(), GraphDatabaseSettings.class );
dataSource = life.add( new LuceneDataSource( directory.graphDbDir(), readOnlyConfig, indexStore,
fileSystemRule.get() ) );
IndexReference indexSearcher = dataSource.getIndexSearcher( indexIdentifier );
IndexReference indexSearcher2 = dataSource.getIndexSearcher( indexIdentifier );
IndexReference indexSearcher3 = dataSource.getIndexSearcher( indexIdentifier );
IndexReference indexSearcher4 = dataSource.getIndexSearcher( indexIdentifier );
assertSame( "Refreshed read only searcher should be the same.", indexSearcher, indexSearcher2 );
assertSame( "Refreshed read only searcher should be the same.", indexSearcher2, indexSearcher3 );
assertSame( "Refreshed read only searcher should be the same.", indexSearcher3, indexSearcher4 );
}
@Test
public void testShouldReturnIndexWriterFromLRUCache() throws Throwable
{
Config config = new Config( config(), GraphDatabaseSettings.class );
dataSource = life.add( new LuceneDataSource( directory.graphDbDir(), config, indexStore, fileSystemRule.get() ) );
IndexIdentifier identifier = identifier( "foo" );
IndexWriter writer = dataSource.getIndexSearcher( identifier ).getWriter();
assertSame( writer, dataSource.getIndexSearcher( identifier ).getWriter() );
}
@Test
public void testShouldReturnIndexSearcherFromLRUCache() throws Throwable
{
Config config = new Config( config(), GraphDatabaseSettings.class );
dataSource = life.add( new LuceneDataSource( directory.graphDbDir(), config, indexStore, fileSystemRule.get() ) );
IndexIdentifier identifier = identifier( "foo" );
IndexReference searcher = dataSource.getIndexSearcher( identifier );
assertSame( searcher, dataSource.getIndexSearcher( identifier ) );
searcher.close();
}
@Test
public void testClosesOldestIndexWriterWhenCacheSizeIsExceeded() throws Throwable
{
addIndex( "bar" );
addIndex( "baz" );
Map<String, String> configMap = config();
configMap.put( GraphDatabaseSettings.lucene_searcher_cache_size.name(), "2" );
Config config = new Config( configMap, GraphDatabaseSettings.class );
dataSource = life.add( new LuceneDataSource( directory.graphDbDir(), config, indexStore, fileSystemRule.get() ) );
IndexIdentifier fooIdentifier = identifier( "foo" );
IndexIdentifier barIdentifier = identifier( "bar" );
IndexIdentifier bazIdentifier = identifier( "baz" );
IndexWriter fooIndexWriter = dataSource.getIndexSearcher( fooIdentifier ).getWriter();
dataSource.getIndexSearcher( barIdentifier );
assertTrue( fooIndexWriter.isOpen() );
dataSource.getIndexSearcher( bazIdentifier );
assertFalse( fooIndexWriter.isOpen() );
}
@Test
public void testClosesOldestIndexSearcherWhenCacheSizeIsExceeded() throws Throwable
{
addIndex( "bar" );
addIndex( "baz" );
Map<String, String> configMap = config();
configMap.put( GraphDatabaseSettings.lucene_searcher_cache_size.name(), "2" );
Config config = new Config( configMap, GraphDatabaseSettings.class );
dataSource = life.add( new LuceneDataSource( directory.graphDbDir(), config, indexStore, fileSystemRule.get() ) );
IndexIdentifier fooIdentifier = identifier( "foo" );
IndexIdentifier barIdentifier = identifier( "bar" );
IndexIdentifier bazIdentifier = identifier( "baz" );
IndexReference fooSearcher = dataSource.getIndexSearcher( fooIdentifier );
IndexReference barSearcher = dataSource.getIndexSearcher( barIdentifier );
assertFalse( fooSearcher.isClosed() );
IndexReference bazSearcher = dataSource.getIndexSearcher( bazIdentifier );
assertTrue( fooSearcher.isClosed() );
barSearcher.close();
bazSearcher.close();
}
@Test
public void testRecreatesSearcherWhenRequestedAgain() throws Throwable
{
addIndex( "bar" );
addIndex( "baz" );
Map<String, String> configMap = config();
configMap.put( GraphDatabaseSettings.lucene_searcher_cache_size.name(), "2" );
Config config = new Config( configMap, GraphDatabaseSettings.class );
dataSource = life.add( new LuceneDataSource( directory.graphDbDir(), config, indexStore, fileSystemRule.get() ) );
IndexIdentifier fooIdentifier = identifier( "foo" );
IndexIdentifier barIdentifier = identifier( "bar" );
IndexIdentifier bazIdentifier = identifier( "baz" );
IndexReference oldFooSearcher = dataSource.getIndexSearcher( fooIdentifier );
IndexReference barSearcher = dataSource.getIndexSearcher( barIdentifier );
IndexReference bazSearcher = dataSource.getIndexSearcher( bazIdentifier );
IndexReference newFooSearcher = dataSource.getIndexSearcher( bazIdentifier );
assertNotSame( oldFooSearcher, newFooSearcher );
assertFalse( newFooSearcher.isClosed() );
oldFooSearcher.close();
barSearcher.close();
bazSearcher.close();
newFooSearcher.close();
}
@Test
public void testRecreatesWriterWhenRequestedAgainAfterCacheEviction() throws Throwable
{
addIndex( "bar" );
addIndex( "baz" );
Map<String, String> configMap = config();
configMap.put( GraphDatabaseSettings.lucene_searcher_cache_size.name(), "2" );
Config config = new Config( configMap, GraphDatabaseSettings.class );
dataSource = life.add( new LuceneDataSource( directory.graphDbDir(), config, indexStore, fileSystemRule.get() ) );
IndexIdentifier fooIdentifier = identifier( "foo" );
IndexIdentifier barIdentifier = identifier( "bar" );
IndexIdentifier bazIdentifier = identifier( "baz" );
IndexWriter oldFooIndexWriter = dataSource.getIndexSearcher( fooIdentifier ).getWriter();
dataSource.getIndexSearcher( barIdentifier );
dataSource.getIndexSearcher( bazIdentifier );
IndexWriter newFooIndexWriter = dataSource.getIndexSearcher( fooIdentifier ).getWriter();
assertNotSame( oldFooIndexWriter, newFooIndexWriter );
assertTrue( newFooIndexWriter.isOpen() );
}
private void stopDataSource() throws IOException
{
dataSource.shutdown();
}
private Map<String, String> config()
{
return MapUtil.stringMap();
}
private void prepareIndexesByIdentifiers( IndexIdentifier indexIdentifier )
{
Config config = new Config( config(), GraphDatabaseSettings.class );
dataSource = life.add( new LuceneDataSource( directory.graphDbDir(), config, indexStore, fileSystemRule.get() ) );
dataSource.getIndexSearcher( indexIdentifier );
dataSource.force();
}
private Map<String, String> readOnlyConfig()
{
Map<String,String> config = config();
config.put( GraphDatabaseSettings.read_only.name(), "true" );
return config;
}
private void addIndex( String name )
{
indexStore.set( Node.class, name, MapUtil.stringMap( IndexManager.PROVIDER, "lucene", "type", "fulltext" ) );
}
private IndexIdentifier identifier( String name )
{
return new IndexIdentifier( IndexEntityType.Node, name );
}
}