-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
BuiltInProceduresIT.java
348 lines (319 loc) · 23 KB
/
BuiltInProceduresIT.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
/*
* 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.impl.api.integrationtest;
import org.hamcrest.Matcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.neo4j.collection.RawIterator;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.collection.MapUtil;
import org.neo4j.internal.kernel.api.TokenWrite;
import org.neo4j.internal.kernel.api.Transaction;
import org.neo4j.internal.kernel.api.exceptions.ProcedureException;
import org.neo4j.internal.kernel.api.procs.ProcedureHandle;
import org.neo4j.kernel.api.ResourceTracker;
import org.neo4j.kernel.api.StubResourceManager;
import org.neo4j.kernel.api.index.IndexProvider;
import org.neo4j.kernel.api.schema.LabelSchemaDescriptor;
import org.neo4j.kernel.api.security.AnonymousContext;
import org.neo4j.kernel.impl.api.index.IndexProviderMap;
import org.neo4j.kernel.impl.api.index.IndexingService;
import org.neo4j.kernel.internal.Version;
import static java.util.Collections.singletonList;
import static java.util.concurrent.TimeUnit.MINUTES;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.fail;
import static org.neo4j.helpers.collection.Iterators.asList;
import static org.neo4j.internal.kernel.api.procs.ProcedureSignature.procedureName;
import static org.neo4j.internal.kernel.api.security.LoginContext.AUTH_DISABLED;
import static org.neo4j.kernel.api.schema.SchemaDescriptorFactory.forLabel;
public class BuiltInProceduresIT extends KernelIntegrationTest
{
@Rule
public ExpectedException exception = ExpectedException.none();
private final ResourceTracker resourceTracker = new StubResourceManager();
@Test
public void listAllLabels() throws Throwable
{
// Given
Transaction transaction = newTransaction( AnonymousContext.writeToken() );
long nodeId = transaction.dataWrite().nodeCreate();
int labelId = transaction.tokenWrite().labelGetOrCreateForName( "MyLabel" );
transaction.dataWrite().nodeAddLabel( nodeId, labelId );
commit();
// When
RawIterator<Object[],ProcedureException> stream =
procs().procedureCallRead( procs().procedureGet( procedureName( "db", "labels" ) ).id(), new Object[0] );
// Then
assertThat( asList( stream ), contains( equalTo( new Object[]{"MyLabel"} ) ) );
}
@Test
public void listPropertyKeys() throws Throwable
{
// Given
TokenWrite ops = tokenWriteInNewTransaction();
ops.propertyKeyGetOrCreateForName( "MyProp" );
commit();
// When
RawIterator<Object[],ProcedureException> stream =
procs().procedureCallRead( procs().procedureGet( procedureName( "db", "propertyKeys" ) ).id(), new Object[0] );
// Then
assertThat( asList( stream ), contains( equalTo( new Object[]{"MyProp"} ) ) );
}
@Test
public void listRelationshipTypes() throws Throwable
{
// Given
Transaction transaction = newTransaction( AnonymousContext.writeToken() );
int relType = transaction.tokenWrite().relationshipTypeGetOrCreateForName( "MyRelType" );
long startNodeId = transaction.dataWrite().nodeCreate();
long endNodeId = transaction.dataWrite().nodeCreate();
transaction.dataWrite().relationshipCreate( startNodeId, relType, endNodeId );
commit();
// When
RawIterator<Object[],ProcedureException> stream =
procs().procedureCallRead( procs().procedureGet( procedureName( "db", "relationshipTypes" ) ).id(), new Object[0] );
// Then
assertThat( asList( stream ), contains( equalTo( new Object[]{"MyRelType"} ) ) );
}
@Test
public void listProcedures() throws Throwable
{
// When
ProcedureHandle procedures = procs().procedureGet( procedureName( "dbms", "procedures" ) );
RawIterator<Object[],ProcedureException> stream = procs().procedureCallRead( procedures.id(), new Object[0] );
// Then
//noinspection unchecked
assertThat( asList( stream ), containsInAnyOrder(
proc( "dbms.listConfig", "(searchString = :: STRING?) :: (name :: STRING?, description :: STRING?, value :: STRING?, dynamic :: BOOLEAN?)",
"List the currently active config of Neo4j.", "DBMS" ),
proc( "db.constraints", "() :: (description :: STRING?)", "List all constraints in the database.", "READ" ),
proc( "db.indexes", "() :: (description :: STRING?, indexName :: STRING?, tokenNames :: LIST? OF STRING?, properties :: " +
"LIST? OF STRING?, state :: STRING?, type :: STRING?, progress :: FLOAT?, provider :: MAP?, id :: INTEGER?, " +
"failureMessage :: STRING?)",
"List all indexes in the database.", "READ" ),
proc( "db.awaitIndex", "(index :: STRING?, timeOutSeconds = 300 :: INTEGER?) :: VOID",
"Wait for an index to come online (for example: CALL db.awaitIndex(\":Person(name)\")).", "READ" ),
proc( "db.awaitIndexes", "(timeOutSeconds = 300 :: INTEGER?) :: VOID",
"Wait for all indexes to come online (for example: CALL db.awaitIndexes(\"500\")).", "READ" ),
proc( "db.resampleIndex", "(index :: STRING?) :: VOID",
"Schedule resampling of an index (for example: CALL db.resampleIndex(\":Person(name)\")).", "READ" ),
proc( "db.resampleOutdatedIndexes", "() :: VOID", "Schedule resampling of all outdated indexes.", "READ" ),
proc( "db.propertyKeys", "() :: (propertyKey :: STRING?)", "List all property keys in the database.", "READ" ),
proc( "db.labels", "() :: (label :: STRING?)", "List all labels in the database.", "READ" ),
proc( "db.schema", "() :: (nodes :: LIST? OF NODE?, relationships :: LIST? " + "OF " + "RELATIONSHIP?)",
"Show the schema of the data.", "READ" ),
proc( "db.schema.visualization","() :: (nodes :: LIST? OF NODE?, relationships :: LIST? OF RELATIONSHIP?)",
"Visualize the schema of the data. Replaces db.schema.", "READ" ),
proc( "db.schema.nodeTypeProperties",
"() :: (nodeType :: STRING?, nodeLabels :: LIST? OF STRING?, propertyName :: STRING?, " +
"propertyTypes :: LIST? OF STRING?, mandatory :: BOOLEAN?)",
"Show the derived property schema of the nodes in tabular form.", "READ" ),
proc( "db.schema.relTypeProperties", "() :: (relType :: STRING?, " +
"propertyName :: STRING?, propertyTypes :: LIST? OF STRING?, mandatory :: BOOLEAN?)",
"Show the derived property schema of the relationships in tabular form.", "READ" ),
proc( "db.relationshipTypes", "() :: (relationshipType :: " + "STRING?)",
"List all relationship types in the database.", "READ" ),
proc( "dbms.procedures", "() :: (name :: STRING?, signature :: " + "STRING?, description :: STRING?, mode :: STRING?)",
"List all procedures in the DBMS.", "DBMS" ),
proc( "dbms.functions", "() :: (name :: STRING?, signature :: " + "STRING?, description :: STRING?)",
"List all user functions in the DBMS.", "DBMS" ),
proc( "dbms.components", "() :: (name :: STRING?, versions :: LIST? OF" + " STRING?, edition :: STRING?)",
"List DBMS components and their versions.", "DBMS" ),
proc( "dbms.queryJmx", "(query :: STRING?) :: (name :: STRING?, " + "description :: STRING?, attributes :: MAP?)",
"Query JMX management data by domain and name." + " For instance, \"org.neo4j:*\"", "DBMS" ),
proc( "db.createLabel", "(newLabel :: STRING?) :: VOID", "Create a label", "WRITE" ),
proc( "db.createProperty", "(newProperty :: STRING?) :: VOID", "Create a Property", "WRITE" ),
proc( "db.createRelationshipType", "(newRelationshipType :: STRING?) :: VOID", "Create a RelationshipType", "WRITE" ),
proc( "db.index.explicit.searchNodes", "(indexName :: STRING?, query :: ANY?) :: (node :: NODE?, weight :: FLOAT?)",
"Search nodes in explicit index. Replaces `START n=node:nodes('key:foo*')`", "READ" ),
proc( "db.index.explicit.seekNodes", "(indexName :: STRING?, key :: STRING?, value :: ANY?) :: (node :: NODE?)",
"Get node from explicit index. Replaces `START n=node:nodes(key = 'A')`", "READ" ),
proc( "db.index.explicit.searchRelationships", "(indexName :: STRING?, query :: ANY?) :: (relationship :: RELATIONSHIP?, weight :: FLOAT?)",
"Search relationship in explicit index. Replaces `START r=relationship:relIndex('key:foo*')`", "READ" ),
proc( "db.index.explicit.auto.searchNodes", "(query :: ANY?) :: (node :: NODE?, weight :: FLOAT?)",
"Search nodes in explicit automatic index. Replaces `START n=node:node_auto_index('key:foo*')`", "READ" ),
proc( "db.index.explicit.auto.seekNodes", "(key :: STRING?, value :: ANY?) :: (node :: NODE?)",
"Get node from explicit automatic index. Replaces `START n=node:node_auto_index(key = 'A')`", "READ" ),
proc( "db.index.explicit.auto.searchRelationships", "(query :: ANY?) :: (relationship :: RELATIONSHIP?, weight :: FLOAT?)",
"Search relationship in explicit automatic index. Replaces `START r=relationship:relationship_auto_index('key:foo*')`", "READ" ),
proc( "db.index.explicit.auto.seekRelationships", "(key :: STRING?, value :: ANY?) :: " + "(relationship :: RELATIONSHIP?)",
"Get relationship from explicit automatic index. Replaces `START r=relationship:relationship_auto_index(key = 'A')`", "READ" ),
proc( "db.index.explicit.addNode", "(indexName :: STRING?, node :: NODE?, key :: STRING?, value :: ANY?) :: (success :: BOOLEAN?)",
"Add a node to an explicit index based on a specified key and value", "WRITE" ),
proc( "db.index.explicit.addRelationship", "(indexName :: STRING?, relationship :: RELATIONSHIP?, key :: STRING?, value :: ANY?) :: " +
"(success :: BOOLEAN?)", "Add a relationship to an explicit index based on a specified key and value", "WRITE" ),
proc( "db.index.explicit.removeNode", "(indexName :: STRING?, node :: NODE?, key = <[9895b15e-8693-4a21-a58b-4b7b87e09b8e]> :: STRING?) " +
":: (success :: BOOLEAN?)",
"Remove a node from an explicit index with an optional key", "WRITE" ),
proc( "db.index.explicit.removeRelationship", "(indexName :: STRING?, relationship :: RELATIONSHIP?, " +
"key = <[9895b15e-8693-4a21-a58b-4b7b87e09b8e]> :: STRING?) :: (success :: BOOLEAN?)",
"Remove a relationship from an explicit index with an optional key", "WRITE" ),
proc( "db.index.explicit.drop", "(indexName :: STRING?) :: (type :: STRING?, name :: STRING?, config :: MAP?)",
"Remove an explicit index - YIELD type,name,config", "WRITE" ),
proc( "db.index.explicit.forNodes", "(indexName :: STRING?, config = {} :: MAP?) :: (type :: STRING?, name :: STRING?, config :: MAP?)",
"Get or create a node explicit index - YIELD type,name,config", "WRITE" ),
proc( "db.index.explicit.forRelationships", "(indexName :: STRING?, config = {} :: MAP?) :: " +
"(type :: STRING?, name :: STRING?, config :: MAP?)", "Get or create a relationship explicit index - YIELD type,name,config",
"WRITE" ),
proc( "db.index.explicit.existsForNodes", "(indexName :: STRING?) :: (success :: BOOLEAN?)", "Check if a node explicit index exists", "READ" ),
proc( "db.index.explicit.existsForRelationships", "(indexName :: STRING?) :: (success :: BOOLEAN?)",
"Check if a relationship explicit index exists", "READ" ),
proc( "db.index.explicit.list", "() :: (type :: STRING?, name :: STRING?, config :: MAP?)",
"List all explicit indexes - YIELD type,name,config", "READ" ),
proc( "db.index.explicit.seekRelationships", "(indexName :: STRING?, key :: STRING?, value :: ANY?) :: (relationship :: RELATIONSHIP?)",
"Get relationship from explicit index. Replaces `START r=relationship:relIndex(key = 'A')`", "READ" ),
proc( "db.index.explicit.searchRelationshipsBetween", "(indexName :: STRING?, in :: NODE?, out :: NODE?, query :: ANY?) :: " +
"(relationship :: RELATIONSHIP?, weight :: FLOAT?)",
"Search relationship in explicit index, starting at the node 'in' and ending at 'out'.", "READ" ),
proc( "db.index.explicit.searchRelationshipsIn", "(indexName :: STRING?, in :: NODE?, query :: ANY?) :: " +
"(relationship :: RELATIONSHIP?, weight :: FLOAT?)", "Search relationship in explicit index, starting at the node 'in'.",
"READ" ),
proc( "db.index.explicit.searchRelationshipsOut", "(indexName :: STRING?, out :: NODE?, query :: ANY?) :: " +
"(relationship :: RELATIONSHIP?, weight :: FLOAT?)", "Search relationship in explicit index, ending at the node 'out'.",
"READ" ),
proc( "dbms.clearQueryCaches", "() :: (value :: STRING?)", "Clears all query caches.", "DBMS" ),
proc( "db.createIndex", "(index :: STRING?, providerName :: STRING?) :: (index :: STRING?, providerName :: STRING?, status :: STRING?)",
"Create a schema index with specified index provider (for example: CALL db.createIndex(\":Person(name)\", \"lucene+native-2.0\")) - " +
"YIELD index, providerName, status", "SCHEMA" ),
proc( "db.createUniquePropertyConstraint", "(index :: STRING?, providerName :: STRING?) :: " +
"(index :: STRING?, providerName :: STRING?, status :: STRING?)",
"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", "SCHEMA" ),
proc( "db.index.fulltext.awaitEventuallyConsistentIndexRefresh", "() :: VOID",
"Wait for the updates from recently committed transactions to be applied to any eventually-consistent fulltext indexes.", "READ" ),
proc( "db.index.fulltext.awaitIndex", "(index :: STRING?, timeOutSeconds = 300 :: INTEGER?) :: VOID",
"Similar to db.awaitIndex(index, timeout), except instead of an index pattern, the index is specified by name. " +
"The name can be quoted by backticks, if necessary.", "READ" ),
proc( "db.index.fulltext.createNodeIndex", "(indexName :: STRING?, labels :: LIST? OF STRING?, propertyNames :: LIST? OF STRING?, " +
"config = {} :: MAP?) :: VOID", startsWith( "Create a node fulltext index for the given labels and properties." ), "SCHEMA" ),
proc( "db.index.fulltext.createRelationshipIndex",
"(indexName :: STRING?, relationshipTypes :: LIST? OF STRING?, propertyNames :: LIST? OF STRING?, config = {} :: MAP?) :: VOID",
startsWith( "Create a relationship fulltext index for the given relationship types and properties." ), "SCHEMA" ),
proc( "db.index.fulltext.drop", "(indexName :: STRING?) :: VOID", "Drop the specified index.", "SCHEMA" ),
proc( "db.index.fulltext.listAvailableAnalyzers", "() :: (analyzer :: STRING?)",
"List the available analyzers that the fulltext indexes can be configured with.", "READ" ),
proc( "db.index.fulltext.queryNodes", "(indexName :: STRING?, queryString :: STRING?) :: (node :: NODE?, score :: FLOAT?)",
"Query the given fulltext index. Returns the matching nodes and their lucene query score, ordered by score.", "READ"),
proc( "db.index.fulltext.queryRelationships", "(indexName :: STRING?, queryString :: STRING?) :: (relationship :: RELATIONSHIP?, " +
"score :: FLOAT?)", "Query the given fulltext index. Returns the matching relationships and their lucene query score, ordered by " +
"score.", "READ" ),
proc( "db.stats.retrieve", "(section :: STRING?) :: (section :: STRING?, data :: MAP?)",
"Retrieve statistical data about the current database.", "READ" ),
proc( "db.stats.retrieveAllAnonymized", "(graphToken :: STRING?) :: (section :: STRING?, data :: MAP?)",
"Retrieve all available statistical data about the current database, in an anonymized form.", "READ" )
) );
commit();
}
private Matcher<Object[]> proc( String procName, String procSignature, String description, String mode )
{
return equalTo( new Object[]{procName, procName + procSignature, description, mode} );
}
@SuppressWarnings( {"unchecked", "TypeParameterExplicitlyExtendsObject"} )
private Matcher<Object[]> proc( String procName, String procSignature, Matcher<String> description, String mode )
{
Matcher<Object> desc = (Matcher<Object>) (Matcher<? extends Object>) description;
Matcher<Object>[] matchers = new Matcher[]{equalTo( procName ), equalTo( procName + procSignature ), desc, equalTo( mode )};
return arrayContaining( matchers );
}
@Test
public void failWhenCallingNonExistingProcedures()
{
try
{
dbmsOperations().procedureCallDbms( procedureName( "dbms", "iDoNotExist" ), new Object[0], dependencyResolver,
AnonymousContext.none().authorize( s -> -1, GraphDatabaseSettings.DEFAULT_DATABASE_NAME ), resourceTracker );
fail( "This should never get here" );
}
catch ( Exception e )
{
// Then
assertThat( e.getClass(), equalTo( ProcedureException.class ) );
}
}
@Test
public void listAllComponents() throws Throwable
{
// Given a running database
// When
RawIterator<Object[],ProcedureException> stream =
procs().procedureCallRead( procs().procedureGet( procedureName( "dbms", "components" ) ).id(), new Object[0] );
// Then
assertThat( asList( stream ), contains( equalTo( new Object[]{"Neo4j Kernel", singletonList( Version.getNeo4jVersion() ), "community"} ) ) );
commit();
}
@Test
public void listAllIndexes() throws Throwable
{
// Given
Transaction transaction = newTransaction( AUTH_DISABLED );
int labelId1 = transaction.tokenWrite().labelGetOrCreateForName( "Person" );
int labelId2 = transaction.tokenWrite().labelGetOrCreateForName( "Age" );
int propertyKeyId1 = transaction.tokenWrite().propertyKeyGetOrCreateForName( "foo" );
int propertyKeyId2 = transaction.tokenWrite().propertyKeyGetOrCreateForName( "bar" );
LabelSchemaDescriptor personFooDescriptor = forLabel( labelId1, propertyKeyId1 );
LabelSchemaDescriptor ageFooDescriptor = forLabel( labelId2, propertyKeyId1 );
LabelSchemaDescriptor personFooBarDescriptor = forLabel( labelId1, propertyKeyId1, propertyKeyId2 );
transaction.schemaWrite().indexCreate( personFooDescriptor );
transaction.schemaWrite().uniquePropertyConstraintCreate( ageFooDescriptor );
transaction.schemaWrite().indexCreate( personFooBarDescriptor );
commit();
//let indexes come online
try ( org.neo4j.graphdb.Transaction tx = db.beginTx() )
{
db.schema().awaitIndexesOnline( 2, MINUTES );
tx.success();
}
// When
RawIterator<Object[],ProcedureException> stream =
procs().procedureCallRead( procs().procedureGet( procedureName( "db", "indexes" ) ).id(), new Object[0] );
Set<Object[]> result = new HashSet<>();
while ( stream.hasNext() )
{
result.add( stream.next() );
}
// Then
IndexProviderMap indexProviderMap = db.getDependencyResolver().resolveDependency( IndexProviderMap.class );
IndexingService indexingService = db.getDependencyResolver().resolveDependency( IndexingService.class );
IndexProvider provider = indexProviderMap.getDefaultProvider();
Map<String,String> pdm = MapUtil.stringMap( // Provider Descriptor Map.
"key", provider.getProviderDescriptor().getKey(), "version", provider.getProviderDescriptor().getVersion() );
assertThat( result, containsInAnyOrder(
new Object[]{"INDEX ON :Age(foo)", "index_1", singletonList( "Age" ), singletonList( "foo" ), "ONLINE",
"node_unique_property", 100D, pdm, indexingService.getIndexId( ageFooDescriptor ), ""},
new Object[]{"INDEX ON :Person(foo)", "Unnamed index", singletonList( "Person" ),
singletonList( "foo" ), "ONLINE", "node_label_property", 100D, pdm, indexingService.getIndexId( personFooDescriptor ), ""},
new Object[]{"INDEX ON :Person(foo, bar)", "Unnamed index", singletonList( "Person" ),
Arrays.asList( "foo", "bar" ), "ONLINE", "node_label_property", 100D, pdm, indexingService.getIndexId( personFooBarDescriptor ), ""}
) );
commit();
}
}