TangCheng
2025-04-13 1aaaf72311b992e1885ecb393aab964d0637ce1f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
 
#include "postgres.h"
 
#include "access/genam.h"
#include "commands/graph_commands.h"
#include "utils/load/age_load.h"
 
#include "pg_fix.h"
 
Datum create_vlabel(PG_FUNCTION_ARGS);
Datum create_elabel(PG_FUNCTION_ARGS);
 
int64 get_nextval_internal(graph_cache_data* graph_cache,
                           label_cache_data* label_cache);
/*
 * Auxiliary function to get the next internal value in the graph,
 * so a new object (node or edge) graph id can be composed.
 */
 
int64 get_nextval_internal(graph_cache_data* graph_cache,
                           label_cache_data* label_cache)
{
    Oid obj_seq_id;
    char* label_seq_name_str;
 
    label_seq_name_str = NameStr(label_cache->seq_name);
    obj_seq_id = get_relname_relid(label_seq_name_str,
                                   graph_cache->namespace);
 
    return nextval_internal(obj_seq_id, true);
}
 
PG_FUNCTION_INFO_V1(create_complete_graph);
 
/*
* SELECT * FROM ag_catalog.create_complete_graph('graph_name',no_of_nodes, 'edge_label', 'node_label'=NULL);
*/
 
Datum create_complete_graph(PG_FUNCTION_ARGS)
{
    Oid graph_oid;
    Name graph_name;
 
    int64 no_vertices;
    int64 i,j,vid = 1, eid, start_vid, end_vid;
 
    Name vtx_label_name = NULL;
    Name edge_label_name;
    int32 vtx_label_id;
    int32 edge_label_id;
 
    agtype *props = NULL;
    graphid object_graph_id;
    graphid start_vertex_graph_id;
    graphid end_vertex_graph_id;
 
    Oid vtx_seq_id;
    Oid edge_seq_id;
 
    char* graph_name_str;
    char* vtx_name_str;
    char* edge_name_str;
 
    graph_cache_data *graph_cache;
    label_cache_data *vertex_cache;
    label_cache_data *edge_cache;
 
    Oid nsp_id;
    Name vtx_seq_name;
    char *vtx_seq_name_str;
 
    Name edge_seq_name;
    char *edge_seq_name_str;
 
    int64 lid;
 
    if (PG_ARGISNULL(0))
    {
        ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                        errmsg("graph name can not be NULL")));
    }
 
    if (PG_ARGISNULL(1))
    {
        ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                        errmsg("number of nodes can not be NULL")));
    }
 
    if (PG_ARGISNULL(2))
    {
        ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                        errmsg("edge label can not be NULL")));
    }
 
    graph_name = PG_GETARG_NAME(0);
    no_vertices = (int64) PG_GETARG_INT64(1);
    edge_label_name = PG_GETARG_NAME(2);
 
    graph_name_str = NameStr(*graph_name);
    vtx_name_str = AG_DEFAULT_LABEL_VERTEX;
    edge_name_str = NameStr(*edge_label_name);
 
    if (!PG_ARGISNULL(3))
    {
        vtx_label_name = PG_GETARG_NAME(3);
        vtx_name_str = NameStr(*vtx_label_name);
 
        // Check if vertex and edge label are same
        if (strcmp(vtx_name_str, edge_name_str) == 0)
        {
            ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                    errmsg("vertex and edge label can not be same")));
        }
    }
 
    if (!graph_exists(graph_name_str))
    {
        DirectFunctionCall1(create_graph, CStringGetDatum(graph_name));
    }
 
    graph_oid = get_graph_oid(graph_name_str);
 
    if (!PG_ARGISNULL(3))
    {
        // Check if label with the input name already exists
        if (!label_exists(vtx_name_str, graph_oid))
        {
            DirectFunctionCall2(create_vlabel,
                                CStringGetDatum(graph_name),
                                CStringGetDatum(vtx_label_name));
        }
    }
 
    if (!label_exists(edge_name_str, graph_oid))
    {
        DirectFunctionCall2(create_elabel,
                            CStringGetDatum(graph_name),
                            CStringGetDatum(edge_label_name));
    }
 
    vtx_label_id = get_label_id(vtx_name_str, graph_oid);
    edge_label_id = get_label_id(edge_name_str, graph_oid);
 
    graph_cache = search_graph_name_cache(graph_name_str);
    vertex_cache = search_label_name_graph_cache(vtx_name_str, graph_oid);
    edge_cache = search_label_name_graph_cache(edge_name_str, graph_oid);
 
    nsp_id = graph_cache->namespace;
    vtx_seq_name = &(vertex_cache->seq_name);
    vtx_seq_name_str = NameStr(*vtx_seq_name);
 
    edge_seq_name = &(edge_cache->seq_name);
    edge_seq_name_str = NameStr(*edge_seq_name);
 
    vtx_seq_id = get_relname_relid(vtx_seq_name_str, nsp_id);
    edge_seq_id = get_relname_relid(edge_seq_name_str, nsp_id);
 
    props = create_empty_agtype();
 
    /* 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_oid, vtx_name_str, object_graph_id, props);
    }
 
    lid = vid;
 
    /* 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_oid, edge_name_str, object_graph_id,
                               start_vertex_graph_id, end_vertex_graph_id,
                               props);
        }
    }
    PG_RETURN_VOID();
}
 
 
PG_FUNCTION_INFO_V1(age_create_barbell_graph);
 
/*
 * The barbell graph is two complete graphs connected by a bridge path
 * Syntax:
 * ag_catalog.age_create_barbell_graph(graph_name Name,
 *                                     m int,
 *                                     n int,
 *                                     vertex_label_name Name DEFAULT = NULL,
 *                                     vertex_properties agtype DEFAULT = NULL,
 *                                     edge_label_name Name DEFAULT = NULL,
 *                                     edge_properties agtype DEFAULT = NULL)
 * Input:
 *
 * graph_name - Name of the graph to be created.
 * m - number of vertices in one complete graph.
 * n - number of vertices in the bridge path.
 * vertex_label_name - Name of the label to assign each vertex to.
 * vertex_properties - Property values to assign each vertex. Default is NULL
 * edge_label_name - Name of the label to assign each edge to.
 * edge_properties - Property values to assign each edge. Default is NULL
 *
 * https://en.wikipedia.org/wiki/Barbell_graph
 */
 
