Skip to content

Commit

Permalink
Merge pull request #6265 from jakewins/3.0-inject
Browse files Browse the repository at this point in the history
API injections in Procedures
  • Loading branch information
Stefan Plantikow committed Jan 22, 2016
2 parents bc90a7b + 50354bc commit e188bb6
Show file tree
Hide file tree
Showing 22 changed files with 625 additions and 298 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.function.Supplier;

import org.neo4j.graphdb.DependencyResolver;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.Relationship;
Expand All @@ -47,7 +48,6 @@
import org.neo4j.kernel.api.exceptions.KernelException;
import org.neo4j.kernel.api.index.SchemaIndexProvider;
import org.neo4j.kernel.api.labelscan.LabelScanStore;
import org.neo4j.kernel.impl.proc.Procedures;
import org.neo4j.kernel.builtinprocs.BuiltInProcedures;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.extension.dependency.HighestSelectionStrategy;
Expand Down Expand Up @@ -85,6 +85,8 @@
import org.neo4j.kernel.impl.locking.Locks;
import org.neo4j.kernel.impl.locking.ReentrantLockService;
import org.neo4j.kernel.impl.logging.LogService;
import org.neo4j.kernel.impl.proc.Procedures;
import org.neo4j.kernel.impl.proc.TypeMappers.SimpleConverter;
import org.neo4j.kernel.impl.storageengine.impl.recordstorage.RecordStorageEngine;
import org.neo4j.kernel.impl.store.MetaDataStore;
import org.neo4j.kernel.impl.store.StoreId;
Expand Down Expand Up @@ -155,14 +157,13 @@
import org.neo4j.logging.Log;
import org.neo4j.logging.LogProvider;
import org.neo4j.logging.Logger;
import org.neo4j.kernel.impl.proc.TypeMappers.SimpleConverter;
import org.neo4j.storageengine.api.StorageEngine;
import org.neo4j.storageengine.api.StoreReadLayer;

import static org.neo4j.kernel.impl.transaction.log.pruning.LogPruneStrategyFactory.fromConfigValue;
import static org.neo4j.kernel.api.proc.Neo4jTypes.NTNode;
import static org.neo4j.kernel.api.proc.Neo4jTypes.NTPath;
import static org.neo4j.kernel.api.proc.Neo4jTypes.NTRelationship;
import static org.neo4j.kernel.impl.transaction.log.pruning.LogPruneStrategyFactory.fromConfigValue;

