use of org.neo4j.internal.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.
the class ProcedureCompiler method compileProcedure.
List<CallableProcedure> compileProcedure(Class<?> procDefinition, String warning, boolean fullAccess) throws KernelException {
try {
List<Method> procedureMethods = Arrays.stream(procDefinition.getDeclaredMethods()).filter(m -> m.isAnnotationPresent(Procedure.class)).collect(Collectors.toList());
if (procedureMethods.isEmpty()) {
return emptyList();
}
assertValidConstructor(procDefinition);
List<CallableProcedure> out = new ArrayList<>(procedureMethods.size());
for (Method method : procedureMethods) {
String valueName = method.getAnnotation(Procedure.class).value();
String definedName = method.getAnnotation(Procedure.class).name();
QualifiedName procName = extractName(procDefinition, method, valueName, definedName);
if (fullAccess || config.isWhitelisted(procName.toString())) {
out.add(compileProcedure(procDefinition, method, warning, fullAccess, procName));
} else {
log.warn(String.format("The procedure '%s' is not on the allowlist and won't be loaded.", procName.toString()));
}
}
out.sort(Comparator.comparing(a -> a.signature().name().toString()));
return out;
} catch (KernelException e) {
throw e;
} catch (Exception e) {
throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, e, "Failed to compile procedure defined in `%s`: %s", procDefinition.getSimpleName(), e.getMessage());
}
}
use of org.neo4j.internal.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.
the class ProcedureCompiler method compileFunction.
List<CallableUserFunction> compileFunction(Class<?> fcnDefinition, boolean isBuiltin) throws KernelException {
try {
List<Method> functionMethods = Arrays.stream(fcnDefinition.getDeclaredMethods()).filter(m -> m.isAnnotationPresent(UserFunction.class)).collect(Collectors.toList());
if (functionMethods.isEmpty()) {
return emptyList();
}
// used for proper error handling
assertValidConstructor(fcnDefinition);
List<CallableUserFunction> out = new ArrayList<>(functionMethods.size());
for (Method method : functionMethods) {
String valueName = method.getAnnotation(UserFunction.class).value();
String definedName = method.getAnnotation(UserFunction.class).name();
QualifiedName funcName = extractName(fcnDefinition, method, valueName, definedName);
if (isBuiltin || config.isWhitelisted(funcName.toString())) {
out.add(compileFunction(fcnDefinition, method, funcName));
} else {
log.warn(String.format("The function '%s' is not on the allowlist and won't be loaded.", funcName.toString()));
}
}
out.sort(Comparator.comparing(a -> a.signature().name().toString()));
return out;
} catch (KernelException e) {
throw e;
} catch (Exception e) {
throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, e, "Failed to compile function defined in `%s`: %s", fcnDefinition.getSimpleName(), e.getMessage());
}
}
use of org.neo4j.internal.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.
the class ProcedureCompiler method compileAggregationFunction.
List<CallableUserAggregationFunction> compileAggregationFunction(Class<?> fcnDefinition) throws KernelException {
try {
List<Method> methods = Arrays.stream(fcnDefinition.getDeclaredMethods()).filter(m -> m.isAnnotationPresent(UserAggregationFunction.class)).collect(Collectors.toList());
if (methods.isEmpty()) {
return emptyList();
}
assertValidConstructor(fcnDefinition);
List<CallableUserAggregationFunction> out = new ArrayList<>(methods.size());
for (Method method : methods) {
String valueName = method.getAnnotation(UserAggregationFunction.class).value();
String definedName = method.getAnnotation(UserAggregationFunction.class).name();
QualifiedName funcName = extractName(fcnDefinition, method, valueName, definedName);
if (config.isWhitelisted(funcName.toString())) {
out.add(compileAggregationFunction(fcnDefinition, method, funcName));
} else {
log.warn(String.format("The function '%s' is not on the allowlist and won't be loaded.", funcName.toString()));
}
}
out.sort(Comparator.comparing(a -> a.signature().name().toString()));
return out;
} catch (KernelException e) {
throw e;
} catch (Exception e) {
throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, e, "Failed to compile function defined in `%s`: %s", fcnDefinition.getSimpleName(), e.getMessage());
}
}
use of org.neo4j.internal.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.
the class ProcedureRegistry method register.
/**
* Register a new function.
*
* @param function the function.
*/
public void register(CallableUserAggregationFunction function, boolean overrideCurrentImplementation, boolean builtIn) throws ProcedureException {
UserFunctionSignature signature = function.signature();
QualifiedName name = signature.name();
if (functions.get(name) != null) {
throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Unable to register aggregation function, because the name `%s` is already in use as a function.", name);
}
CallableUserAggregationFunction oldImplementation = aggregationFunctions.get(name);
if (oldImplementation == null) {
aggregationFunctions.put(name, function, signature.caseInsensitive());
} else {
if (overrideCurrentImplementation) {
aggregationFunctions.put(name, function, signature.caseInsensitive());
} else {
throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Unable to register aggregation function, because the name `%s` is already in use.", name);
}
}
if (builtIn) {
builtInAggregatingFunctionIds.add(aggregationFunctions.idOf(name));
}
}
use of org.neo4j.internal.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.
the class SchemaStatementProcedure method createStatement.
public static String createStatement(Function<String, IndexDescriptor> indexLookup, TokenRead tokenRead, ConstraintDescriptor constraint) throws ProcedureException {
try {
String name = constraint.getName();
if (constraint.isIndexBackedConstraint()) {
final String labelsOrRelTypes = labelsOrRelTypesAsStringArray(tokenRead, constraint.schema());
final String properties = propertiesAsStringArray(tokenRead, constraint.schema());
IndexDescriptor backingIndex = indexLookup.apply(name);
String providerName = backingIndex.getIndexProvider().name();
String config = btreeConfigAsString(backingIndex);
if (constraint.isUniquenessConstraint()) {
return format(CREATE_UNIQUE_PROPERTY_CONSTRAINT, name, labelsOrRelTypes, properties, providerName, config);
}
if (constraint.isNodeKeyConstraint()) {
return format(CREATE_NODE_KEY_CONSTRAINT, name, labelsOrRelTypes, properties, providerName, config);
}
}
if (constraint.isNodePropertyExistenceConstraint()) {
// "create CONSTRAINT ON (a:A) ASSERT (a.p) IS NOT NULL"
int labelId = constraint.schema().getLabelId();
String label = tokenRead.nodeLabelName(labelId);
int propertyId = constraint.schema().getPropertyId();
String property = tokenRead.propertyKeyName(propertyId);
return format(CREATE_NODE_EXISTENCE_CONSTRAINT, escapeBackticks(name), escapeBackticks(label), escapeBackticks(property));
}
if (constraint.isRelationshipPropertyExistenceConstraint()) {
// "create CONSTRAINT ON ()-[r:R]-() ASSERT (r.p) IS NOT NULL"
int relationshipTypeId = constraint.schema().getRelTypeId();
String relationshipType = tokenRead.relationshipTypeName(relationshipTypeId);
int propertyId = constraint.schema().getPropertyId();
String property = tokenRead.propertyKeyName(propertyId);
return format(CREATE_RELATIONSHIP_EXISTENCE_CONSTRAINT, escapeBackticks(name), escapeBackticks(relationshipType), escapeBackticks(property));
}
throw new IllegalArgumentException("Did not recognize constraint type " + constraint);
} catch (KernelException e) {
throw new ProcedureException(Status.General.UnknownError, e, "Failed to re-create create statement.");
}
}
Aggregations