use of org.neo4j.kernel.api.procedure.Context in project neo4j by neo4j.
the class BuiltInProceduresTest method setup.
@BeforeEach
void setup() throws Exception {
procs.registerComponent(KernelTransaction.class, ctx -> ctx.internalTransaction().kernelTransaction(), false);
procs.registerComponent(DependencyResolver.class, Context::dependencyResolver, false);
procs.registerComponent(GraphDatabaseAPI.class, Context::graphDatabaseAPI, false);
procs.registerComponent(Transaction.class, Context::internalTransaction, true);
procs.registerComponent(SecurityContext.class, Context::securityContext, true);
procs.registerComponent(ProcedureCallContext.class, Context::procedureCallContext, true);
procs.registerComponent(SystemGraphComponents.class, ctx -> systemGraphComponents, false);
procs.registerComponent(Log.class, ctx -> log, false);
procs.registerType(Node.class, NTNode);
procs.registerType(Relationship.class, NTRelationship);
procs.registerType(Path.class, NTPath);
new SpecialBuiltInProcedures("1.3.37", Edition.COMMUNITY.toString()).accept(procs);
procs.registerProcedure(BuiltInProcedures.class);
procs.registerProcedure(BuiltInDbmsProcedures.class);
when(transaction.kernelTransaction()).thenReturn(tx);
when(tx.tokenRead()).thenReturn(tokens);
when(tx.dataRead()).thenReturn(read);
when(tx.schemaRead()).thenReturn(schemaRead);
when(tx.securityContext()).thenReturn(SecurityContext.AUTH_DISABLED);
when(callContext.isCalledFromCypher()).thenReturn(false);
when(schemaRead.snapshot()).thenReturn(schemaReadCore);
when(tokens.propertyKeyGetAllTokens()).thenAnswer(asTokens(propKeys));
when(tokens.labelsGetAllTokens()).thenAnswer(asTokens(labels));
when(tokens.relationshipTypesGetAllTokens()).thenAnswer(asTokens(relTypes));
when(schemaReadCore.indexesGetAll()).thenAnswer(i -> Iterators.concat(indexes.iterator(), uniqueIndexes.iterator()));
when(schemaReadCore.index(any(SchemaDescriptor.class))).thenAnswer((Answer<IndexDescriptor>) invocationOnMock -> {
SchemaDescriptor schema = invocationOnMock.getArgument(0);
return getIndexReference(schema);
});
when(schemaReadCore.constraintsGetAll()).thenAnswer(i -> constraints.iterator());
when(tokens.propertyKeyName(anyInt())).thenAnswer(invocation -> propKeys.get(invocation.getArgument(0)));
when(tokens.nodeLabelName(anyInt())).thenAnswer(invocation -> labels.get(invocation.getArgument(0)));
when(tokens.relationshipTypeName(anyInt())).thenAnswer(invocation -> relTypes.get(invocation.getArgument(0)));
when(tokens.propertyKeyGetName(anyInt())).thenAnswer(invocation -> propKeys.get(invocation.getArgument(0)));
when(tokens.labelGetName(anyInt())).thenAnswer(invocation -> labels.get(invocation.getArgument(0)));
when(tokens.relationshipTypeGetName(anyInt())).thenAnswer(invocation -> relTypes.get(invocation.getArgument(0)));
when(tokens.entityTokensGetNames(any(), any())).then(invocation -> {
EntityType type = invocation.getArgument(0);
int[] ids = invocation.getArgument(1);
Map<Integer, String> mapping = type == EntityType.NODE ? labels : relTypes;
return Arrays.stream(ids).mapToObj(mapping::get).toArray(String[]::new);
});
when(schemaReadCore.constraintsGetForRelationshipType(anyInt())).thenReturn(emptyIterator());
when(schemaReadCore.indexesGetForLabel(anyInt())).thenReturn(emptyIterator());
when(schemaReadCore.indexesGetForRelationshipType(anyInt())).thenReturn(emptyIterator());
when(schemaReadCore.constraintsGetForLabel(anyInt())).thenReturn(emptyIterator());
when(read.countsForNode(anyInt())).thenReturn(1L);
when(read.countsForRelationship(anyInt(), anyInt(), anyInt())).thenReturn(1L);
when(schemaReadCore.indexGetState(any(IndexDescriptor.class))).thenReturn(InternalIndexState.ONLINE);
}
use of org.neo4j.kernel.api.procedure.Context in project neo4j by neo4j.
the class BuiltInProceduresTest method call.
private List<Object[]> call(String name, Object... args) throws ProcedureException, IndexNotFoundKernelException {
DefaultValueMapper valueMapper = new DefaultValueMapper(mock(InternalTransaction.class));
Context ctx = buildContext(resolver, valueMapper).withTransaction(transaction).withProcedureCallContext(callContext).context();
when(graphDatabaseAPI.getDependencyResolver()).thenReturn(resolver);
when(resolver.resolveDependency(GraphDatabaseAPI.class)).thenReturn(graphDatabaseAPI);
when(resolver.resolveDependency(GlobalProcedures.class)).thenReturn(procs);
when(resolver.resolveDependency(IndexingService.class)).thenReturn(indexingService);
when(schemaReadCore.indexGetPopulationProgress(any(IndexDescriptor.class))).thenReturn(PopulationProgress.DONE);
AnyValue[] input = Arrays.stream(args).map(ValueUtils::of).toArray(AnyValue[]::new);
int procId = procs.procedure(ProcedureSignature.procedureName(name.split("\\."))).id();
List<AnyValue[]> anyValues = Iterators.asList(procs.callProcedure(ctx, procId, input, EMPTY_RESOURCE_TRACKER));
List<Object[]> toReturn = new ArrayList<>(anyValues.size());
for (AnyValue[] anyValue : anyValues) {
Object[] values = new Object[anyValue.length];
for (int i = 0; i < anyValue.length; i++) {
AnyValue value = anyValue[i];
values[i] = value.map(valueMapper);
}
toReturn.add(values);
}
return toReturn;
}
use of org.neo4j.kernel.api.procedure.Context in project neo4j by neo4j.
the class ProcedureCompilation method toAnyValue.
/**
* Takes an expression evaluating to one of the supported java values and turns
* it into the corresponding AnyValue
*
* @param expression the expression to evaluate
* @param userType the type of the expression to map
* @return an expression properly mapped to AnyValue
*/
private static Expression toAnyValue(Expression expression, Class<?> userType, Expression context) {
if (AnyValue.class.isAssignableFrom(userType)) {
return nullCheck(expression, cast(userType, expression));
}
String type = userType.getCanonicalName();
if (type.equals(LONG)) {
return invoke(methodReference(Values.class, LongValue.class, "longValue", long.class), expression);
} else if (type.equals(BOXED_LONG)) {
return nullCheck(expression, invoke(methodReference(Values.class, LongValue.class, "longValue", long.class), unbox(expression)));
} else if (type.equals(DOUBLE)) {
return invoke(methodReference(Values.class, DoubleValue.class, "doubleValue", double.class), expression);
} else if (type.equals(BOXED_DOUBLE)) {
return nullCheck(expression, invoke(methodReference(Values.class, DoubleValue.class, "doubleValue", double.class), unbox(expression)));
} else if (type.equals(NUMBER)) {
return nullCheck(expression, invoke(methodReference(Values.class, NumberValue.class, "numberValue", Number.class), expression));
} else if (type.equals(BOOLEAN)) {
return invoke(methodReference(Values.class, BooleanValue.class, "booleanValue", boolean.class), expression);
} else if (type.equals(BOXED_BOOLEAN)) {
return nullCheck(expression, invoke(methodReference(Values.class, BooleanValue.class, "booleanValue", boolean.class), unbox(expression)));
} else if (type.equals(STRING)) {
return invoke(methodReference(Values.class, Value.class, "stringOrNoValue", String.class), expression);
} else if (type.equals(BYTE_ARRAY)) {
return nullCheck(expression, invoke(methodReference(Values.class, ByteArray.class, "byteArray", byte[].class), expression));
} else if (type.equals(LIST)) {
return nullCheck(expression, invoke(methodReference(ValueUtils.class, ListValue.class, "asListValue", Iterable.class), expression));
} else if (type.equals(MAP)) {
return nullCheck(expression, invoke(methodReference(ValueUtils.class, MapValue.class, "asMapValue", Map.class), expression));
} else if (type.equals(ZONED_DATE_TIME)) {
return nullCheck(expression, invoke(methodReference(DateTimeValue.class, DateTimeValue.class, "datetime", ZonedDateTime.class), expression));
} else if (type.equals(OFFSET_TIME)) {
return nullCheck(expression, invoke(methodReference(TimeValue.class, TimeValue.class, "time", OffsetTime.class), expression));
} else if (type.equals(LOCAL_DATE)) {
return nullCheck(expression, invoke(methodReference(DateValue.class, DateValue.class, "date", LocalDate.class), expression));
} else if (type.equals(LOCAL_TIME)) {
return nullCheck(expression, invoke(methodReference(LocalTimeValue.class, LocalTimeValue.class, "localTime", LocalTime.class), expression));
} else if (type.equals(LOCAL_DATE_TIME)) {
return nullCheck(expression, invoke(methodReference(LocalDateTimeValue.class, LocalDateTimeValue.class, "localDateTime", LocalDateTime.class), expression));
} else if (type.equals(TEMPORAL_AMOUNT)) {
return nullCheck(expression, invoke(methodReference(Values.class, DurationValue.class, "durationValue", TemporalAmount.class), expression));
} else if (type.equals(NODE)) {
Expression internalTransaction = invoke(context, methodReference(Context.class, InternalTransaction.class, "internalTransactionOrNull"));
Expression getNode = invoke(internalTransaction, methodReference(InternalTransaction.class, Entity.class, "validateSameDB", Entity.class), expression);
return nullCheck(expression, invoke(methodReference(ValueUtils.class, NodeValue.class, "fromNodeEntity", Node.class), getNode));
} else if (type.equals(RELATIONSHIP)) {
Expression internalTransaction = invoke(context, methodReference(Context.class, InternalTransaction.class, "internalTransactionOrNull"));
Expression getRelationship = invoke(internalTransaction, methodReference(InternalTransaction.class, Entity.class, "validateSameDB", Entity.class), expression);
return nullCheck(expression, invoke(methodReference(ValueUtils.class, RelationshipValue.class, "fromRelationshipEntity", Relationship.class), getRelationship));
} else if (type.equals(PATH)) {
return nullCheck(expression, invoke(methodReference(ValueUtils.class, PathValue.class, "fromPath", Path.class), expression));
} else if (type.equals(POINT)) {
return nullCheck(expression, invoke(methodReference(ValueUtils.class, PointValue.class, "asPointValue", Point.class), expression));
} else {
return invoke(methodReference(ValueUtils.class, AnyValue.class, "of", Object.class), expression);
}
}
use of org.neo4j.kernel.api.procedure.Context in project neo4j by neo4j.
the class ProcedureCompilation method generateIterator.
/**
* Generates a tailored iterator mapping from the user-stream to the internal RawIterator
*
* The generated iterator extends {@link BaseStreamIterator} and generates a tailored
* {@link BaseStreamIterator#map(Object)} method based on the signature of the user procedure.
*
* @param codeGenerator the current generator
* @param outputType the output type of the user procedure
* @return a tailored iterator with appropriate code mappings
*/
private static Class<?> generateIterator(CodeGenerator codeGenerator, Class<?> outputType) {
if (outputType.equals(void.class)) {
return VOID_ITERATOR.getClass();
}
ClassHandle handle;
try (ClassGenerator generator = codeGenerator.generateClass(BaseStreamIterator.class, PACKAGE, iteratorName(outputType))) {
FieldReference context = generator.field(Context.class, "ctx");
try (CodeBlock constructor = generator.generateConstructor(param(Stream.class, "stream"), param(ResourceTracker.class, "tracker"), param(ProcedureSignature.class, "signature"), param(Context.class, "ctx"))) {
constructor.expression(invokeSuper(typeReference(BaseStreamIterator.class), constructor.load("stream"), constructor.load("tracker"), constructor.load("signature")));
constructor.put(constructor.self(), context, constructor.load("ctx"));
}
try (CodeBlock method = generator.generate(method(AnyValue[].class, "map", param(Object.class, "in")))) {
method.assign(outputType, "casted", cast(outputType, method.load("in")));
// we know all fields are properly typed
List<Field> fields = ProcedureOutputSignatureCompiler.instanceFields(outputType);
Expression[] mapped = new Expression[fields.size()];
for (int i = 0; i < fields.size(); i++) {
Field f = fields.get(i);
mapped[i] = toAnyValue(get(method.load("casted"), field(f)), f.getType(), get(method.self(), context));
}
method.returns(Expression.newInitializedArray(typeReference(AnyValue.class), mapped));
}
handle = generator.handle();
}
try {
return handle.loadClass();
} catch (CompilationFailureException e) {
// We are being called from a lambda so it'll have to do with runtime exceptions here
throw new RuntimeException("Failed to generate iterator", e);
}
}
use of org.neo4j.kernel.api.procedure.Context in project neo4j by neo4j.
the class BoltKeepAliveSchedulingIT method installSleepProcedure.
private static void installSleepProcedure(GraphDatabaseService db) throws ProcedureException {
GraphDatabaseAPI dbApi = (GraphDatabaseAPI) db;
dbApi.getDependencyResolver().resolveDependency(GlobalProcedures.class).register(new CallableProcedure.BasicProcedure(procedureSignature("boltissue", "sleep").out(ProcedureSignature.VOID).build()) {
@Override
public RawIterator<AnyValue[], ProcedureException> apply(Context context, AnyValue[] objects, ResourceTracker resourceTracker) throws ProcedureException {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new ProcedureException(Status.General.UnknownError, e, "Interrupted");
}
return RawIterator.empty();
}
});
}
Aggregations