-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
InMemoryIndex.java
337 lines (296 loc) · 9.4 KB
/
InMemoryIndex.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
/*
* 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.kernel.impl.api.index.inmemory;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.helpers.collection.BoundedIterable;
import org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException;
import org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException;
import org.neo4j.kernel.api.index.IndexAccessor;
import org.neo4j.kernel.api.index.IndexEntryUpdate;
import org.neo4j.kernel.api.index.IndexPopulator;
import org.neo4j.kernel.api.index.IndexUpdater;
import org.neo4j.kernel.api.index.InternalIndexState;
import org.neo4j.kernel.api.index.PropertyAccessor;
import org.neo4j.kernel.impl.api.index.IndexUpdateMode;
import org.neo4j.storageengine.api.schema.IndexReader;
import org.neo4j.storageengine.api.schema.IndexSample;
import org.neo4j.values.storable.Value;
import static org.neo4j.helpers.collection.Iterators.emptyResourceIterator;
class InMemoryIndex
{
protected final InMemoryIndexImplementation indexData;
private InternalIndexState state = InternalIndexState.POPULATING;
String failure;
InMemoryIndex()
{
this( new HashBasedIndex() );
}
private InMemoryIndex( InMemoryIndexImplementation indexData )
{
this.indexData = indexData;
}
@Override
public String toString()
{
if ( failure != null )
{
return String.format( "%s[failure=\"%s\"]%s", getClass().getSimpleName(), failure, indexData );
}
else
{
return String.format( "%s%s", getClass().getSimpleName(), indexData );
}
}
final IndexPopulator getPopulator()
{
return new Populator();
}
final IndexAccessor getOnlineAccessor()
{
return new OnlineAccessor();
}
protected boolean add( long nodeId, Value[] propertyValues, boolean applyIdempotently )
{
assert propertyValues.length > 0;
return indexData.add( nodeId, applyIdempotently, propertyValues );
}
protected void remove( long nodeId, Value[] propertyValues )
{
assert propertyValues.length > 0;
indexData.remove( nodeId, propertyValues );
}
protected void remove( long nodeId )
{
indexData.remove( nodeId );
}
InternalIndexState getState()
{
return state;
}
private class Populator implements IndexPopulator
{
private Populator()
{
}
@Override
public void create()
{
indexData.initialize();
}
@Override
public void add( Collection<? extends IndexEntryUpdate<?>> updates )
throws IndexEntryConflictException, IOException
{
for ( IndexEntryUpdate<?> update : updates )
{
InMemoryIndex.this.add( update.getEntityId(), update.values(), false );
}
}
@Override
public void verifyDeferredConstraints( PropertyAccessor accessor ) throws IndexEntryConflictException, IOException
{
InMemoryIndex.this.verifyDeferredConstraints( accessor );
}
@Override
public IndexUpdater newPopulatingUpdater( PropertyAccessor propertyAccessor ) throws IOException
{
return InMemoryIndex.this.newUpdater( IndexUpdateMode.ONLINE, true );
}
@Override
public void drop() throws IOException
{
indexData.drop();
}
@Override
public void close( boolean populationCompletedSuccessfully ) throws IOException
{
if ( populationCompletedSuccessfully )
{
state = InternalIndexState.ONLINE;
}
}
@Override
public void markAsFailed( String failureString )
{
failure = failureString;
state = InternalIndexState.FAILED;
}
@Override
public void includeSample( IndexEntryUpdate<?> update )
{
}
@Override
public IndexSample sampleResult()
{
try
{
return indexData.createSampler().sampleIndex();
}
catch ( IndexNotFoundKernelException e )
{
throw new IllegalStateException( e );
}
}
}
public void verifyDeferredConstraints( PropertyAccessor accessor ) throws IndexEntryConflictException, IOException
{
}
private class OnlineAccessor implements IndexAccessor
{
@Override
public void force()
{
}
@Override
public void refresh()
{
}
@Override
public void drop()
{
indexData.drop();
}
@Override
public IndexUpdater newUpdater( final IndexUpdateMode mode )
{
return InMemoryIndex.this.newUpdater( mode, false );
}
@Override
public void close()
{
}
@Override
public IndexReader newReader()
{
return indexData;
}
@Override
public BoundedIterable<Long> newAllEntriesReader()
{
return indexData;
}
@Override
public ResourceIterator<File> snapshotFiles()
{
return emptyResourceIterator();
}
@Override
public void verifyDeferredConstraints( PropertyAccessor propertyAccessor )
throws IndexEntryConflictException, IOException
{
InMemoryIndex.this.verifyDeferredConstraints( propertyAccessor );
}
}
protected IndexUpdater newUpdater( IndexUpdateMode mode, boolean populating )
{
return new InMemoryIndexUpdater( populating );
}
private class InMemoryIndexUpdater implements IndexUpdater
{
private final boolean applyIdempotently;
private InMemoryIndexUpdater( boolean applyIdempotently )
{
this.applyIdempotently = applyIdempotently;
}
@Override
public void process( IndexEntryUpdate<?> update ) throws IOException, IndexEntryConflictException
{
switch ( update.updateMode() )
{
case ADDED:
InMemoryIndex.this.add( update.getEntityId(), update.values(), applyIdempotently );
break;
case CHANGED:
InMemoryIndex.this.remove( update.getEntityId(), update.beforeValues() );
add( update.getEntityId(), update.values(), applyIdempotently );
break;
case REMOVED:
InMemoryIndex.this.remove( update.getEntityId(), update.values() );
break;
default:
throw new UnsupportedOperationException();
}
}
@Override
public void close() throws IOException, IndexEntryConflictException
{
}
}
InMemoryIndex snapshot()
{
InMemoryIndex snapshot = new InMemoryIndex( indexData.snapshot() );
snapshot.failure = this.failure;
snapshot.state = this.state;
return snapshot;
}
static String encodeAsString( Object propertyValue )
{
String repr;
if ( propertyValue instanceof int[] )
{
repr = Arrays.toString( (int[]) propertyValue );
}
else if ( propertyValue instanceof long[] )
{
repr = Arrays.toString( (long[]) propertyValue );
}
else if ( propertyValue instanceof boolean[] )
{
repr = Arrays.toString( (boolean[]) propertyValue );
}
else if ( propertyValue instanceof double[] )
{
repr = Arrays.toString( (double[]) propertyValue );
}
else if ( propertyValue instanceof float[] )
{
repr = Arrays.toString( (float[]) propertyValue );
}
else if ( propertyValue instanceof short[] )
{
repr = Arrays.toString( (short[]) propertyValue );
}
else if ( propertyValue instanceof byte[] )
{
repr = Arrays.toString( (byte[]) propertyValue );
}
else if ( propertyValue instanceof char[] )
{
repr = Arrays.toString( (char[]) propertyValue );
}
else if ( propertyValue instanceof Object[] )
{
repr = Arrays.toString( (Object[]) propertyValue );
}
else
{
repr = propertyValue.toString();
}
return repr;
}
boolean hasSameContentsAs( InMemoryIndex otherIndex )
{
return indexData.hasSameContentsAs( otherIndex.indexData );
}
}