-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
FullTxData.java
447 lines (411 loc) · 13.7 KB
/
FullTxData.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
* Copyright (c) 2002-2015 "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.lucene.legacy;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.neo4j.collection.primitive.PrimitiveLongIterator;
import org.neo4j.index.lucene.QueryContext;
import org.neo4j.kernel.api.impl.index.DocValuesCollector;
import static java.util.Collections.emptyList;
import static org.neo4j.index.lucene.legacy.LuceneIndex.KEY_DOC_ID;
class FullTxData extends TxData
{
/*
* The concept of orphan exists to find entities when querying where the transaction state
* (i.e. a FullTxData object) has seen removed entities w/o key and potentially also w/o value.
* A TxData instance receiving "add" calls with null key/value is an instance used to track removals.
* A Lucene document storing state about e.g. {@code index.remove( myNode, "name" )}
* <pre>
* {
* __all__: "name"
* }
* </pre>
*
* A Lucene document storing state about e.g. {@code index.remove( myNode )}
* <pre>
* {
* __all__: "1"
* }
* where queries would (if there are any orphans at all stored) include the "all orphans" value ("1") as
* well as any specific key which is pulled out from the incoming query.
*/
private static final String ORPHANS_KEY = "__all__";
/**
* When querying we need to distinguish between documents coming from the store and documents
* coming from transaction state. A field with this key is put on all documents in transaction state.
*/
public static final String TX_STATE_KEY = "__tx_state__";
private static final byte[] TX_STATE_VALUE = new byte[] {1};
private static final String ORPHANS_VALUE = "1";
private Directory directory;
private IndexWriter writer;
private boolean modified;
private IndexReader reader;
private IndexSearcher searcher;
private final Map<Long, Document> cachedDocuments = new HashMap<Long, Document>();
private Set<String> orphans;
FullTxData( LuceneIndex index )
{
super( index );
}
@Override
void add( TxDataHolder holder, EntityId entityId, String key, Object value )
{
try
{
ensureLuceneDataInstantiated();
long id = entityId.id();
Document document = findDocument( id );
boolean add = false;
if ( document == null )
{
document = IndexType.newDocument( entityId );
document.add( new StoredField( TX_STATE_KEY, TX_STATE_VALUE ) );
cachedDocuments.put( id, document );
add = true;
}
if ( key == null && value == null )
{
// Set a special "always hit" flag
document.add( new StringField( ORPHANS_KEY, ORPHANS_VALUE, Store.NO ) );
addOrphan( null );
}
else if ( value == null )
{
// Set a special "always hit" flag
document.add( new StringField( ORPHANS_KEY, key, Store.NO ) );
addOrphan( key );
}
else
{
index.type.addToDocument( document, key, value );
}
if ( add )
{
writer.addDocument( document );
}
else
{
writer.updateDocument( index.type.idTerm( id ), document );
}
invalidateSearcher();
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
}
private void addOrphan( String key )
{
if ( orphans == null )
{
orphans = new HashSet<>();
}
orphans.add( key );
}
private Document findDocument( long id )
{
return cachedDocuments.get( id );
}
private void ensureLuceneDataInstantiated()
{
if ( this.directory == null )
{
try
{
this.directory = new RAMDirectory();
IndexWriterConfig writerConfig = new IndexWriterConfig( index.type.analyzer );
this.writer = new IndexWriter( directory, writerConfig );
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
}
}
@Override
void remove( TxDataHolder holder, EntityId entityId, String key, Object value )
{
try
{
ensureLuceneDataInstantiated();
long id = entityId.id();
Document document = findDocument( id );
if ( document != null )
{
index.type.removeFromDocument( document, key, value );
if ( LuceneDataSource.documentIsEmpty( document ) )
{
writer.deleteDocuments( index.type.idTerm( id ) );
}
else
{
writer.updateDocument( index.type.idTerm( id ), document );
}
}
invalidateSearcher();
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
}
@Override
Collection<EntityId> query( TxDataHolder holder, Query query, QueryContext contextOrNull )
{
return internalQuery( query, contextOrNull );
}
private Collection<EntityId> internalQuery( Query query, QueryContext contextOrNull )
{
if ( this.directory == null )
{
return Collections.emptySet();
}
try
{
Sort sorting = contextOrNull != null ? contextOrNull.getSorting() : null;
boolean prioritizeCorrectness = contextOrNull == null || !contextOrNull.getTradeCorrectnessForSpeed();
IndexSearcher theSearcher = searcher( prioritizeCorrectness );
query = includeOrphans( query );
DocValuesCollector docValuesCollector = new DocValuesCollector( prioritizeCorrectness );
theSearcher.search( query, docValuesCollector );
Collection<EntityId> result = new ArrayList<>();
PrimitiveLongIterator valuesIterator = docValuesCollector.getSortedValuesIterator( KEY_DOC_ID, sorting );
while ( valuesIterator.hasNext() )
{
result.add( new EntityId.IdData( valuesIterator.next() ) );
}
return result;
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
}
private Query includeOrphans( Query query )
{
if ( orphans == null )
{
return query;
}
return new BooleanQuery.Builder()
.add( injectOrphans( query ), Occur.SHOULD )
.add( new TermQuery( new Term( ORPHANS_KEY, ORPHANS_VALUE ) ), Occur.SHOULD )
.build();
}
private Query injectOrphans( Query query )
{
if ( query instanceof BooleanQuery )
{
BooleanQuery source = (BooleanQuery) query;
BooleanQuery.Builder builder = new BooleanQuery.Builder();
for ( BooleanClause clause : source.clauses() )
{
builder.add( injectOrphans( clause.getQuery() ), clause.getOccur() );
}
return builder.build();
}
String orphanField = extractTermField( query );
if ( orphanField == null )
{
return query;
}
return new BooleanQuery.Builder()
.add( query, Occur.SHOULD )
.add( new TermQuery( new Term( ORPHANS_KEY, orphanField ) ), Occur.SHOULD )
.build();
}
private String extractTermField( Query query )
{
// Try common types of queries
if ( query instanceof TermQuery )
{
return ((TermQuery)query).getTerm().field();
}
else if ( query instanceof WildcardQuery )
{
return ((WildcardQuery)query).getTerm().field();
}
else if ( query instanceof PrefixQuery )
{
return ((PrefixQuery)query).getPrefix().field();
}
else if ( query instanceof MatchAllDocsQuery )
{
return null;
}
// Try to extract terms and get it that way
String field = getFieldFromExtractTerms( query );
if ( field != null )
{
return field;
}
// Last resort: since Query doesn't have a common interface for getting
// the term/field of its query this is one option.
return getFieldViaReflection( query );
}
private String getFieldViaReflection( Query query )
{
try
{
try
{
Term term = (Term) query.getClass().getMethod( "getTerm" ).invoke( query );
return term.field();
}
catch ( NoSuchMethodException e )
{
return (String) query.getClass().getMethod( "getField" ).invoke( query );
}
}
catch ( Exception e )
{
throw new RuntimeException( e );
}
}
private String getFieldFromExtractTerms( Query query )
{
Set<Term> terms = new HashSet<Term>();
try
{
searcher.createNormalizedWeight( query, false ).extractTerms( terms );
}
catch ( IOException ioe )
{
throw new UnsupportedOperationException( ioe );
}
catch ( UnsupportedOperationException ue )
{
// TODO This is for "*" queries and such. Lucene doesn't seem
// to be able/willing to rewrite such queries.
// Just ignore the orphans then... OK?
}
return terms.isEmpty() ? null : terms.iterator().next().field();
}
@Override
void close()
{
LuceneUtil.close( this.writer );
LuceneUtil.close( this.reader );
LuceneUtil.close( this.searcher );
}
private void invalidateSearcher()
{
this.modified = true;
}
private IndexSearcher searcher( boolean allowRefreshSearcher )
{
if ( this.searcher != null && (!modified || !allowRefreshSearcher) )
{
return this.searcher;
}
try
{
IndexReader newReader = this.reader == null ?
DirectoryReader.open( this.writer, true ) :
DirectoryReader.openIfChanged( (DirectoryReader) this.reader );
if ( newReader == this.reader )
{
return this.searcher;
}
LuceneUtil.close( reader );
this.reader = newReader;
LuceneUtil.close( searcher );
searcher = new IndexSearcher( reader );
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
finally
{
if ( allowRefreshSearcher )
{
this.modified = false;
}
}
return this.searcher;
}
private static void safeClose( Object object )
{
if ( object == null )
{
return;
}
try
{
if ( object instanceof IndexWriter )
{
( ( IndexWriter ) object ).close();
}
else if ( object instanceof IndexReader )
{
( ( IndexReader ) object ).close();
}
}
catch ( IOException e )
{
// Ok
}
}
@Override
IndexSearcher asSearcher( TxDataHolder holder, QueryContext context )
{
boolean refresh = context == null || !context.getTradeCorrectnessForSpeed();
return searcher( refresh );
}
@Override
Collection<EntityId> get( TxDataHolder holder, String key, Object value )
{
return internalQuery( index.type.get( key, value ), null );
}
@Override
Collection<EntityId> getOrphans( String key )
{
return emptyList();
}
}