In this blog post, we're going to discuss an upgrade to the create_complete_graph
function in Apache AGE, continuing our series. This function generates a complete graph with the given parameters, such as the graph name, number of nodes, and edge label. We'll be enhancing the function to add support for pre-modeled starting properties when creating nodes and edges.
To understand this modification, let's first dive into the create_complete_graph
function in its initial state. The function takes in the following parameters:
- 'graph_name': The name of the graph.
- 'no_of_nodes': The number of nodes to create.
- 'edge_label': The label for the edges.
- 'node_label': The label for the nodes (optional).
In the existing code, node properties and edge properties are initialized as empty by default:
agtype *props = create_empty_agtype();
Then, these empty properties are passed to both insert_vertex_simple
and insert_edge_simple
functions while creating vertices and edges.
Modifications
Now, we'll modify the function to accept two additional parameters: 'edge_properties' and 'node_properties'. This will allow us to provide initial properties for edges and nodes while creating them.
Here are the steps to implement this enhancement:
-
Add Parameters: First, we need to add two more parameters in the
age--1.3.0.sql
file:
CREATE FUNCTION ag_catalog.create_complete_graph(
graph_name name,
number_of_nodes int,
edge_label name,
edge_properties agtype = NULL,
node_label name = NULL,
node_properties agtype = NULL)
RETURNS void
LANGUAGE c
CALLED ON NULL INPUT
PARALLEL SAFE
AS 'MODULE_PATHNAME';
-
Declare Properties: Next, declare two
agtype
variables, one foredge_properties
and one fornode_properties
. Initialize them toNULL
.
agtype* edge_properties = NULL;
agtype* node_properties = NULL;
-
Assign Properties: Within the function, we will assign values to
edge_properties
andnode_properties
only if they are not NULL.
For edge_properties
:
if(!PG_ARGISNULL(3))
{
edge_properties = (agtype*)PG_GETARG_ARRAYTYPE_P(3);
}
For node_properties
:
if(!PG_ARGISNULL(5))
{
node_properties = (agtype*)PG_GETARG_ARRAYTYPE_P(5);
}
-
Modify Vertex and Edge Creation: Replace the
props
variable ininsert_vertex_simple
andinsert_edge_simple
function calls withnode_properties
andedge_properties
respectively.
For vertex creation:
insert_vertex_simple(
graph_id,
vtx_name_str,
object_graph_id,
node_properties);
For edge creation:
insert_edge_simple(graph_id, edge_name_str,
object_graph_id, start_vertex_graph_id,
end_vertex_graph_id, edge_properties);
And that's it! You have now successfully added the ability to include pre-modeled starting properties for nodes and edges in the create_complete_graph
function. This will allow developers to add their own properties to nodes and edges upon creation, making the function more flexible and customizable.
To call the function now you can put properties as agtype in the parameters, like this:
SELECT * FROM create_complete_graph(
'test_graph', 5,
'edges_label', '{"edge_property":"test"}'::agtype,
'vertices_label', '{"node_property":"test"}'::agtype);
Remember to compile the modified extension and install it in your PostgreSQL server to make use of these enhancements.
I make these posts in order to guide people into the development of a new technology. If you find anything incorrect, I urge you to comment below so I can fix it. Thanks!
Check Apache AGE: https://age.apache.org/.
Overview — Apache AGE master documentation: https://age.apache.org/age-manual/master/intro/overview.html.
GitHub - apache/age: https://github.com/apache/age
Top comments (0)