Skip to content
Navigation Menu
{{ message }}
forked from RedisGraph/RedisGraph
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheffects_apply.c
More file actions
587 lines (469 loc) · 16.3 KB
/
Copy patheffects_apply.c
File metadata and controls
587 lines (469 loc) · 16.3 KB
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/*
* Copyright (c) 2006-Present, Redis Ltd.
* All rights reserved.
*
* Licensed under your choice of the Redis Source Available License 2.0
* (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the
* GNU Affero General Public License v3 (AGPLv3).
*/
#include "RG.h"
#include "effects.h"
#include "../graph/graph_hub.h"
#include <stdio.h>
// read effect type from stream
static inline EffectType ReadEffectType
(
FILE *stream // effects stream
) {
EffectType t = EFFECT_UNKNOWN; // default to unknown effect type
// read EffectType off of stream
fread_assert(&t, sizeof(EffectType), stream);
return t;
}
static AttributeSet ReadAttributeSet
(
FILE *stream
) {
//--------------------------------------------------------------------------
// effect format:
// attribute count
// attributes (id,value) pair
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// read attribute count
//--------------------------------------------------------------------------
ushort attr_count;
fread_assert(&attr_count, sizeof(attr_count), stream);
//--------------------------------------------------------------------------
// read attributes
//--------------------------------------------------------------------------
SIValue values[attr_count];
Attribute_ID ids[attr_count];
for(ushort i = 0; i < attr_count; i++) {
// read attribute ID
fread_assert(ids + i, sizeof(Attribute_ID), stream);
// read attribute value
values[i] = SIValue_FromBinary(stream);
}
AttributeSet attr_set = NULL;
AttributeSet_AddNoClone(&attr_set, ids, values, attr_count, false);
return attr_set;
}
static void ApplyCreateNode
(
FILE *stream, // effects stream
GraphContext *gc // graph to operate on
) {
//--------------------------------------------------------------------------
// effect format:
// label count
// labels
// attribute count
// attributes (id,value) pair
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// read label count
//--------------------------------------------------------------------------
ushort lbl_count;
fread_assert(&lbl_count, sizeof(lbl_count), stream);
//--------------------------------------------------------------------------
// read labels
//--------------------------------------------------------------------------
LabelID labels[lbl_count];
for(ushort i = 0; i < lbl_count; i++) {
fread_assert(labels + i, sizeof(LabelID), stream);
}
//--------------------------------------------------------------------------
// read attributes
//--------------------------------------------------------------------------
AttributeSet attr_set = ReadAttributeSet(stream);
//--------------------------------------------------------------------------
// create node
//--------------------------------------------------------------------------
Node n = GE_NEW_NODE();
CreateNode(gc, &n, labels, lbl_count, attr_set, false);
}
static void ApplyCreateEdge
(
FILE *stream, // effects stream
GraphContext *gc // graph to operate on
) {
//--------------------------------------------------------------------------
// effect format:
// effect type
// relationship count
// relationships
// src node ID
// dest node ID
// attribute count
// attributes (id,value) pair
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// read relationship type count
//--------------------------------------------------------------------------
ushort rel_count;
fread_assert(&rel_count, sizeof(rel_count), stream);
ASSERT(rel_count == 1);
//--------------------------------------------------------------------------
// read relationship type
//--------------------------------------------------------------------------
RelationID r;
fread_assert(&r, sizeof(r), stream);
//--------------------------------------------------------------------------
// read src node ID
//--------------------------------------------------------------------------
NodeID src_id;
fread_assert(&src_id, sizeof(NodeID), stream);
//--------------------------------------------------------------------------
// read dest node ID
//--------------------------------------------------------------------------
NodeID dest_id;
fread_assert(&dest_id, sizeof(NodeID), stream);
//--------------------------------------------------------------------------
// read attributes
//--------------------------------------------------------------------------
AttributeSet attr_set = ReadAttributeSet(stream);
//--------------------------------------------------------------------------
// create edge
//--------------------------------------------------------------------------
Edge e;
CreateEdge(gc, &e, src_id, dest_id, r, attr_set, false);
}
static void ApplyLabels
(
FILE *stream, // effects stream
GraphContext *gc, // graph to operate on
bool add // add or remove labels
) {
//--------------------------------------------------------------------------
// effect format:
// effect type
// node ID
// labels count
// label IDs
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// read node ID
//--------------------------------------------------------------------------
EntityID id;
fread_assert(&id, sizeof(id), stream);
//--------------------------------------------------------------------------
// get updated node
//--------------------------------------------------------------------------
Node n;
Graph *g = gc->g;
bool found = Graph_GetNode(g, id, &n);
ASSERT(found == true);
//--------------------------------------------------------------------------
// read labels count
//--------------------------------------------------------------------------
uint8_t lbl_count;
fread_assert(&lbl_count, sizeof(lbl_count), stream);
ASSERT(lbl_count > 0);
// TODO: move to LabelID
uint n_add_labels = 0;
uint n_remove_labels = 0;
const char **add_labels = NULL;
const char **remove_labels = NULL;
const char *lbl[lbl_count];
// assign lbl to the appropriate array
if(add) {
add_labels = lbl;
n_add_labels = lbl_count;
} else {
remove_labels = lbl;
n_remove_labels = lbl_count;
}
//--------------------------------------------------------------------------
// read labels
//--------------------------------------------------------------------------
for(ushort i = 0; i < lbl_count; i++) {
LabelID l;
fread_assert(&l, sizeof(LabelID), stream);
Schema *s = GraphContext_GetSchemaByID(gc, l, SCHEMA_NODE);
ASSERT(s != NULL);
lbl[i] = Schema_GetName(s);
}
//--------------------------------------------------------------------------
// update node labels
//--------------------------------------------------------------------------
UpdateNodeLabels(gc, &n, add_labels, remove_labels, n_add_labels,
n_remove_labels, false);
}
static void ApplyAddSchema
(
FILE *stream, // effects stream
GraphContext *gc // graph to operate on
) {
//--------------------------------------------------------------------------
// effect format:
// effect type
// schema type
// schema name
//--------------------------------------------------------------------------
// read schema type
SchemaType t;
fread_assert(&t, sizeof(t), stream);
// read schema name
// read string length
size_t l;
fread_assert(&l, sizeof(l), stream);
// read string
char schema_name[l];
fread_assert(schema_name, l, stream);
// create schema
AddSchema(gc, schema_name, t, false);
}
static void ApplyAddAttribute
(
FILE *stream, // effects stream
GraphContext *gc // graph to operate on
) {
//--------------------------------------------------------------------------
// effect format:
// effect type
// attribute name
//--------------------------------------------------------------------------
// read attribute name length
size_t l;
fread_assert(&l, sizeof(l), stream);
// read attribute name
const char attr[l];
fread_assert(attr, l, stream);
// attr should not exist
ASSERT(GraphContext_GetAttributeID(gc, attr) == ATTRIBUTE_ID_NONE);
// add attribute
FindOrAddAttribute(gc, attr, false);
}
// process Update_Edge effect
static void ApplyUpdateEdge
(
FILE *stream, // effects stream
GraphContext *gc // graph to operate on
) {
//--------------------------------------------------------------------------
// effect format:
// edge ID
// attribute ID
// attribute value
//--------------------------------------------------------------------------
SIValue v; // updated value
uint props_set; // number of attributes updated
uint props_removed; // number of attributes removed
Attribute_ID attr_id; // entity ID
NodeID s_id = INVALID_ENTITY_ID; // edge src node ID
NodeID t_id = INVALID_ENTITY_ID; // edge dest node ID
RelationID r_id = GRAPH_UNKNOWN_RELATION; // edge rel-type
EntityID id = INVALID_ENTITY_ID;
//--------------------------------------------------------------------------
// read edge ID
//--------------------------------------------------------------------------
fread_assert(&id, sizeof(EntityID), stream);
ASSERT(id != INVALID_ENTITY_ID);
//--------------------------------------------------------------------------
// read relation ID
//--------------------------------------------------------------------------
fread_assert(&r_id, sizeof(RelationID), stream);
ASSERT(r_id >= 0);
//--------------------------------------------------------------------------
// read src ID
//--------------------------------------------------------------------------
fread_assert(&s_id, sizeof(NodeID), stream);
ASSERT(s_id != INVALID_ENTITY_ID);
//--------------------------------------------------------------------------
// read dest ID
//--------------------------------------------------------------------------
fread_assert(&t_id, sizeof(NodeID), stream);
ASSERT(t_id != INVALID_ENTITY_ID);
//--------------------------------------------------------------------------
// read attribute ID
//--------------------------------------------------------------------------
fread_assert(&attr_id, sizeof(Attribute_ID), stream);
//--------------------------------------------------------------------------
// read attribute value
//--------------------------------------------------------------------------
v = SIValue_FromBinary(stream);
ASSERT(SI_TYPE(v) & (SI_VALID_PROPERTY_VALUE | T_NULL));
ASSERT((attr_id != ATTRIBUTE_ID_ALL || SIValue_IsNull(v)) && attr_id != ATTRIBUTE_ID_NONE);
UpdateEdgeProperty(gc, id, r_id, s_id, t_id, attr_id, v);
}
// process UpdateNode effect
static void ApplyUpdateNode
(
FILE *stream, // effects stream
GraphContext *gc // graph to operate on
) {
//--------------------------------------------------------------------------
// effect format:
// entity ID
// attribute ID
// attribute value
//--------------------------------------------------------------------------
SIValue v; // updated value
uint props_set; // number of attributes updated
uint props_removed; // number of attributes removed
Attribute_ID attr_id; // entity ID
EntityID id = INVALID_ENTITY_ID;
//--------------------------------------------------------------------------
// read node ID
//--------------------------------------------------------------------------
fread_assert(&id, sizeof(EntityID), stream);
//--------------------------------------------------------------------------
// read attribute ID
//--------------------------------------------------------------------------
fread_assert(&attr_id, sizeof(Attribute_ID), stream);
//--------------------------------------------------------------------------
// read attribute ID
//--------------------------------------------------------------------------
v = SIValue_FromBinary(stream);
ASSERT(SI_TYPE(v) & (SI_VALID_PROPERTY_VALUE | T_NULL));
ASSERT((attr_id != ATTRIBUTE_ID_ALL || SIValue_IsNull(v)) && attr_id != ATTRIBUTE_ID_NONE);
UpdateNodeProperty(gc, id, attr_id, v);
}
// process DeleteNode effect
static void ApplyDeleteNode
(
FILE *stream, // effects stream
GraphContext *gc // graph to operate on
) {
//--------------------------------------------------------------------------
// effect format:
// node ID
//--------------------------------------------------------------------------
Node n; // node to delete
EntityID id; // node ID
Graph *g = gc->g; // graph to delete node from
// read node ID off of stream
fread_assert(&id, sizeof(EntityID), stream);
// retrieve node from graph
int res = Graph_GetNode(g, id, &n);
ASSERT(res != 0);
// delete node
DeleteNodes(gc, &n, 1, false);
}
// process DeleteNode effect
static void ApplyDeleteEdge
(
FILE *stream, // effects stream
GraphContext *gc // graph to operate on
) {
//--------------------------------------------------------------------------
// effect format:
// edge ID
// relation ID
// src ID
// dest ID
//--------------------------------------------------------------------------
Edge e; // edge to delete
EntityID id = INVALID_ENTITY_ID; // edge ID
int r_id = GRAPH_UNKNOWN_RELATION; // edge rel-type
NodeID s_id = INVALID_ENTITY_ID; // edge src node ID
NodeID t_id = INVALID_ENTITY_ID; // edge dest node ID
int res;
UNUSED(res);
Graph *g = gc->g; // graph to delete edge from
// read edge ID
fread_assert(&id, sizeof(EntityID), stream);
// read relation ID
fread_assert(&r_id, sizeof(RelationID), stream);
// read src node ID
fread_assert(&s_id, sizeof(EntityID), stream);
// read dest node ID
fread_assert(&t_id, sizeof(EntityID), stream);
// get edge from the graph
res = Graph_GetEdge(g, id, (Edge*)&e);
ASSERT(res != 0);
// set edge relation, src and destination node
Edge_SetSrcNodeID(&e, s_id);
Edge_SetDestNodeID(&e, t_id);
Edge_SetRelationID(&e, r_id);
// delete edge
DeleteEdges(gc, &e, 1, false);
}
// returns false in case of effect encode/decode version mismatch
static bool ValidateVersion
(
FILE *stream // effects stream
) {
ASSERT(stream != NULL);
// read version
uint8_t v;
fread_assert(&v, sizeof(uint8_t), stream);
if(v != EFFECTS_VERSION) {
// unexpected effects version
RedisModule_Log(NULL, "warning",
"GRAPH.EFFECT version mismatch expected: %d got: %d",
EFFECTS_VERSION, v);
return false;
}
return true;
}
// applys effects encoded in buffer
void Effects_Apply
(
GraphContext *gc, // graph to operate on
const char *effects_buff, // encoded effects
size_t l // size of buffer
) {
// validations
ASSERT(l > 0); // buffer can't be empty
ASSERT(effects_buff != NULL); // buffer can't be NULL
// read buffer in a stream fashion
FILE *stream = fmemopen((void*)effects_buff, l, "r");
// validate effects version
if(ValidateVersion(stream) == false) {
// replica/primary out of sync
exit(1);
}
// lock graph for writing
Graph *g = GraphContext_GetGraph(gc);
Graph_AcquireWriteLock(g);
// update graph sync policy
MATRIX_POLICY policy = Graph_SetMatrixPolicy(g, SYNC_POLICY_RESIZE);
// as long as there's data in stream
while(ftell(stream) < l) {
// read effect type
EffectType t = ReadEffectType(stream);
switch(t) {
case EFFECT_DELETE_NODE:
ApplyDeleteNode(stream, gc);
break;
case EFFECT_DELETE_EDGE:
ApplyDeleteEdge(stream, gc);
break;
case EFFECT_UPDATE_NODE:
ApplyUpdateNode(stream, gc);
break;
case EFFECT_UPDATE_EDGE:
ApplyUpdateEdge(stream, gc);
break;
case EFFECT_CREATE_NODE:
ApplyCreateNode(stream, gc);
break;
case EFFECT_CREATE_EDGE:
ApplyCreateEdge(stream, gc);
break;
case EFFECT_SET_LABELS:
ApplyLabels(stream, gc, true);
break;
case EFFECT_REMOVE_LABELS:
ApplyLabels(stream, gc, false);
break;
case EFFECT_ADD_SCHEMA:
ApplyAddSchema(stream, gc);
break;
case EFFECT_ADD_ATTRIBUTE:
ApplyAddAttribute(stream, gc);
break;
default:
assert(false && "unknown effect type");
break;
}
}
// restore graph sync policy
Graph_SetMatrixPolicy(g, policy);
// release write lock
Graph_ReleaseLock(g);
// close stream
fclose(stream);
}
You can’t perform that action at this time.
