-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
LuceneIndexImplementation.java
193 lines (172 loc) · 6.39 KB
/
LuceneIndexImplementation.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
/*
* Copyright (c) 2002-2017 "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 java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.graphdb.index.IndexManager;
import org.neo4j.helpers.collection.MapUtil;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.api.TransactionApplier;
import org.neo4j.kernel.impl.factory.OperationalMode;
import org.neo4j.kernel.impl.index.IndexConfigStore;
import org.neo4j.kernel.lifecycle.LifecycleAdapter;
import org.neo4j.kernel.spi.legacyindex.IndexCommandFactory;
import org.neo4j.kernel.spi.legacyindex.IndexImplementation;
import org.neo4j.kernel.spi.legacyindex.LegacyIndexProviderTransaction;
public class LuceneIndexImplementation extends LifecycleAdapter implements IndexImplementation
{
static final String KEY_TYPE = "type";
static final String KEY_ANALYZER = "analyzer";
static final String KEY_TO_LOWER_CASE = "to_lower_case";
static final String KEY_SIMILARITY = "similarity";
public static final String SERVICE_NAME = "lucene";
public static final Map<String, String> EXACT_CONFIG =
Collections.unmodifiableMap( MapUtil.stringMap(
IndexManager.PROVIDER, SERVICE_NAME, KEY_TYPE, "exact" ) );
public static final Map<String, String> FULLTEXT_CONFIG =
Collections.unmodifiableMap( MapUtil.stringMap(
IndexManager.PROVIDER, SERVICE_NAME, KEY_TYPE, "fulltext",
KEY_TO_LOWER_CASE, "true" ) );
private LuceneDataSource dataSource;
private final File storeDir;
private final Config config;
private final Supplier<IndexConfigStore> indexStore;
private final FileSystemAbstraction fileSystemAbstraction;
private final OperationalMode operationalMode;
public LuceneIndexImplementation( File storeDir, Config config, Supplier<IndexConfigStore> indexStore,
FileSystemAbstraction fileSystemAbstraction, OperationalMode operationalMode )
{
this.storeDir = storeDir;
this.config = config;
this.indexStore = indexStore;
this.fileSystemAbstraction = fileSystemAbstraction;
this.operationalMode = operationalMode;
}
@Override
public void init() throws Throwable
{
this.dataSource = new LuceneDataSource( storeDir, config, indexStore.get(), fileSystemAbstraction, operationalMode );
this.dataSource.init();
}
@Override
public void start() throws Throwable
{
this.dataSource.start();
}
@Override
public void stop() throws Throwable
{
this.dataSource.stop();
}
@Override
public void shutdown() throws Throwable
{
this.dataSource.shutdown();
this.dataSource = null;
}
@Override
public File getIndexImplementationDirectory(File storeDir)
{
return LuceneDataSource.getLuceneIndexStoreDirectory(storeDir);
}
@Override
public LegacyIndexProviderTransaction newTransaction( IndexCommandFactory commandFactory )
{
return new LuceneLegacyIndexTransaction( dataSource, commandFactory );
}
@Override
public Map<String, String> fillInDefaults( Map<String, String> source )
{
Map<String, String> result = source != null ?
new HashMap<>( source ) : new HashMap<>();
String analyzer = result.get( KEY_ANALYZER );
if ( analyzer == null )
{
// Type is only considered if "analyzer" isn't supplied
String type = result.get( KEY_TYPE );
if ( type == null )
{
type = "exact";
result.put( KEY_TYPE, type );
}
if ( type.equals( "fulltext" ) &&
!result.containsKey( LuceneIndexImplementation.KEY_TO_LOWER_CASE ) )
{
result.put( KEY_TO_LOWER_CASE, "true" );
}
}
// Try it on for size. Calling this will reveal configuration problems.
IndexType.getIndexType( result );
return result;
}
@Override
public boolean configMatches( Map<String, String> storedConfig, Map<String, String> config )
{
return match( storedConfig, config, KEY_TYPE, null ) &&
match( storedConfig, config, KEY_TO_LOWER_CASE, "true" ) &&
match( storedConfig, config, KEY_ANALYZER, null ) &&
match( storedConfig, config, KEY_SIMILARITY, null );
}
private boolean match( Map<String, String> storedConfig, Map<String, String> config,
String key, String defaultValue )
{
String value1 = storedConfig.get( key );
String value2 = config.get( key );
if ( value1 == null || value2 == null )
{
if ( value1 == value2 )
{
return true;
}
if ( defaultValue != null )
{
value1 = value1 != null ? value1 : defaultValue;
value2 = value2 != null ? value2 : defaultValue;
return value1.equals( value2 );
}
}
else
{
return value1.equals( value2 );
}
return false;
}
@Override
public TransactionApplier newApplier( boolean recovery )
{
return new LuceneCommandApplier( dataSource, recovery );
}
@Override
public ResourceIterator<File> listStoreFiles() throws IOException
{
return dataSource.listStoreFiles();
}
@Override
public void force()
{
dataSource.force();
}
}