Skip to content
jon-ivmark edited this page Jul 15, 2012 · 22 revisions

All of the examples relates to this small graph:

Example

Get all nodes

private List<Node> getAllNodes(PropertyGraph graph) {
    return graph.getNodes().asList();
}

Get all users

private List<Node> getUsers(PropertyGraph graph) {
    return graph.getNodes().filter(new Predicate<Node>() {
        @Override
        public boolean apply(Node input) {
            return input.getType().name().equals("User");
        }
    }).asList();
}

Get all user names

private List<String> getUserNames(PropertyGraph graph) {
    return graph.getNodes().filter(new Predicate<Node>() {
        @Override
        public boolean apply(Node input) {
            return input.getType().name().equals("User");
        }
    }).transform(new Function<Node, String>() {
        @Override
        public String apply(Node user) {
            return (String) user.getProperty("Name");
        }
    }).asList();
}

Get all purchases for a user

private List<Node> getPurchases(PropertyGraph graph, String userId) {
    NodeId start = new NodeId(graph.getNodeType("User"), userId);
    EdgeType bought = graph.getEdgeType("Bought");
    return graph.getNeighbors(start, bought,
                              EdgeDirection.OUTGOING).asList();
}

Get 2 purchases for a user

private List<Node> getPurchases(PropertyGraph graph, String userId) {
    NodeId start = new NodeId(graph.getNodeType("User"), userId);
    EdgeType bought = graph.getEdgeType("Bought");
    return graph.getNeighbors(start, bought,
                              EdgeDirection.OUTGOING).limit(2).asList();
}

Get all purchases for a user where the track cost less than 1

private List<Node> getCheapPurchases(PropertyGraph graph, String userId) {
    NodeId start = new NodeId(graph.getNodeType("User"), userId);
    EdgeType bought = graph.getEdgeType("Bought");
    return graph.getNeighbors(start, bought, EdgeDirection.OUTGOING)
        .filter(new Predicate<Node>() {
            @Override
            public boolean apply(Node track) {
                return (Double) track.getProperty("Price") < 1;
            }
        }).asList();
}

Get all users that bought a certain track

private List<Node> getBuyers(PropertyGraph graph, String trackId) {
    NodeId end = new NodeId(graph.getNodeType("Track"), trackId);
    EdgeType bought = graph.getEdgeType("Bought");
    return graph.getNeighbors(end, bought,
                              EdgeDirection.INCOMING).asList();
}

Add a user

private Node addUser(PropertyGraph graph, String userId) {
    NodeId nodeId = new NodeId(graph.getOrCreateNodeType("User"), userId);
    return graph.addNode(nodeId);
}

Set user properties

private void setUserProperties(PropertyGraph graph, String userId,
                               Map<String, Object> properties) {
    NodeId nodeId =
        new NodeId(graph.getNodeType("User"), userId);
    Properties props = new HashMapProperties(properties);
    graph.setNodeProperties(nodeId, props);
}

Remove a user

private Node removeUser(PropertyGraph graph, String userId) {
    NodeId nodeId = new NodeId(graph.getNodeType("User"), userId);
    return graph.removeNode(nodeId);
}

Add a purchase

private Edge addPurchase(PropertyGraph graph, String userId,
                         String trackId) {
    NodeId user =
        new NodeId(graph.getNodeType("User"), userId);
    NodeId track =
        new NodeId(graph.getNodeType("Track"), trackId);
    EdgeType bought = graph.getOrCreateEdgeType("Bought");
    return graph.addEdge(user, track, bought);
}

Clone this wiki locally