All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tinkerpop.gremlin.tinkergraph.process.computer.TinkerMessenger Maven / Gradle / Ivy

There is a newer version: 3.0.0.M7
Show newest version
package com.tinkerpop.gremlin.tinkergraph.process.computer;

import com.tinkerpop.gremlin.process.computer.MessageCombiner;
import com.tinkerpop.gremlin.process.computer.MessageType;
import com.tinkerpop.gremlin.process.computer.Messenger;
import com.tinkerpop.gremlin.structure.Edge;
import com.tinkerpop.gremlin.structure.Vertex;
import com.tinkerpop.gremlin.util.StreamFactory;

import java.util.Arrays;
import java.util.Optional;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * @author Marko A. Rodriguez (http://markorodriguez.com)
 */
public class TinkerMessenger implements Messenger {

    private final Vertex vertex;
    private final TinkerMessageBoard messageBoard;
    private final Optional> combiner;


    public TinkerMessenger(final Vertex vertex, final TinkerMessageBoard messageBoard, final Optional> combiner) {
        this.vertex = vertex;
        this.messageBoard = messageBoard;
        this.combiner = combiner;
    }

    @Override
    public Iterable receiveMessages(final MessageType messageType) {
        if (messageType instanceof MessageType.Local) {
            final MessageType.Local localMessageType = (MessageType.Local) messageType;
            final Edge[] edge = new Edge[1]; // simulates storage side-effects available in Gremlin, but not Java8 streams
            return StreamFactory.iterable(StreamFactory.stream(localMessageType.edges(this.vertex).reverse())
                    .map(e -> {
                        edge[0] = e;
                        return this.messageBoard.receiveMessages.get(e.toV(localMessageType.getDirection()).next());
                    })
                    .filter(q -> null != q)
                    .flatMap(q -> q.stream())
                    .map(message -> localMessageType.getEdgeFunction().apply(message, edge[0])));

        } else {
            return StreamFactory.iterable(Arrays.asList(this.vertex).stream()
                    .map(this.messageBoard.receiveMessages::get)
                    .filter(q -> null != q)
                    .flatMap(q -> q.stream()));
        }
    }

    @Override
    public void sendMessage(final MessageType messageType, final M message) {
        if (messageType instanceof MessageType.Local) {
            getMessageList(this.vertex).add(message);
        } else {
            ((MessageType.Global) messageType).vertices().forEach(v -> {
                final Queue queue = getMessageList(v);
                if (this.combiner.isPresent() && !queue.isEmpty()) {
                    this.combiner.get().combine(queue.remove(), message).forEachRemaining(queue::add);
                } else
                    queue.add(message);
            });
        }
    }

    private Queue getMessageList(final Vertex vertex) {
        Queue messages = this.messageBoard.sendMessages.get(vertex);
        if (null == messages) {
            messages = new ConcurrentLinkedQueue<>();
            this.messageBoard.sendMessages.put(vertex, messages);
        }
        return messages;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy