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

com.tinkerpop.blueprints.impls.dex.DexVertex Maven / Gradle / Ivy

There is a newer version: 2.4.0
Show newest version
/**
 *
 */
package com.tinkerpop.blueprints.impls.dex;

import com.tinkerpop.blueprints.CloseableIterable;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Query;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.util.DefaultQuery;
import com.tinkerpop.blueprints.util.MultiIterable;
import com.tinkerpop.blueprints.util.StringFactory;
import com.tinkerpop.blueprints.util.WrappingCloseableIterable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * {@link Vertex} implementation for Dex.
 *
 * @author Sparsity
 *         Technologies
 */
class DexVertex extends DexElement implements Vertex {
    /**
     * Creates a new instance.
     *
     * @param g   DexGraph.
     * @param oid Dex OID.
     */
    protected DexVertex(final DexGraph g, final long oid) {
        super(g, oid);
    }

    public CloseableIterable getEdges(final Direction direction, final String... labels) {
        if (direction.equals(Direction.OUT)) {
            return this.getOutEdges(labels);
        } else if (direction.equals(Direction.IN))
            return this.getInEdges(labels);
        else {
            return new MultiIterable(new ArrayList>(Arrays.asList(this.getInEdges(labels), this.getOutEdges(labels))));
        }
    }

    public CloseableIterable getVertices(final Direction direction, final String... labels) {
        if (direction.equals(Direction.OUT)) {
            return this.getOutVertices(labels);
        } else if (direction.equals(Direction.IN))
            return this.getInVertices(labels);
        else {
            return new MultiIterable(new ArrayList>(Arrays.asList(this.getInVertices(labels), this.getOutVertices(labels))));
        }
    }

    private CloseableIterable getOutEdgesNoLabels() {
        com.sparsity.dex.gdb.TypeList tlist = graph.getRawGraph().findEdgeTypes();
        final List> edges = new ArrayList>();
        for (Integer etype : tlist) {
            edges.add(getOutEdgesSingleType(etype));
        }
        tlist.delete();
        tlist = null;
        return new MultiIterable(edges);
    }

    private CloseableIterable getOutVerticesNoLabels() {
        com.sparsity.dex.gdb.TypeList tlist = graph.getRawGraph().findEdgeTypes();
        final List> vertices = new ArrayList>();
        for (Integer etype : tlist) {
            vertices.add(getOutVerticesSingleType(etype));
        }
        tlist.delete();
        tlist = null;
        return new MultiIterable(vertices);
    }

    private CloseableIterable getInEdgesNoLabels() {
        com.sparsity.dex.gdb.TypeList tlist = graph.getRawGraph().findEdgeTypes();
        final List> edges = new ArrayList>();
        for (Integer etype : tlist) {
            edges.add(getInEdgesSingleType(etype));
        }
        tlist.delete();
        tlist = null;
        return new MultiIterable(edges);
    }

    private CloseableIterable getInVerticesNoLabels() {
        com.sparsity.dex.gdb.TypeList tlist = graph.getRawGraph().findEdgeTypes();
        final List> vertices = new ArrayList>();
        for (Integer etype : tlist) {
            vertices.add(getInVerticesSingleType(etype));
        }
        tlist.delete();
        tlist = null;
        return new MultiIterable(vertices);
    }

    private CloseableIterable getOutEdgesSingleLabel(final String label) {
        int type = graph.getRawGraph().findType(label);
        if (type == com.sparsity.dex.gdb.Type.InvalidType) {
            return new WrappingCloseableIterable((Iterable) Collections.emptyList());
        }

        return getOutEdgesSingleType(type);
    }

    private CloseableIterable getOutVerticesSingleLabel(final String label) {
        int type = graph.getRawGraph().findType(label);
        if (type == com.sparsity.dex.gdb.Type.InvalidType) {
            return new WrappingCloseableIterable((Iterable) Collections.emptyList());
        }

        return getOutVerticesSingleType(type);
    }

    private CloseableIterable getOutEdgesSingleType(final int type) {
        com.sparsity.dex.gdb.Objects objs = graph.getRawGraph().explode(oid, type, com.sparsity.dex.gdb.EdgesDirection.Outgoing);
        return new DexIterable(graph, objs, Edge.class);
    }

    private CloseableIterable getOutVerticesSingleType(final int type) {
        com.sparsity.dex.gdb.Objects objs = graph.getRawGraph().neighbors(oid, type, com.sparsity.dex.gdb.EdgesDirection.Outgoing);
        return new DexIterable(graph, objs, Vertex.class);
    }

    private CloseableIterable getInEdgesSingleLabel(final String label) {
        int type = graph.getRawGraph().findType(label);
        if (type == com.sparsity.dex.gdb.Type.InvalidType) {
            return new WrappingCloseableIterable((Iterable) Collections.emptyList());
        }

        return getInEdgesSingleType(type);
    }

    private CloseableIterable getInVerticesSingleLabel(final String label) {
        int type = graph.getRawGraph().findType(label);
        if (type == com.sparsity.dex.gdb.Type.InvalidType) {
            return new WrappingCloseableIterable((Iterable) Collections.emptyList());
        }

        return getInVerticesSingleType(type);
    }

    private CloseableIterable getInEdgesSingleType(final int type) {
        com.sparsity.dex.gdb.Objects objs = graph.getRawGraph().explode(oid, type, com.sparsity.dex.gdb.EdgesDirection.Ingoing);
        return new DexIterable(graph, objs, Edge.class);
    }

    private CloseableIterable getInVerticesSingleType(final int type) {
        com.sparsity.dex.gdb.Objects objs = graph.getRawGraph().neighbors(oid, type, com.sparsity.dex.gdb.EdgesDirection.Ingoing);
        return new DexIterable(graph, objs, Vertex.class);
    }

    public String toString() {
        return StringFactory.vertexString(this);
    }

    private CloseableIterable getInEdges(final String... labels) {
        if (labels.length == 0)
            return this.getInEdgesNoLabels();
        else if (labels.length == 1) {
            return this.getInEdgesSingleLabel(labels[0]);
        } else {
            final List> edges = new ArrayList>();
            for (final String label : labels) {
                edges.add(this.getInEdgesSingleLabel(label));
            }
            return new MultiIterable(edges);
        }
    }

    private CloseableIterable getInVertices(final String... labels) {
        if (labels.length == 0)
            return this.getInVerticesNoLabels();
        else if (labels.length == 1) {
            return this.getInVerticesSingleLabel(labels[0]);
        } else {
            final List> vertices = new ArrayList>();
            for (final String label : labels) {
                vertices.add(this.getInVerticesSingleLabel(label));
            }
            return new MultiIterable(vertices);
        }
    }

    private CloseableIterable getOutEdges(final String... labels) {
        if (labels.length == 0)
            return this.getOutEdgesNoLabels();
        else if (labels.length == 1) {
            return this.getOutEdgesSingleLabel(labels[0]);
        } else {
            final List> edges = new ArrayList>();
            for (final String label : labels) {
                edges.add(this.getOutEdgesSingleLabel(label));
            }
            return new MultiIterable(edges);
        }
    }

    private CloseableIterable getOutVertices(final String... labels) {
        if (labels.length == 0)
            return this.getOutVerticesNoLabels();
        else if (labels.length == 1) {
            return this.getOutVerticesSingleLabel(labels[0]);
        } else {
            final List> vertices = new ArrayList>();
            for (final String label : labels) {
                vertices.add(this.getOutVerticesSingleLabel(label));
            }
            return new MultiIterable(vertices);
        }
    }

    public Query query() {
        return new DefaultQuery(this);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy