-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
LuceneIndexAccessorReader.java
194 lines (177 loc) · 6.46 KB
/
LuceneIndexAccessorReader.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
/*
* 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.kernel.api.impl.index;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermEnum;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.HashSet;
import java.util.Set;
import org.neo4j.collection.primitive.PrimitiveLongIterator;
import org.neo4j.helpers.CancellationRequest;
import org.neo4j.index.impl.lucene.Hits;
import org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException;
import org.neo4j.kernel.api.index.IndexReader;
import org.neo4j.kernel.impl.api.index.sampling.NonUniqueIndexSampler;
import static org.neo4j.kernel.api.impl.index.LuceneDocumentStructure.*;
import static org.neo4j.kernel.api.impl.index.LuceneDocumentStructure.NODE_ID_KEY;
import static org.neo4j.register.Register.DoubleLong;
class LuceneIndexAccessorReader implements IndexReader
{
private final IndexSearcher searcher;
private final LuceneDocumentStructure documentLogic;
private final Closeable onClose;
private final CancellationRequest cancellation;
private final int bufferSizeLimit;
LuceneIndexAccessorReader( IndexSearcher searcher, LuceneDocumentStructure documentLogic, Closeable onClose,
CancellationRequest cancellation, int bufferSizeLimit )
{
this.searcher = searcher;
this.documentLogic = documentLogic;
this.onClose = onClose;
this.cancellation = cancellation;
this.bufferSizeLimit = bufferSizeLimit;
}
@Override
public long sampleIndex( DoubleLong.Out result ) throws IndexNotFoundKernelException
{
NonUniqueIndexSampler sampler = new NonUniqueIndexSampler( bufferSizeLimit );
org.apache.lucene.index.IndexReader indexReader = luceneIndexReader();
try ( TermEnum terms = indexReader.terms() )
{
while ( terms.next() )
{
Term term = terms.term();
if ( !NODE_ID_KEY.equals( term.field() ))
{
String value = term.text();
int frequency = terms.docFreq();
sampler.include( value, frequency );
}
checkCancellation();
}
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
sampler.result( result );
// Here do not return the count from the sampler, since when traversing all terms lucene will consider also the
// logical delete once that are gonna be physically deleted in the next compaction. The index size can be
// computed exactly by asking the current number of documents in the lucene index.
return indexReader.numDocs();
}
@Override
public PrimitiveLongIterator lookup( Object value )
{
try
{
Hits hits = new Hits( searcher, documentLogic.newQuery( value ), null );
return new HitsPrimitiveLongIterator( hits, documentLogic );
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
}
@Override
public int getIndexedCount( long nodeId, Object propertyValue )
{
Query nodeIdQuery = new TermQuery( documentLogic.newQueryForChangeOrRemove( nodeId ) );
Query valueQuery = documentLogic.newQuery( propertyValue );
BooleanQuery nodeIdAndValueQuery = new BooleanQuery( true );
nodeIdAndValueQuery.add( nodeIdQuery, BooleanClause.Occur.MUST );
nodeIdAndValueQuery.add( valueQuery, BooleanClause.Occur.MUST );
try
{
Hits hits = new Hits( searcher, nodeIdAndValueQuery, null );
// A <label,propertyKeyId,nodeId> tuple should only match at most a single propertyValue
return hits.length();
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
}
@Override
public Set<Class> valueTypesInIndex()
{
Set<Class> types = new HashSet<>();
try ( TermEnum terms = luceneIndexReader().terms() )
{
while ( terms.next() )
{
String field = terms.term().field();
if ( !NODE_ID_KEY.equals( field ) )
{
switch ( ValueEncoding.fromKey( field ) )
{
case Number:
types.add( Number.class );
break;
case String:
types.add( String.class );
break;
case Array:
types.add( Array.class );
break;
case Bool:
types.add( Boolean.class );
break;
}
}
}
}
catch ( IOException ex )
{
throw new RuntimeException( ex );
}
return types;
}
@Override
public void close()
{
try
{
onClose.close();
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
}
protected void checkCancellation() throws IndexNotFoundKernelException
{
if ( cancellation.cancellationRequested() )
{
throw new IndexNotFoundKernelException( "Index dropped while sampling." );
}
}
protected org.apache.lucene.index.IndexReader luceneIndexReader()
{
return searcher.getIndexReader();
}
}