-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
FulltextProvider.java
193 lines (180 loc) · 6.33 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
/*
* 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.impl.logging.LogService;
import org.neo4j.logging.Log;
/**
* Provider class that manages and provides fulltext indices. This is the main entry point for the fulltext addon.
*/
public class FulltextProvider implements AutoCloseable
{
private static FulltextProvider instance;
private final GraphDatabaseService db;
private final Log log;
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 boolean closed;
private FulltextProvider( GraphDatabaseService db, Log log )
{
this.db = db;
this.log = log;
closed = false;
fulltextTransactionEventUpdater = new FulltextTransactionEventUpdater( this, log );
db.registerTransactionEventHandler( fulltextTransactionEventUpdater );
nodeProperties = new HashSet<>();
relationshipProperties = new HashSet<>();
writableNodeIndices = new HashSet<>();
writableRelationshipIndices = new HashSet<>();
nodeIndices = new HashMap<>();
relationshipIndices = new HashMap<>();
}
/**
* Fetch the current instance of the provider. If there is none, create one associated with the given database.
* @param db Database used for eventual creation of the provider.
* @param logService
* @return The instance for the given database.
*/
public static synchronized FulltextProvider instance( GraphDatabaseService db, LogService logService )
{
if ( instance == null || instance.closed )
{
instance = new FulltextProvider( db, logService.getInternalLog( FulltextProvider.class ) );
}
return instance;
}
/**
* Closes the provider and all associated resources.
*/
@Override
public synchronized void close()
{
if ( !closed )
{
closed = true;
db.unregisterTransactionEventHandler( fulltextTransactionEventUpdater );
nodeIndices.values().forEach( luceneFulltextIndex ->
{
try
{
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 );
}
} );
}
}
synchronized void register( LuceneFulltext fulltextIndex ) throws IOException
{
fulltextIndex.open();
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";
}
}
}
}