public class NeoStoreDataSource implements Lifecycle, IndexProviders
{
Expand Down Expand Up @@ -822,6 +823,11 @@ private Procedures setupProcedures() throws KernelException, IOException
procedures.registerType( Node.class, new SimpleConverter( NTNode, Node.class ) );
procedures.registerType( Relationship.class, new SimpleConverter( NTRelationship, Relationship.class ) );
procedures.registerType( Path.class, new SimpleConverter( NTPath, Path.class ) );

//register API
procedures.registerComponent( GraphDatabaseService.class,
(ctx) -> dependencyResolver.resolveDependency( GraphDatabaseService.class ) );

BuiltInProcedures.addTo( procedures );
procedures.loadFromDirectory( config.get( GraphDatabaseSettings.plugin_dir ) );
return procedures;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,11 @@
package org.neo4j.kernel.api.proc;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.neo4j.collection.RawIterator;
import org.neo4j.kernel.api.exceptions.ProcedureException;
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.storageengine.api.Token;

import static java.util.Spliterator.IMMUTABLE;
import static java.util.Spliterator.ORDERED;

public interface Procedure
{
Expand Down Expand Up @@ -104,10 +96,5 @@ public ProcedureSignature signature()

@Override
public abstract RawIterator<Object[], ProcedureException> apply( Context ctx, Object[] input ) throws ProcedureException;

public static Stream<Token> stream( Iterator<Token> iter )
{
return StreamSupport.stream( Spliterators.spliteratorUnknownSize( iter, ORDERED | IMMUTABLE), false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.neo4j.helpers.collection.Iterables;
import org.neo4j.kernel.api.proc.Neo4jTypes.AnyType;

import static java.util.Arrays.asList;
Expand All @@ -36,21 +36,6 @@
*/
public class ProcedureSignature
{
private static String iterableToString( Iterable<?> values, String separator )
{
Iterator<?> it = values.iterator();
StringBuilder sb = new StringBuilder();
while(it.hasNext())
{
sb.append( it.next().toString() );
if(it.hasNext())
{
sb.append( separator );
}
}
return sb.toString();
}

public static class ProcedureName
{
private final String[] namespace;
Expand Down Expand Up @@ -80,7 +65,7 @@ public String name()
@Override
public String toString()
{
String strNamespace = namespace.length > 0 ? iterableToString( asList( namespace ), "." ) + "." : "";
String strNamespace = namespace.length > 0 ? Iterables.toString( asList( namespace ), "." ) + "." : "";
return String.format("%s%s", strNamespace, name);
}

Expand Down Expand Up @@ -202,8 +187,8 @@ public int hashCode()
@Override
public String toString()
{
String strInSig = inputSignature == null ? "..." : iterableToString( inputSignature, ", " );
String strOutSig = outputSignature == null ? "..." : iterableToString( outputSignature, ", " );
String strInSig = inputSignature == null ? "..." : Iterables.toString( inputSignature, ", " );
String strOutSig = outputSignature == null ? "..." : Iterables.toString( outputSignature, ", " );
return String.format( "%s(%s) :: (%s)", name, strInSig, strOutSig );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.neo4j.kernel.api.proc.Neo4jTypes;
import org.neo4j.kernel.api.proc.Procedure;
import org.neo4j.kernel.api.proc.ProcedureSignature.ProcedureName;
import org.neo4j.storageengine.api.Token;

import static org.neo4j.kernel.api.ReadOperations.readStatement;
import static org.neo4j.kernel.api.proc.ProcedureSignature.procedureSignature;
Expand All @@ -34,12 +35,13 @@ public class ListLabelsProcedure extends Procedure.BasicProcedure
{
public ListLabelsProcedure( ProcedureName name )
{
super( procedureSignature( name ).out( "label", Neo4jTypes.NTString ).build());
super( procedureSignature( name ).out( "label", Neo4jTypes.NTString ).build() );
}

@Override
public RawIterator<Object[], ProcedureException> apply( Context ctx, Object[] input ) throws ProcedureException
{
return map( (t) -> new Object[]{t.name()}, asRawIterator( ctx.get( readStatement ).labelsGetAllTokens() ) );
RawIterator<Token,ProcedureException> tokens = asRawIterator( ctx.get( readStatement ).labelsGetAllTokens() );
return map( ( token ) -> new Object[]{ token.name() }, tokens );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.neo4j.kernel.api.proc.Neo4jTypes;
import org.neo4j.kernel.api.proc.Procedure;
import org.neo4j.kernel.api.proc.ProcedureSignature;
import org.neo4j.storageengine.api.Token;

import static org.neo4j.kernel.api.ReadOperations.readStatement;
import static org.neo4j.kernel.api.proc.ProcedureSignature.procedureSignature;
Expand All @@ -40,6 +41,7 @@ public ListPropertyKeysProcedure( ProcedureSignature.ProcedureName name )
@Override
public RawIterator<Object[], ProcedureException> apply( Context ctx, Object[] input ) throws ProcedureException
{
return map( (t) -> new Object[]{t.name()}, asRawIterator( ctx.get( readStatement ).propertyKeyGetAllTokens() ) );
RawIterator<Token,ProcedureException> tokens = asRawIterator( ctx.get( readStatement ).propertyKeyGetAllTokens() );
return map( ( token ) -> new Object[]{ token.name() }, tokens );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.neo4j.kernel.api.proc.Neo4jTypes;
import org.neo4j.kernel.api.proc.Procedure;
import org.neo4j.kernel.api.proc.ProcedureSignature;
import org.neo4j.storageengine.api.Token;

import static org.neo4j.kernel.api.ReadOperations.readStatement;
import static org.neo4j.kernel.api.proc.ProcedureSignature.procedureSignature;
Expand All @@ -40,6 +41,7 @@ public ListRelationshipTypesProcedure( ProcedureSignature.ProcedureName name )
@Override
public RawIterator<Object[], ProcedureException> apply( Context ctx, Object[] input ) throws ProcedureException
{
return map( (t) -> new Object[]{t.name()}, asRawIterator( ctx.get( readStatement ).relationshipTypesGetAllTokens() ) );
RawIterator<Token,ProcedureException> tokens = asRawIterator( ctx.get( readStatement ).relationshipTypesGetAllTokens() );
return map( ( token ) -> new Object[]{ token.name() }, tokens );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2002-2016 "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.proc;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

import org.neo4j.kernel.api.proc.Procedure;

/**
* Tracks components that can be injected into user-defined procedures.
*/
public class ComponentRegistry
{
private final Map<Class<?>, Function<Procedure.Context, ?>> suppliers;

public ComponentRegistry()
{
this( new HashMap<>() );
}

public ComponentRegistry( Map<Class<?>,Function<Procedure.Context,?>> suppliers )
{
this.suppliers = suppliers;
}

public Function<Procedure.Context,?> supplierFor( Class<?> type )
{
return suppliers.get( type );
}

public <T> void register( Class<T> cls, Function<Procedure.Context,T> supplier )
{
suppliers.put( cls, supplier );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* Copyright (c) 2002-2016 "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.proc;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;

import org.neo4j.kernel.api.exceptions.ProcedureException;
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.kernel.api.proc.Procedure;

/**
* Injects annotated fields with appropriate values.
*/
public class FieldInjections
{
private final ComponentRegistry components;

public FieldInjections( ComponentRegistry components )
{
this.components = components;
}

/**
* On calling apply, injects the `value` for the field `field` on the provided `object`.
*/
public static class FieldSetter
{
private final Field field;
private final MethodHandle setter;
private final Function<Procedure.Context,?> supplier;

public FieldSetter( Field field, MethodHandle setter, Function<Procedure.Context,?> supplier )
{
this.field = field;
this.setter = setter;
this.supplier = supplier;
}

void apply( Procedure.Context ctx, Object object ) throws ProcedureException
{
try
{
setter.invoke( object, supplier.apply( ctx ) );
}
catch ( Throwable e )
{
throw new ProcedureException( Status.Procedure.CallFailed, e,
"Unable to inject component to field `%s`, please ensure it is public and non-final: %s",
field.getName(), e.getMessage() );
}
}
}

/**
* For each annotated field in the provided class, creates a `FieldSetter`.
* @param cls The class where injection should happen.
* @return A list of `FieldSetters`
* @throws ProcedureException if the type of the injected field does not match what has been registered.
*/
public List<FieldSetter> setters( Class<?> cls ) throws ProcedureException
{
List<FieldSetter> setters = new LinkedList<>();
Class<?> currentClass = cls;

do
{
for ( Field field : currentClass.getDeclaredFields() )
{
if ( Modifier.isStatic( field.getModifiers() ) )
{
continue;
}

assertValidForInjection( cls, field );
setters.add( createInjector( cls, field ) );
}
} while( (currentClass = currentClass.getSuperclass()) != null );

return setters;
}

private FieldSetter createInjector( Class<?> cls, Field field ) throws ProcedureException
{
try
{
Function<Procedure.Context,?> supplier = components.supplierFor( field.getType() );
if( supplier == null )
{
throw new ProcedureException( Status.Procedure.FailedRegistration,
"Unable to set up injection for procedure `%s`, the field `%s` " +
"has type `%s` which is not a known injectable component.",
cls.getSimpleName(), field.getName(), field.getType());
}

MethodHandle setter = MethodHandles.lookup().unreflectSetter( field );
return new FieldSetter( field, setter, supplier );
}
catch ( IllegalAccessException e )
{
throw new ProcedureException( Status.Procedure.FailedRegistration,
"Unable to set up injection for `%s`, failed to access field `%s`: %s",
e, cls.getSimpleName(), field.getName(), e.getMessage() );
}
}

private void assertValidForInjection( Class<?> cls, Field field ) throws ProcedureException
{
if( !field.isAnnotationPresent( Resource.class ) )
{
throw new ProcedureException( Status.Procedure.FailedRegistration,
"Field `%s` on `%s` is not annotated as a @Resource and is not static. " +
"If you want to store state along with your procedure, please use a static field.",
field.getName(), cls.getSimpleName() );
}

if( !Modifier.isPublic( field.getModifiers() ) || Modifier.isFinal( field.getModifiers() ) )
{
throw new ProcedureException( Status.Procedure.FailedRegistration,
"Field `%s` on `%s` must be non-final and public.",
field.getName(), cls.getSimpleName() );

}
}
}

0 comments on commit e188bb6

Please sign in to comment.