-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
BuiltInProcedures.java
1021 lines (924 loc) · 40.2 KB
/
BuiltInProcedures.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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.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.builtinprocs;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Spliterator;
import java.util.concurrent.TimeUnit;
import java.util.function.LongFunction;
import java.util.stream.Stream;
import org.neo4j.collection.PrimitiveLongResourceIterator;
import org.neo4j.graphdb.DependencyResolver;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.index.Index;
import org.neo4j.graphdb.index.IndexManager;
import org.neo4j.graphdb.index.IndexPopulationProgress;
import org.neo4j.graphdb.index.RelationshipIndex;
import org.neo4j.helpers.collection.Iterators;
import org.neo4j.helpers.collection.MapUtil;
import org.neo4j.helpers.collection.PrefetchingResourceIterator;
import org.neo4j.internal.kernel.api.IndexReference;
import org.neo4j.internal.kernel.api.InternalIndexState;
import org.neo4j.internal.kernel.api.NodeExplicitIndexCursor;
import org.neo4j.internal.kernel.api.RelationshipExplicitIndexCursor;
import org.neo4j.internal.kernel.api.SchemaRead;
import org.neo4j.internal.kernel.api.TokenNameLookup;
import org.neo4j.internal.kernel.api.TokenRead;
import org.neo4j.internal.kernel.api.exceptions.KernelException;
import org.neo4j.internal.kernel.api.exceptions.ProcedureException;
import org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException;
import org.neo4j.internal.kernel.api.schema.SchemaDescriptor;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.SilentTokenNameLookup;
import org.neo4j.kernel.api.Statement;
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.kernel.impl.api.TokenAccess;
import org.neo4j.kernel.impl.api.index.IndexingService;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Description;
import org.neo4j.procedure.Mode;
import org.neo4j.procedure.Name;
import org.neo4j.procedure.Procedure;
import org.neo4j.storageengine.api.schema.PopulationProgress;
import static org.neo4j.helpers.collection.Iterators.asList;
import static org.neo4j.procedure.Mode.READ;
import static org.neo4j.procedure.Mode.SCHEMA;
import static org.neo4j.procedure.Mode.WRITE;
@SuppressWarnings( {"unused", "WeakerAccess"} )
public class BuiltInProcedures
{
private static final int NOT_EXISTING_INDEX_ID = -1;
public static final String EXPLICIT_INDEX_DEPRECATION = "This procedure is deprecated by the schema and full-text indexes, and will be removed in 4.0.";
public static final String DB_SCHEMA_DEPRECATION = "This procedure is deprecated by the db.schema.visualization procedure, and will be removed in 4.0.";
@Context
public KernelTransaction tx;
@Context
public DependencyResolver resolver;
@Context
public GraphDatabaseAPI graphDatabaseAPI;
@Description( "List all labels in the database." )
@Procedure( name = "db.labels", mode = READ )
public Stream<LabelResult> listLabels()
{
List<LabelResult> labelResults = asList( TokenAccess.LABELS.inUse( tx ).map( LabelResult::new ) );
return labelResults.stream();
}
@Description( "List all property keys in the database." )
@Procedure( name = "db.propertyKeys", mode = READ )
public Stream<PropertyKeyResult> listPropertyKeys()
{
List<PropertyKeyResult> propertyKeys =
asList( TokenAccess.PROPERTY_KEYS.inUse( tx ).map( PropertyKeyResult::new ) );
return propertyKeys.stream();
}
@Description( "List all relationship types in the database." )
@Procedure( name = "db.relationshipTypes", mode = READ )
public Stream<RelationshipTypeResult> listRelationshipTypes()
{
List<RelationshipTypeResult> relationshipTypes =
asList( TokenAccess.RELATIONSHIP_TYPES.inUse( tx ).map( RelationshipTypeResult::new ) );
return relationshipTypes.stream();
}
@Description( "List all indexes in the database." )
@Procedure( name = "db.indexes", mode = READ )
public Stream<IndexResult> listIndexes() throws ProcedureException
{
try ( Statement ignore = tx.acquireStatement() )
{
TokenRead tokenRead = tx.tokenRead();
TokenNameLookup tokens = new SilentTokenNameLookup( tokenRead );
IndexingService indexingService = resolver.resolveDependency( IndexingService.class );
SchemaRead schemaRead = tx.schemaRead();
List<IndexReference> indexes = asList( schemaRead.indexesGetAll() );
indexes.sort( Comparator.comparing( a -> a.userDescription( tokens ) ) );
ArrayList<IndexResult> result = new ArrayList<>();
for ( IndexReference index : indexes )
{
try
{
String type;
if ( index.isUnique() )
{
type = IndexType.NODE_UNIQUE_PROPERTY.typeName();
}
else
{
type = IndexType.NODE_LABEL_PROPERTY.typeName();
}
SchemaDescriptor schema = index.schema();
long indexId = getIndexId( indexingService, schema );
List<String> tokenNames = Arrays.asList( tokens.entityTokensGetNames( schema.entityType(), schema.getEntityTokenIds() ) );
List<String> propertyNames = propertyNames( tokens, index );
String description = "INDEX ON " + schema.userDescription( tokens );
InternalIndexState internalIndexState = schemaRead.indexGetState( index );
String state = internalIndexState.toString();
Map<String,String> providerDescriptorMap = indexProviderDescriptorMap( schemaRead.index( schema ) );
PopulationProgress progress = schemaRead.indexGetPopulationProgress( index );
IndexPopulationProgress indexProgress = new IndexPopulationProgress( progress.getCompleted(), progress.getTotal() );
String failureMessage = internalIndexState == InternalIndexState.FAILED ? schemaRead.indexGetFailure( index ) : "";
result.add( new IndexResult( indexId,
description,
index.name(),
tokenNames,
propertyNames,
state,
type,
indexProgress.getCompletedPercentage(),
providerDescriptorMap,
failureMessage ) );
}
catch ( IndexNotFoundKernelException e )
{
throw new ProcedureException( Status.Schema.IndexNotFound, e,
"No index on ", index.userDescription( tokens ) );
}
}
return result.stream();
}
}
@Description( "Wait for an index to come online (for example: CALL db.awaitIndex(\":Person(name)\"))." )
@Procedure( name = "db.awaitIndex", mode = READ )
public void awaitIndex( @Name( "index" ) String index,
@Name( value = "timeOutSeconds", defaultValue = "300" ) long timeout )
throws ProcedureException
{
try ( IndexProcedures indexProcedures = indexProcedures() )
{
indexProcedures.awaitIndex( index, timeout, TimeUnit.SECONDS );
}
}
@Description( "Wait for all indexes to come online (for example: CALL db.awaitIndexes(\"500\"))." )
@Procedure( name = "db.awaitIndexes", mode = READ )
public void awaitIndexes( @Name( value = "timeOutSeconds", defaultValue = "300" ) long timeout )
{
graphDatabaseAPI.schema().awaitIndexesOnline( timeout, TimeUnit.SECONDS );
}
@Description( "Schedule resampling of an index (for example: CALL db.resampleIndex(\":Person(name)\"))." )
@Procedure( name = "db.resampleIndex", mode = READ )
public void resampleIndex( @Name( "index" ) String index ) throws ProcedureException
{
try ( IndexProcedures indexProcedures = indexProcedures() )
{
indexProcedures.resampleIndex( index );
}
}
@Description( "Schedule resampling of all outdated indexes." )
@Procedure( name = "db.resampleOutdatedIndexes", mode = READ )
public void resampleOutdatedIndexes()
{
try ( IndexProcedures indexProcedures = indexProcedures() )
{
indexProcedures.resampleOutdatedIndexes();
}
}
@Procedure( name = "okapi.schema", mode = Mode.READ )
@Description( "Show the derived property schema of the data in tabular form." )
public Stream<SchemaInfoResult> propertySchema()
{
return new SchemaCalculator( tx ).calculateTabularResultStream();
}
@Deprecated
@Description( "Show the schema of the data." )
@Procedure( name = "db.schema", mode = READ, deprecatedBy = DB_SCHEMA_DEPRECATION )
public Stream<SchemaProcedure.GraphResult> schema()
{
return schemaVisualization();
}
@Description( "Visualize the schema of the data. Replaces db.schema." )
@Procedure( name = "db.schema.visualization", mode = READ )
public Stream<SchemaProcedure.GraphResult> schemaVisualization()
{
return Stream.of( new SchemaProcedure( graphDatabaseAPI, tx ).buildSchemaGraph() );
}
@Description( "List all constraints in the database." )
@Procedure( name = "db.constraints", mode = READ )
public Stream<ConstraintResult> listConstraints()
{
SchemaRead schemaRead = tx.schemaRead();
TokenNameLookup tokens = new SilentTokenNameLookup( tx.tokenRead() );
return asList( schemaRead.constraintsGetAll() )
.stream()
.map( constraint -> constraint.prettyPrint( tokens ) )
.sorted()
.map( ConstraintResult::new );
}
@Description( "Create a schema index with specified index provider (for example: CALL db.createIndex(\":Person(name)\", \"lucene+native-2.0\")) - " +
"YIELD index, providerName, status" )
@Procedure( name = "db.createIndex", mode = SCHEMA )
public Stream<SchemaIndexInfo> createIndex(
@Name( "index" ) String index,
@Name( "providerName" ) String providerName )
throws ProcedureException
{
try ( IndexProcedures indexProcedures = indexProcedures() )
{
return indexProcedures.createIndex( index, providerName );
}
}
@Description( "Create a unique property constraint with index backed by specified index provider " +
"(for example: CALL db.createUniquePropertyConstraint(\":Person(name)\", \"lucene+native-2.0\")) - " +
"YIELD index, providerName, status" )
@Procedure( name = "db.createUniquePropertyConstraint", mode = SCHEMA )
public Stream<BuiltInProcedures.SchemaIndexInfo> createUniquePropertyConstraint(
@Name( "index" ) String index,
@Name( "providerName" ) String providerName )
throws ProcedureException
{
try ( IndexProcedures indexProcedures = indexProcedures() )
{
return indexProcedures.createUniquePropertyConstraint( index, providerName );
}
}
@Deprecated
@Description( "Get node from explicit index. Replaces `START n=node:nodes(key = 'A')`" )
@Procedure( name = "db.index.explicit.seekNodes", mode = READ, deprecatedBy = EXPLICIT_INDEX_DEPRECATION )
public Stream<NodeResult> nodeManualIndexSeek( @Name( "indexName" ) String explicitIndexName,
@Name( "key" ) String key,
@Name( "value" ) Object value )
throws ProcedureException
{
try ( Statement ignore = tx.acquireStatement() )
{
NodeExplicitIndexCursor cursor = tx.cursors().allocateNodeExplicitIndexCursor();
tx.indexRead().nodeExplicitIndexLookup( cursor, explicitIndexName, key, value );
return toStream( cursor, id -> new NodeResult( graphDatabaseAPI.getNodeById( id ) ) );
}
catch ( KernelException e )
{
throw new ProcedureException( Status.LegacyIndex.LegacyIndexNotFound, "Node index %s not found",
explicitIndexName );
}
}
@Deprecated
@Description( "Search nodes in explicit index. Replaces `START n=node:nodes('key:foo*')`" )
@Procedure( name = "db.index.explicit.searchNodes", mode = READ, deprecatedBy = EXPLICIT_INDEX_DEPRECATION )
public Stream<WeightedNodeResult> nodeManualIndexSearch( @Name( "indexName" ) String manualIndexName,
@Name( "query" ) Object query )
throws ProcedureException
{
try ( Statement ignore = tx.acquireStatement() )
{
NodeExplicitIndexCursor cursor = tx.cursors().allocateNodeExplicitIndexCursor();
tx.indexRead().nodeExplicitIndexQuery( cursor, manualIndexName, query );
return toWeightedNodeResultStream( cursor );
}
catch ( KernelException e )
{
throw new ProcedureException( Status.LegacyIndex.LegacyIndexNotFound, "Node index %s not found",
manualIndexName );
}
}
@Deprecated
@Description( "Get relationship from explicit index. Replaces `START r=relationship:relIndex(key = 'A')`" )
@Procedure( name = "db.index.explicit.seekRelationships", mode = READ, deprecatedBy = EXPLICIT_INDEX_DEPRECATION )
public Stream<RelationshipResult> relationshipManualIndexSeek( @Name( "indexName" ) String manualIndexName,
@Name( "key" ) String key,
@Name( "value" ) Object value )
throws ProcedureException
{
try ( Statement ignore = tx.acquireStatement() )
{
RelationshipExplicitIndexCursor cursor = tx.cursors().allocateRelationshipExplicitIndexCursor();
tx.indexRead().relationshipExplicitIndexLookup( cursor, manualIndexName, key, value,
-1, -1 );
return toStream( cursor, id -> new RelationshipResult( graphDatabaseAPI.getRelationshipById( id ) ) );
}
catch ( KernelException e )
{
throw new ProcedureException( Status.LegacyIndex.LegacyIndexNotFound, "Relationship index %s not found",
manualIndexName );
}
}
@Deprecated
@Description( "Search relationship in explicit index. Replaces `START r=relationship:relIndex('key:foo*')`" )
@Procedure( name = "db.index.explicit.searchRelationships", mode = READ, deprecatedBy = EXPLICIT_INDEX_DEPRECATION )
public Stream<WeightedRelationshipResult> relationshipManualIndexSearch(
@Name( "indexName" ) String manualIndexName,
@Name( "query" ) Object query )
throws ProcedureException
{
try ( Statement ignore = tx.acquireStatement() )
{
RelationshipExplicitIndexCursor cursor = tx.cursors().allocateRelationshipExplicitIndexCursor();
tx.indexRead().relationshipExplicitIndexQuery( cursor, manualIndexName, query, -1, -1 );
return toWeightedRelationshipResultStream( cursor );
}
catch ( KernelException e )
{
throw new ProcedureException( Status.LegacyIndex.LegacyIndexNotFound, "Relationship index %s not found",
manualIndexName );
}
}
@Deprecated
@Description( "Search relationship in explicit index, starting at the node 'in'." )
@Procedure( name = "db.index.explicit.searchRelationshipsIn", mode = READ, deprecatedBy = EXPLICIT_INDEX_DEPRECATION )
public Stream<WeightedRelationshipResult> relationshipManualIndexSearchWithBoundStartNode(
@Name( "indexName" ) String indexName,
@Name( "in" ) Node in,
@Name( "query" ) Object query )
throws ProcedureException
{
try ( Statement ignore = tx.acquireStatement() )
{
RelationshipExplicitIndexCursor cursor = tx.cursors().allocateRelationshipExplicitIndexCursor();
tx.indexRead().relationshipExplicitIndexQuery( cursor, indexName, query, in.getId(), -1 );
return toWeightedRelationshipResultStream( cursor );
}
catch ( KernelException e )
{
throw new ProcedureException( Status.LegacyIndex.LegacyIndexNotFound, "Relationship index %s not found",
indexName );
}
}
@Deprecated
@Description( "Search relationship in explicit index, ending at the node 'out'." )
@Procedure( name = "db.index.explicit.searchRelationshipsOut", mode = READ, deprecatedBy = EXPLICIT_INDEX_DEPRECATION )
public Stream<WeightedRelationshipResult> relationshipManualIndexSearchWithBoundEndNode(
@Name( "indexName" ) String indexName,
@Name( "out" ) Node out,
@Name( "query" ) Object query )
throws ProcedureException
{
try ( Statement ignore = tx.acquireStatement() )
{
RelationshipExplicitIndexCursor cursor = tx.cursors().allocateRelationshipExplicitIndexCursor();
tx.indexRead().relationshipExplicitIndexQuery( cursor, indexName, query, -1, out.getId() );
return toWeightedRelationshipResultStream( cursor );
}
catch ( KernelException e )
{
throw new ProcedureException( Status.LegacyIndex.LegacyIndexNotFound, "Relationship index %s not found",
indexName );
}
}
@Deprecated
@Description( "Search relationship in explicit index, starting at the node 'in' and ending at 'out'." )
@Procedure( name = "db.index.explicit.searchRelationshipsBetween", mode = READ, deprecatedBy = EXPLICIT_INDEX_DEPRECATION )
public Stream<WeightedRelationshipResult> relationshipManualIndexSearchWithBoundNodes(
@Name( "indexName" ) String indexName,
@Name( "in" ) Node in,
@Name( "out" ) Node out,
@Name( "query" ) Object query )
throws ProcedureException
{
try ( Statement ignore = tx.acquireStatement() )
{
RelationshipExplicitIndexCursor cursor = tx.cursors().allocateRelationshipExplicitIndexCursor();
tx.indexRead().relationshipExplicitIndexQuery( cursor, indexName, query, in.getId(), out.getId() );
return toWeightedRelationshipResultStream( cursor );
}
catch ( KernelException e )
{
throw new ProcedureException( Status.LegacyIndex.LegacyIndexNotFound, "Relationship index %s not found",
indexName );
}
}
@Deprecated
@Description( "Get node from explicit automatic index. Replaces `START n=node:node_auto_index(key = 'A')`" )
@Procedure( name = "db.index.explicit.auto.seekNodes", mode = READ, deprecatedBy = EXPLICIT_INDEX_DEPRECATION )
public Stream<NodeResult> nodeAutoIndexSeek( @Name( "key" ) String key, @Name( "value" ) Object value )
{
try ( Statement ignore = tx.acquireStatement() )
{
NodeExplicitIndexCursor cursor = tx.cursors().allocateNodeExplicitIndexCursor();
tx.indexRead().nodeExplicitIndexLookup( cursor, "node_auto_index", key, value );
return toStream( cursor, id -> new NodeResult( graphDatabaseAPI.getNodeById( id ) ) );
}
catch ( KernelException e )
{
// auto index will not exist if no nodes have been added that match the auto-index rules
return Stream.empty();
}
}
@Deprecated
@Description( "Search nodes in explicit automatic index. Replaces `START n=node:node_auto_index('key:foo*')`" )
@Procedure( name = "db.index.explicit.auto.searchNodes", mode = READ, deprecatedBy = EXPLICIT_INDEX_DEPRECATION )
public Stream<WeightedNodeResult> nodeAutoIndexSearch( @Name( "query" ) Object query )
{
try ( Statement ignore = tx.acquireStatement() )
{
NodeExplicitIndexCursor cursor = tx.cursors().allocateNodeExplicitIndexCursor();
tx.indexRead().nodeExplicitIndexQuery( cursor, "node_auto_index", query );
return toWeightedNodeResultStream( cursor );
}
catch ( KernelException e )
{
// auto index will not exist if no nodes have been added that match the auto-index rules
return Stream.empty();
}
}
@Deprecated
@Description( "Get relationship from explicit automatic index. Replaces `START r=relationship:relationship_auto_index(key " +
"= 'A')`" )
@Procedure( name = "db.index.explicit.auto.seekRelationships", mode = READ, deprecatedBy = EXPLICIT_INDEX_DEPRECATION )
public Stream<RelationshipResult> relationshipAutoIndexSeek( @Name( "key" ) String key,
@Name( "value" ) Object value )
{
try ( Statement ignore = tx.acquireStatement() )
{
RelationshipExplicitIndexCursor cursor = tx.cursors().allocateRelationshipExplicitIndexCursor();
tx.indexRead()
.relationshipExplicitIndexLookup( cursor, "relationship_auto_index", key, value, -1, -1 );
return toStream( cursor, id -> new RelationshipResult( graphDatabaseAPI.getRelationshipById( id ) ) );
}
catch ( KernelException e )
{
// auto index will not exist if no relationships have been added that match the auto-index rules
return Stream.empty();
}
}
@Deprecated
@Description(
"Search relationship in explicit automatic index. Replaces `START r=relationship:relationship_auto_index" +
"('key:foo*')`" )
@Procedure( name = "db.index.explicit.auto.searchRelationships", mode = READ, deprecatedBy = EXPLICIT_INDEX_DEPRECATION )
public Stream<WeightedRelationshipResult> relationshipAutoIndexSearch( @Name( "query" ) Object query )
{
try ( Statement ignore = tx.acquireStatement() )
{
RelationshipExplicitIndexCursor cursor = tx.cursors().allocateRelationshipExplicitIndexCursor();
tx.indexRead().relationshipExplicitIndexQuery( cursor, "relationship_auto_index", query, -1, -1 );
return toWeightedRelationshipResultStream( cursor );
}
catch ( KernelException e )
{
// auto index will not exist if no relationships have been added that match the auto-index rules
return Stream.empty();
}
}
@Deprecated
@Description( "Get or create a node explicit index - YIELD type,name,config" )
@Procedure( name = "db.index.explicit.forNodes", mode = WRITE, deprecatedBy = EXPLICIT_INDEX_DEPRECATION )
public Stream<ExplicitIndexInfo> nodeManualIndex( @Name( "indexName" ) String explicitIndexName,
@Name( value = "config", defaultValue = "" ) Map<String,String> config )
{
IndexManager mgr = graphDatabaseAPI.index();
Index<Node> index;
if ( config == null || config.isEmpty() )
{
index = mgr.forNodes( explicitIndexName );
}
else
{
index = mgr.forNodes( explicitIndexName, config );
}
return Stream.of( new ExplicitIndexInfo( "NODE", explicitIndexName, mgr.getConfiguration( index ) ) );
}
@Deprecated
@Description( "Get or create a relationship explicit index - YIELD type,name,config" )
@Procedure( name = "db.index.explicit.forRelationships", mode = WRITE, deprecatedBy = EXPLICIT_INDEX_DEPRECATION )
public Stream<ExplicitIndexInfo> relationshipManualIndex( @Name( "indexName" ) String explicitIndexName,
@Name( value = "config", defaultValue = "" ) Map<String,String> config )
{
IndexManager mgr = graphDatabaseAPI.index();
Index<Relationship> index;
if ( config == null || config.isEmpty() )
{
index = mgr.forRelationships( explicitIndexName );
}
else
{
index = mgr.forRelationships( explicitIndexName, config );
}
return Stream.of( new ExplicitIndexInfo( "RELATIONSHIP", explicitIndexName, mgr.getConfiguration( index ) ) );
}
@Deprecated
@Description( "Check if a node explicit index exists" )
@Procedure( name = "db.index.explicit.existsForNodes", mode = READ, deprecatedBy = EXPLICIT_INDEX_DEPRECATION )
public Stream<BooleanResult> nodeManualIndexExists( @Name( "indexName" ) String explicitIndexName )
{
return Stream.of( new BooleanResult( graphDatabaseAPI.index().existsForNodes( explicitIndexName ) ) );
}
@Deprecated
@Description( "Check if a relationship explicit index exists" )
@Procedure( name = "db.index.explicit.existsForRelationships", mode = READ, deprecatedBy = EXPLICIT_INDEX_DEPRECATION )
public Stream<BooleanResult> relationshipManualIndexExists( @Name( "indexName" ) String explicitIndexName )
{
return Stream.of( new BooleanResult( graphDatabaseAPI.index().existsForRelationships( explicitIndexName ) ) );
}
@Deprecated
@Description( "List all explicit indexes - YIELD type,name,config" )
@Procedure( name = "db.index.explicit.list", mode = READ, deprecatedBy = EXPLICIT_INDEX_DEPRECATION )
public Stream<ExplicitIndexInfo> list()
{
IndexManager mgr = graphDatabaseAPI.index();
List<ExplicitIndexInfo> indexInfos = new ArrayList<>( 100 );
for ( String name : mgr.nodeIndexNames() )
{
Index<Node> index = mgr.forNodes( name );
indexInfos.add( new ExplicitIndexInfo( "NODE", name, mgr.getConfiguration( index ) ) );
}
for ( String name : mgr.relationshipIndexNames() )
{
RelationshipIndex index = mgr.forRelationships( name );
indexInfos.add( new ExplicitIndexInfo( "RELATIONSHIP", name, mgr.getConfiguration( index ) ) );
}
return indexInfos.stream();
}
@Deprecated
@Description( "Remove an explicit index - YIELD type,name,config" )
@Procedure( name = "db.index.explicit.drop", mode = WRITE, deprecatedBy = EXPLICIT_INDEX_DEPRECATION )
public Stream<ExplicitIndexInfo> manualIndexDrop( @Name( "indexName" ) String explicitIndexName )
{
IndexManager mgr = graphDatabaseAPI.index();
List<ExplicitIndexInfo> results = new ArrayList<>( 2 );
if ( mgr.existsForNodes( explicitIndexName ) )
{
Index<Node> index = mgr.forNodes( explicitIndexName );
results.add( new ExplicitIndexInfo( "NODE", explicitIndexName, mgr.getConfiguration( index ) ) );
index.delete();
}
if ( mgr.existsForRelationships( explicitIndexName ) )
{
RelationshipIndex index = mgr.forRelationships( explicitIndexName );
results.add( new ExplicitIndexInfo( "RELATIONSHIP", explicitIndexName, mgr.getConfiguration( index ) ) );
index.delete();
}
return results.stream();
}
@Deprecated
@Description( "Add a node to an explicit index based on a specified key and value" )
@Procedure( name = "db.index.explicit.addNode", mode = WRITE, deprecatedBy = EXPLICIT_INDEX_DEPRECATION )
public Stream<BooleanResult> nodeManualIndexAdd( @Name( "indexName" ) String explicitIndexName,
@Name( "node" ) Node node, @Name( "key" ) String key,
@Name( "value" ) Object value )
{
graphDatabaseAPI.index().forNodes( explicitIndexName ).add( node, key, value );
// Failures will be expressed as exceptions before the return
return Stream.of( new BooleanResult( Boolean.TRUE ) );
}
@Deprecated
@Description( "Add a relationship to an explicit index based on a specified key and value" )
@Procedure( name = "db.index.explicit.addRelationship", mode = WRITE, deprecatedBy = EXPLICIT_INDEX_DEPRECATION )
public Stream<BooleanResult> relationshipManualIndexAdd( @Name( "indexName" ) String explicitIndexName,
@Name( "relationship" ) Relationship relationship,
@Name( "key" ) String key, @Name( "value" ) Object value )
{
graphDatabaseAPI.index().forRelationships( explicitIndexName ).add( relationship, key, value );
// Failures will be expressed as exceptions before the return
return Stream.of( new BooleanResult( Boolean.TRUE ) );
}
private static final String DEFAULT_KEY = " <[9895b15e-8693-4a21-a58b-4b7b87e09b8e]> ";
@Deprecated
@Description( "Remove a node from an explicit index with an optional key" )
@Procedure( name = "db.index.explicit.removeNode", mode = WRITE, deprecatedBy = EXPLICIT_INDEX_DEPRECATION )
public Stream<BooleanResult> nodeManualIndexRemove( @Name( "indexName" ) String explicitIndexName,
@Name( "node" ) Node node, @Name( value = "key", defaultValue = DEFAULT_KEY ) String key )
{
if ( key.equals( DEFAULT_KEY ) )
{
graphDatabaseAPI.index().forNodes( explicitIndexName ).remove( node );
}
else
{
graphDatabaseAPI.index().forNodes( explicitIndexName ).remove( node, key );
}
// Failures will be expressed as exceptions before the return
return Stream.of( new BooleanResult( Boolean.TRUE ) );
}
@Deprecated
@Description( "Remove a relationship from an explicit index with an optional key" )
@Procedure( name = "db.index.explicit.removeRelationship", mode = WRITE, deprecatedBy = EXPLICIT_INDEX_DEPRECATION )
public Stream<BooleanResult> relationshipManualIndexRemove( @Name( "indexName" ) String explicitIndexName,
@Name( "relationship" ) Relationship relationship,
@Name( value = "key", defaultValue = DEFAULT_KEY ) String key )
{
if ( key.equals( DEFAULT_KEY ) )
{
graphDatabaseAPI.index().forRelationships( explicitIndexName ).remove( relationship );
}
else
{
graphDatabaseAPI.index().forRelationships( explicitIndexName ).remove( relationship, key );
}
// Failures will be expressed as exceptions before the return
return Stream.of( new BooleanResult( Boolean.TRUE ) );
}
private static long getIndexId( IndexingService indexingService, SchemaDescriptor schema )
{
try
{
return indexingService.getIndexId( schema );
}
catch ( IndexNotFoundKernelException e )
{
return NOT_EXISTING_INDEX_ID;
}
}
private static Map<String,String> indexProviderDescriptorMap( IndexReference indexReference )
{
return MapUtil.stringMap(
"key", indexReference.providerKey(),
"version", indexReference.providerVersion() );
}
private static List<String> propertyNames( TokenNameLookup tokens, IndexReference index )
{
int[] propertyIds = index.properties();
List<String> propertyNames = new ArrayList<>( propertyIds.length );
for ( int propertyId : propertyIds )
{
propertyNames.add( tokens.propertyKeyGetName( propertyId ) );
}
return propertyNames;
}
private static <T> Stream<T> toStream( NodeExplicitIndexCursor cursor, LongFunction<T> mapper )
{
PrefetchingResourceIterator<T> it = new PrefetchingResourceIterator<T>()
{
@Override
protected T fetchNextOrNull()
{
if ( cursor.next() )
{
return mapper.apply( cursor.nodeReference() );
}
else
{
close();
return null;
}
}
@Override
public void close()
{
cursor.close();
}
};
return Iterators.stream( it, Spliterator.ORDERED );
}
private static <T> Stream<T> toStream( RelationshipExplicitIndexCursor cursor, LongFunction<T> mapper )
{
PrefetchingResourceIterator<T> it = new PrefetchingResourceIterator<T>()
{
@Override
protected T fetchNextOrNull()
{
if ( cursor.next() )
{
return mapper.apply( cursor.relationshipReference() );
}
else
{
close();
return null;
}
}
@Override
public void close()
{
cursor.close();
}
};
return Iterators.stream( it, Spliterator.ORDERED );
}
private static <T> Stream<T> toStream( PrimitiveLongResourceIterator iterator, LongFunction<T> mapper )
{
Iterator<T> it = new Iterator<T>()
{
@Override
public boolean hasNext()
{
return iterator.hasNext();
}
@Override
public T next()
{
return mapper.apply( iterator.next() );
}
};
return Iterators.stream( it, Spliterator.ORDERED );
}
private Stream<WeightedNodeResult> toWeightedNodeResultStream( NodeExplicitIndexCursor cursor )
{
Iterator<WeightedNodeResult> it = new PrefetchingResourceIterator<WeightedNodeResult>()
{
@Override
public void close()
{
cursor.close();
}
@Override
protected WeightedNodeResult fetchNextOrNull()
{
if ( cursor.next() )
{
return new WeightedNodeResult( graphDatabaseAPI.getNodeById( cursor.nodeReference() ),
cursor.score() );
}
else
{
close();
return null;
}
}
};
return Iterators.stream( it, Spliterator.ORDERED );
}
private Stream<WeightedRelationshipResult> toWeightedRelationshipResultStream(
RelationshipExplicitIndexCursor cursor )
{
Iterator<WeightedRelationshipResult> it = new PrefetchingResourceIterator<WeightedRelationshipResult>()
{
@Override
public void close()
{
cursor.close();
}
@Override
protected WeightedRelationshipResult fetchNextOrNull()
{
if ( cursor.next() )
{
return new WeightedRelationshipResult(
graphDatabaseAPI.getRelationshipById( cursor.relationshipReference() ), cursor.score() );
}
else
{
close();
return null;
}
}
};
return Iterators.stream( it, Spliterator.ORDERED );
}
private IndexProcedures indexProcedures()
{
return new IndexProcedures( tx, resolver.resolveDependency( IndexingService.class ) );
}
public static class LabelResult
{
public final String label;
private LabelResult( Label label )
{
this.label = label.name();
}
}
public static class PropertyKeyResult
{
public final String propertyKey;
private PropertyKeyResult( String propertyKey )
{
this.propertyKey = propertyKey;
}
}
public static class RelationshipTypeResult
{
public final String relationshipType;
private RelationshipTypeResult( RelationshipType relationshipType )
{
this.relationshipType = relationshipType.name();
}
}
public static class BooleanResult
{
public BooleanResult( Boolean success )
{
this.success = success;
}
public final Boolean success;
}
public static class IndexResult
{
public final String description;
public final String indexName;
public final List<String> tokenNames;
public final List<String> properties;
public final String state;
public final String type;
public final Double progress;
public final Map<String,String> provider;
public final long id;
public final String failureMessage;
private IndexResult( long id,
String description,
String indexName,
List<String> tokenNames,
List<String> properties,
String state,
String type,
Float progress,
Map<String,String> provider,
String failureMessage )
{
this.id = id;
this.description = description;
this.indexName = indexName;
this.tokenNames = tokenNames;
this.properties = properties;
this.state = state;
this.type = type;
this.progress = progress.doubleValue();
this.provider = provider;
this.failureMessage = failureMessage;
}
}
public static class SchemaIndexInfo
{
public final String index;
public final String providerName;
public final String status;
public SchemaIndexInfo( String index, String providerName, String status )
{
this.index = index;
this.providerName = providerName;
this.status = status;
}
}
public static class ExplicitIndexInfo
{
public final String type;
public final String name;
public final Map<String,String> config;
public ExplicitIndexInfo( String type, String name, Map<String,String> config )
{
this.type = type;
this.name = name;
this.config = config;
}
}
public static class ConstraintResult
{
public final String description;
private ConstraintResult( String description )
{
this.description = description;
}
}
public static class NodeResult
{
public NodeResult( Node node )
{
this.node = node;
}
public final Node node;
}
public static class WeightedNodeResult
{
public final Node node;
public final double weight;
public WeightedNodeResult( Node node, double weight )
{
this.node = node;
this.weight = weight;
}
}
public static class WeightedRelationshipResult
{
public final Relationship relationship;
public final double weight;
public WeightedRelationshipResult( Relationship relationship, double weight )
{
this.relationship = relationship;
this.weight = weight;
}
}
public static class RelationshipResult
{
public RelationshipResult( Relationship relationship )
{
this.relationship = relationship;
}
public final Relationship relationship;
}