-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
SpatialKnownIndex.java
559 lines (509 loc) · 17.9 KB
/
SpatialKnownIndex.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
* Copyright (c) 2002-2018 "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.index.schema;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.neo4j.concurrent.WorkSync;
import org.neo4j.gis.spatial.index.Envelope;
import org.neo4j.gis.spatial.index.curves.HilbertSpaceFillingCurve2D;
import org.neo4j.gis.spatial.index.curves.SpaceFillingCurve;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.helpers.collection.BoundedIterable;
import org.neo4j.index.internal.gbptree.GBPTree;
import org.neo4j.index.internal.gbptree.RecoveryCleanupWorkCollector;
import org.neo4j.index.internal.gbptree.Writer;
import org.neo4j.internal.kernel.api.InternalIndexState;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.pagecache.IOLimiter;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.kernel.api.index.IndexDirectoryStructure;
import org.neo4j.kernel.api.index.IndexEntryUpdate;
import org.neo4j.kernel.api.index.IndexUpdater;
import org.neo4j.kernel.api.index.SchemaIndexProvider;
import org.neo4j.kernel.api.schema.index.IndexDescriptor;
import org.neo4j.kernel.impl.api.index.sampling.DefaultNonUniqueIndexSampler;
import org.neo4j.kernel.impl.api.index.sampling.IndexSamplingConfig;
import org.neo4j.kernel.impl.api.index.sampling.UniqueIndexSampler;
import org.neo4j.kernel.impl.index.schema.NativeSchemaIndexPopulator.IndexUpdateApply;
import org.neo4j.kernel.impl.index.schema.NativeSchemaIndexPopulator.IndexUpdateWork;
import org.neo4j.storageengine.api.schema.IndexReader;
import org.neo4j.storageengine.api.schema.IndexSample;
import org.neo4j.values.storable.CoordinateReferenceSystem;
import static org.neo4j.helpers.collection.Iterators.asResourceIterator;
import static org.neo4j.helpers.collection.Iterators.iterator;
import static org.neo4j.index.internal.gbptree.GBPTree.NO_HEADER_WRITER;
import static org.neo4j.kernel.impl.index.schema.NativeSchemaIndexPopulator.BYTE_FAILED;
import static org.neo4j.kernel.impl.index.schema.NativeSchemaIndexPopulator.BYTE_ONLINE;
import static org.neo4j.kernel.impl.index.schema.NativeSchemaIndexPopulator.BYTE_POPULATING;
import static org.neo4j.values.storable.CoordinateReferenceSystem.WGS84;
/**
* An instance of this class represents a dynamically created sub-index specific to a particular coordinate reference system.
* This allows the fusion index design to be extended to an unknown number of sub-indexes, one for each CRS.
*/
public class SpatialKnownIndex
{
private UniqueIndexSampler uniqueSampler;
private final File indexFile;
private final PageCache pageCache;
private final CoordinateReferenceSystem crs;
private final long indexId;
private final FileSystemAbstraction fs;
private final SchemaIndexProvider.Monitor monitor;
private final RecoveryCleanupWorkCollector recoveryCleanupWorkCollector;
private final SpaceFillingCurve curve;
private State state;
private boolean dropped;
private byte[] failureBytes;
private SpatialSchemaKey treeKey;
private NativeSchemaValue treeValue;
private SpatialLayout layout;
private NativeSchemaIndexUpdater<SpatialSchemaKey,NativeSchemaValue> singleUpdater;
private Writer<SpatialSchemaKey,NativeSchemaValue> singleTreeWriter;
private NativeSchemaIndex<SpatialSchemaKey,NativeSchemaValue> schemaIndex;
private WorkSync<IndexUpdateApply<SpatialSchemaKey,NativeSchemaValue>,IndexUpdateWork<SpatialSchemaKey,NativeSchemaValue>> workSync;
private DefaultNonUniqueIndexSampler generalSampler;
/**
* Create a representation of a spatial index for a specific coordinate reference system.
* This constructor should be used for first time creation.
*/
public SpatialKnownIndex( IndexDirectoryStructure directoryStructure, CoordinateReferenceSystem crs, long indexId, PageCache pageCache,
FileSystemAbstraction fs, SchemaIndexProvider.Monitor monitor, RecoveryCleanupWorkCollector recoveryCleanupWorkCollector )
{
this.crs = crs;
this.indexId = indexId;
this.pageCache = pageCache;
this.fs = fs;
this.monitor = monitor;
this.recoveryCleanupWorkCollector = recoveryCleanupWorkCollector;
// Depends on crs
SchemaIndexProvider.Descriptor crsDescriptor =
new SchemaIndexProvider.Descriptor( Integer.toString( crs.getTable().getTableId() ), Integer.toString( crs.getCode() ) );
IndexDirectoryStructure indexDir =
IndexDirectoryStructure.directoriesBySubProvider( directoryStructure ).forProvider( crsDescriptor );
indexFile = new File( indexDir.directoryForIndex( indexId ), "index-" + indexId );
curve = new HilbertSpaceFillingCurve2D( envelopeFromCRS( crs ), 8 );
state = State.NONE;
}
/**
* Makes sure that the index is initialized
*/
public void init( IndexDescriptor descriptor, IndexSamplingConfig samplingConfig )
{
if ( state == State.NONE )
{
initialize( descriptor, samplingConfig );
}
}
/**
* Makes sure that the index is ready to populate
*/
public void startPopulation( IndexDescriptor descriptor, IndexSamplingConfig samplingConfig ) throws IOException
{
init( descriptor, samplingConfig );
if ( state == State.INIT )
{
// First add to sub-index, make sure to create
create();
}
if ( state != State.POPULATING )
{
throw new IllegalStateException( "Failed to start populating index." );
}
}
/**
* Makes sure that the index is online
*/
public void takeOnline( IndexDescriptor descriptor, IndexSamplingConfig samplingConfig ) throws IOException
{
init( descriptor, samplingConfig );
if ( !indexExists() )
{
throw new IOException( "Index file does not exist." );
}
if ( state == State.INIT || state == State.POPULATED )
{
online();
}
if ( state != State.ONLINE )
{
throw new IllegalStateException( "Failed to bring index online." );
}
}
public IndexUpdater updaterWithCreate( IndexDescriptor descriptor, IndexSamplingConfig samplingConfig, boolean populating ) throws IOException
{
if ( populating )
{
if ( state == State.NONE )
{
// sub-index didn't exist, create in populating mode
initialize( descriptor, samplingConfig );
create();
}
return newPopulatingUpdater();
}
else
{
if ( state == State.NONE )
{
// sub-index didn't exist, create and make it online
initialize( descriptor, samplingConfig );
create();
finishPopulation( true );
online();
}
return newUpdater();
}
}
// ONLINE
public void close() throws IOException
{
schemaIndex.closeTree();
}
public BoundedIterable<Long> newAllEntriesReader()
{
return new NativeAllEntriesReader<>( schemaIndex.tree, layout );
}
public IndexReader newReader( IndexSamplingConfig samplingConfig, IndexDescriptor descriptor )
{
schemaIndex.assertOpen();
return new SpatialSchemaIndexReader<>( schemaIndex.tree, layout, samplingConfig, descriptor );
}
public ResourceIterator<File> snapshotFiles()
{
return asResourceIterator( iterator( indexFile ) );
}
public void force() throws IOException
{
// TODO add IOLimiter arg
schemaIndex.tree.checkpoint( IOLimiter.unlimited() );
}
private IndexUpdater newUpdater()
{
schemaIndex.assertOpen();
try
{
return singleUpdater.initialize( schemaIndex.tree.writer(), true );
}
catch ( IOException e )
{
throw new UncheckedIOException( e );
}
}
// POPULATING
public synchronized void finishPopulation( boolean populationCompletedSuccessfully ) throws IOException
{
assert state == State.POPULATING;
closeWriter();
if ( populationCompletedSuccessfully && failureBytes != null )
{
throw new IllegalStateException( "Can't mark index as online after it has been marked as failure" );
}
try
{
if ( populationCompletedSuccessfully )
{
schemaIndex.assertOpen();
markTreeAsOnline();
state = State.POPULATED;
}
else
{
assertNotDropped();
ensureTreeInstantiated();
markTreeAsFailed();
state = State.FAILED;
}
}
finally
{
schemaIndex.closeTree();
}
}
public void add( Collection<IndexEntryUpdate<?>> updates ) throws IOException
{
applyWithWorkSync( updates );
}
public void includeSample( IndexEntryUpdate<?> update )
{
if ( uniqueSampler != null )
{
uniqueSampler.increment( 1 );
}
else if ( generalSampler != null )
{
generalSampler.include( SamplingUtil.encodedStringValuesForSampling( (Object[]) update.values() ) );
}
else
{
throw new UnsupportedOperationException();
}
}
public IndexSample sampleResult()
{
if ( uniqueSampler != null )
{
return uniqueSampler.result();
}
else if ( generalSampler != null )
{
// Close the writer before scanning
try
{
closeWriter();
}
catch ( IOException e )
{
throw new UncheckedIOException( e );
}
try
{
return generalSampler.result();
}
finally
{
try
{
instantiateWriter();
}
catch ( IOException e )
{
throw new UncheckedIOException( e );
}
}
}
else
{
throw new UnsupportedOperationException();
}
}
private IndexUpdater newPopulatingUpdater()
{
return new IndexUpdater()
{
private boolean closed;
private final Collection<IndexEntryUpdate<?>> updates = new ArrayList<>();
@Override
public void process( IndexEntryUpdate<?> update )
{
assertOpen();
updates.add( update );
}
@Override
public void close() throws IOException
{
applyWithWorkSync( updates );
closed = true;
}
private void assertOpen()
{
if ( closed )
{
throw new IllegalStateException( "Updater has been closed" );
}
}
};
}
// GENERAL
public synchronized void drop() throws IOException
{
try
{
closeWriter();
schemaIndex.closeTree();
schemaIndex.gbpTreeFileUtil.deleteFileIfPresent( indexFile );
}
finally
{
dropped = true;
state = State.NONE;
}
}
public void markAsFailed( String failure )
{
failureBytes = failure.getBytes( StandardCharsets.UTF_8 );
state = State.FAILED;
}
public boolean indexExists()
{
return fs.fileExists( indexFile );
}
public String readPopulationFailure( IndexDescriptor descriptor ) throws IOException
{
NativeSchemaIndexHeaderReader headerReader = new NativeSchemaIndexHeaderReader();
GBPTree.readHeader( pageCache, indexFile, layout( descriptor ), headerReader );
return headerReader.failureMessage;
}
public InternalIndexState readState( IndexDescriptor descriptor ) throws IOException
{
NativeSchemaIndexHeaderReader headerReader = new NativeSchemaIndexHeaderReader();
GBPTree.readHeader( pageCache, indexFile, layout( descriptor ), headerReader );
switch ( headerReader.state )
{
case BYTE_FAILED:
return InternalIndexState.FAILED;
case BYTE_ONLINE:
return InternalIndexState.ONLINE;
case BYTE_POPULATING:
return InternalIndexState.POPULATING;
default:
throw new IllegalStateException( "Unexpected initial state byte value " + headerReader.state );
}
}
private synchronized void create() throws IOException
{
assert state == State.INIT;
schemaIndex.gbpTreeFileUtil.deleteFileIfPresent( indexFile );
schemaIndex.instantiateTree( RecoveryCleanupWorkCollector.IMMEDIATE, new NativeSchemaIndexHeaderWriter( BYTE_POPULATING ) );
instantiateWriter();
workSync = new WorkSync<>( new IndexUpdateApply<>( treeKey, treeValue, singleTreeWriter, new ConflictDetectingValueMerger<>() ) );
state = State.POPULATING;
}
private void initialize( IndexDescriptor descriptor, IndexSamplingConfig samplingConfig )
{
assert state == State.NONE;
layout = layout( descriptor );
treeKey = layout.newKey();
treeValue = layout.newValue();
schemaIndex = new NativeSchemaIndex<>( pageCache, fs, indexFile, layout, monitor, descriptor, indexId );
if ( isUnique( descriptor ) )
{
uniqueSampler = new UniqueIndexSampler();
}
else
{
generalSampler = new DefaultNonUniqueIndexSampler( samplingConfig.sampleSizeLimit() );
}
state = State.INIT;
}
private void online() throws IOException
{
assert state == State.POPULATED || state == State.INIT;
singleUpdater = new NativeSchemaIndexUpdater<>( treeKey, treeValue );
schemaIndex.instantiateTree( recoveryCleanupWorkCollector, NO_HEADER_WRITER );
state = State.ONLINE;
}
private void instantiateWriter() throws IOException
{
assert singleTreeWriter == null;
singleTreeWriter = schemaIndex.tree.writer();
}
private void applyWithWorkSync( Collection<? extends IndexEntryUpdate<?>> updates ) throws IOException
{
try
{
workSync.apply( new IndexUpdateWork<>( updates ) );
}
catch ( ExecutionException e )
{
throw new IOException( e );
}
}
private void closeWriter() throws IOException
{
singleTreeWriter = schemaIndex.closeIfPresent( singleTreeWriter );
}
private void markTreeAsOnline() throws IOException
{
schemaIndex.tree.checkpoint( IOLimiter.unlimited(), pc -> pc.putByte( BYTE_ONLINE ) );
}
private void markTreeAsFailed() throws IOException
{
if ( failureBytes == null )
{
failureBytes = new byte[0];
}
schemaIndex.tree.checkpoint( IOLimiter.unlimited(), new FailureHeaderWriter( failureBytes ) );
}
private void assertNotDropped()
{
if ( dropped )
{
throw new IllegalStateException( "Populator has already been dropped." );
}
}
private void ensureTreeInstantiated() throws IOException
{
if ( schemaIndex.tree == null )
{
schemaIndex.instantiateTree( RecoveryCleanupWorkCollector.IGNORE, NO_HEADER_WRITER );
}
}
private SpatialLayout layout( IndexDescriptor descriptor )
{
SpatialLayout layout;
if ( isUnique( descriptor ) )
{
layout = new SpatialLayoutUnique( crs, curve );
}
else
{
layout = new SpatialLayoutNonUnique( crs, curve );
}
return layout;
}
private boolean isUnique( IndexDescriptor descriptor )
{
switch ( descriptor.type() )
{
case GENERAL:
return false;
case UNIQUE:
return true;
default:
throw new UnsupportedOperationException( "Unexpected index type " + descriptor.type() );
}
}
private enum State
{
NONE,
INIT,
POPULATING,
POPULATED,
ONLINE,
FAILED
}
public interface Factory
{
SpatialKnownIndex selectAndCreate( Map<CoordinateReferenceSystem,SpatialKnownIndex> indexMap, long indexId,
CoordinateReferenceSystem crs );
}
static Envelope envelopeFromCRS( CoordinateReferenceSystem crs )
{
Envelope curveEnvelope;
//TODO: Support more CRS
if ( crs.equals( WGS84 ) )
{
curveEnvelope = new Envelope( -180, 180, -90, 90 );
}
else
{
// TODO: support user configurable default envelope ranges (neo4j.conf or other)
curveEnvelope = new Envelope( -1000000, 1000000, -1000000, 1000000 );
}
//TODO: support 3D
return curveEnvelope;
}
}