MCPcopy Create free account
hub / github.com/RedisGraph/RedisGraph / Proc_BFS_Invoke

Function Proc_BFS_Invoke

src/procedures/proc_bfs.c:66–141  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

64}
65
66static ProcedureResult Proc_BFS_Invoke
67(
68 ProcedureCtx *ctx,
69 const SIValue *args,
70 const char **yield
71) {
72 // validate inputs
73 ASSERT(ctx != NULL);
74 ASSERT(args != NULL);
75
76 if(array_len((SIValue *)args) != 3) return PROCEDURE_ERR;
77 if(SI_TYPE(args[0]) != T_NODE || // source node
78 SI_TYPE(args[1]) != T_INT64 || // max level to iterate to, unlimited if 0
79 !(SI_TYPE(args[2]) & (T_NULL | T_STRING))) // relationship type to traverse if not NULL
80 return PROCEDURE_ERR;
81
82 BFSCtx *bfs_ctx = ctx->privateData;
83 _process_yield(bfs_ctx, yield);
84
85 //--------------------------------------------------------------------------
86 // Process inputs
87 //--------------------------------------------------------------------------
88
89 Node *source_node = args[0].ptrval;
90 int64_t max_level = args[1].longval;
91 const char *reltype = SIValue_IsNull(args[2]) ? NULL : args[2].stringval;
92
93 GrB_Index src_id = ENTITY_GET_ID(source_node);
94
95 // Get edge matrix and transpose matrix, if available.
96 GrB_Matrix R = NULL;
97 GraphContext *gc = QueryCtx_GetGraphCtx();
98
99 if(reltype == NULL) {
100 RG_Matrix_export(&R, Graph_GetAdjacencyMatrix(gc->g, false));
101 } else {
102 Schema *s = GraphContext_GetSchema(gc, reltype, SCHEMA_EDGE);
103 // failed to find schema, first step will return NULL
104 if(!s) return PROCEDURE_OK;
105
106 bfs_ctx->reltype_id = s->id;
107 RG_Matrix_export(&R, Graph_GetRelationMatrix(gc->g, s->id, false));
108 }
109
110 // if we're not collecting edges, pass a NULL parent pointer
111 // so that the algorithm will not perform unnecessary work
112 GrB_Vector V = GrB_NULL; // vector of results
113 GrB_Vector PI = GrB_NULL; // vector backtracking results to their parents
114 GrB_Vector *pPI = Π
115 if(!bfs_ctx->yield_edges) pPI = NULL;
116 GrB_Info res = LG_BreadthFirstSearch_SSGrB(&V, pPI, R, src_id, NULL, max_level);
117 ASSERT(res == GrB_SUCCESS);
118
119 // remove all values with a level less than or equal to 1
120 // values of 0 are not connected to the source, and values of 1 are the source
121 GxB_Scalar thunk;
122 GxB_Scalar_new(&thunk, GrB_UINT64);
123 GxB_Scalar_setElement_UINT64(thunk, 0);

Callers

nothing calls this directly

Calls 15

array_lenFunction · 0.85
SIValue_IsNullFunction · 0.85
QueryCtx_GetGraphCtxFunction · 0.85
RG_Matrix_exportFunction · 0.85
Graph_GetAdjacencyMatrixFunction · 0.85
GraphContext_GetSchemaFunction · 0.85
Graph_GetRelationMatrixFunction · 0.85
GxB_Scalar_newFunction · 0.85
GxB_Vector_selectFunction · 0.85
GxB_Scalar_freeFunction · 0.85
GrB_Vector_nvalsFunction · 0.85

Tested by

no test coverage detected