1

I've been studying some of AGE's source code so that I can create functions to generate different kinds of graphs, such as the Barabási–Albert graph. Although another function like the create_complete_graph does show how to create a graph and insert vertices and edges into it, it does not use functions that require the GRAPH_global_context structure to be used.

Some of these functions, such as the ListGraphId *get_vertex_entry_edges_out(vertex_entry* ve) needs to pass a vertex_entry data type which the only way to retrieve it is by using:

vertex_entry *get_vertex_entry(GRAPH_global_context *ggctx, graphid vertex_id);

This would help creating the Barabasi-Albert graph because the creation of it depends on the amount of edges each vertex has, so it would be faster to retrieve the edges that comes in and out the vertices with the function above.

But, it is not written in the source code how or when to create a GRAPH_global_context or how to properly. How can I implement this for the creation of the graph?

Matheus Farias
  • 716
  • 1
  • 10

2 Answers2

0

The static variable global_graph_contexts in file age_global_graph.c stores all global graph contexts, and you can retrieve the GRAPH_global_context using the function manage_GRAPH_global_contexts by passing the graph name and Oid.

You can get the Oid for the graph that you want by using the function get_graph_oid passing the graph name as a parameter or running a query like this:

SELECT * FROM ag_catalog.ag_graph;

Keep in mind that the function manage_GRAPH_global_contexts will create the context for the specified graph if the context does not already exist or is not valid.

Wendel
  • 763
  • 1
  • 12
  • Hello Wendel! Yeah I'm using that but when other functions that require the `GRAPH_global_context` to be used as parameter are called, one of the two things might happen: or the server stops unexpectedly or the functions does not work at all. For instance, retrieving the head of the vertices list from that structure stops the server unexpectedly. – Matheus Farias May 03 '23 at 20:17
  • Have you used the function `get_graph_vertices` to get the vertices list? The function `age_vertex_stats` seems to get the context and use it for the function `get_vertex_entry` – Wendel May 03 '23 at 21:09
  • I tried that also but calling it with `get_graph_vertices(ggctx->vertices)` throws an error `ERROR: pointer to incomplete class type "struct GRAPH_global_context" is not allowed` – Matheus Farias May 03 '23 at 21:20
  • Try `get_graph_vertices(ggctx)` to get the ListGraphId, and I believe the function `peek_stack_head` will return a reference for the head entry for you. – Wendel May 03 '23 at 21:33
0

I think you can take a look into the PR created for the function (create_complete_graph)
Here it is: https://github.com/apache/age/pull/662/files

Specially looking into src/backend/utils/graph_generation.c

At create_complete_graph function

That part responsible for creating a new graph

    if (!graph_exists(graph_name_str))
    {
        DirectFunctionCall1(create_graph, CStringGetDatum(graph_name));
    }

    graph_id = get_graph_oid(graph_name_str);

That part responsible for creating vertices

    /* Creating vertices*/
    for (i=(int64)1;i<=no_vertices;i++)
    {   
        vid = nextval_internal(vtx_seq_id, true);
        object_graph_id = make_graphid(vtx_label_id, vid);
        insert_vertex_simple(graph_id,vtx_name_str,object_graph_id,props);
    }

And finally those lines for edges:

    /* Creating edges*/
    for (i = 1;i<=no_vertices-1;i++)
    {   
        start_vid = lid-no_vertices+i;
        for(j=i+1;j<=no_vertices;j++)
        {  
            end_vid = lid-no_vertices+j;
            eid = nextval_internal(edge_seq_id, true);
            object_graph_id = make_graphid(edge_label_id, eid);

            start_vertex_graph_id = make_graphid(vtx_label_id, start_vid);
            end_vertex_graph_id = make_graphid(vtx_label_id, end_vid);
          
            insert_edge_simple(graph_id, edge_name_str,
                            object_graph_id, start_vertex_graph_id,
                            end_vertex_graph_id, props);
        }
    }
    PG_RETURN_VOID();
}

References: