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

com.vesoft.nebula.driver.graph.data.Relationship Maven / Gradle / Ivy

The newest version!
package com.vesoft.nebula.driver.graph.data;

import com.vesoft.nebula.proto.common.Edge;
import com.vesoft.nebula.proto.common.Value;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class Relationship extends BaseDataObject {
    private final Edge edge;

    /**
     * Relationship is a wrapper around the Edge type returned by nebula-graph
     *
     * @param edge the Edge type returned by nebula-graph
     */
    public Relationship(Edge edge) {
        if (edge == null) {
            throw new RuntimeException("Input an null edge object");
        }
        this.edge = edge;
    }

    /**
     * get graph
     *
     * @return String
     */
    public String getGraph() {
        return edge.getGraph();
    }

    /**
     * get edge type name
     *
     * @return String
     */
    public String getType() {
        return edge.getType();
    }

    /**
     * if the edge is directed
     *
     * @return true if edge is directed
     */
    public boolean isDirected() {
        return edge.getDirection() == Edge.Direction.DIRECTED;
    }

    /**
     * get edge labels
     *
     * @return list of edge labels
     */
    public List getLabels() {
        return edge.getLabelsList();
    }

    /**
     * get the src id
     *
     * @return long
     */
    public long getSrcId() {
        return edge.getSrcId();
    }

    /**
     * get the dst id from the relationship
     *
     * @return long
     */
    public long getDstId() {
        return edge.getDstId();
    }

    /**
     * get rank from the relationship
     *
     * @return long
     */
    public long getRank() {
        return edge.getRank();
    }

    /**
     * get all property name
     *
     * @return the List of property names
     */
    public List getColumnNames() {
        return new ArrayList<>(edge.getPropertiesMap().keySet());
    }

    /**
     * get all property values
     *
     * @return the List of property values
     */
    public List getPropertyValues() {
        List values = new ArrayList<>();
        for (Map.Entry kv : edge.getPropertiesMap().entrySet()) {
            values.add(new ValueWrapper(kv.getValue()));
        }
        return values;
    }

    /**
     * get property names and values from the relationship
     *
     * @return HashMap, property name -> property value
     */
    public HashMap getProperties() {
        HashMap properties = new HashMap<>();
        for (String key : edge.getPropertiesMap().keySet()) {
            properties.put(key, new ValueWrapper(edge.getPropertiesMap().get(key)));
        }
        return properties;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Relationship that = (Relationship) o;
        if (edge.getDirection() == Edge.Direction.DIRECTED) {
            return getRank() == that.getRank()
                    && getSrcId() == that.getSrcId()
                    && getDstId() == that.getDstId()
                    && Objects.equals(getType(), that.getType());
        } else {
            return getRank() == that.getRank()
                    && ((getSrcId() == that.getSrcId() && getDstId() == that.getDstId())
                    || (getSrcId() == that.getDstId() && getDstId() == that.getSrcId()))
                    && Objects.equals(getType(), that.getType());
        }
    }

    @Override
    public int hashCode() {
        return Objects.hash(edge, getDecodeType());
    }

    @Override
    public String toString() {
        List propStrs = new ArrayList<>();
        Map props = getProperties();
        for (String key : props.keySet()) {
            propStrs.add(key + ":" + props.get(key).toString());
        }
        if (edge.getDirection() == Edge.Direction.DIRECTED) {
            return String.format("(%d)-[%d@%s:%s{%s}]->(%d)",
                    getSrcId(),
                    getRank(),
                    getType(),
                    String.join("&", getLabels()),
                    String.join(",", propStrs),
                    getDstId());
        } else {
            return String.format("(%d)~[%d@%s:%s{%s}]~(%d)",
                    getSrcId(),
                    getRank(),
                    getType(),
                    String.join("&", getLabels()),
                    String.join(", ", propStrs),
                    getDstId());
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy