Search in sources :

Example 1 with Response

use of org.neo4j.ogm.response.Response in project neo4j-ogm by neo4j.

the class GraphEntityMapper method map.

/**
 * @param type                 the type of the entities to return
 * @param graphModelResponse   The response of graph models to work on
 * @param additionalNodeFilter An optional filter to exclude entities based on some nodes from the result
 * @param <T>                  The type of the class of the entities to return
 * @return The list of entities represented by the list of graph models.
 */
<T> List<T> map(Class<T> type, Response<GraphModel> graphModelResponse, BiFunction<GraphModel, Long, Boolean> additionalNodeFilter) {
    // Those are the ids of all mapped nodes.
    Set<Long> mappedNodeIds = new LinkedHashSet<>();
    Set<Long> returnedNodeIds = new LinkedHashSet<>();
    Set<Long> mappedRelationshipIds = new LinkedHashSet<>();
    Set<Long> returnedRelationshipIds = new LinkedHashSet<>();
    // Execute mapping for each individual model
    Consumer<GraphModel> mapContentOfIndividualModel = graphModel -> mapContentOf(graphModel, additionalNodeFilter, returnedNodeIds, mappedRelationshipIds, returnedRelationshipIds, mappedNodeIds);
    GraphModel graphModel = null;
    while ((graphModel = graphModelResponse.next()) != null) {
        mapContentOfIndividualModel.accept(graphModel);
    }
    graphModelResponse.close();
    // Execute postload after all models and only for new ids
    executePostLoad(mappedNodeIds, mappedRelationshipIds);
    // Collect result
    Predicate<Object> entityPresentAndCompatible = entity -> entity != null && type.isAssignableFrom(entity.getClass());
    List<T> results = returnedNodeIds.stream().map(mappingContext::getNodeEntity).filter(entityPresentAndCompatible).map(type::cast).collect(toList());
    // only look for REs if no node entities were found
    if (results.isEmpty()) {
        results = returnedRelationshipIds.stream().map(mappingContext::getRelationshipEntity).filter(entityPresentAndCompatible).map(type::cast).collect(toList());
    }
    return results;
}
Also used : EntityAccessManager(org.neo4j.ogm.metadata.reflect.EntityAccessManager) EndNode(org.neo4j.ogm.annotation.EndNode) EntityInstantiator(org.neo4j.ogm.session.EntityInstantiator) java.util(java.util) CompositeAttributeConverter(org.neo4j.ogm.typeconversion.CompositeAttributeConverter) BiFunction(java.util.function.BiFunction) MethodInfo(org.neo4j.ogm.metadata.MethodInfo) LoggerFactory(org.slf4j.LoggerFactory) Property(org.neo4j.ogm.model.Property) Response(org.neo4j.ogm.response.Response) Direction(org.neo4j.ogm.annotation.Relationship.Direction) GraphModel(org.neo4j.ogm.model.GraphModel) Edge(org.neo4j.ogm.model.Edge) PropertyModel(org.neo4j.ogm.response.model.PropertyModel) Node(org.neo4j.ogm.model.Node) EntityUtils(org.neo4j.ogm.utils.EntityUtils) EntityFactory(org.neo4j.ogm.metadata.reflect.EntityFactory) Logger(org.slf4j.Logger) Predicate(java.util.function.Predicate) MetaData(org.neo4j.ogm.metadata.MetaData) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) Consumer(java.util.function.Consumer) StartNode(org.neo4j.ogm.annotation.StartNode) DescriptorMappings(org.neo4j.ogm.metadata.DescriptorMappings) ClassInfo(org.neo4j.ogm.metadata.ClassInfo) MappingException(org.neo4j.ogm.exception.core.MappingException) FieldInfo(org.neo4j.ogm.metadata.FieldInfo) GraphModel(org.neo4j.ogm.model.GraphModel)

Example 2 with Response

use of org.neo4j.ogm.response.Response in project neo4j-ogm by neo4j.

the class RequestExecutor method executeSave.

/**
 * Execute a save request.
 * Decides how the request is split depending upon characteristics of what is to be saved.
 * Processes the response(s) and updates the mapping context.
 *
 * @param context the CompileContext for this request
 */
public void executeSave(CompileContext context) {
    Compiler compiler = context.getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    List<ReferenceMapping> entityReferenceMappings = new ArrayList<>();
    List<ReferenceMapping> relReferenceMappings = new ArrayList<>();
    boolean forceTx = compiler.updateNodesStatements().stream().anyMatch(st -> st.optimisticLockingConfig().isPresent()) || compiler.updateRelationshipStatements().stream().anyMatch(st -> st.optimisticLockingConfig().isPresent());
    session.doInTransaction(() -> {
        // we must create the new nodes first, and then use their node IDs when creating relationships between them
        if (compiler.hasStatementsDependentOnNewNodes()) {
            // execute the statements to create new nodes. The ids will be returned
            // and will be used in subsequent statements that refer to these new nodes.
            executeStatements(context, entityReferenceMappings, relReferenceMappings, compiler.createNodesStatements());
            List<Statement> statements = new ArrayList<>();
            statements.addAll(compiler.createRelationshipsStatements());
            statements.addAll(compiler.updateNodesStatements());
            statements.addAll(compiler.updateRelationshipStatements());
            statements.addAll(compiler.deleteRelationshipStatements());
            statements.addAll(compiler.deleteRelationshipEntityStatements());
            executeStatements(context, entityReferenceMappings, relReferenceMappings, statements);
        } else {
            // only update / delete statements
            List<Statement> statements = compiler.getAllStatements();
            executeStatements(context, entityReferenceMappings, relReferenceMappings, statements);
        }
    }, forceTx, Transaction.Type.READ_WRITE);
    // Update the mapping context now that the request is successful
    updateNodeEntities(context, entityReferenceMappings);
    updateRelationshipEntities(context, relReferenceMappings);
    updateRelationships(context, relReferenceMappings);
}
Also used : MappedRelationship(org.neo4j.ogm.context.MappedRelationship) RelationshipEntity(org.neo4j.ogm.annotation.RelationshipEntity) AbstractTransaction(org.neo4j.ogm.transaction.AbstractTransaction) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Response(org.neo4j.ogm.response.Response) HashMap(java.util.HashMap) CompileContext(org.neo4j.ogm.cypher.compiler.CompileContext) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) MappingContext(org.neo4j.ogm.context.MappingContext) Statement(org.neo4j.ogm.request.Statement) ArrayList(java.util.ArrayList) List(java.util.List) RowModel(org.neo4j.ogm.model.RowModel) Map(java.util.Map) Compiler(org.neo4j.ogm.cypher.compiler.Compiler) ClassInfo(org.neo4j.ogm.metadata.ClassInfo) Transaction(org.neo4j.ogm.transaction.Transaction) EntityUtils(org.neo4j.ogm.utils.EntityUtils) TransientRelationship(org.neo4j.ogm.context.TransientRelationship) Compiler(org.neo4j.ogm.cypher.compiler.Compiler) Statement(org.neo4j.ogm.request.Statement) ArrayList(java.util.ArrayList)

Example 3 with Response

use of org.neo4j.ogm.response.Response in project neo4j-ogm by neo4j.

the class EmbeddedRequest method execute.

@Override
public Response<RowModel> execute(DefaultRequest query) {
    // TODO this is a hack to get the embedded driver to work with executing multiple statements
    final List<RowModel> rowmodels = new ArrayList<>();
    String[] columns = null;
    for (Statement statement : query.getStatements()) {
        Result result = executeRequest(statement);
        if (columns == null) {
            columns = result.columns().toArray(new String[result.columns().size()]);
        }
        RowModelResponse rowModelResponse = new RowModelResponse(result, entityAdapter);
        RowModel model;
        while ((model = rowModelResponse.next()) != null) {
            rowmodels.add(model);
        }
        result.close();
    }
    final String[] finalColumns = columns;
    return new Response<>() {

        int currentRow = 0;

        @Override
        public RowModel next() {
            if (currentRow < rowmodels.size()) {
                return rowmodels.get(currentRow++);
            }
            return null;
        }

        @Override
        public void close() {
        }

        @Override
        public String[] columns() {
            return finalColumns;
        }
    };
}
Also used : Response(org.neo4j.ogm.response.Response) EmptyResponse(org.neo4j.ogm.response.EmptyResponse) GraphRowModelResponse(org.neo4j.ogm.drivers.embedded.response.GraphRowModelResponse) GraphModelResponse(org.neo4j.ogm.drivers.embedded.response.GraphModelResponse) RowModelResponse(org.neo4j.ogm.drivers.embedded.response.RowModelResponse) RestModelResponse(org.neo4j.ogm.drivers.embedded.response.RestModelResponse) GraphRowModelResponse(org.neo4j.ogm.drivers.embedded.response.GraphRowModelResponse) RowModelResponse(org.neo4j.ogm.drivers.embedded.response.RowModelResponse) Statement(org.neo4j.ogm.request.Statement) ArrayList(java.util.ArrayList) RowModel(org.neo4j.ogm.model.RowModel) Result(org.neo4j.graphdb.Result)

Example 4 with Response

use of org.neo4j.ogm.response.Response in project neo4j-ogm by neo4j.

the class GraphRowListModelMapper method map.

public <T> Iterable<T> map(Class<T> type, Response<GraphRowListModel> response) {
    Set<Long> idsOfResultEntities = new LinkedHashSet<>();
    Response<GraphModel> graphResponse = new Response<GraphModel>() {

        GraphRowListModel currentIteratedModel;

        int currentIndex = 0;

        @Override
        public GraphModel next() {
            if (currentIteratedModel == null) {
                currentIteratedModel = response.next();
                if (currentIteratedModel == null) {
                    return null;
                }
                currentIndex = 0;
            }
            List<GraphRowModel> listOfRowModels = currentIteratedModel.model();
            if (listOfRowModels.size() <= currentIndex) {
                currentIteratedModel = null;
                return next();
            }
            GraphRowModel graphRowModel = listOfRowModels.get(currentIndex++);
            Set<Long> idsInCurrentRow = Arrays.stream(graphRowModel.getRow()).filter(Number.class::isInstance).map(Number.class::cast).map(Number::longValue).collect(toSet());
            idsOfResultEntities.addAll(idsInCurrentRow);
            return graphRowModel.getGraph();
        }

        @Override
        public void close() {
            response.close();
        }

        @Override
        public String[] columns() {
            return response.columns();
        }
    };
    // although it looks like that the `idsOfResultEntities` will stay empty, they won't, trust us.
    BiFunction<GraphModel, Long, Boolean> includeModelObject = (graphModel, nativeId) -> idsOfResultEntities.contains(nativeId);
    return delegate.map(type, graphResponse, includeModelObject);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) EntityInstantiator(org.neo4j.ogm.session.EntityInstantiator) Arrays(java.util.Arrays) List(java.util.List) GraphRowModel(org.neo4j.ogm.model.GraphRowModel) MetaData(org.neo4j.ogm.metadata.MetaData) BiFunction(java.util.function.BiFunction) Set(java.util.Set) Response(org.neo4j.ogm.response.Response) GraphModel(org.neo4j.ogm.model.GraphModel) GraphRowListModel(org.neo4j.ogm.model.GraphRowListModel) Collectors(java.util.stream.Collectors) LinkedHashSet(java.util.LinkedHashSet) GraphRowModel(org.neo4j.ogm.model.GraphRowModel) Response(org.neo4j.ogm.response.Response) GraphModel(org.neo4j.ogm.model.GraphModel) GraphRowListModel(org.neo4j.ogm.model.GraphRowListModel)

Example 5 with Response

use of org.neo4j.ogm.response.Response in project neo4j-ogm by neo4j.

the class DeleteDelegate method deleteOneOrMoreObjects.

// TODO : this is being done in multiple requests at the moment, one per object. Why not put them in a single request?
private void deleteOneOrMoreObjects(List<?> objects, Set<Object> neighbours) {
    Set<Object> notified = new HashSet<>();
    if (session.eventsEnabled()) {
        for (Object affectedObject : neighbours) {
            if (!notified.contains(affectedObject)) {
                session.notifyListeners(new PersistenceEvent(affectedObject, Event.TYPE.PRE_SAVE));
                notified.add(affectedObject);
            }
        }
    }
    for (Object object : objects) {
        MetaData metaData = session.metaData();
        ClassInfo classInfo = metaData.classInfo(object);
        if (classInfo == null) {
            session.warn(object.getClass().getName() + " is not an instance of a persistable class");
        } else {
            MappingContext mappingContext = session.context();
            Long id = mappingContext.optionalNativeId(object).filter(possibleId -> possibleId >= 0).orElseGet(() -> {
                session.warn(String.format("Instance of class %s has to be reloaded to be deleted. This can happen if the session has " + "been cleared between loading and deleting or using an object from a different transaction.", object.getClass()));
                return classInfo.getPrimaryIndexOrIdReader().apply(object).map(primaryIndexOrId -> session.load(object.getClass(), (Serializable) primaryIndexOrId)).flatMap(reloadedObject -> mappingContext.optionalNativeId(reloadedObject)).orElse(-1L);
            });
            if (id >= 0) {
                Statement request = getDeleteStatement(object, id, classInfo);
                if (session.eventsEnabled() && !notified.contains(object)) {
                    session.notifyListeners(new PersistenceEvent(object, Event.TYPE.PRE_DELETE));
                    notified.add(object);
                }
                RowModelRequest query = new DefaultRowModelRequest(request.getStatement(), request.getParameters());
                session.doInTransaction(() -> {
                    try (Response<RowModel> response = session.requestHandler().execute(query)) {
                        if (request.optimisticLockingConfig().isPresent()) {
                            List<RowModel> rowModels = response.toList();
                            session.optimisticLockingChecker().checkResultsCount(rowModels, request);
                        }
                        if (metaData.isRelationshipEntity(classInfo.name())) {
                            session.detachRelationshipEntity(id);
                        } else {
                            session.detachNodeEntity(id);
                        }
                        // enabled in the first place.
                        if (notified.contains(object)) {
                            session.notifyListeners(new PersistenceEvent(object, Event.TYPE.POST_DELETE));
                        }
                    }
                }, Transaction.Type.READ_WRITE);
            }
        }
    }
    if (session.eventsEnabled()) {
        for (Object affectedObject : neighbours) {
            if (notified.contains(affectedObject)) {
                session.notifyListeners(new PersistenceEvent(affectedObject, Event.TYPE.POST_SAVE));
            }
        }
    }
}
Also used : Arrays(java.util.Arrays) CypherQuery(org.neo4j.ogm.cypher.query.CypherQuery) Response(org.neo4j.ogm.response.Response) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) MappingContext(org.neo4j.ogm.context.MappingContext) RowModelRequest(org.neo4j.ogm.request.RowModelRequest) RelationshipDeleteStatements(org.neo4j.ogm.session.request.strategy.impl.RelationshipDeleteStatements) ArrayList(java.util.ArrayList) DefaultRowModelRequest(org.neo4j.ogm.cypher.query.DefaultRowModelRequest) HashSet(java.util.HashSet) RowModel(org.neo4j.ogm.model.RowModel) Map(java.util.Map) Event(org.neo4j.ogm.session.event.Event) Transaction(org.neo4j.ogm.transaction.Transaction) PersistenceEvent(org.neo4j.ogm.session.event.PersistenceEvent) Result(org.neo4j.ogm.model.Result) DeleteStatements(org.neo4j.ogm.session.request.strategy.DeleteStatements) MetaData(org.neo4j.ogm.metadata.MetaData) Set(java.util.Set) NodeDeleteStatements(org.neo4j.ogm.session.request.strategy.impl.NodeDeleteStatements) Filter(org.neo4j.ogm.cypher.Filter) Statement(org.neo4j.ogm.request.Statement) Serializable(java.io.Serializable) List(java.util.List) ClassInfo(org.neo4j.ogm.metadata.ClassInfo) Collections(java.util.Collections) Serializable(java.io.Serializable) Statement(org.neo4j.ogm.request.Statement) RowModelRequest(org.neo4j.ogm.request.RowModelRequest) DefaultRowModelRequest(org.neo4j.ogm.cypher.query.DefaultRowModelRequest) MappingContext(org.neo4j.ogm.context.MappingContext) PersistenceEvent(org.neo4j.ogm.session.event.PersistenceEvent) MetaData(org.neo4j.ogm.metadata.MetaData) RowModel(org.neo4j.ogm.model.RowModel) HashSet(java.util.HashSet) ClassInfo(org.neo4j.ogm.metadata.ClassInfo) DefaultRowModelRequest(org.neo4j.ogm.cypher.query.DefaultRowModelRequest)

Aggregations

Response (org.neo4j.ogm.response.Response)6 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ClassInfo (org.neo4j.ogm.metadata.ClassInfo)3 MetaData (org.neo4j.ogm.metadata.MetaData)3 GraphModel (org.neo4j.ogm.model.GraphModel)3 RowModel (org.neo4j.ogm.model.RowModel)3 Statement (org.neo4j.ogm.request.Statement)3 Arrays (java.util.Arrays)2 Map (java.util.Map)2 Set (java.util.Set)2 BiFunction (java.util.function.BiFunction)2 Collectors (java.util.stream.Collectors)2 MappingContext (org.neo4j.ogm.context.MappingContext)2 EntityInstantiator (org.neo4j.ogm.session.EntityInstantiator)2 Neo4jSession (org.neo4j.ogm.session.Neo4jSession)2 EntityUtils (org.neo4j.ogm.utils.EntityUtils)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Serializable (java.io.Serializable)1