0

Short and simple question:

For a social network platform would you create a separate node for the friend requests and creating the edge after confirmation, or creating the edge directly and set a confirmed flag?

What are the advantages / disadvantages? I am interested in your comments.

1

3 Answers 3

2

One advantage of using the flag option is when either of the user nodes are deleted by delete vertex the friend request edge will be deleted automatically by OrientDB to maintain graph consistency. If you use a seperate node for the request then you need to delete that node manually.

Performance wise, I guess, the question you linked is relevant to OrientDB too.

For such decisions, I'd also consider the readability of the code. One advantage of using a graph DB is your code becomes easier to understand and reason about. So you can write the queries for different options and judge yourself about which code is more readable. Let's try it for the flag option:

# create
CREATE EDGE Friend 
    FROM (SELECT FROM User where name = "Alice") 
    TO (SELECT FROM User where name = "Bob") 
    SET status = "requested"  # or confirmed = False
# confirmed
UPDATE Friend SET status = "confirmed"  # or confirmed = True
    WHERE out.name = "Alice" AND in.name = "Bob"
# query
SELECT in.name FROM Friend 
  WHERE out.name = "Alice" AND status = "confirmed"  
# output: Bob
# another method
SELECT outE(Friend)[status = "confirmed"].in.name
  FROM User WHERE name = "Alice"
# output: Bob

I'll argue that if you are familiar with graphs as mathematical objects and get used to the OrientDB syntax and terminology, this option enables you to write very understandable code.

If you don't like this option, as an alternative to keeping requests in a different node (class/table), I'll also suggest storing them inside the User nodes as a LINKSET or something similar.

2
  • Thanks you, especially for the code example. Actually I did not know that I can use a dot notation to query by properties (out.name). I thought that I would need to get the record id of the vertex first. What about performance? Is this the way doing that kind of stuff?
    – Vincenzo
    Commented Apr 14, 2015 at 8:24
  • No problem. I just started using OrientDB so currently not very informed about the performance. Their docs are giving impressive numbers and Studio is running the queries very fast (after each query result, it reports the Query time). Something to keep in mind is using a Fetch Plan though.
    – pembeci
    Commented Apr 14, 2015 at 8:39
1

I believe you should also take into consideration the memory you'll have available. If you store that info in the edge, that probably means you'll have to define an index on that property to have faster queries. And this means more memory needed.

I advise you to store friend requests in a different node.

Find friends is easier:

select expand(both('Friend')) from #12:0

Find requests is easier:

select expand(in('Request')) from #12:0

And they are very likely faster than an index on some property.

8
  • doh, thats a good viewpoint as well. Showing that both approaches do have their advantage / disadvantages. What do you think about the other answers?
    – Vincenzo
    Commented Apr 14, 2015 at 8:31
  • To maintain the advantage of keeping the graph consistent when deleting a user vertex I could implement the request also as an edge?
    – Vincenzo
    Commented Apr 14, 2015 at 8:37
  • 1
    @vitorenesduarte has a point but as his examples show I'll still not use another node but another edge class for Request. That way you have the best of both worlds (faster query + graph consistency + good syntax). In the future, if they add a move edge command like move vertex, you'll also have the benefit of converting your Request edge to a Friend edge in a single command rather than deleting one and creating another one.
    – pembeci
    Commented Apr 14, 2015 at 8:46
  • Yes, that's a possibility, but it should be a rule. I don't know what's your situation, but imagine that users can post things in their wall (e.g. just like Facebook). Should we store the posts in edges just to keep the graph consistent when deleting a user? I don't think so. The programmer can be in charge of that. Commented Apr 14, 2015 at 8:48
  • Instead of using subqueries such as "CREATE EDGE Friend FROM (SELECT FROM User where name = "Alice") TO (SELECT FROM User where name = "Bob")". Should I whenever possible use the record id instead? Currently I am mapping the orientDB record.getOData() to a model class but thats leaving out the record id.
    – Vincenzo
    Commented Apr 14, 2015 at 9:07
1

Using a model such User1(V)---Friendship(E)---->User2(V) is enough to represent the friendship bind between users and by using properties you can implement all the workflow from the request to the completion. This design is pretty basic so you'll have a standard complexity when it comes to query/traverse .... that can be more difficult more you add constraints on properties..... a disadvantage is that an Edge is not a Vertex and this will affect its interaction with others vertexes, if you need such interaction then an approach where friendship is a vertex is the way to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.