-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
FulltextProvider.java
245 lines (228 loc) · 8.82 KB
/
FulltextProvider.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
/*
* 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 Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.api.impl.fulltext;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.kernel.AvailabilityGuard;
import org.neo4j.kernel.impl.transaction.log.TransactionIdStore;
import org.neo4j.logging.Log;
import org.neo4j.scheduler.JobScheduler;
/**
* Provider class that manages and provides fulltext indices. This is the main entry point for the fulltext addon.
*/
public class FulltextProvider implements AutoCloseable
{
public static final String LUCENE_FULLTEXT_ADDON_PREFIX = "__lucene__fulltext__addon__";
public static final String FIELD_ENTITY_ID = LUCENE_FULLTEXT_ADDON_PREFIX + "internal__id__";
public static final String FIELD_METADATA_DOC = LUCENE_FULLTEXT_ADDON_PREFIX + "metadata__doc__field__";
public static final String FIELD_CONFIG_ANALYZER = LUCENE_FULLTEXT_ADDON_PREFIX + "analyzer";
public static final String FIELD_CONFIG_PROPERTIES = LUCENE_FULLTEXT_ADDON_PREFIX + "properties";
public static final String FIELD_LAST_COMMITTED_TX_ID = LUCENE_FULLTEXT_ADDON_PREFIX + "tx__id";
private final GraphDatabaseService db;
private final Log log;
private final TransactionIdStore transactionIdStore;
private final FulltextTransactionEventUpdater fulltextTransactionEventUpdater;
private final Set<String> nodeProperties;
private final Set<String> relationshipProperties;
private final Set<WritableFulltext> writableNodeIndices;
private final Set<WritableFulltext> writableRelationshipIndices;
private final Map<String,LuceneFulltext> nodeIndices;
private final Map<String,LuceneFulltext> relationshipIndices;
private final FulltextUpdateApplier applier;
/**
* Creates a provider of fulltext indices for the given database. This is the entry point for all fulltext index operations.
* @param db Database that this provider should work with.
* @param log For logging errors.
* @param availabilityGuard Used for waiting with populating the index until the database is available.
* @param scheduler For background work.
* @param transactionIdStore
*/
public FulltextProvider( GraphDatabaseService db, Log log, AvailabilityGuard availabilityGuard,
JobScheduler scheduler, TransactionIdStore transactionIdStore )
{
this.db = db;
this.log = log;
this.transactionIdStore = transactionIdStore;
applier = new FulltextUpdateApplier( log, availabilityGuard, scheduler );
applier.start();
fulltextTransactionEventUpdater = new FulltextTransactionEventUpdater( this, log, applier );
nodeProperties = new HashSet<>();
relationshipProperties = new HashSet<>();
writableNodeIndices = new HashSet<>();
writableRelationshipIndices = new HashSet<>();
nodeIndices = new HashMap<>();
relationshipIndices = new HashMap<>();
}
public void init() throws IOException
{
for ( WritableFulltext index : writableNodeIndices )
{
index.open();
if ( !matchesConfiguration( index ) )
{
index.drop();
index.open();
applier.populateNodes( index, db );
}
}
for ( WritableFulltext index : writableRelationshipIndices )
{
index.open();
if ( !matchesConfiguration( index ) )
{
index.drop();
index.open();
applier.populateRelationships( index, db );
}
}
db.registerTransactionEventHandler( fulltextTransactionEventUpdater );
}
private boolean matchesConfiguration( WritableFulltext index ) throws IOException
{
long txId = transactionIdStore.getLastCommittedTransactionId();
FulltextIndexConfiguration currentConfig =
new FulltextIndexConfiguration( index.getAnalyzerName(), index.getProperties(), txId );
try ( ReadOnlyFulltext indexReader = index.getIndexReader() )
{
FulltextIndexConfiguration storedConfig = indexReader.getConfigurationDocument();
return storedConfig != null && storedConfig.equals( currentConfig );
}
}
/**
* Wait for the asynchronous background population, if one is on-going, to complete.
*
* Such population, where the entire store is scanned for data to write to the index, will be started if the index
* needs to recover after an unclean shut-down, or a configuration change.
* @throws IOException If it was not possible to wait for the population to finish, for some reason.
*/
public void awaitPopulation() throws IOException
{
applier.writeBarrier().awaitCompletion();
}
/**
* Closes the provider and all associated resources.
*/
@Override
public void close()
{
db.unregisterTransactionEventHandler( fulltextTransactionEventUpdater );
applier.stop();
nodeIndices.values().forEach( luceneFulltextIndex ->
{
try
{
luceneFulltextIndex.saveConfiguration( transactionIdStore.getLastCommittedTransactionId() );
luceneFulltextIndex.close();
}
catch ( IOException e )
{
log.error( "Unable to close fulltext node index.", e );
}
} );
relationshipIndices.values().forEach( luceneFulltextIndex ->
{
try
{
luceneFulltextIndex.close();
}
catch ( IOException e )
{
log.error( "Unable to close fulltext relationship index.", e );
}
} );
}
void register( LuceneFulltext fulltextIndex ) throws IOException
{
if ( fulltextIndex.getType() == FulltextIndexType.NODES )
{
nodeIndices.put( fulltextIndex.getIdentifier(), fulltextIndex );
writableNodeIndices.add( new WritableFulltext( fulltextIndex ) );
nodeProperties.addAll( fulltextIndex.getProperties() );
}
else
{
relationshipIndices.put( fulltextIndex.getIdentifier(), fulltextIndex );
writableRelationshipIndices.add( new WritableFulltext( fulltextIndex ) );
relationshipProperties.addAll( fulltextIndex.getProperties() );
}
}
String[] getNodeProperties()
{
return nodeProperties.toArray( new String[0] );
}
String[] getRelationshipProperties()
{
return relationshipProperties.toArray( new String[0] );
}
Set<WritableFulltext> writableNodeIndices()
{
return Collections.unmodifiableSet( writableNodeIndices );
}
Set<WritableFulltext> writableRelationshipIndices()
{
return Collections.unmodifiableSet( writableRelationshipIndices );
}
/**
* Returns a reader for the specified index.
*
* @param identifier Identifier for the index.
* @param type Type of the index.
* @return A {@link ReadOnlyFulltext} for the index, or null if no such index is found.
* @throws IOException
*/
public ReadOnlyFulltext getReader( String identifier, FulltextIndexType type ) throws IOException
{
if ( type == FulltextIndexType.NODES )
{
return nodeIndices.get( identifier ).getIndexReader();
}
else
{
return relationshipIndices.get( identifier ).getIndexReader();
}
}
/**
* Fulltext index type.
*/
public enum FulltextIndexType
{
NODES
{
@Override
public String toString()
{
return "Nodes";
}
},
RELATIONSHIPS
{
@Override
public String toString()
{
return "Relationships";
}
}
}
}