summaryrefslogtreecommitdiff
path: root/storage/oqgraph/graphstore.c
blob: c5478b56ca5def4df3b0cce054509f183fac25c7 (plain)
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
354
355
356
/*
 * Graph Engine - Copyright (C) 2007 by Arjen Lentz (arjen@openquery.com.au)
 * graphstore.c internal storage system
 */
#include <stdlib.h>
#include <string.h>
#include <my_global.h>
#include <my_sys.h>
#include "graphstore.h"


/*
	create a new vertex, and add it to the list (or start a list)
	NOTE! gspp is ptr to base ptr

	returns 1 for ok, 0 for error
*/
static int _add_vertex (GRAPHSTORE **gspp, GRAPH_VERTEXID id)
{
	GRAPHSTORE *newgsp;
	GRAPHSTORE *gscurp;

	if (gspp == NULL)
		return 0;

	/* not allowing 0 */
	if (!id)
		return 0;

	if (*gspp != NULL) {
		for (gscurp = *gspp; gscurp != NULL; gscurp = gscurp->next) {
			if (gscurp->vertex->id == id)
				return 1;	/* we can ignore, id already exists */
		}
	}

	/* allocate and initialise */
	if ((newgsp = my_malloc(sizeof (GRAPHSTORE),MYF(MY_ZEROFILL))) == NULL)
		return 0;

	if ((newgsp->vertex = my_malloc(sizeof (GRAPH_VERTEX),MYF(MY_ZEROFILL))) == NULL) {
		my_free(newgsp,MYF(0));
		return 0;
	}

	newgsp->vertex->id = id;
	/* add new vertex to end of list */
	if (*gspp != NULL) {
		for (gscurp = *gspp; gscurp->next != NULL; gscurp = gscurp->next);
		gscurp->next = newgsp;
	}
	else /* new list */
		*gspp = newgsp;

	/* ok */
	return 1;
}


/*
	find a vertex by id

	returns ptr or NULL
*/
static GRAPH_VERTEX *_find_vertex (GRAPHSTORE *gsp, GRAPH_VERTEXID id)
{
	/* just loop through the list to find id */
	while (gsp != NULL && gsp->vertex->id != id)
		gsp = gsp->next;

	/* return ptr to vertex, or NULL */
	return (gsp != NULL ? gsp->vertex : NULL);
}


/*
	add edge
	both vertices must already exist; graphstore_insert() does this

	return 1 for ok, 0 for error (already exists, alloc error, etc)
*/
static int _add_edge (GRAPHSTORE *gsp, GRAPH_VERTEXID origid, GRAPH_VERTEXID destid, GRAPH_WEIGHT weight)
{
	GRAPH_VERTEX *origvp, *destvp;
	GRAPH_EDGE	*ep, *newep;

	/* find both vertices */
	if ((origvp = _find_vertex(gsp,origid)) == NULL ||
		(destvp = _find_vertex(gsp,destid)) == NULL)
		return 0;

	/* check if edge already exists */
	for (ep = origvp->forward_edge; ep != NULL; ep = ep->next_edge) {
		if (ep->vertex->id == destid)
			return 0;
	}

	/* allocate and initialise new edge */
	if ((newep = my_malloc(sizeof (GRAPH_EDGE),MYF(MY_ZEROFILL))) == NULL)
		return 0;

	newep->vertex = destvp;
	newep->weight = weight;

	/* insert new edge at start of chain, that's easiest */
	ep = origvp->forward_edge;
	origvp->forward_edge = newep;
	newep->next_edge = ep;

	/* ok */
	return 1;
}


/*
	create a new row, and add it to the graph set (or start set)
	NOTE! gsetpp is ptr to base ptr

	returns 1 for ok, 0 for error
*/
static int _add_graph_set (GRAPH_SET **gsetpp, GRAPH_TUPLE *gtp)
{
	GRAPH_SET *newgsetp;
	GRAPH_SET *gsetcurp;

	if (gsetpp == NULL || gtp == NULL)
		return 0;

	/* allocate and initialise */
	if ((newgsetp = my_malloc(sizeof (GRAPH_SET),MYF(MY_ZEROFILL))) == NULL)
		return 0;

	/* put in the data */
	memcpy(&newgsetp->tuple,gtp,sizeof (GRAPH_TUPLE));

	/* add new row to end of set */
	if (*gsetpp != NULL) {
		for (gsetcurp = *gsetpp; gsetcurp->next != NULL; gsetcurp = gsetcurp->next);
		gsetcurp->next = newgsetp;
	}
	else {	/* new set */
		*gsetpp = newgsetp;
	}

	/* ok */
	return 1;
}


/*
	free a graph set (release memory)

	returns 1 for ok, 0 for error
*/
int free_graph_set (GRAPH_SET *gsetp)
{
	GRAPH_SET *nextgsetp;

	if (gsetp == NULL)
		return 0;

	while (gsetp != NULL) {
		nextgsetp = gsetp->next;
		/* free() is a void function, nothing to check */
		my_free(gsetp,MYF(0));
		gsetp = nextgsetp;
	}

	/* ok */
	return 1;
}


/*
	insert new data into graphstore
	this can be either a vertex or an edge, depending on the params
	NOTE! gspp is ptr to base ptr

	returns 1 for ok, 0 for error
*/
int graphstore_insert (GRAPHSTORE **gspp, GRAPH_TUPLE *gtp)
{
	if (gspp == NULL)
		return 0;

	/* if nada or no orig vertex, we can't do anything */
	if (gtp == NULL || !gtp->origid)
		return 0;

#if 0
printf("inserting: origid=%lu destid=%lu weight=%lu\n",gtp->origid,gtp->destid,gtp->weight);
#endif

	if (!gtp->destid)	/* no edge param so just adding vertex */
		return _add_vertex(gspp,gtp->origid);

	/*
		add an edge
		first add both vertices just in case they didn't yet exist...
		not checking result there: if there's a prob, _add_edge() will catch.
	*/
	_add_vertex(gspp,gtp->origid);
	_add_vertex(gspp,gtp->destid);
	return _add_edge(*gspp,gtp->origid,gtp->destid,gtp->weight);
}


/*
	this is an internal function used by graphstore_query()

	find any path from originating vertex to destid
	if found, add to the result set on the way back
	NOTE: recursive function!
	
	returns 1 for hit, 0 for nothing, -1 for error
*/
int _find_any_path(GRAPH_SET **gsetpp, GRAPH_VERTEXID origid, GRAPH_VERTEXID destid, GRAPH_VERTEX *gvp, GRAPH_SEQ depth)
{
	GRAPH_EDGE *gep;
	GRAPH_TUPLE tup;
	int res;

	if (gvp->id == destid) {
		/* found target! */
		bzero(&tup,sizeof (GRAPH_TUPLE));
		tup.origid	= origid;
		tup.destid	= destid;
		tup.seq		= depth;
		tup.linkid	= gvp->id;
		return (_add_graph_set(gsetpp,&tup) ? 1 : -1);
	}

	/* walk through all edges for this vertex */
	for (gep = gvp->forward_edge; gep; gep = gep->next_edge) {
		/* recurse */
		res = _find_any_path(gsetpp,origid,destid,gep->vertex,depth+1);
		if (res < 0)
			return res;
		if (res > 0) {
			/* found somewhere below this one, insert ourselves and return */
			bzero(&tup,sizeof (GRAPH_TUPLE));
			tup.origid	= origid;
			tup.destid	= destid;
			tup.weight  = gep->weight;
			tup.seq		= depth;
			tup.linkid	= gvp->id;
			return (_add_graph_set(gsetpp,&tup) ? 1 : -1);			
		}
	}

	/* nothing found but no error */
	return 0;
}


/*
	query graphstore
	latch specifies what operation to perform

	we need to feed the conditions in... (through engine condition pushdown)
	for now we just presume one condition per field so we just feed in a tuple
	this also means we can just find constants, not ranges

	return ptr to GRAPH_SET
	caller must free with free_graph_set()
*/
GRAPH_SET *graphstore_query (GRAPHSTORE *gsp, GRAPH_TUPLE *gtp)
{
	GRAPH_SET *gsetp = NULL;
	GRAPH_SET *gsetcurp;
	GRAPH_SET *newgsetp;

	if (gsp == NULL || gtp == NULL)
		return (NULL);

	switch (gtp->latch) {
		case 0: /* return all vertices/edges */
			{
				GRAPHSTORE *gscurp;
				GRAPH_EDGE *gep;
				GRAPH_TUPLE tup;

				/* walk through all vertices */
				for (gscurp = gsp; gscurp != NULL; gscurp = gscurp->next) {
					/* check for condition */
					if (gtp->origid && gscurp->vertex->id != gtp->origid)
						continue;

					bzero(&tup,sizeof (GRAPH_TUPLE));
					tup.origid = gscurp->vertex->id;

					/* no edges? */
					if (gscurp->vertex->forward_edge == NULL) {
						/* just add vertex to set */
						if (!_add_graph_set(&gsetp,&tup)) {
							if (gsetp != NULL)	/* clean up */
								my_free(gsetp,MYF(0));
							return (NULL);
						}
					}
					else {
						/* walk through all edges */
						for (gep = gscurp->vertex->forward_edge; gep; gep = gep->next_edge) {
							tup.destid	= gep->vertex->id;
							tup.weight	= gep->weight;

							/* just add vertex to set */
							if (!_add_graph_set(&gsetp,&tup)) {
								if (gsetp != NULL)	/* clean up */
									my_free(gsetp,MYF(0));
								return (NULL);
							}
						}
					}
				}
			}
			break;

		case 1:	/* find a path between origid and destid */
				/* yes it'll just go with the first path it finds! */
			{
				GRAPHSTORE *gscurp;
				GRAPH_VERTEX *origvp;
				GRAPH_TUPLE tup;

				if (!gtp->origid || !gtp->destid)
					return NULL;

				/* find both vertices */
				if ((origvp = _find_vertex(gsp,gtp->origid)) == NULL ||
					_find_vertex(gsp,gtp->destid) == NULL)
					return NULL;

				if (_find_any_path(&gsetp,gtp->origid,gtp->destid,origvp,0) < 0) {	/* error? */
					if (gsetp != NULL)	/* clean up */
						my_free(gsetp,MYF(0));
					return NULL;
				}
			}
			break;

		default:
			/* this ends up being an empty set */
			break;
	}

	/* Fix up latch column with the proper value - to be relationally correct */
	for (gsetcurp = gsetp; gsetcurp != NULL; gsetcurp = gsetcurp->next)
		gsetcurp->tuple.latch = gtp->latch;

	return gsetp;
}



/* end of graphstore.c */