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 pathbulk_insert.c
More file actions
422 lines (356 loc) · 10.6 KB
/
Copy pathbulk_insert.c
File metadata and controls
422 lines (356 loc) · 10.6 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
/*
* 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 "bulk_insert.h"
#include "../datatypes/array.h"
#include "../schema/schema.h"
#include "../util/arr.h"
#include "../util/rmalloc.h"
// the first byte of each property in the binary stream
// is used to indicate the type of the subsequent SIValue
typedef enum {
BI_NULL = 0,
BI_BOOL = 1,
BI_DOUBLE = 2,
BI_STRING = 3,
BI_LONG = 4,
BI_ARRAY = 5,
} TYPE;
/* binary header format:
* - entity name : null-terminated C string
* - property count : 4-byte unsigned integer
* [0..property_count] : null-terminated C string
*/
// read the label strings from a header, update schemas, and retrieve the label IDs
static int* _BulkInsert_ReadHeaderLabels
(
GraphContext* gc,
SchemaType t,
const char* data,
size_t* data_idx
) {
ASSERT(gc != NULL);
ASSERT(data != NULL);
ASSERT(data_idx != NULL);
// first sequence is entity label(s)
const char* labels = data + *data_idx;
int labels_len = strlen(labels);
*data_idx += labels_len + 1;
// array of all label IDs
int* label_ids = array_new(int, 1);
// stack variable to contain a single label
char label[labels_len + 1];
while (true) {
// look for a colon delimiting another label
char* found = strchr(labels, ':');
if (found) {
ASSERT(t == SCHEMA_NODE); // only nodes can have multiple labels
// this entity file describes multiple labels, copy the current one
size_t len = found - labels;
memcpy(label, labels, len);
label[len] = '\0';
// update the labels pointer for the next seek
labels += len + 1;
} else {
// reached the last (or only) label; copy it
size_t len = strlen(labels);
// Also copy the terminating NULL character.
memcpy(label, labels, len + 1);
}
// try to retrieve the label's schema
Schema* s = GraphContext_GetSchema(gc, label, t);
// create the schema if it does not already exist
if (s == NULL) {
s = GraphContext_AddSchema(gc, label, t);
}
// store the label ID
array_append(label_ids, Schema_GetID(s));
// break if we've exhausted all labels
if (!found) break;
}
return label_ids;
}
// read the property keys from a header
static Attribute_ID* _BulkInsert_ReadHeaderProperties
(
GraphContext* gc,
SchemaType t,
const char* data,
size_t* data_idx,
uint* prop_count
) {
ASSERT(gc != NULL);
ASSERT(data != NULL);
ASSERT(data_idx != NULL);
ASSERT(prop_count != NULL);
// next 4 bytes are property count
*prop_count = *(uint*)&data[*data_idx];
*data_idx += sizeof(unsigned int);
if (*prop_count == 0) return NULL;
Attribute_ID* prop_indices = rm_malloc(*prop_count * sizeof(Attribute_ID));
// the rest of the line is [char *prop_key] * prop_count
for (uint j = 0; j < *prop_count; j++) {
char* prop_key = (char*)data + *data_idx;
*data_idx += strlen(prop_key) + 1;
// add properties to schemas
prop_indices[j] = GraphContext_FindOrAddAttribute(gc, prop_key, NULL);
}
return prop_indices;
}
// read an SIValue from the data stream and update the index appropriately
static SIValue _BulkInsert_ReadProperty
(
const char* data,
size_t* data_idx
) {
// binary property format:
// - property type : 1-byte integer corresponding to TYPE enum
// - Nothing if type is NULL
// - 1-byte true/false if type is boolean
// - 8-byte double if type is double
// - 8-byte integer if type is integer
// - Null-terminated C string if type is string
// - 8-byte array length followed by N values if type is array
// possible property values
bool b;
double d;
int64_t i;
int64_t len;
const char* s;
SIValue v = SI_NullVal();
TYPE t = data[*data_idx];
*data_idx += 1;
switch (t) {
case BI_NULL:
v = SI_NullVal();
break;
case BI_BOOL:
b = data[*data_idx];
*data_idx += 1;
v = SI_BoolVal(b);
break;
case BI_DOUBLE:
d = *(double*)&data[*data_idx];
*data_idx += sizeof(double);
v = SI_DoubleVal(d);
break;
case BI_LONG:
i = *(int64_t*)&data[*data_idx];
*data_idx += sizeof(int64_t);
v = SI_LongVal(i);
break;
case BI_STRING:
s = data + *data_idx;
*data_idx += strlen(s) + 1;
// The string itself will be cloned when added to the GraphEntity properties.
v = SI_ConstStringVal((char*)s);
break;
case BI_ARRAY:
// The first 8 bytes of a received array will be the array length.
len = *(int64_t*)&data[*data_idx];
*data_idx += sizeof(int64_t);
v = SIArray_New(len);
for (uint i = 0; i < len; i++) {
// Convert every element and add to array.
SIArray_Append(&v, _BulkInsert_ReadProperty(data, data_idx));
}
break;
default:
ASSERT(false);
break;
}
return v;
}
static int _BulkInsert_ProcessNodeFile
(
GraphContext* gc,
const char* data,
size_t data_len
) {
uint prop_count;
size_t data_idx = 0;
// read the CSV file header labels and update all schemas
int* label_ids = _BulkInsert_ReadHeaderLabels(gc, SCHEMA_NODE, data, &data_idx);
uint label_count = array_len(label_ids);
// read the CSV header properties and collect their indices
Attribute_ID* prop_indices = _BulkInsert_ReadHeaderProperties(gc, SCHEMA_NODE, data,
&data_idx, &prop_count);
// sync each matrix once
ASSERT(Graph_GetMatrixPolicy(gc->g) == SYNC_POLICY_RESIZE);
for (uint i = 0; i < label_count; i++) {
Graph_GetLabelMatrix(gc->g, label_ids[i]);
}
// sync node-label matrix
Graph_GetNodeLabelMatrix(gc->g);
Graph_SetMatrixPolicy(gc->g, SYNC_POLICY_NOP);
//--------------------------------------------------------------------------
// load nodes
//--------------------------------------------------------------------------
while (data_idx < data_len) {
Node n = GE_NEW_NODE();
GraphEntity* ge;
Graph_CreateNode(gc->g, &n, label_ids, label_count);
ge = (GraphEntity*)&n;
// process entity attributes
for (uint i = 0; i < prop_count; i++) {
SIValue value = _BulkInsert_ReadProperty(data, &data_idx);
// skip invalid attribute values
if (!(SI_TYPE(value) & SI_VALID_PROPERTY_VALUE))
continue;
GraphEntity_AddProperty(ge, prop_indices[i], value);
}
}
Graph_SetMatrixPolicy(gc->g, SYNC_POLICY_RESIZE);
if (prop_indices) rm_free(prop_indices);
array_free(label_ids);
return BULK_OK;
}
static int _BulkInsert_ProcessEdgeFile
(
GraphContext* gc,
const char* data,
size_t data_len
) {
int relation_id;
uint prop_count;
size_t data_idx = 0;
// read the CSV file header
// and commit all labels and properties it introduces
int* type_ids = _BulkInsert_ReadHeaderLabels(gc, SCHEMA_EDGE, data, &data_idx);
uint type_count = array_len(type_ids);
// edges can only have one type
ASSERT(type_count == 1);
int type_id = type_ids[0];
Attribute_ID* prop_indices = _BulkInsert_ReadHeaderProperties(gc, SCHEMA_EDGE,
data, &data_idx, &prop_count);
// sync matrix once
ASSERT(Graph_GetMatrixPolicy(gc->g) == SYNC_POLICY_RESIZE);
Graph_GetRelationMatrix(gc->g, type_id, false);
Graph_GetAdjacencyMatrix(gc->g, false);
Graph_SetMatrixPolicy(gc->g, SYNC_POLICY_NOP);
//--------------------------------------------------------------------------
// load edges
//--------------------------------------------------------------------------
while (data_idx < data_len) {
Edge e;
GraphEntity* ge;
// next 8 bytes are source ID
NodeID src = *(NodeID*)&data[data_idx];
data_idx += sizeof(NodeID);
// next 8 bytes are destination ID
NodeID dest = *(NodeID*)&data[data_idx];
data_idx += sizeof(NodeID);
Graph_CreateEdge(gc->g, src, dest, type_id, &e);
ge = (GraphEntity*)&e;
// process entity attributes
for (uint i = 0; i < prop_count; i++) {
SIValue value = _BulkInsert_ReadProperty(data, &data_idx);
// skip invalid attribute values
if (!(SI_TYPE(value) & SI_VALID_PROPERTY_VALUE)) {
continue;
}
GraphEntity_AddProperty(ge, prop_indices[i], value);
}
}
array_free(type_ids);
if (prop_indices) rm_free(prop_indices);
Graph_SetMatrixPolicy(gc->g, SYNC_POLICY_RESIZE);
return BULK_OK;
}
static int _BulkInsert_ProcessTokens
(
GraphContext* gc,
int token_count,
RedisModuleString** argv,
SchemaType type
) {
for (int i = 0; i < token_count; i++) {
size_t len;
// retrieve a pointer to the next binary stream and record its length
const char* data = RedisModule_StringPtrLen(argv[i], &len);
int rc = (type == SCHEMA_NODE)
? _BulkInsert_ProcessNodeFile(gc, data, len)
: _BulkInsert_ProcessEdgeFile(gc, data, len);
UNUSED(rc);
ASSERT(rc == BULK_OK);
}
return BULK_OK;
}
int BulkInsert
(
RedisModuleCtx* ctx,
GraphContext* gc,
RedisModuleString** argv,
int argc,
uint node_count,
uint edge_count
) {
ASSERT(gc != NULL);
ASSERT(ctx != NULL);
ASSERT(argv != NULL);
if (argc < 2) {
RedisModule_ReplyWithError(ctx, "Bulk insert format error, \
failed to parse bulk insert sections.");
return BULK_FAIL;
}
// read the number of node tokens
long long node_token_count;
long long relation_token_count;
if (RedisModule_StringToLongLong(*argv++, &node_token_count) != REDISMODULE_OK) {
RedisModule_ReplyWithError(ctx, "Error parsing number of node \
descriptor tokens.");
return BULK_FAIL;
}
// read the number of relation tokens
if (RedisModule_StringToLongLong(*argv++, &relation_token_count) != REDISMODULE_OK) {
RedisModule_ReplyWithError(ctx, "Error parsing number of relation \
descriptor tokens.");
return BULK_FAIL;
}
Graph* g = gc->g;
int res = BULK_OK;
// lock graph under write lock
// allocate space for new nodes and edges
// set graph sync policy to resize only
Graph_AcquireWriteLock(g);
Graph_SetMatrixPolicy(g, SYNC_POLICY_RESIZE);
Graph_AllocateNodes(g, node_count);
Graph_AllocateEdges(g, edge_count);
argc -= 2;
if (node_token_count > 0) {
ASSERT(argc >= node_token_count);
// process all node files
if (_BulkInsert_ProcessTokens(gc, node_token_count, argv,
SCHEMA_NODE)
!= BULK_OK) {
res = BULK_FAIL;
goto cleanup;
}
argv += node_token_count;
argc -= node_token_count;
}
if (relation_token_count > 0) {
ASSERT(argc >= relation_token_count);
// Process all relationship files
if (_BulkInsert_ProcessTokens(gc, relation_token_count, argv,
SCHEMA_EDGE)
!= BULK_OK) {
res = BULK_FAIL;
goto cleanup;
}
argv += relation_token_count;
argc -= relation_token_count;
}
ASSERT(argc == 0);
cleanup:
// reset graph sync policy
Graph_SetMatrixPolicy(g, SYNC_POLICY_FLUSH_RESIZE);
Graph_ReleaseLock(g);
return res;
}
You can’t perform that action at this time.