Datum age_create_barbell_graph(PG_FUNCTION_ARGS)
{
    FunctionCallInfo arguments;
    Oid graph_oid;
    Name graph_name;
    char* graph_name_str;
 
    int64 start_node_index, end_node_index, nextval;
 
    Name node_label_name = NULL;
    int32 node_label_id;
    char* node_label_str;
 
    Name edge_label_name;
    int32 edge_label_id;
    char* edge_label_str;
 
    graphid object_graph_id;
    graphid start_node_graph_id;
    graphid end_node_graph_id;
 
    graph_cache_data* graph_cache;
    label_cache_data* edge_cache;
 
    agtype* properties = NULL;
 
    arguments = fcinfo;
 
    // Checking for possible NULL arguments
    // Name graph_name
    if (PG_ARGISNULL(0))
    {
        ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                        errmsg("Graph name cannot be NULL")));
    }
 
    graph_name = PG_GETARG_NAME(0);
    graph_name_str = NameStr(*graph_name);
 
    // int graph size (number of nodes in each complete graph)
    if (PG_ARGISNULL(1) && PG_GETARG_INT32(1) < 3)
    {
        ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                errmsg("Graph size cannot be NULL or lower than 3")));
    }
 
    /*
     * int64 bridge_size: currently only stays at zero.
     * to do: implement bridge with variable number of nodes.
     */
    if (PG_ARGISNULL(2) || PG_GETARG_INT32(2) < 0 )
    {
        ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                errmsg("Bridge size cannot be NULL or lower than 0")));
    }
 
    // node label: if null, gets default label, which is "_ag_label_vertex"
    if (PG_ARGISNULL(3))
    {
        namestrcpy(node_label_name, AG_DEFAULT_LABEL_VERTEX);
    }
    else
    {
        node_label_name = PG_GETARG_NAME(3);
    }
    node_label_str = NameStr(*node_label_name);
 
    /* Name edge_label */
    if (PG_ARGISNULL(5))
    {
        ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                errmsg("edge label can not be NULL")));
    }
 
    edge_label_name = PG_GETARG_NAME(5);
    edge_label_str = NameStr(*edge_label_name);
 
 
    // create two separate complete graphs
    DirectFunctionCall4(create_complete_graph, arguments->args[0].value,
                                               arguments->args[1].value,
                                               arguments->args[5].value,
                                               arguments->args[3].value);
    DirectFunctionCall4(create_complete_graph, arguments->args[0].value,
                                               arguments->args[1].value,
                                               arguments->args[5].value,
                                               arguments->args[3].value);
 
    graph_oid = get_graph_oid(graph_name_str);
    node_label_id = get_label_id(node_label_str, graph_oid);
    edge_label_id = get_label_id(edge_label_str, graph_oid);
 
    /*
     * Fetching caches to get next values for graph id's, and access nodes
     * to be connected with edges.
     */
    graph_cache = search_graph_name_cache(graph_name_str);
    edge_cache = search_label_name_graph_cache(edge_label_str,graph_oid);
 
    // connect a node from each graph
    start_node_index = 1; // first created node, from the first complete graph
    end_node_index = arguments->args[1].value*2; // last created node, second graph
 
    // next index to be assigned to a node or edge
    nextval = get_nextval_internal(graph_cache, edge_cache);
 
    // build the graph id's of the edge to be created
    object_graph_id = make_graphid(edge_label_id, nextval);
    start_node_graph_id = make_graphid(node_label_id, start_node_index);
    end_node_graph_id = make_graphid(node_label_id, end_node_index);
    properties = create_empty_agtype();
 
    // connect two nodes
    insert_edge_simple(graph_oid, edge_label_str,
                       object_graph_id, start_node_graph_id,
                       end_node_graph_id, properties);
 
    PG_RETURN_VOID();
}