durable_rules 0.34.57 → 2.00.001

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/src/rules/json.h CHANGED
@@ -10,12 +10,10 @@
10
10
  #define JSON_ARRAY 0x05
11
11
  #define JSON_OBJECT 0x06
12
12
  #define JSON_NIL 0x07
13
- #define JSON_STATE_PROPERTY 0x08
14
- #define JSON_EVENT_PROPERTY 0x09
15
- #define JSON_EVENT_LOCAL_PROPERTY 0x0A
16
- #define JSON_STATE_IDIOM 0x0B
17
- #define JSON_EVENT_IDIOM 0x0C
18
- #define JSON_EVENT_LOCAL_IDIOM 0x0D
13
+ #define JSON_IDENTIFIER 0x09
14
+ #define JSON_MESSAGE_IDENTIFIER 0x0A
15
+ #define JSON_EXPRESSION 0x0C
16
+ #define JSON_MESSAGE_EXPRESSION 0x0D
19
17
  #define JSON_REGEX 0x0E
20
18
  #define JSON_IREGEX 0x0F
21
19
 
@@ -25,6 +23,12 @@
25
23
  #define FNV_64_OFFSET_BASIS 0xcbf29ce484222325
26
24
  #define FNV_64_PRIME 1099511628211
27
25
 
26
+ #define CHECK_PARSE_RESULT(result) do { \
27
+ if (result != PARSE_OK) { \
28
+ return result; \
29
+ } \
30
+ } while(0)
31
+
28
32
  unsigned int readNextName(char *start, char **first, char **last, unsigned int *hash);
29
33
  unsigned int readNextValue(char *start, char **first, char **last, unsigned char *type);
30
34
  unsigned int readNextArrayValue(char *start, char **first, char **last, unsigned char *type);
data/src/rules/rete.c CHANGED
@@ -4,9 +4,9 @@
4
4
  #include <time.h>
5
5
  #include <string.h>
6
6
  #include "rules.h"
7
- #include "net.h"
8
7
  #include "json.h"
9
8
  #include "regex.h"
9
+ #include "rete.h"
10
10
 
11
11
  #define HASH_ALL 321211332 // all
12
12
  #define HASH_ANY 740945997 // any
@@ -28,7 +28,6 @@
28
28
  #define HASH_NEX 2605470202 // $nex
29
29
  #define HASH_OR 340911698 // $or
30
30
  #define HASH_AND 3746487396 // $and
31
- #define HASH_S 1186729920 // $s
32
31
  #define HASH_M 1690058490 // $m
33
32
  #define HASH_NAME 2369371622 // name
34
33
  #define HASH_ADD 4081054038 // $add
@@ -39,25 +38,26 @@
39
38
  #define HASH_R 1203507539 //$r
40
39
  #define HASH_FORWARD 739185624 // $forward
41
40
 
42
- typedef struct path {
43
- unsigned char operator;
44
- struct path **parents;
45
- unsigned int parentsLength;
46
- unsigned short count;
47
- expression *expressions;
48
- unsigned int expressionsLength;
49
- } path;
50
-
51
- typedef struct any {
52
- struct all **nodes;
53
- unsigned int nodesLength;
54
- } any;
55
-
56
- typedef struct all {
57
- unsigned short count;
58
- unsigned int *expressions;
59
- unsigned short expressionsLength;
60
- } all;
41
+ #define MAX_ACTIONS 4096
42
+
43
+ #define GET_EXPRESSION(tree, betaOffset, expr) do { \
44
+ expressionSequence *exprs = &tree->nodePool[betaOffset].value.b.expressionSequence; \
45
+ expr = &exprs->expressions[exprs->length]; \
46
+ ++exprs->length; \
47
+ if (exprs->length == MAX_SEQUENCE_EXPRESSIONS) { \
48
+ return ERR_EXPRESSION_LIMIT_EXCEEDED; \
49
+ } \
50
+ } while(0)
51
+
52
+ #define APPEND_EXPRESSION(tree, betaOffset, op) do { \
53
+ expressionSequence *exprs = &tree->nodePool[betaOffset].value.b.expressionSequence; \
54
+ exprs->expressions[exprs->length].operator = op; \
55
+ ++exprs->length; \
56
+ if (exprs->length == MAX_SEQUENCE_EXPRESSIONS) { \
57
+ return ERR_EXPRESSION_LIMIT_EXCEEDED; \
58
+ } \
59
+ } while(0)
60
+
61
61
 
62
62
  unsigned int firstEmptyEntry = 1;
63
63
  unsigned int lastEmptyEntry = MAX_HANDLES -1;
@@ -65,14 +65,6 @@ char entriesInitialized = 0;
65
65
 
66
66
  static unsigned int validateAlgebra(char *rule);
67
67
 
68
- static unsigned int createBeta(ruleset *tree,
69
- char *rule,
70
- unsigned char operator,
71
- unsigned short distinct,
72
- unsigned int nextOffset,
73
- path *nextPath,
74
- path **outPath);
75
-
76
68
  static unsigned int storeString(ruleset *tree,
77
69
  char *newString,
78
70
  unsigned int *stringOffset,
@@ -103,8 +95,8 @@ static unsigned int storeString(ruleset *tree,
103
95
  }
104
96
 
105
97
  static unsigned int storeExpression(ruleset *tree,
106
- expression **newExpression,
107
- unsigned int *expressionOffset) {
98
+ expression **newExpression,
99
+ unsigned int *expressionOffset) {
108
100
 
109
101
  if (!tree->expressionPool) {
110
102
  tree->expressionPool = malloc(sizeof(expression));
@@ -129,87 +121,9 @@ static unsigned int storeExpression(ruleset *tree,
129
121
  return RULES_OK;
130
122
  }
131
123
 
132
- static unsigned int storeIdiom(ruleset *tree,
133
- idiom **newIdiom,
134
- unsigned int *idiomOffset) {
135
-
136
- if (!tree->idiomPool) {
137
- tree->idiomPool = malloc(sizeof(idiom));
138
- if (!tree->idiomPool) {
139
- return ERR_OUT_OF_MEMORY;
140
- }
141
-
142
- *idiomOffset = 0;
143
- *newIdiom = &tree->idiomPool[0];
144
- tree->idiomOffset = 1;
145
- } else {
146
- tree->idiomPool = realloc(tree->idiomPool, (tree->idiomOffset + 1) * sizeof(idiom));
147
- if (!tree->idiomPool) {
148
- return ERR_OUT_OF_MEMORY;
149
- }
150
-
151
- *idiomOffset = tree->idiomOffset;
152
- *newIdiom = &tree->idiomPool[tree->idiomOffset];
153
- tree->idiomOffset = tree->idiomOffset + 1;
154
- }
155
-
156
- return RULES_OK;
157
- }
158
-
159
- static unsigned int appendTerm(expression *expr, unsigned int nodeOffset) {
160
- if (expr->termsLength == 0) {
161
- expr->termsLength = 1;
162
- expr->t.termsPointer = malloc(sizeof(unsigned int));
163
- if (!expr->t.termsPointer) {
164
- return ERR_OUT_OF_MEMORY;
165
- }
166
-
167
- expr->t.termsPointer[0] = nodeOffset;
168
- }
169
- else {
170
- expr->termsLength = expr->termsLength + 1;
171
- expr->t.termsPointer = realloc(expr->t.termsPointer, expr->termsLength * sizeof(unsigned int));
172
- if (!expr->t.termsPointer) {
173
- return ERR_OUT_OF_MEMORY;
174
- }
175
-
176
- expr->t.termsPointer[expr->termsLength - 1] = nodeOffset;
177
- }
178
-
179
- return RULES_OK;
180
- }
181
-
182
- static unsigned int storeJoin(ruleset *tree,
183
- join **newJoin,
184
- unsigned int *joinOffset) {
185
-
186
- if (!tree->joinPool) {
187
- tree->joinPool = malloc(sizeof(join));
188
- if (!tree->joinPool) {
189
- return ERR_OUT_OF_MEMORY;
190
- }
191
-
192
- *joinOffset = 0;
193
- *newJoin = &tree->joinPool[0];
194
- tree->joinOffset = 1;
195
- } else {
196
- tree->joinPool = realloc(tree->joinPool, (tree->joinOffset + 1) * sizeof(join));
197
- if (!tree->joinPool) {
198
- return ERR_OUT_OF_MEMORY;
199
- }
200
-
201
- *joinOffset = tree->joinOffset;
202
- *newJoin = &tree->joinPool[tree->joinOffset];
203
- tree->joinOffset = tree->joinOffset + 1;
204
- }
205
-
206
- return RULES_OK;
207
- }
208
-
209
124
  static unsigned int storeNode(ruleset *tree,
210
125
  node **newNode,
211
126
  unsigned int *nodeOffset) {
212
-
213
127
  if (!tree->nodePool) {
214
128
  tree->nodePool = malloc(sizeof(node));
215
129
  if (!tree->nodePool) {
@@ -300,29 +214,71 @@ static unsigned int ensureBetaList(ruleset *tree, node *newNode) {
300
214
  return RULES_OK;
301
215
  }
302
216
 
217
+ static void copyOperand(operand *op,
218
+ operand *target) {
219
+ target->type = op->type;
220
+ switch(op->type) {
221
+ case JSON_IDENTIFIER:
222
+ case JSON_MESSAGE_IDENTIFIER:
223
+ target->value.id.propertyNameHash = op->value.id.propertyNameHash;
224
+ target->value.id.propertyNameOffset = op->value.id.propertyNameOffset;
225
+ target->value.id.nameOffset = op->value.id.nameOffset;
226
+ target->value.id.nameHash = op->value.id.nameHash;
227
+ break;
228
+ case JSON_EXPRESSION:
229
+ case JSON_MESSAGE_EXPRESSION:
230
+ target->value.expressionOffset = op->value.expressionOffset;
231
+ break;
232
+ case JSON_STRING:
233
+ target->value.stringOffset = op->value.stringOffset;
234
+ break;
235
+ case JSON_INT:
236
+ target->value.i = op->value.i;
237
+ break;
238
+ case JSON_DOUBLE:
239
+ target->value.d = op->value.d;
240
+ break;
241
+ case JSON_BOOL:
242
+ target->value.b = op->value.b;
243
+ break;
244
+ case JSON_REGEX:
245
+ case JSON_IREGEX:
246
+ target->value.regex.stringOffset = op->value.regex.stringOffset;
247
+ target->value.regex.vocabularyLength = op->value.regex.vocabularyLength;
248
+ target->value.regex.statesLength = op->value.regex.statesLength;
249
+ target->value.regex.stateMachineOffset = op->value.regex.stateMachineOffset;
250
+ break;
251
+ }
252
+ }
253
+
254
+ static void copyExpression(expression *expr, expression *target) {
255
+ target->operator = expr->operator;
256
+ copyOperand(&expr->right, &target->right);
257
+ copyOperand(&expr->left, &target->left);
258
+ }
259
+
303
260
  static unsigned int copyValue(ruleset *tree,
304
- jsonValue *right,
261
+ operand *right,
305
262
  char *first,
306
263
  char *last,
307
- unsigned int idiomOffset,
308
- reference *ref,
264
+ unsigned int expressionOffset,
265
+ identifier *id,
309
266
  unsigned char type) {
310
267
  unsigned int result = RULES_OK;
311
268
  right->type = type;
312
269
  unsigned int leftLength;
313
270
  char temp;
314
271
  switch(type) {
315
- case JSON_EVENT_PROPERTY:
316
- case JSON_EVENT_LOCAL_PROPERTY:
317
- case JSON_STATE_PROPERTY:
318
- right->value.property.nameHash = ref->nameHash;
319
- right->value.property.nameOffset = ref->nameOffset;
320
- right->value.property.idOffset = ref->idOffset;
272
+ case JSON_IDENTIFIER:
273
+ case JSON_MESSAGE_IDENTIFIER:
274
+ right->value.id.propertyNameHash = id->propertyNameHash;
275
+ right->value.id.propertyNameOffset = id->propertyNameOffset;
276
+ right->value.id.nameOffset = id->nameOffset;
277
+ right->value.id.nameHash = id->nameHash;
321
278
  break;
322
- case JSON_STATE_IDIOM:
323
- case JSON_EVENT_IDIOM:
324
- case JSON_EVENT_LOCAL_IDIOM:
325
- right->value.idiomOffset = idiomOffset;
279
+ case JSON_EXPRESSION:
280
+ case JSON_MESSAGE_EXPRESSION:
281
+ right->value.expressionOffset = expressionOffset;
326
282
  break;
327
283
  case JSON_STRING:
328
284
  leftLength = last - first;
@@ -351,11 +307,11 @@ static unsigned int copyValue(ruleset *tree,
351
307
  case JSON_REGEX:
352
308
  case JSON_IREGEX:
353
309
  leftLength = last - first;
354
- result = storeString(tree, first, &right->value.regex.stringOffset, leftLength);
355
- if (result != RULES_OK) {
356
- return result;
357
- }
358
-
310
+ CHECK_RESULT(storeString(tree,
311
+ first,
312
+ &right->value.regex.stringOffset,
313
+ leftLength));
314
+
359
315
  result = compileRegex(tree,
360
316
  first,
361
317
  last,
@@ -370,10 +326,10 @@ static unsigned int copyValue(ruleset *tree,
370
326
  }
371
327
 
372
328
  static unsigned char compareValue(ruleset *tree,
373
- jsonValue *right,
329
+ operand *right,
374
330
  char *first,
375
331
  char *last,
376
- reference *ref,
332
+ identifier *id,
377
333
  unsigned char type) {
378
334
 
379
335
  if (right->type != type) {
@@ -383,18 +339,17 @@ static unsigned char compareValue(ruleset *tree,
383
339
  unsigned int leftLength;
384
340
  char temp;
385
341
  switch(type) {
386
- case JSON_EVENT_PROPERTY:
387
- case JSON_EVENT_LOCAL_PROPERTY:
388
- case JSON_STATE_PROPERTY:
389
- if (right->value.property.nameHash == ref->nameHash &&
390
- right->value.property.nameOffset == ref->nameOffset &&
391
- right->value.property.idOffset == ref->idOffset)
342
+ case JSON_IDENTIFIER:
343
+ case JSON_MESSAGE_IDENTIFIER:
344
+ if (right->value.id.propertyNameHash == id->propertyNameHash &&
345
+ right->value.id.propertyNameOffset == id->propertyNameOffset &&
346
+ right->value.id.nameHash == id->nameHash &&
347
+ right->value.id.nameOffset == id->nameOffset)
392
348
  return 1;
393
349
 
394
350
  return 0;
395
- case JSON_STATE_IDIOM:
396
- case JSON_EVENT_IDIOM:
397
- case JSON_EVENT_LOCAL_IDIOM:
351
+ case JSON_EXPRESSION:
352
+ case JSON_MESSAGE_EXPRESSION:
398
353
  return 0;
399
354
  case JSON_STRING:
400
355
  {
@@ -446,7 +401,7 @@ static unsigned int validateSetting(unsigned int settingHash, char *rule, unsign
446
401
  return ERR_UNEXPECTED_TYPE;
447
402
  }
448
403
 
449
- return PARSE_OK;
404
+ return RULES_OK;
450
405
  }
451
406
 
452
407
  result = readNextName(last, &first, &last, &hash);
@@ -455,63 +410,31 @@ static unsigned int validateSetting(unsigned int settingHash, char *rule, unsign
455
410
  return ERR_SETTING_NOT_FOUND;
456
411
  }
457
412
 
458
- static unsigned int validateReference(char *rule, unsigned char *referenceType) {
413
+ static unsigned int validateIdentifier(char *rule, unsigned char *identifierType) {
459
414
  char *first;
460
415
  char *last;
461
- unsigned char type;
462
416
  unsigned int hash;
463
- unsigned int result;
464
-
465
- result = readNextName(rule, &first, &last, &hash);
466
- if (result != PARSE_OK) {
467
- return result;
468
- }
469
-
470
- if (hash != HASH_S) {
471
- *referenceType = JSON_EVENT_PROPERTY;
472
-
473
- if (hash == HASH_M) {
474
- *referenceType = JSON_EVENT_LOCAL_PROPERTY;
475
- }
417
+
418
+ CHECK_PARSE_RESULT(readNextName(rule,
419
+ &first,
420
+ &last,
421
+ &hash));
476
422
 
477
- result = readNextString(last, &first, &last, &hash);
478
- if (result != PARSE_OK) {
479
- return result;
480
- }
481
- } else {
482
- *referenceType = JSON_STATE_PROPERTY;
483
- if (readNextString(last, &first, &last, &hash) != PARSE_OK) {
484
- result = readNextValue(last, &first, &last, &type);
485
- if (result != PARSE_OK) {
486
- return result;
487
- }
488
-
489
- unsigned int result = readNextName(first, &first, &last, &hash);
490
- while (result == PARSE_OK) {
491
- switch (hash) {
492
- case HASH_NAME:
493
- result = readNextString(last, &first, &last, &hash);
494
- break;
495
- case HASH_ID:
496
- result = readNextValue(last, &first, &last, &type);
497
- break;
498
- default:
499
- result = readNextValue(last, &first, &last, &type);
500
- break;
501
- }
423
+ *identifierType = JSON_IDENTIFIER;
502
424
 
503
- if (result != PARSE_OK) {
504
- return result;
505
- }
506
- result = readNextName(last, &first, &last, &hash);
507
- }
508
- }
425
+ if (hash == HASH_M) {
426
+ *identifierType = JSON_MESSAGE_IDENTIFIER;
509
427
  }
510
428
 
511
- return PARSE_OK;
429
+ CHECK_PARSE_RESULT(readNextString(last,
430
+ &first,
431
+ &last,
432
+ &hash));
433
+
434
+ return RULES_OK;
512
435
  }
513
436
 
514
- static unsigned int validateIdiom(char *rule, unsigned char *idiomType) {
437
+ static unsigned int validateExpression(char *rule, unsigned char *expressionType) {
515
438
  char *first;
516
439
  char *last;
517
440
  unsigned char type;
@@ -525,54 +448,41 @@ static unsigned int validateIdiom(char *rule, unsigned char *idiomType) {
525
448
 
526
449
  if (hash != HASH_ADD && hash != HASH_SUB &&
527
450
  hash != HASH_MUL && hash != HASH_DIV) {
528
- return validateReference(rule, idiomType);
451
+ return validateIdentifier(rule, expressionType);
529
452
  } else {
530
453
  result = readNextValue(last, &first, &last, &type);
531
454
  if (result != PARSE_OK) {
532
455
  return result;
533
456
  }
534
457
 
535
- *idiomType = 0;
458
+ *expressionType = 0;
536
459
  result = readNextName(first, &first, &last, &hash);
537
460
  while (result == PARSE_OK) {
538
- unsigned char newIdiomType = 0;
539
- result = readNextValue(last, &first, &last, &type);
540
- if (result != PARSE_OK) {
541
- return result;
542
- }
543
-
461
+ unsigned char newExpressionType = 0;
462
+ CHECK_PARSE_RESULT(readNextValue(last,
463
+ &first,
464
+ &last,
465
+ &type));
466
+
544
467
  if (type == JSON_OBJECT) {
545
- result = validateIdiom(first, &newIdiomType);
546
- if (result != PARSE_OK) {
547
- return result;
548
- }
468
+ CHECK_RESULT(validateExpression(first,
469
+ &newExpressionType));
549
470
  }
550
471
 
551
- if (newIdiomType == JSON_EVENT_PROPERTY || newIdiomType == JSON_EVENT_IDIOM) {
552
- if (*idiomType == JSON_STATE_PROPERTY || *idiomType == JSON_STATE_IDIOM ||
553
- *idiomType == JSON_EVENT_LOCAL_PROPERTY || *idiomType == JSON_EVENT_LOCAL_PROPERTY) {
472
+ if (newExpressionType == JSON_IDENTIFIER || newExpressionType == JSON_EXPRESSION) {
473
+ if (*expressionType == JSON_MESSAGE_IDENTIFIER) {
554
474
  return ERR_UNEXPECTED_TYPE;
555
475
  }
556
476
 
557
- *idiomType = JSON_EVENT_IDIOM;
558
- }
559
-
560
- if (newIdiomType == JSON_STATE_PROPERTY || newIdiomType == JSON_STATE_IDIOM) {
561
- if (*idiomType == JSON_EVENT_PROPERTY || *idiomType == JSON_EVENT_IDIOM ||
562
- *idiomType == JSON_EVENT_LOCAL_PROPERTY || *idiomType == JSON_EVENT_LOCAL_IDIOM) {
563
- return ERR_UNEXPECTED_TYPE;
564
- }
477
+ *expressionType = JSON_EXPRESSION;
478
+ }
565
479
 
566
- *idiomType = JSON_STATE_IDIOM;
567
- }
568
-
569
- if (newIdiomType == JSON_EVENT_LOCAL_PROPERTY || newIdiomType == JSON_EVENT_LOCAL_IDIOM) {
570
- if (*idiomType == JSON_STATE_PROPERTY || *idiomType == JSON_STATE_IDIOM ||
571
- *idiomType == JSON_EVENT_PROPERTY || *idiomType == JSON_EVENT_PROPERTY) {
480
+ if (newExpressionType == JSON_MESSAGE_IDENTIFIER || newExpressionType == JSON_MESSAGE_EXPRESSION) {
481
+ if (*expressionType == JSON_IDENTIFIER) {
572
482
  return ERR_UNEXPECTED_TYPE;
573
483
  }
574
484
 
575
- *idiomType = JSON_EVENT_IDIOM;
485
+ *expressionType = JSON_EXPRESSION;
576
486
  }
577
487
 
578
488
  if (hash != HASH_L && hash != HASH_R) {
@@ -583,11 +493,11 @@ static unsigned int validateIdiom(char *rule, unsigned char *idiomType) {
583
493
  }
584
494
  }
585
495
 
586
- return PARSE_OK;
496
+ return RULES_OK;
587
497
  }
588
498
 
589
499
 
590
- static unsigned int validateExpression(char *rule) {
500
+ static unsigned int validateExpressionSequence(char *rule) {
591
501
  char *first;
592
502
  char *last;
593
503
  unsigned char type;
@@ -644,10 +554,7 @@ static unsigned int validateExpression(char *rule) {
644
554
 
645
555
  result = readNextArrayValue(first, &first, &last, &type);
646
556
  while (result == PARSE_OK) {
647
- result = validateExpression(first);
648
- if (result != PARSE_OK) {
649
- return result;
650
- }
557
+ CHECK_RESULT(validateExpressionSequence(first));
651
558
 
652
559
  result = readNextArrayValue(last, &first, &last, &type);
653
560
  }
@@ -659,28 +566,28 @@ static unsigned int validateExpression(char *rule) {
659
566
  operator = OP_EQ;
660
567
  first = rule;
661
568
  } else {
662
- result = readNextValue(last, &first, &last, &type);
663
- if (result != PARSE_OK) {
664
- return result;
665
- }
569
+ CHECK_PARSE_RESULT(readNextValue(last,
570
+ &first,
571
+ &last,
572
+ &type));
666
573
  if (type != JSON_OBJECT) {
667
574
  return ERR_UNEXPECTED_TYPE;
668
575
  }
669
576
  }
670
577
 
671
- result = readNextName(first, &first, &last, &hash);
672
- if (result != PARSE_OK) {
673
- return result;
674
- }
675
-
676
- // Validating expression rValue
677
- result = readNextValue(last, &first, &last, &type);
678
- if (result != PARSE_OK) {
679
- return result;
680
- }
681
-
578
+ CHECK_PARSE_RESULT(readNextName(first,
579
+ &first,
580
+ &last,
581
+ &hash));
582
+
583
+ // Validating expressionSequence rValue
584
+ CHECK_PARSE_RESULT(readNextValue(last,
585
+ &first,
586
+ &last,
587
+ &type));
588
+
682
589
  if (operator == OP_IALL || operator == OP_IANY) {
683
- result = validateExpression(first);
590
+ result = validateExpressionSequence(first);
684
591
  return result;
685
592
  }
686
593
 
@@ -689,10 +596,8 @@ static unsigned int validateExpression(char *rule) {
689
596
  return ERR_UNEXPECTED_TYPE;
690
597
  }
691
598
 
692
- result = validateRegex(first, last);
693
- if (result != PARSE_OK) {
694
- return result;
695
- }
599
+ CHECK_RESULT(validateRegex(first,
600
+ last));
696
601
  }
697
602
 
698
603
  if (type == JSON_ARRAY) {
@@ -700,14 +605,12 @@ static unsigned int validateExpression(char *rule) {
700
605
  }
701
606
 
702
607
  if (type == JSON_OBJECT) {
703
- unsigned char idiomType = 0;
704
- result = validateIdiom(first, &idiomType);
705
- if (result != PARSE_OK) {
706
- return result;
707
- }
608
+ unsigned char expressionType = 0;
609
+ CHECK_RESULT(validateExpression(first,
610
+ &expressionType));
708
611
  }
709
612
 
710
- return PARSE_OK;
613
+ return RULES_OK;
711
614
  }
712
615
 
713
616
  static unsigned int validateAlgebra(char *rule) {
@@ -717,9 +620,16 @@ static unsigned int validateAlgebra(char *rule) {
717
620
  unsigned int hash;
718
621
  unsigned char type;
719
622
  unsigned char reenter = 0;
720
- unsigned int result = readNextArrayValue(rule, &first, &lastArrayValue, &type);
623
+ unsigned int result = readNextArrayValue(rule,
624
+ &first,
625
+ &lastArrayValue,
626
+ &type);
721
627
  while (result == PARSE_OK) {
722
- result = readNextName(first, &first, &last, &hash);
628
+ result = readNextName(first,
629
+ &first,
630
+ &last,
631
+ &hash);
632
+
723
633
  unsigned int nameLength = last - first;
724
634
  if (nameLength >= 4) {
725
635
  if (!strncmp("$all", last - 4, 4)) {
@@ -737,24 +647,27 @@ static unsigned int validateAlgebra(char *rule) {
737
647
  }
738
648
  }
739
649
 
740
- result = readNextValue(last, &first, &last, &type);
741
- if (result != PARSE_OK) {
742
- return result;
743
- }
650
+ CHECK_PARSE_RESULT(readNextValue(last,
651
+ &first,
652
+ &last,
653
+ &type));
744
654
 
745
655
  if (!reenter) {
746
- result = validateExpression(first);
656
+ result = validateExpressionSequence(first);
747
657
  }
748
658
  else {
749
659
  result = validateAlgebra(first);
750
660
  reenter = 0;
751
661
  }
752
662
 
753
- if (result != PARSE_OK) {
663
+ if (result != RULES_OK) {
754
664
  return result;
755
665
  }
756
666
 
757
- result = readNextArrayValue(lastArrayValue, &first, &lastArrayValue, &type);
667
+ result = readNextArrayValue(lastArrayValue,
668
+ &first,
669
+ &lastArrayValue,
670
+ &type);
758
671
  }
759
672
 
760
673
  return (result == PARSE_END ? PARSE_OK: result);
@@ -769,22 +682,30 @@ static unsigned int validateRuleset(char *rules) {
769
682
  unsigned char type;
770
683
  unsigned int hash;
771
684
  unsigned int result;
772
- result = readNextName(rules, &firstName, &lastName, &hash);
685
+ result = readNextName(rules,
686
+ &firstName,
687
+ &lastName,
688
+ &hash);
689
+
773
690
  while (result == PARSE_OK) {
774
- result = readNextValue(lastName, &first, &lastRuleValue, &type);
775
- if (result != PARSE_OK) {
776
- return result;
777
- }
691
+ CHECK_PARSE_RESULT(readNextValue(lastName,
692
+ &first,
693
+ &lastRuleValue,
694
+ &type));
778
695
  if (type != JSON_OBJECT) {
779
696
  return ERR_UNEXPECTED_TYPE;
780
697
  }
781
698
 
782
- unsigned int countResult = validateSetting(HASH_COUNT, first, JSON_INT);
699
+ unsigned int countResult = validateSetting(HASH_COUNT,
700
+ first,
701
+ JSON_INT);
783
702
  if (countResult != PARSE_OK && countResult != ERR_SETTING_NOT_FOUND) {
784
703
  return countResult;
785
704
  }
786
705
 
787
- unsigned int capResult = validateSetting(HASH_CAP, first, JSON_INT);
706
+ unsigned int capResult = validateSetting(HASH_CAP,
707
+ first,
708
+ JSON_INT);
788
709
  if (capResult != PARSE_OK && capResult != ERR_SETTING_NOT_FOUND) {
789
710
  return capResult;
790
711
  }
@@ -793,22 +714,26 @@ static unsigned int validateRuleset(char *rules) {
793
714
  return ERR_UNEXPECTED_NAME;
794
715
  }
795
716
 
796
- result = validateSetting(HASH_PRI, first, JSON_INT);
717
+ result = validateSetting(HASH_PRI,
718
+ first,
719
+ JSON_INT);
797
720
  if (result != PARSE_OK && result != ERR_SETTING_NOT_FOUND) {
798
721
  return result;
799
722
  }
800
723
 
801
- result = validateSetting(HASH_DIST, first, JSON_INT);
724
+ result = validateSetting(HASH_DIST,
725
+ first,
726
+ JSON_INT);
802
727
  if (result != PARSE_OK && result != ERR_SETTING_NOT_FOUND) {
803
728
  return result;
804
729
  }
805
730
 
806
731
  result = readNextName(first, &first, &last, &hash);
807
732
  while (result == PARSE_OK) {
808
- result = readNextValue(last, &first, &last, &type);
809
- if (result != PARSE_OK) {
810
- return result;
811
- }
733
+ CHECK_PARSE_RESULT(readNextValue(last,
734
+ &first,
735
+ &last,
736
+ &type));
812
737
 
813
738
  if (hash == HASH_ALL || hash == HASH_ANY) {
814
739
  result = validateAlgebra(first);
@@ -831,16 +756,13 @@ static unsigned int validateRuleset(char *rules) {
831
756
  static unsigned int linkAlpha(ruleset *tree,
832
757
  unsigned int parentOffset,
833
758
  unsigned int nextOffset) {
834
- unsigned int result;
835
759
  unsigned int entry;
836
760
  node *parentAlpha = &tree->nodePool[parentOffset];
837
761
  node *nextNode = &tree->nodePool[nextOffset];
838
762
  if (nextNode->type != NODE_ALPHA) {
839
- result = ensureBetaList(tree, parentAlpha);
840
- if (result != RULES_OK) {
841
- return result;
842
- }
843
-
763
+ CHECK_RESULT(ensureBetaList(tree,
764
+ parentAlpha));
765
+
844
766
  unsigned int *parentBetaList = &tree->nextPool[parentAlpha->value.a.betaListOffset];
845
767
  for (entry = 0; parentBetaList[entry] != 0; ++entry) {
846
768
  if (entry == BETA_LIST_LENGTH) {
@@ -849,12 +771,10 @@ static unsigned int linkAlpha(ruleset *tree,
849
771
  }
850
772
 
851
773
  parentBetaList[entry] = nextOffset;
852
- } else if (nextNode->value.a.operator == OP_NEX) {
853
- result = ensureNextList(tree, parentAlpha);
854
- if (result != RULES_OK) {
855
- return result;
856
- }
857
-
774
+ } else if (nextNode->value.a.expression.operator == OP_NEX) {
775
+ CHECK_RESULT(ensureNextList(tree,
776
+ parentAlpha));
777
+
858
778
  unsigned int *parentNextList = &tree->nextPool[parentAlpha->value.a.nextListOffset];
859
779
  unsigned int entry;
860
780
  for (entry = 0; parentNextList[entry] != 0; ++entry) {
@@ -865,13 +785,11 @@ static unsigned int linkAlpha(ruleset *tree,
865
785
 
866
786
  parentNextList[entry] = nextOffset;
867
787
  } else {
868
- result = ensureNextHashset(tree, parentAlpha);
869
- if (result != RULES_OK) {
870
- return result;
871
- }
872
-
788
+ CHECK_RESULT(ensureNextHashset(tree,
789
+ parentAlpha));
790
+
873
791
  unsigned int *parentNext = &tree->nextPool[parentAlpha->value.a.nextOffset];
874
- unsigned int hash = nextNode->value.a.hash;
792
+ unsigned int hash = nextNode->value.a.expression.left.value.id.propertyNameHash;
875
793
  for (entry = hash & HASH_MASK; parentNext[entry] != 0; entry = (entry + 1) % NEXT_BUCKET_LENGTH) {
876
794
  if ((entry + 1) % NEXT_BUCKET_LENGTH == (hash & HASH_MASK)) {
877
795
  return ERR_RULE_LIMIT_EXCEEDED;
@@ -884,82 +802,37 @@ static unsigned int linkAlpha(ruleset *tree,
884
802
  return RULES_OK;
885
803
  }
886
804
 
887
- static unsigned int readReference(ruleset *tree, char *rule, unsigned char *idiomType, reference *ref) {
805
+ static unsigned int readIdentifier(ruleset *tree, char *rule, unsigned char *expressionType, identifier *id) {
888
806
  char *first;
889
807
  char *last;
890
- unsigned char type;
891
808
  unsigned int hash;
892
- unsigned int result;
893
- *idiomType = JSON_EVENT_PROPERTY;
894
-
895
- ref->idOffset = 0;
809
+ *expressionType = JSON_IDENTIFIER;
810
+ id->nameOffset = 0;
896
811
  readNextName(rule, &first, &last, &hash);
897
- if (hash != HASH_S) {
898
- result = storeString(tree, first, &ref->idOffset, last - first);
899
- if (result != RULES_OK) {
900
- return result;
901
- }
902
-
903
- if (hash == HASH_M) {
904
- *idiomType = JSON_EVENT_LOCAL_PROPERTY;
905
- }
906
-
907
- readNextString(last, &first, &last, &hash);
908
- ref->nameHash = hash;
909
- result = storeString(tree, first, &ref->nameOffset, last - first);
910
- if (result != RULES_OK) {
911
- return result;
912
- }
913
- } else {
914
- *idiomType = JSON_STATE_PROPERTY;
915
- if (readNextString(last, &first, &last, &hash) == PARSE_OK) {
916
- ref->nameHash = hash;
917
- result = storeString(tree, first, &ref->nameOffset, last - first);
918
- if (result != RULES_OK) {
919
- return result;
920
- }
921
- } else {
922
- readNextValue(last, &first, &last, &type);
923
- result = readNextName(first, &first, &last, &hash);
924
- while (result == PARSE_OK) {
925
- switch (hash) {
926
- case HASH_NAME:
927
- readNextString(last, &first, &last, &hash);
928
- ref->nameHash = hash;
929
- result = storeString(tree, first, &ref->nameOffset, last - first);
930
- if (result != RULES_OK) {
931
- return result;
932
- }
933
- break;
934
- case HASH_ID:
935
- readNextValue(last, &first, &last, &type);
936
- if (type == JSON_STRING) {
937
- result = storeString(tree, first, &ref->idOffset, last - first);
938
- if (result != RULES_OK) {
939
- return result;
940
- }
941
- } else{
942
- result = storeString(tree, first, &ref->idOffset, last - first + 1);
943
- if (result != RULES_OK) {
944
- return result;
945
- }
946
- }
947
-
948
- break;
949
- default:
950
- readNextValue(last, &first, &last, &type);
951
- break;
952
- }
953
-
954
- result = readNextName(last, &first, &last, &hash);
955
- }
956
- }
957
- }
958
-
812
+ id->nameHash = hash;
813
+ CHECK_RESULT(storeString(tree,
814
+ first,
815
+ &id->nameOffset,
816
+ last - first));
817
+
818
+ if (hash == HASH_M) {
819
+ *expressionType = JSON_MESSAGE_IDENTIFIER;
820
+ }
821
+
822
+ CHECK_PARSE_RESULT(readNextString(last,
823
+ &first,
824
+ &last,
825
+ &hash));
826
+ id->propertyNameHash = hash;
827
+ CHECK_RESULT(storeString(tree,
828
+ first,
829
+ &id->propertyNameOffset,
830
+ last - first));
831
+
959
832
  return RULES_OK;
960
833
  }
961
834
 
962
- static unsigned int readIdiom(ruleset *tree, char *rule, unsigned char *idiomType, unsigned int *idiomOffset, reference *ref) {
835
+ static unsigned int readExpression(ruleset *tree, char *rule, unsigned char *expressionType, unsigned int *expressionOffset, identifier *id) {
963
836
  char *first;
964
837
  char *last;
965
838
  unsigned char type = 0;
@@ -967,7 +840,10 @@ static unsigned int readIdiom(ruleset *tree, char *rule, unsigned char *idiomTyp
967
840
  unsigned int hash;
968
841
  unsigned int result;
969
842
 
970
- readNextName(rule, &first, &last, &hash);
843
+ CHECK_PARSE_RESULT(readNextName(rule,
844
+ &first,
845
+ &last,
846
+ &hash));
971
847
  switch (hash) {
972
848
  case HASH_ADD:
973
849
  operator = OP_ADD;
@@ -984,48 +860,55 @@ static unsigned int readIdiom(ruleset *tree, char *rule, unsigned char *idiomTyp
984
860
  }
985
861
 
986
862
  if (operator == OP_NOP) {
987
- result = readReference(tree, rule, idiomType, ref);
988
- if (result != RULES_OK) {
989
- return result;
990
- }
863
+ CHECK_RESULT(readIdentifier(tree,
864
+ rule,
865
+ expressionType,
866
+ id));
991
867
  } else {
992
- idiom *newIdiom = NULL;
993
- result = storeIdiom(tree, &newIdiom, idiomOffset);
994
- if (result != RULES_OK) {
995
- return result;
996
- }
868
+ expression *newExpression = NULL;
869
+ CHECK_RESULT(storeExpression(tree,
870
+ &newExpression,
871
+ expressionOffset));
872
+
873
+ *expressionType = JSON_EXPRESSION;
874
+ newExpression->operator = operator;
875
+ CHECK_PARSE_RESULT(readNextValue(last,
876
+ &first,
877
+ &last,
878
+ &type));
997
879
 
998
- *idiomType = JSON_STATE_IDIOM;
999
- newIdiom->operator = operator;
1000
- readNextValue(last, &first, &last, &type);
1001
880
  result = readNextName(first, &first, &last, &hash);
1002
881
  while (result == PARSE_OK) {
1003
- unsigned int newIdiomOffset = 0;
1004
- reference newRef;
1005
- readNextValue(last, &first, &last, &type);
882
+ unsigned int newExpressionOffset = 0;
883
+ identifier newRef;
884
+ CHECK_PARSE_RESULT(readNextValue(last,
885
+ &first,
886
+ &last,
887
+ &type));
1006
888
  if (type == JSON_OBJECT) {
1007
- result = readIdiom(tree, first, &type, &newIdiomOffset, &newRef);
1008
- if (result != RULES_OK) {
1009
- return result;
1010
- }
889
+ CHECK_RESULT(readExpression(tree,
890
+ first,
891
+ &type,
892
+ &newExpressionOffset,
893
+ &newRef));
1011
894
  }
1012
895
 
1013
- if (type == JSON_EVENT_PROPERTY || type == JSON_EVENT_IDIOM) {
1014
- *idiomType = JSON_EVENT_IDIOM;
896
+ if (type == JSON_IDENTIFIER || type == JSON_EXPRESSION) {
897
+ *expressionType = JSON_EXPRESSION;
1015
898
  }
1016
899
 
1017
- if (*idiomType != JSON_EVENT_IDIOM && (type == JSON_EVENT_LOCAL_PROPERTY || type == JSON_EVENT_LOCAL_IDIOM)) {
1018
- *idiomType = JSON_EVENT_LOCAL_IDIOM;
900
+ if (*expressionType != JSON_EXPRESSION && (type == JSON_MESSAGE_IDENTIFIER || type == JSON_MESSAGE_EXPRESSION)) {
901
+ *expressionType = JSON_MESSAGE_EXPRESSION;
1019
902
  }
1020
903
 
1021
- // newIdiom address might have changed after readIdiom
1022
- newIdiom = &tree->idiomPool[*idiomOffset];
904
+ // newExpression address might have changed after readExpression
905
+ newExpression = &tree->expressionPool[*expressionOffset];
1023
906
  switch (hash) {
1024
907
  case HASH_L:
1025
- copyValue(tree, &newIdiom->left, first, last, newIdiomOffset, &newRef, type);
908
+ copyValue(tree, &newExpression->left, first, last, newExpressionOffset, &newRef, type);
1026
909
  break;
1027
910
  case HASH_R:
1028
- copyValue(tree, &newIdiom->right, first, last, newIdiomOffset, &newRef, type);
911
+ copyValue(tree, &newExpression->right, first, last, newExpressionOffset, &newRef, type);
1029
912
  break;
1030
913
  }
1031
914
 
@@ -1040,7 +923,7 @@ static unsigned int findAlpha(ruleset *tree,
1040
923
  unsigned int parentOffset,
1041
924
  unsigned char operator,
1042
925
  char *rule,
1043
- expression *expr,
926
+ unsigned int betaOffset,
1044
927
  unsigned int *resultOffset) {
1045
928
  char *first;
1046
929
  char *last;
@@ -1049,17 +932,25 @@ static unsigned int findAlpha(ruleset *tree,
1049
932
  unsigned char type;
1050
933
  unsigned int entry;
1051
934
  unsigned int hash;
1052
- unsigned int idiomOffset;
1053
- unsigned int result;
1054
- reference ref;
935
+ unsigned int expressionOffset;
936
+ identifier id;
1055
937
 
1056
- readNextName(rule, &firstName, &lastName, &hash);
1057
- readNextValue(lastName, &first, &last, &type);
938
+ CHECK_PARSE_RESULT(readNextName(rule,
939
+ &firstName,
940
+ &lastName,
941
+ &hash));
942
+
943
+ CHECK_PARSE_RESULT(readNextValue(lastName,
944
+ &first,
945
+ &last,
946
+ &type));
947
+
1058
948
  if (type == JSON_OBJECT && operator != OP_IALL && operator != OP_IANY) {
1059
- result = readIdiom(tree, first, &type, &idiomOffset, &ref);
1060
- if (result != RULES_OK) {
1061
- return result;
1062
- }
949
+ CHECK_RESULT(readExpression(tree,
950
+ first,
951
+ &type,
952
+ &expressionOffset,
953
+ &id));
1063
954
  }
1064
955
 
1065
956
  node *parent = &tree->nodePool[parentOffset];
@@ -1068,10 +959,10 @@ static unsigned int findAlpha(ruleset *tree,
1068
959
  parentNext = &tree->nextPool[parent->value.a.nextOffset];
1069
960
  for (entry = hash & HASH_MASK; parentNext[entry] != 0; entry = (entry + 1) % NEXT_BUCKET_LENGTH) {
1070
961
  node *currentNode = &tree->nodePool[parentNext[entry]];
1071
- if (currentNode->value.a.hash == hash &&
1072
- currentNode->value.a.operator == operator) {
962
+ if (currentNode->value.a.expression.left.value.id.propertyNameHash == hash &&
963
+ currentNode->value.a.expression.operator == operator) {
1073
964
  if (operator != OP_IALL && operator != OP_IANY) {
1074
- if (compareValue(tree, &currentNode->value.a.right, first, last, &ref, type)) {
965
+ if (compareValue(tree, &currentNode->value.a.expression.right, first, last, &id, type)) {
1075
966
  *resultOffset = parentNext[entry];
1076
967
  return RULES_OK;
1077
968
  }
@@ -1084,10 +975,10 @@ static unsigned int findAlpha(ruleset *tree,
1084
975
  parentNext = &tree->nextPool[parent->value.a.nextListOffset];
1085
976
  for (entry = 0; parentNext[entry] != 0; ++entry) {
1086
977
  node *currentNode = &tree->nodePool[parentNext[entry]];
1087
- if (currentNode->value.a.hash == hash &&
1088
- currentNode->value.a.operator == operator) {
978
+ if (currentNode->value.a.expression.left.value.id.propertyNameHash == hash &&
979
+ currentNode->value.a.expression.operator == operator) {
1089
980
  if (operator != OP_IALL && operator != OP_IANY) {
1090
- if (compareValue(tree, &currentNode->value.a.right, first, last, &ref, type)) {
981
+ if (compareValue(tree, &currentNode->value.a.expression.right, first, last, &id, type)) {
1091
982
  *resultOffset = parentNext[entry];
1092
983
  return RULES_OK;
1093
984
  }
@@ -1097,21 +988,24 @@ static unsigned int findAlpha(ruleset *tree,
1097
988
  }
1098
989
 
1099
990
  unsigned int stringOffset;
1100
- result = storeString(tree, firstName, &stringOffset, lastName - firstName);
1101
- if (result != RULES_OK) {
1102
- return result;
1103
- }
1104
-
991
+ CHECK_RESULT(storeString(tree,
992
+ firstName,
993
+ &stringOffset,
994
+ lastName - firstName));
995
+
1105
996
  node *newAlpha;
1106
- result = storeAlpha(tree, &newAlpha, resultOffset);
1107
- if (result != RULES_OK) {
1108
- return result;
1109
- }
1110
-
997
+ CHECK_RESULT(storeAlpha(tree,
998
+ &newAlpha,
999
+ resultOffset));
1000
+
1111
1001
  newAlpha->nameOffset = stringOffset;
1112
1002
  newAlpha->type = NODE_ALPHA;
1113
- newAlpha->value.a.hash = hash;
1114
- newAlpha->value.a.operator = operator;
1003
+ newAlpha->value.a.expression.left.type = JSON_MESSAGE_IDENTIFIER;
1004
+ newAlpha->value.a.expression.left.value.id.propertyNameHash = hash;
1005
+ newAlpha->value.a.expression.left.value.id.propertyNameOffset = newAlpha->nameOffset;
1006
+ newAlpha->value.a.expression.left.value.id.nameOffset = 0;
1007
+ newAlpha->value.a.expression.left.value.id.nameHash = 0;
1008
+ newAlpha->value.a.expression.operator = operator;
1115
1009
  if (operator == OP_MT) {
1116
1010
  type = JSON_REGEX;
1117
1011
  }
@@ -1120,34 +1014,44 @@ static unsigned int findAlpha(ruleset *tree,
1120
1014
  type = JSON_IREGEX;
1121
1015
  }
1122
1016
 
1123
- if (operator == OP_IANY || operator == OP_IALL) {
1124
- newAlpha->value.a.right.type = JSON_NIL;
1125
- } else {
1126
- result = copyValue(tree, &newAlpha->value.a.right, first, last, idiomOffset, &ref, type);
1127
- if (result != RULES_OK) {
1128
- return result;
1129
- }
1130
-
1131
- if (type == JSON_EVENT_PROPERTY || type == JSON_EVENT_IDIOM) {
1132
- result = appendTerm(expr, *resultOffset);
1133
- if (result != RULES_OK) {
1134
- return result;
1135
- }
1017
+ //if (operator == OP_IANY || operator == OP_IALL) {
1018
+ // newAlpha->value.a.expression.right.type = JSON_NIL;
1019
+ //} else {
1020
+ CHECK_RESULT(copyValue(tree,
1021
+ &newAlpha->value.a.expression.right,
1022
+ first,
1023
+ last,
1024
+ expressionOffset,
1025
+ &id,
1026
+ type));
1027
+
1028
+ if (type == JSON_IDENTIFIER || type == JSON_EXPRESSION) {
1029
+ expression *expr;
1030
+ GET_EXPRESSION(tree,
1031
+ betaOffset,
1032
+ expr);
1033
+ copyExpression(&newAlpha->value.a.expression, expr);
1136
1034
  }
1137
- }
1035
+ //}
1138
1036
 
1139
1037
  return linkAlpha(tree, parentOffset, *resultOffset);
1140
1038
  }
1141
1039
 
1142
- static void getSetting(unsigned int settingHash, char *rule, unsigned short *value) {
1040
+ static unsigned int getSetting(unsigned int settingHash, char *rule, unsigned short *value) {
1143
1041
  char *first;
1144
1042
  char *last;
1145
1043
  char temp;
1146
1044
  unsigned int hash;
1147
1045
  unsigned char type;
1148
- unsigned int result = readNextName(rule, &first, &last, &hash);
1046
+ unsigned int result = readNextName(rule,
1047
+ &first,
1048
+ &last,
1049
+ &hash);
1149
1050
  while (result == PARSE_OK) {
1150
- readNextValue(last, &first, &last, &type);
1051
+ CHECK_PARSE_RESULT(readNextValue(last,
1052
+ &first,
1053
+ &last,
1054
+ &type));
1151
1055
  if (hash == settingHash) {
1152
1056
  temp = first[last - first + 1];
1153
1057
  first[last - first + 1] = '\0';
@@ -1155,29 +1059,37 @@ static void getSetting(unsigned int settingHash, char *rule, unsigned short *val
1155
1059
  first[last - first + 1] = temp;
1156
1060
  break;
1157
1061
  }
1158
- result = readNextName(last, &first, &last, &hash);
1062
+ result = readNextName(last,
1063
+ &first,
1064
+ &last,
1065
+ &hash);
1159
1066
  }
1067
+
1068
+ return RULES_OK;
1160
1069
  }
1161
1070
 
1162
1071
  static unsigned int createForwardAlpha(ruleset *tree,
1163
1072
  unsigned int *newOffset) {
1164
1073
  node *newAlpha;
1165
- unsigned int result = storeAlpha(tree, &newAlpha, newOffset);
1166
- if (result != RULES_OK) {
1167
- return result;
1168
- }
1169
-
1074
+ CHECK_RESULT(storeAlpha(tree,
1075
+ &newAlpha,
1076
+ newOffset));
1077
+
1170
1078
  newAlpha->nameOffset = 0;
1171
1079
  newAlpha->type = NODE_ALPHA;
1172
- newAlpha->value.a.hash = HASH_FORWARD;
1173
- newAlpha->value.a.operator = OP_NEX;
1080
+ newAlpha->value.a.expression.left.type = JSON_MESSAGE_IDENTIFIER;
1081
+ newAlpha->value.a.expression.left.value.id.propertyNameOffset = 0;
1082
+ newAlpha->value.a.expression.left.value.id.propertyNameHash = HASH_FORWARD;
1083
+ newAlpha->value.a.expression.left.value.id.nameOffset = 0;
1084
+ newAlpha->value.a.expression.left.value.id.nameHash = 0;
1085
+ newAlpha->value.a.expression.operator = OP_NEX;
1174
1086
 
1175
1087
  return RULES_OK;
1176
1088
  }
1177
1089
 
1178
1090
  static unsigned int createAlpha(ruleset *tree,
1179
1091
  char *rule,
1180
- expression *expr,
1092
+ unsigned int betaOffset,
1181
1093
  unsigned int nextOffset,
1182
1094
  unsigned int *newOffset) {
1183
1095
  char *first;
@@ -1187,7 +1099,10 @@ static unsigned int createAlpha(ruleset *tree,
1187
1099
  unsigned int hash;
1188
1100
  unsigned int result;
1189
1101
  unsigned int parentOffset = *newOffset;
1190
- readNextName(rule, &first, &last, &hash);
1102
+ CHECK_PARSE_RESULT(readNextName(rule,
1103
+ &first,
1104
+ &last,
1105
+ &hash));
1191
1106
  switch (hash) {
1192
1107
  case HASH_EQ:
1193
1108
  operator = OP_EQ;
@@ -1226,71 +1141,83 @@ static unsigned int createAlpha(ruleset *tree,
1226
1141
  operator = OP_LTE;
1227
1142
  break;
1228
1143
  case HASH_AND:
1229
- result = appendTerm(expr, tree->andNodeOffset);
1230
- if (result != RULES_OK) {
1231
- return result;
1232
- }
1144
+ APPEND_EXPRESSION(tree,
1145
+ betaOffset,
1146
+ OP_AND);
1147
+
1148
+ CHECK_PARSE_RESULT(readNextValue(last,
1149
+ &first,
1150
+ &last,
1151
+ &type));
1233
1152
 
1234
- readNextValue(last, &first, &last, &type);
1235
1153
  unsigned int previousOffset = 0;
1236
1154
  unsigned int resultOffset = parentOffset;
1237
- result = readNextArrayValue(first, &first, &last, &type);
1155
+ result = readNextArrayValue(first,
1156
+ &first,
1157
+ &last,
1158
+ &type);
1238
1159
  while (result == PARSE_OK) {
1239
- result = createAlpha(tree, first, expr, 0, &resultOffset);
1240
- if (result != RULES_OK) {
1241
- return result;
1242
- }
1243
-
1160
+ CHECK_RESULT(createAlpha(tree,
1161
+ first,
1162
+ betaOffset,
1163
+ 0,
1164
+ &resultOffset));
1165
+
1244
1166
  previousOffset = resultOffset;
1245
- result = readNextArrayValue(last, &first, &last, &type);
1167
+ result = readNextArrayValue(last,
1168
+ &first,
1169
+ &last,
1170
+ &type);
1246
1171
  }
1247
1172
  *newOffset = previousOffset;
1248
1173
 
1249
- result = appendTerm(expr, tree->endNodeOffset);
1250
- if (result != RULES_OK) {
1251
- return result;
1252
- }
1253
-
1174
+ APPEND_EXPRESSION(tree,
1175
+ betaOffset,
1176
+ OP_END);
1254
1177
  if (nextOffset != 0) {
1255
- return linkAlpha(tree, previousOffset, nextOffset);
1178
+ return linkAlpha(tree,
1179
+ previousOffset,
1180
+ nextOffset);
1256
1181
  }
1257
1182
 
1258
1183
  return RULES_OK;
1259
1184
  case HASH_OR:
1260
- result = appendTerm(expr, tree->orNodeOffset);
1261
- if (result != RULES_OK) {
1262
- return result;
1263
- }
1264
-
1265
- result = createForwardAlpha(tree, newOffset);
1266
- if (result != RULES_OK) {
1267
- return result;
1268
- }
1185
+ APPEND_EXPRESSION(tree, betaOffset, OP_OR);
1186
+ CHECK_RESULT(createForwardAlpha(tree,
1187
+ newOffset));
1188
+
1189
+ CHECK_PARSE_RESULT(readNextValue(last,
1190
+ &first,
1191
+ &last,
1192
+ &type));
1269
1193
 
1270
- readNextValue(last, &first, &last, &type);
1271
1194
  result = readNextArrayValue(first, &first, &last, &type);
1272
1195
  while (result == PARSE_OK) {
1273
1196
  unsigned int single_offset = parentOffset;
1274
- result = createAlpha(tree, first, expr, 0, &single_offset);
1275
- if (result != RULES_OK) {
1276
- return result;
1277
- }
1278
-
1279
- result = linkAlpha(tree, single_offset, *newOffset);
1280
- if (result != RULES_OK) {
1281
- return result;
1282
- }
1283
-
1284
- result = readNextArrayValue(last, &first, &last, &type);
1285
- }
1286
-
1287
- result = appendTerm(expr, tree->endNodeOffset);
1288
- if (result != RULES_OK) {
1289
- return result;
1197
+ CHECK_RESULT(createAlpha(tree,
1198
+ first,
1199
+ betaOffset,
1200
+ 0,
1201
+ &single_offset));
1202
+
1203
+ CHECK_RESULT(linkAlpha(tree,
1204
+ single_offset,
1205
+ *newOffset));
1206
+
1207
+
1208
+ result = readNextArrayValue(last,
1209
+ &first,
1210
+ &last,
1211
+ &type);
1290
1212
  }
1291
1213
 
1214
+ APPEND_EXPRESSION(tree,
1215
+ betaOffset,
1216
+ OP_END);
1292
1217
  if (nextOffset != 0) {
1293
- return linkAlpha(tree, *newOffset, nextOffset);
1218
+ return linkAlpha(tree,
1219
+ *newOffset,
1220
+ nextOffset);
1294
1221
  }
1295
1222
 
1296
1223
  return RULES_OK;
@@ -1300,81 +1227,106 @@ static unsigned int createAlpha(ruleset *tree,
1300
1227
  operator = OP_EQ;
1301
1228
  first = rule;
1302
1229
  } else {
1303
- readNextValue(last, &first, &last, &type);
1230
+ CHECK_PARSE_RESULT(readNextValue(last,
1231
+ &first,
1232
+ &last,
1233
+ &type));
1304
1234
  }
1305
1235
 
1306
1236
  if (operator == OP_IANY || operator == OP_IALL) {
1307
- unsigned int result = findAlpha(tree, parentOffset, operator, first, expr, newOffset);
1308
- if (result != RULES_OK) {
1309
- return result;
1310
- }
1311
-
1237
+ CHECK_RESULT(findAlpha(tree,
1238
+ parentOffset,
1239
+ operator,
1240
+ first,
1241
+ betaOffset,
1242
+ newOffset));
1243
+
1312
1244
  unsigned int inner_offset = *newOffset;
1313
- readNextName(first, &first, &last, &hash);
1314
- readNextValue(last, &first, &last, &type);
1315
- result = createAlpha(tree, first, expr, nextOffset, &inner_offset);
1316
- if (result != RULES_OK) {
1317
- return result;
1318
- }
1319
-
1245
+ CHECK_PARSE_RESULT(readNextName(first,
1246
+ &first,
1247
+ &last,
1248
+ &hash));
1249
+
1250
+ CHECK_PARSE_RESULT(readNextValue(last,
1251
+ &first,
1252
+ &last,
1253
+ &type));
1254
+
1255
+ CHECK_RESULT(createAlpha(tree,
1256
+ first,
1257
+ betaOffset,
1258
+ nextOffset,
1259
+ &inner_offset));
1260
+
1320
1261
  node *newAlpha = &tree->nodePool[inner_offset];
1321
- if (newAlpha->value.a.right.type != JSON_EVENT_PROPERTY && newAlpha->value.a.right.type != JSON_EVENT_IDIOM) {
1322
- return linkAlpha(tree, *newOffset, nextOffset);
1262
+ if (newAlpha->value.a.expression.right.type != JSON_IDENTIFIER && newAlpha->value.a.expression.right.type != JSON_EXPRESSION) {
1263
+ return linkAlpha(tree,
1264
+ *newOffset,
1265
+ nextOffset);
1323
1266
  } else {
1324
1267
  // Functions that can execute in client or backend should follow this pattern
1325
1268
  node *oldAlpha = &tree->nodePool[*newOffset];
1326
- idiom *newIdiom = NULL;
1327
- unsigned int idiomOffset = 0;
1328
- result = storeIdiom(tree, &newIdiom, &idiomOffset);
1329
- if (result != RULES_OK) {
1330
- return result;
1331
- }
1269
+ expression *newExpression = NULL;
1270
+ unsigned int expressionOffset = 0;
1271
+ CHECK_RESULT(storeExpression(tree,
1272
+ &newExpression,
1273
+ &expressionOffset));
1274
+
1275
+ oldAlpha->value.a.expression.right.type = JSON_EXPRESSION;
1276
+ oldAlpha->value.a.expression.right.value.expressionOffset = expressionOffset;
1277
+ copyExpression(&newAlpha->value.a.expression, newExpression);
1332
1278
 
1333
- oldAlpha->value.a.right.type = JSON_EVENT_IDIOM;
1334
- oldAlpha->value.a.right.value.idiomOffset = idiomOffset;
1279
+ expression *betaExpression;
1280
+ GET_EXPRESSION(tree,
1281
+ betaOffset,
1282
+ betaExpression);
1335
1283
 
1336
- newIdiom->operator = newAlpha->value.a.operator;
1337
- newIdiom->right.type = newAlpha->value.a.right.type;
1338
- if (newAlpha->value.a.right.type == JSON_EVENT_IDIOM) {
1339
- newIdiom->right.value.idiomOffset = newAlpha->value.a.right.value.idiomOffset;
1340
- } else {
1341
- newIdiom->right.value.property.nameHash = newAlpha->value.a.right.value.property.nameHash;
1342
- newIdiom->right.value.property.nameOffset = newAlpha->value.a.right.value.property.nameOffset;
1343
- newIdiom->right.value.property.idOffset = newAlpha->value.a.right.value.property.idOffset;
1344
- }
1345
- newIdiom->left.type = JSON_EVENT_PROPERTY;
1346
- newIdiom->left.value.property.nameHash = newAlpha->value.a.hash;
1347
- newIdiom->left.value.property.nameOffset = newAlpha->nameOffset;
1348
- newIdiom->left.value.property.idOffset = 0;
1284
+ copyExpression(newExpression, betaExpression);
1349
1285
 
1350
- expr->t.termsPointer[expr->termsLength - 1] = *newOffset;
1351
- return linkAlpha(tree, *newOffset, nextOffset);
1286
+ return linkAlpha(tree,
1287
+ *newOffset,
1288
+ nextOffset);
1352
1289
  }
1353
1290
  }
1354
1291
 
1355
1292
  if (nextOffset == 0) {
1356
- return findAlpha(tree, parentOffset, operator, first, expr, newOffset);
1293
+ return findAlpha(tree,
1294
+ parentOffset,
1295
+ operator,
1296
+ first,
1297
+ betaOffset,
1298
+ newOffset);
1357
1299
  } else {
1358
- unsigned int result = findAlpha(tree, parentOffset, operator, first, expr, newOffset);
1359
- if (result != RULES_OK) {
1360
- return result;
1361
- }
1362
-
1363
- return linkAlpha(tree, *newOffset, nextOffset);
1300
+ CHECK_RESULT(findAlpha(tree,
1301
+ parentOffset,
1302
+ operator,
1303
+ first,
1304
+ betaOffset,
1305
+ newOffset));
1306
+
1307
+ return linkAlpha(tree,
1308
+ *newOffset,
1309
+ nextOffset);
1364
1310
  }
1365
1311
  }
1366
1312
 
1367
- static unsigned int createBetaConnector(ruleset *tree,
1368
- char *rule,
1369
- path *betaPath,
1370
- unsigned short distinct,
1371
- unsigned int nextOffset) {
1313
+ static unsigned int createBeta(ruleset *tree,
1314
+ char *rule,
1315
+ unsigned char gateType,
1316
+ unsigned short distinct,
1317
+ unsigned int nextOffset) {
1372
1318
  char *first;
1373
1319
  char *last;
1374
1320
  char *lastArrayValue;
1375
1321
  unsigned int hash;
1322
+ unsigned int previousOffset = 0;
1376
1323
  unsigned char type;
1377
- unsigned int result = readNextArrayValue(rule, &first, &lastArrayValue, &type);
1324
+ unsigned char nextGateType = GATE_AND;
1325
+
1326
+ unsigned int result = readNextArrayValue(rule,
1327
+ &first,
1328
+ &lastArrayValue,
1329
+ &type);
1378
1330
  while (result == PARSE_OK) {
1379
1331
  readNextName(first, &first, &last, &hash);
1380
1332
  unsigned int nameLength = last - first;
@@ -1384,6 +1336,7 @@ static unsigned int createBetaConnector(ruleset *tree,
1384
1336
  operator = OP_ALL;
1385
1337
  } else if (!strncmp("$any", last - 4, 4)) {
1386
1338
  operator = OP_ANY;
1339
+ nextGateType = GATE_OR;
1387
1340
  } else if (!strncmp("$not", last - 4, 4)) {
1388
1341
  operator = OP_NOT;
1389
1342
  }
@@ -1395,400 +1348,303 @@ static unsigned int createBetaConnector(ruleset *tree,
1395
1348
  }
1396
1349
 
1397
1350
  unsigned int stringOffset;
1398
- result = storeString(tree, first, &stringOffset, nameLength);
1399
- if (result != RULES_OK) {
1400
- return result;
1401
- }
1402
-
1403
- node *connector;
1404
- unsigned int connectorOffset;
1405
- result = storeNode(tree, &connector, &connectorOffset);
1406
- if (result != RULES_OK) {
1407
- return result;
1351
+ CHECK_RESULT(storeString(tree,
1352
+ first,
1353
+ &stringOffset,
1354
+ nameLength));
1355
+
1356
+ node *newBeta;
1357
+ unsigned int betaOffset;
1358
+ CHECK_RESULT(storeNode(tree,
1359
+ &newBeta,
1360
+ &betaOffset));
1361
+
1362
+ newBeta->nameOffset = stringOffset;
1363
+ newBeta->value.b.nextOffset = nextOffset;
1364
+ newBeta->value.b.aOffset = betaOffset;
1365
+ newBeta->value.b.bOffset = betaOffset;
1366
+ newBeta->value.b.gateType = gateType;
1367
+ newBeta->value.b.not = (operator == OP_NOT) ? 1: 0;
1368
+ newBeta->value.b.distinct = (distinct != 0) ? 1 : 0;
1369
+ newBeta->value.b.hash = hash;
1370
+
1371
+ if (operator != OP_ALL && operator != OP_ANY) {
1372
+ newBeta->value.b.index = tree->betaCount;
1373
+ ++tree->betaCount;
1374
+ newBeta->type = NODE_BETA;
1375
+ } else {
1376
+ newBeta->value.b.index = tree->connectorCount;
1377
+ ++tree->connectorCount;
1378
+ newBeta->type = NODE_CONNECTOR;
1408
1379
  }
1409
1380
 
1410
- expression *expr;
1411
- connector->nameOffset = stringOffset;
1412
- connector->type = NODE_BETA_CONNECTOR;
1413
- connector->value.b.nextOffset = nextOffset;
1414
- connector->value.b.not = (operator == OP_NOT) ? 1 : 0;
1415
- connector->value.b.hash = hash;
1416
- if (betaPath->expressionsLength == 0) {
1417
- betaPath->expressionsLength = 1;
1418
- betaPath->expressions = malloc(sizeof(expression));
1419
- if (!betaPath->expressions) {
1420
- return ERR_OUT_OF_MEMORY;
1421
- }
1422
-
1423
- expr = betaPath->expressions;
1424
- }
1425
- else {
1426
- betaPath->expressionsLength = betaPath->expressionsLength + 1;
1427
- betaPath->expressions = realloc(betaPath->expressions, betaPath->expressionsLength * sizeof(expression));
1428
- if (!betaPath->expressions) {
1429
- return ERR_OUT_OF_MEMORY;
1381
+ if (previousOffset == 0) {
1382
+ newBeta->value.b.isFirst = 1;
1383
+ } else {
1384
+ newBeta->value.b.isFirst = 0;
1385
+ tree->nodePool[previousOffset].value.b.nextOffset = betaOffset;
1386
+ if (newBeta->type == NODE_CONNECTOR) {
1387
+ // always set 'a' to previous beta in array
1388
+ newBeta->value.b.aOffset = previousOffset;
1430
1389
  }
1390
+ }
1431
1391
 
1432
- expr = &betaPath->expressions[betaPath->expressionsLength - 1];
1392
+ if (tree->nodePool[nextOffset].type == NODE_CONNECTOR) {
1393
+ // always set 'b' to last beta in array
1394
+ tree->nodePool[nextOffset].value.b.bOffset = betaOffset;
1433
1395
  }
1396
+
1397
+ previousOffset = betaOffset;
1434
1398
 
1435
- expr->nameOffset = stringOffset;
1436
- expr->aliasOffset = stringOffset;
1437
- expr->not = (operator == OP_NOT) ? 1 : 0;
1438
- expr->termsLength = 0;
1439
- expr->t.termsPointer = NULL;
1440
- expr->distinct = (distinct != 0) ? 1 : 0;
1399
+ newBeta->value.b.expressionSequence.nameOffset = stringOffset;
1400
+ newBeta->value.b.expressionSequence.aliasOffset = stringOffset;
1401
+ newBeta->value.b.expressionSequence.not = (operator == OP_NOT) ? 1 : 0;
1402
+ newBeta->value.b.expressionSequence.length = 0;
1441
1403
  if (operator == OP_NOP || operator == OP_NOT) {
1442
1404
  unsigned int resultOffset = NODE_M_OFFSET;
1443
- readNextValue(last, &first, &last, &type);
1444
- result = createAlpha(tree, first, expr, connectorOffset, &resultOffset);
1405
+ CHECK_PARSE_RESULT(readNextValue(last,
1406
+ &first,
1407
+ &last,
1408
+ &type));
1409
+ CHECK_RESULT(createAlpha(tree,
1410
+ first,
1411
+ betaOffset,
1412
+ betaOffset,
1413
+ &resultOffset));
1414
+
1445
1415
  }
1446
1416
  else {
1447
- readNextValue(last, &first, &last, &type);
1448
- result = createBeta(tree, first, operator, distinct, connectorOffset, betaPath, NULL);
1449
- }
1417
+ CHECK_PARSE_RESULT(readNextValue(last,
1418
+ &first,
1419
+ &last,
1420
+ &type));
1450
1421
 
1451
- if (result != RULES_OK) {
1452
- return result;
1422
+ CHECK_RESULT(createBeta(tree,
1423
+ first,
1424
+ nextGateType,
1425
+ distinct,
1426
+ betaOffset));
1453
1427
  }
1454
1428
 
1455
- result = readNextArrayValue(lastArrayValue, &first, &lastArrayValue, &type);
1429
+ result = readNextArrayValue(lastArrayValue,
1430
+ &first,
1431
+ &lastArrayValue,
1432
+ &type);
1456
1433
  }
1457
1434
 
1458
1435
  return (result == PARSE_END ? RULES_OK: result);
1459
1436
  }
1460
1437
 
1461
- static unsigned int createBeta(ruleset *tree,
1462
- char *rule,
1463
- unsigned char operator,
1464
- unsigned short distinct,
1465
- unsigned int nextOffset,
1466
- path *nextPath,
1467
- path **outPath) {
1468
-
1469
- path *betaPath = malloc(sizeof(path));
1470
- if (!betaPath) {
1471
- return ERR_OUT_OF_MEMORY;
1472
- }
1438
+ #ifdef _PRINT
1473
1439
 
1474
- betaPath->operator = operator;
1475
- betaPath->expressionsLength = 0;
1476
- betaPath->expressions = NULL;
1477
- betaPath->parents = NULL;
1478
- betaPath->parentsLength = 0;
1479
-
1480
- if (nextPath) {
1481
- if (nextPath->parentsLength == 0) {
1482
- nextPath->parentsLength = nextPath->expressionsLength;
1483
- nextPath->parents = calloc(nextPath->expressionsLength, sizeof(path*));
1484
- if (!nextPath->parents) {
1485
- return ERR_OUT_OF_MEMORY;
1440
+ static void printExpression(ruleset *tree, operand *newValue) {
1441
+ char *rightProperty;
1442
+ char *rightAlias;
1443
+ char *valueString;
1444
+ expression *newExpression;
1445
+ switch (newValue->type) {
1446
+ case JSON_IDENTIFIER:
1447
+ rightProperty = &tree->stringPool[newValue->value.id.propertyNameOffset];
1448
+ rightAlias = &tree->stringPool[newValue->value.id.nameOffset];
1449
+ printf("frame[\"%s\"][\"%s\"]", rightAlias, rightProperty);
1450
+ break;
1451
+ case JSON_MESSAGE_IDENTIFIER:
1452
+ rightProperty = &tree->stringPool[newValue->value.id.propertyNameOffset];
1453
+ printf("message[\"%s\"]", rightProperty);
1454
+ break;
1455
+ case JSON_MESSAGE_EXPRESSION:
1456
+ case JSON_EXPRESSION:
1457
+ newExpression = &tree->expressionPool[newValue->value.expressionOffset];
1458
+ printf("(");
1459
+ printExpression(tree, &newExpression->left);
1460
+ switch (newExpression->operator) {
1461
+ case OP_ADD:
1462
+ printf("+");
1463
+ break;
1464
+ case OP_SUB:
1465
+ printf("-");
1466
+ break;
1467
+ case OP_MUL:
1468
+ printf("*");
1469
+ break;
1470
+ case OP_DIV:
1471
+ printf("/");
1472
+ break;
1486
1473
  }
1487
-
1488
- nextPath->parents[nextPath->expressionsLength - 1] = betaPath;
1489
- } else {
1490
- int lengthDiff = nextPath->expressionsLength - nextPath->parentsLength;
1491
- nextPath->parents = realloc(nextPath->parents, sizeof(path*) * nextPath->expressionsLength);
1492
- if (!nextPath->parents) {
1493
- return ERR_OUT_OF_MEMORY;
1474
+ printExpression(tree, &newExpression->right);
1475
+ printf(")");
1476
+ break;
1477
+ case JSON_STRING:
1478
+ valueString = &tree->stringPool[newValue->value.stringOffset];
1479
+ printf("%s", valueString);
1480
+ break;
1481
+ case JSON_INT:
1482
+ printf("%ld", newValue->value.i);
1483
+ break;
1484
+ case JSON_DOUBLE:
1485
+ printf("%g", newValue->value.d);
1486
+ break;
1487
+ case JSON_BOOL:
1488
+ if (newValue->value.b == 0) {
1489
+ printf("false");
1494
1490
  }
1495
-
1496
- for (int i = 0; i < lengthDiff -1; ++i) {
1497
- nextPath->parents[nextPath->parentsLength + i] = NULL;
1491
+ else {
1492
+ printf("true");
1498
1493
  }
1499
-
1500
- nextPath->parentsLength = nextPath->expressionsLength;
1501
- nextPath->parents[nextPath->parentsLength - 1] = betaPath;
1502
- }
1503
- }
1504
-
1505
- if (outPath) {
1506
- *outPath = betaPath;
1507
- }
1508
-
1509
- return createBetaConnector(tree, rule, betaPath, distinct, nextOffset);
1510
- }
1511
-
1512
- static unsigned int add(any *right, any *left, any **result) {
1513
- right->nodes = realloc(right->nodes, (right->nodesLength + left->nodesLength) * sizeof(all*));
1514
- if (!right->nodes) {
1515
- return ERR_OUT_OF_MEMORY;
1516
- }
1517
- for (unsigned int i = 0; i < left->nodesLength; ++i) {
1518
- right->nodes[right->nodesLength + i] = left->nodes[i];
1494
+ break;
1519
1495
  }
1520
- right->nodesLength = right->nodesLength + left->nodesLength;
1521
- free(left->nodes);
1522
- free(left);
1523
- *result = right;
1524
- return RULES_OK;
1525
1496
  }
1526
1497
 
1527
- static unsigned int multiply(ruleset *tree, any *right, any *left, any **result) {
1528
- any *product = malloc(sizeof(any));
1529
- product->nodesLength = right->nodesLength * left->nodesLength;
1530
- product->nodes = malloc(sizeof(all*) * product->nodesLength);
1531
-
1532
- for (unsigned int i = 0; i < left->nodesLength; ++i) {
1533
- all *leftAll = left->nodes[i];
1534
- for (unsigned int ii = 0; ii < right->nodesLength; ++ii) {
1535
- all *rightAll = right->nodes[ii];
1536
- all *newAll = malloc(sizeof(all));
1537
- if (!newAll) {
1538
- return ERR_OUT_OF_MEMORY;
1539
- }
1540
-
1541
- newAll->count = (leftAll->count > rightAll->count ? leftAll->count: rightAll->count);
1542
- newAll->expressions = malloc((rightAll->expressionsLength + leftAll->expressionsLength) * sizeof(unsigned int));
1543
- if (!newAll->expressions) {
1544
- return ERR_OUT_OF_MEMORY;
1545
- }
1546
-
1547
- for (unsigned int iii = 0; iii < rightAll->expressionsLength; ++iii) {
1548
- newAll->expressions[iii] = rightAll->expressions[iii];
1549
- }
1550
-
1551
- for (unsigned int iii = 0; iii < leftAll->expressionsLength; ++iii) {
1552
- newAll->expressions[rightAll->expressionsLength + iii] = leftAll->expressions[iii];
1553
- }
1554
-
1555
- newAll->expressionsLength = rightAll->expressionsLength + leftAll->expressionsLength;
1556
- product->nodes[i + ii * left->nodesLength] = newAll;
1498
+ void printSimpleExpression(ruleset *tree, expression *currentExpression, unsigned char first, char *comp) {
1499
+ if (currentExpression->operator == OP_IALL || currentExpression->operator == OP_IANY) {
1500
+ char *leftProperty = &tree->stringPool[currentExpression->left.value.id.propertyNameOffset];
1501
+ printf("compare array: %s", leftProperty);
1502
+ } else {
1503
+ char *op = "";
1504
+ switch (currentExpression->operator) {
1505
+ case OP_LT:
1506
+ op = "<";
1507
+ break;
1508
+ case OP_LTE:
1509
+ op = "<=";
1510
+ break;
1511
+ case OP_GT:
1512
+ op = ">";
1513
+ break;
1514
+ case OP_GTE:
1515
+ op = ">=";
1516
+ break;
1517
+ case OP_EQ:
1518
+ op = "==";
1519
+ break;
1520
+ case OP_NEQ:
1521
+ op = "!=";
1522
+ break;
1523
+ case OP_NOP:
1524
+ case OP_NEX:
1525
+ case OP_EX:
1526
+ case OP_TYPE:
1527
+ case OP_MT:
1528
+ case OP_IMT:
1529
+ return;
1530
+ }
1531
+
1532
+ char *leftProperty = &tree->stringPool[currentExpression->left.value.id.propertyNameOffset];
1533
+ if (!first) {
1534
+ printf(" %s message[\"%s\"] %s ", comp, leftProperty, op);
1535
+ } else {
1536
+ printf("message[\"%s\"] %s ", leftProperty, op);
1557
1537
  }
1558
1538
 
1559
- free(leftAll->expressions);
1560
- free(leftAll);
1561
- }
1562
-
1563
- for (unsigned int i = 0; i < right->nodesLength; ++i) {
1564
- all *rightAll = right->nodes[i];
1565
- free(rightAll->expressions);
1566
- free(rightAll);
1539
+ printExpression(tree, &currentExpression->right);
1567
1540
  }
1568
1541
 
1569
- free(right->nodes);
1570
- free(right);
1571
- free(left->nodes);
1572
- free(left);
1573
- *result = product;
1574
- return RULES_OK;
1575
1542
  }
1576
1543
 
1577
- static unsigned int createSingleQuery(ruleset *tree,
1578
- char *name,
1579
- expression *expr,
1580
- any **resultAny) {
1581
- any *newAny = malloc(sizeof(any));
1582
- if (!newAny) {
1583
- return ERR_OUT_OF_MEMORY;
1584
- }
1585
-
1586
- newAny->nodesLength = 1;
1587
- newAny->nodes = malloc(sizeof(all*));
1588
- if (!newAny->nodes) {
1589
- return ERR_OUT_OF_MEMORY;
1590
- }
1591
-
1592
- newAny->nodes[0] = malloc(sizeof(all));
1593
- if (!newAny->nodes[0]) {
1594
- return ERR_OUT_OF_MEMORY;
1595
- }
1596
-
1597
- newAny->nodes[0]->expressionsLength = 1;
1598
- newAny->nodes[0]->expressions = malloc(sizeof(unsigned int));
1599
- if (!newAny->nodes[0]->expressions) {
1600
- return ERR_OUT_OF_MEMORY;
1601
- }
1602
-
1603
- expression *newExpression;
1604
- unsigned int result = storeExpression(tree, &newExpression, newAny->nodes[0]->expressions);
1605
- if (result != RULES_OK) {
1606
- return result;
1607
- }
1608
-
1609
- result = storeString(tree, name, &newExpression->nameOffset, strlen(name));
1610
- if (result != RULES_OK) {
1611
- return result;
1612
- }
1613
-
1614
- newExpression->aliasOffset = expr->aliasOffset;
1615
- newExpression->termsLength = expr->termsLength;
1616
- newExpression->not = expr->not;
1617
- newExpression->distinct = expr->distinct;
1618
- if (expr->termsLength) {
1619
- result = allocateNext(tree, expr->termsLength, &newExpression->t.termsOffset);
1620
- if (result != RULES_OK) {
1621
- return result;
1622
- }
1623
-
1624
- for (unsigned short i = 0; i < expr->termsLength; ++i) {
1625
- tree->nextPool[newExpression->t.termsOffset + i] = expr->t.termsPointer[i];
1626
- }
1627
-
1628
- free(expr->t.termsPointer);
1544
+ void printExpressionSequence(ruleset *tree, expressionSequence *exprs, int level) {
1545
+ for (int i = 0; i < level; ++ i) {
1546
+ printf(" ");
1629
1547
  }
1630
1548
 
1631
- *resultAny = newAny;
1632
- return RULES_OK;
1633
- }
1634
-
1635
- static unsigned int createQueries(ruleset *tree,
1636
- char *postfix,
1637
- path *betaPath,
1638
- any **anyResult) {
1639
- any *currentAny = NULL;
1640
- for (unsigned short i = 0; i < betaPath->expressionsLength; ++ i) {
1641
- unsigned int result;
1642
- expression *expr = &betaPath->expressions[i];
1643
- if (betaPath->operator == OP_NOP) {
1644
- result = createSingleQuery(tree, postfix, expr, anyResult);
1645
- if (result != RULES_OK) {
1646
- return result;
1647
- }
1648
-
1649
- free(betaPath->expressions);
1650
- free(betaPath);
1651
- return RULES_OK;
1652
- }
1653
-
1654
- char *name = &tree->stringPool[expr->nameOffset];
1655
- int nameLength = strlen(name);
1656
- #ifdef _WIN32
1657
- char *nextPostfix = (char *)_alloca(sizeof(char)*(strlen(postfix) + nameLength + 2));
1658
- #else
1659
- char nextPostfix[strlen(postfix) + nameLength + 2];
1660
- #endif
1661
- strcpy(nextPostfix, name);
1662
- nextPostfix[nameLength] = '!';
1663
- strcpy(&nextPostfix[nameLength + 1], postfix);
1664
-
1665
- any *newAny = NULL;
1666
- if (i >= betaPath->parentsLength || !betaPath->parents[i]) {
1667
- result = createSingleQuery(tree, nextPostfix, expr, &newAny);
1668
- if (result != RULES_OK) {
1669
- return result;
1670
- }
1671
- }
1672
- else {
1673
- result = createQueries(tree, nextPostfix, betaPath->parents[i], &newAny);
1674
- if (result != RULES_OK) {
1675
- return result;
1549
+ char *comp = NULL;
1550
+ char *compStack[32];
1551
+ unsigned char compTop = 0;
1552
+ unsigned char first = 1;
1553
+ for (unsigned short i = 0; i < exprs->length; ++i) {
1554
+ expression *currentExpression = &exprs->expressions[i];
1555
+ if (currentExpression->operator == OP_AND || currentExpression->operator == OP_OR) {
1556
+ if (first) {
1557
+ printf("(");
1558
+ } else {
1559
+ printf("%s (", comp);
1676
1560
  }
1677
- }
1561
+
1562
+ compStack[compTop] = comp;
1563
+ ++compTop;
1564
+ first = 1;
1678
1565
 
1679
- if (!currentAny) {
1680
- currentAny = newAny;
1681
- }
1682
- else if (betaPath->operator == OP_ANY) {
1683
- result = add(currentAny, newAny, &currentAny);
1684
- if (result != RULES_OK) {
1685
- return result;
1686
- }
1687
- }
1688
- else if (betaPath->operator == OP_ALL) {
1689
- result = multiply(tree, currentAny, newAny, &currentAny);
1690
- if (result != RULES_OK) {
1691
- return result;
1566
+ if (currentExpression->operator == OP_AND) {
1567
+ comp = "and";
1568
+ } else {
1569
+ comp = "or";
1692
1570
  }
1571
+
1572
+ } else if (currentExpression->operator == OP_END) {
1573
+ --compTop;
1574
+ comp = compStack[compTop];
1575
+ printf(")");
1576
+ } else {
1577
+ printSimpleExpression(tree, currentExpression, first, comp);
1578
+ first = 0;
1693
1579
  }
1694
1580
  }
1695
-
1696
- free(betaPath->expressions);
1697
- free(betaPath->parents);
1698
- free(betaPath);
1699
- *anyResult = currentAny;
1700
- return RULES_OK;
1701
- }
1702
-
1703
- static unsigned int fixupQueries(ruleset *tree, unsigned int actionOffset, char* postfix, path *betaPath) {
1704
- node *actionNode = &tree->nodePool[actionOffset];
1705
- #ifdef _WIN32
1706
- char *copyPostfix = (char *)_alloca(sizeof(char)*(strlen(postfix) + 1));
1707
- #else
1708
- char copyPostfix[strlen(postfix) + 1];
1709
- #endif
1710
-
1711
- strcpy(copyPostfix, postfix);
1712
-
1713
- any *query;
1714
- unsigned int result = createQueries(tree, copyPostfix, betaPath, &query);
1715
- if (result != RULES_OK) {
1716
- return result;
1717
- }
1718
-
1719
- actionNode->value.c.joinsLength = query->nodesLength;
1720
- result = allocateNext(tree, query->nodesLength, &actionNode->value.c.joinsOffset);
1721
- if (result != RULES_OK) {
1722
- return result;
1723
- }
1724
-
1725
- for (unsigned int i = 0; i < query->nodesLength; ++i) {
1726
- all *currentAll = query->nodes[i];
1727
-
1728
- join *newJoin;
1729
- result = storeJoin(tree, &newJoin, &tree->nextPool[actionNode->value.c.joinsOffset + i]);
1730
- if (result != RULES_OK) {
1731
- return result;
1732
- }
1733
-
1734
- newJoin->expressionsLength = currentAll->expressionsLength;
1735
- result = allocateNext(tree, currentAll->expressionsLength, &newJoin->expressionsOffset);
1736
- if (result != RULES_OK) {
1737
- return result;
1738
- }
1739
-
1740
- for (unsigned int ii = 0; ii < currentAll->expressionsLength; ++ii) {
1741
- tree->nextPool[newJoin->expressionsOffset + ii] = currentAll->expressions[ii];
1742
- }
1743
-
1744
- free(currentAll->expressions);
1745
- free(currentAll);
1746
- }
1747
-
1748
- free(query->nodes);
1749
- free(query);
1750
- return RULES_OK;
1581
+ printf("\n");
1751
1582
  }
1752
1583
 
1753
- #ifdef _PRINT
1754
-
1755
- static void printActionNode(ruleset *tree, node *actionNode, int level) {
1584
+ static void printActionNode(ruleset *tree, node *actionNode, int level, unsigned int offset) {
1756
1585
  for (int i = 0; i < level; ++ i) {
1757
1586
  printf(" ");
1758
1587
  }
1759
1588
 
1760
- printf("-> action: name %s, count %d, cap %d, priority %d\n",
1589
+ printf("-> action: name %s, count %d, cap %d, priority %d, index %d, offset %u\n",
1761
1590
  &tree->stringPool[actionNode->nameOffset],
1762
1591
  actionNode->value.c.count,
1763
1592
  actionNode->value.c.cap,
1764
- actionNode->value.c.priority);
1593
+ actionNode->value.c.priority,
1594
+ actionNode->value.c.index,
1595
+ offset);
1765
1596
  }
1766
1597
 
1767
- static void printBetaNode(ruleset *tree, node *betaNode, int level) {
1598
+ static void printBetaNode(ruleset *tree, node *betaNode, int level, unsigned int offset) {
1768
1599
  for (int i = 0; i < level; ++ i) {
1769
1600
  printf(" ");
1770
1601
  }
1771
1602
 
1772
- printf("-> beta: name %s, not %d\n", &tree->stringPool[betaNode->nameOffset], betaNode->value.b.not);
1603
+ if (betaNode->type == NODE_BETA) {
1604
+ printf("-> beta: ");
1605
+ } else {
1606
+ printf("-> connector: ");
1607
+ }
1608
+ printf("name %s, isFirst %d, not %d, distinct %d, gate %d, index %d, a %d, b %d, offset %u\n",
1609
+ &tree->stringPool[betaNode->nameOffset],
1610
+ betaNode->value.b.isFirst,
1611
+ betaNode->value.b.not,
1612
+ betaNode->value.b.distinct,
1613
+ betaNode->value.b.gateType,
1614
+ betaNode->value.b.index,
1615
+ betaNode->value.b.aOffset,
1616
+ betaNode->value.b.bOffset,
1617
+ offset);
1618
+ if (betaNode->value.b.expressionSequence.length != 0) {
1619
+ printExpressionSequence(tree, &betaNode->value.b.expressionSequence, level);
1620
+ }
1773
1621
  node *currentNode = &tree->nodePool[betaNode->value.b.nextOffset];
1774
1622
  if (currentNode->type == NODE_ACTION) {
1775
- printActionNode(tree, currentNode, level + 1);
1623
+ printActionNode(tree, currentNode, level + 1, betaNode->value.b.nextOffset);
1776
1624
  } else {
1777
- printBetaNode(tree, currentNode, level + 1);
1625
+ printBetaNode(tree, currentNode, level + 1, betaNode->value.b.nextOffset);
1778
1626
  }
1779
1627
  }
1780
1628
 
1781
- static void printAlphaNode(ruleset *tree, node *alphaNode, int level) {
1629
+ static void printAlphaNode(ruleset *tree, node *alphaNode, int level, unsigned int offset) {
1782
1630
  for (int i = 0; i < level; ++ i) {
1783
1631
  printf(" ");
1784
1632
  }
1785
1633
 
1786
- printf("-> alpha: name %s, operator %x\n", &tree->stringPool[alphaNode->nameOffset], alphaNode->value.a.operator);
1634
+ printf("-> alpha: name %s, offset %u\n", &tree->stringPool[alphaNode->nameOffset], offset);
1635
+
1636
+ for (int i = 0; i < level; ++ i) {
1637
+ printf(" ");
1638
+ }
1639
+
1640
+ printSimpleExpression(tree, &alphaNode->value.a.expression, 1, NULL);
1641
+
1642
+ printf("\n");
1787
1643
  if (alphaNode->value.a.nextOffset) {
1788
1644
  unsigned int *nextHashset = &tree->nextPool[alphaNode->value.a.nextOffset];
1789
1645
  for (unsigned int entry = 0; entry < NEXT_BUCKET_LENGTH; ++entry) {
1790
1646
  if (nextHashset[entry]) {
1791
- printAlphaNode(tree, &tree->nodePool[nextHashset[entry]], level + 1);
1647
+ printAlphaNode(tree, &tree->nodePool[nextHashset[entry]], level + 1, nextHashset[entry]);
1792
1648
  }
1793
1649
  }
1794
1650
  }
@@ -1796,14 +1652,14 @@ static void printAlphaNode(ruleset *tree, node *alphaNode, int level) {
1796
1652
  if (alphaNode->value.a.nextListOffset) {
1797
1653
  unsigned int *nextList = &tree->nextPool[alphaNode->value.a.nextListOffset];
1798
1654
  for (unsigned int entry = 0; nextList[entry] != 0; ++entry) {
1799
- printAlphaNode(tree, &tree->nodePool[nextList[entry]], level + 1);
1655
+ printAlphaNode(tree, &tree->nodePool[nextList[entry]], level + 1, nextList[entry]);
1800
1656
  }
1801
1657
  }
1802
1658
 
1803
1659
  if (alphaNode->value.a.betaListOffset) {
1804
1660
  unsigned int *betaList = &tree->nextPool[alphaNode->value.a.betaListOffset];
1805
1661
  for (unsigned int entry = 0; betaList[entry] != 0; ++entry) {
1806
- printBetaNode(tree, &tree->nodePool[betaList[entry]], level + 1);
1662
+ printBetaNode(tree, &tree->nodePool[betaList[entry]], level + 1, betaList[entry]);
1807
1663
  }
1808
1664
  }
1809
1665
  }
@@ -1818,89 +1674,125 @@ static unsigned int createTree(ruleset *tree, char *rules) {
1818
1674
  char *lastName;
1819
1675
  unsigned char type;
1820
1676
  unsigned int hash;
1821
- unsigned int result = readNextName(rules, &firstName, &lastName, &hash);
1677
+ unsigned int result = readNextName(rules,
1678
+ &firstName,
1679
+ &lastName,
1680
+ &hash);
1681
+ unsigned int ruleActions[MAX_ACTIONS];
1822
1682
  while (result == PARSE_OK) {
1823
- path *betaPath = NULL;
1824
1683
  node *ruleAction;
1825
1684
  unsigned int actionOffset;
1826
- result = storeNode(tree, &ruleAction, &actionOffset);
1827
- if (result != RULES_OK) {
1828
- return result;
1829
- }
1685
+ CHECK_RESULT(storeNode(tree,
1686
+ &ruleAction,
1687
+ &actionOffset));
1830
1688
 
1689
+ if (tree->actionCount == MAX_ACTIONS) {
1690
+ return ERR_RULE_LIMIT_EXCEEDED;
1691
+ }
1692
+
1831
1693
  ruleAction->value.c.index = tree->actionCount;
1694
+ ruleActions[tree->actionCount] = actionOffset;
1832
1695
  ++tree->actionCount;
1833
1696
  ruleAction->type = NODE_ACTION;
1834
-
1697
+
1835
1698
  // tree->stringPool can change after storing strings
1836
1699
  // need to resolve namespace every time it is used.
1837
- char *namespace = &tree->stringPool[tree->nameOffset];
1838
- int namespaceLength = strlen(namespace);
1839
- #ifdef _WIN32
1840
- char *runtimeActionName = (char *)_alloca(sizeof(char)*(namespaceLength + lastName - firstName + 2));
1841
- #else
1842
- char runtimeActionName[namespaceLength + lastName - firstName + 2];
1843
- #endif
1700
+ CHECK_RESULT(storeString(tree,
1701
+ firstName,
1702
+ &ruleAction->nameOffset,
1703
+ lastName - firstName));
1844
1704
 
1845
- strncpy(runtimeActionName, firstName, lastName - firstName);
1846
- runtimeActionName[lastName - firstName] = '!';
1847
- strncpy(&runtimeActionName[lastName - firstName + 1], namespace, namespaceLength);
1848
- runtimeActionName[namespaceLength + lastName - firstName + 1] = '\0';
1849
- result = storeString(tree, runtimeActionName, &ruleAction->nameOffset, namespaceLength + lastName - firstName + 1);
1850
- if (result != RULES_OK) {
1851
- return result;
1852
- }
1853
-
1854
- readNextValue(lastName, &first, &lastRuleValue, &type);
1705
+ CHECK_PARSE_RESULT(readNextValue(lastName,
1706
+ &first,
1707
+ &lastRuleValue,
1708
+ &type));
1855
1709
  ruleAction->value.c.priority = 0;
1856
1710
  ruleAction->value.c.count = 0;
1857
1711
  ruleAction->value.c.cap = 0;
1858
- getSetting(HASH_PRI, first, &ruleAction->value.c.priority);
1859
- getSetting(HASH_COUNT, first, &ruleAction->value.c.count);
1860
- getSetting(HASH_CAP, first, &ruleAction->value.c.cap);
1712
+ CHECK_RESULT(getSetting(HASH_PRI,
1713
+ first,
1714
+ &ruleAction->value.c.priority));
1715
+
1716
+ CHECK_RESULT(getSetting(HASH_COUNT,
1717
+ first,
1718
+ &ruleAction->value.c.count));
1719
+
1720
+ CHECK_RESULT(getSetting(HASH_CAP,
1721
+ first,
1722
+ &ruleAction->value.c.cap));
1723
+
1861
1724
  if (!ruleAction->value.c.count && !ruleAction->value.c.cap) {
1862
1725
  ruleAction->value.c.count = 1;
1863
1726
  }
1864
1727
 
1728
+ //Ensure action index is assigned based on priority
1729
+ unsigned int currentIndex = ruleAction->value.c.index;
1730
+ while (currentIndex) {
1731
+ node *currentAction = &tree->nodePool[ruleActions[currentIndex]];
1732
+ node *previousAction = &tree->nodePool[ruleActions[currentIndex - 1]];
1733
+ if (currentAction->value.c.priority >= previousAction->value.c.priority) {
1734
+ break;
1735
+ } else {
1736
+ unsigned int tempAction = ruleActions[currentIndex];
1737
+ ruleActions[currentIndex] = ruleActions[currentIndex - 1];
1738
+ previousAction->value.c.index = currentIndex;
1739
+
1740
+ --currentIndex;
1741
+
1742
+ ruleActions[currentIndex] = tempAction;
1743
+ currentAction->value.c.index = currentIndex;
1744
+ }
1745
+ }
1746
+
1865
1747
  unsigned short distinct = 1;
1866
- getSetting(HASH_DIST, first, &distinct);
1867
- result = readNextName(first, &first, &last, &hash);
1748
+ CHECK_RESULT(getSetting(HASH_DIST,
1749
+ first,
1750
+ &distinct));
1751
+
1752
+ result = readNextName(first,
1753
+ &first,
1754
+ &last,
1755
+ &hash);
1868
1756
  while (result == PARSE_OK) {
1869
- readNextValue(last, &first, &last, &type);
1870
- switch (hash) {
1871
- case HASH_ANY:
1872
- result = createBeta(tree, first, OP_ANY, distinct, actionOffset, NULL, &betaPath);
1873
- break;
1874
- case HASH_ALL:
1875
- result = createBeta(tree, first, OP_ALL, distinct, actionOffset, NULL, &betaPath);
1876
- break;
1877
- }
1878
- if (result != RULES_OK) {
1879
- return result;
1757
+ CHECK_PARSE_RESULT(readNextValue(last,
1758
+ &first,
1759
+ &last,
1760
+ &type));
1761
+ if (hash == HASH_ANY || hash == HASH_ALL) {
1762
+ CHECK_RESULT(createBeta(tree,
1763
+ first,
1764
+ (hash == HASH_ALL) ? GATE_AND : GATE_OR,
1765
+ distinct,
1766
+ actionOffset));
1880
1767
  }
1881
1768
 
1882
- result = readNextName(last, &first, &last, &hash);
1769
+ result = readNextName(last,
1770
+ &first,
1771
+ &last,
1772
+ &hash);
1883
1773
  }
1884
1774
 
1885
- result = fixupQueries(tree, actionOffset, runtimeActionName, betaPath);
1886
- if (result != RULES_OK) {
1887
- return result;
1888
- }
1889
-
1890
- result = readNextName(lastRuleValue, &firstName, &lastName, &hash);
1775
+ result = readNextName(lastRuleValue,
1776
+ &firstName,
1777
+ &lastName,
1778
+ &hash);
1891
1779
  }
1892
1780
 
1893
1781
  #ifdef _PRINT
1894
- printAlphaNode(tree, &tree->nodePool[NODE_M_OFFSET], 0);
1782
+ printAlphaNode(tree,
1783
+ &tree->nodePool[NODE_M_OFFSET],
1784
+ 0,
1785
+ NODE_M_OFFSET);
1895
1786
  #endif
1896
1787
 
1897
1788
  return RULES_OK;
1898
1789
  }
1899
1790
 
1900
- unsigned int createRuleset(unsigned int *handle, char *name, char *rules, unsigned int stateCaheSize) {
1791
+ unsigned int createRuleset(unsigned int *handle, char *name, char *rules) {
1901
1792
  INITIALIZE_ENTRIES;
1902
1793
 
1903
1794
  #ifdef _PRINT
1795
+ printf("%lu\n", sizeof(jsonObject));
1904
1796
  printf("%s\n", rules);
1905
1797
  #endif
1906
1798
 
@@ -1925,69 +1817,37 @@ unsigned int createRuleset(unsigned int *handle, char *name, char *rules, unsign
1925
1817
  tree->nextOffset = 0;
1926
1818
  tree->expressionPool = NULL;
1927
1819
  tree->expressionOffset = 0;
1928
- tree->idiomPool = NULL;
1929
- tree->idiomOffset = 0;
1930
- tree->joinPool = NULL;
1931
- tree->joinOffset = 0;
1932
1820
  tree->regexStateMachinePool = NULL;
1933
1821
  tree->regexStateMachineOffset = 0;
1822
+ tree->betaCount = 0;
1823
+ tree->connectorCount = 0;
1934
1824
  tree->actionCount = 0;
1935
1825
  tree->bindingsList = NULL;
1936
- tree->stateLength = 0;
1937
- tree->state = calloc(stateCaheSize, sizeof(stateEntry));
1938
- tree->maxStateLength = stateCaheSize;
1939
- tree->stateBucketsLength = stateCaheSize / 4;
1940
- tree->stateBuckets = malloc(tree->stateBucketsLength * sizeof(unsigned int));
1941
- memset(tree->stateBuckets, 0xFF, tree->stateBucketsLength * sizeof(unsigned int));
1942
- tree->lruStateOffset = UNDEFINED_HASH_OFFSET;
1943
- tree->mruStateOffset = UNDEFINED_HASH_OFFSET;
1944
-
1945
- result = storeString(tree, name, &tree->nameOffset, strlen(name));
1946
- if (result != RULES_OK) {
1947
- return result;
1948
- }
1949
-
1950
- result = storeString(tree, "m", &stringOffset, 1);
1951
- if (result != RULES_OK) {
1952
- return result;
1953
- }
1954
-
1955
- result = storeAlpha(tree, &newNode, &nodeOffset);
1956
- if (result != RULES_OK) {
1957
- return result;
1958
- }
1826
+ tree->currentStateIndex = 0;
1827
+ memset(tree->stateIndex, 0, MAX_STATE_INDEX_LENGTH * sizeof(unsigned int) * 2);
1828
+ memset(tree->reverseStateIndex, 0, MAX_STATE_INDEX_LENGTH * sizeof(unsigned int));
1829
+ initStatePool(tree);
1830
+
1831
+ CHECK_RESULT(storeString(tree,
1832
+ name,
1833
+ &tree->nameOffset,
1834
+ strlen(name)));
1835
+
1836
+ CHECK_RESULT(storeString(tree,
1837
+ "m",
1838
+ &stringOffset,
1839
+ 1));
1840
+
1841
+ CHECK_RESULT(storeAlpha(tree,
1842
+ &newNode,
1843
+ &nodeOffset));
1959
1844
 
1960
1845
  newNode->nameOffset = stringOffset;
1961
1846
  newNode->type = NODE_ALPHA;
1962
- newNode->value.a.operator = OP_TYPE;
1963
- result = storeAlpha(tree, &newNode, &tree->andNodeOffset);
1964
- if (result != RULES_OK) {
1965
- return result;
1966
- }
1967
-
1968
- newNode->nameOffset = 0;
1969
- newNode->type = NODE_ALPHA;
1970
- newNode->value.a.operator = OP_AND;
1971
- result = storeAlpha(tree, &newNode, &tree->orNodeOffset);
1972
- if (result != RULES_OK) {
1973
- return result;
1974
- }
1975
-
1976
- newNode->nameOffset = 0;
1977
- newNode->type = NODE_ALPHA;
1978
- newNode->value.a.operator = OP_OR;
1979
- result = storeAlpha(tree, &newNode, &tree->endNodeOffset);
1980
- if (result != RULES_OK) {
1981
- return result;
1982
- }
1983
-
1984
- newNode->nameOffset = 0;
1985
- newNode->type = NODE_ALPHA;
1986
- newNode->value.a.operator = OP_END;
1847
+ newNode->value.a.expression.operator = OP_TYPE;
1987
1848
 
1988
1849
  // will use random numbers for state stored event mids
1989
1850
  srand(time(NULL));
1990
-
1991
1851
  CREATE_HANDLE(tree, handle);
1992
1852
  return createTree(tree, rules);
1993
1853
  }
@@ -1996,88 +1856,11 @@ unsigned int deleteRuleset(unsigned int handle) {
1996
1856
  ruleset *tree;
1997
1857
  RESOLVE_HANDLE(handle, &tree);
1998
1858
 
1999
- deleteBindingsList(tree);
2000
1859
  free(tree->nodePool);
2001
1860
  free(tree->nextPool);
2002
1861
  free(tree->stringPool);
2003
1862
  free(tree->expressionPool);
2004
- free(tree->idiomPool);
2005
- free(tree->joinPool);
2006
- free(tree->stateBuckets);
2007
- for (unsigned int i = 0; i < tree->stateLength; ++i) {
2008
- stateEntry *entry = &tree->state[i];
2009
- if (entry->state) {
2010
- free(entry->state);
2011
- }
2012
-
2013
- if (entry->sid) {
2014
- free(entry->sid);
2015
- }
2016
- }
2017
- free(tree->state);
2018
- free(tree);
2019
- DELETE_HANDLE(handle);
2020
- return RULES_OK;
2021
- }
2022
-
2023
- unsigned int createClient(unsigned int *handle, char *name, unsigned int stateCaheSize) {
2024
- INITIALIZE_ENTRIES;
2025
-
2026
- ruleset *tree = malloc(sizeof(ruleset));
2027
- if (!tree) {
2028
- return ERR_OUT_OF_MEMORY;
2029
- }
2030
-
2031
- tree->stringPool = NULL;
2032
- tree->stringPoolLength = 0;
2033
- tree->nodePool = NULL;
2034
- tree->nodeOffset = 0;
2035
- tree->nextPool = NULL;
2036
- tree->nextOffset = 0;
2037
- tree->expressionPool = NULL;
2038
- tree->expressionOffset = 0;
2039
- tree->idiomPool = NULL;
2040
- tree->idiomOffset = 0;
2041
- tree->joinPool = NULL;
2042
- tree->joinOffset = 0;
2043
- tree->actionCount = 0;
2044
- tree->bindingsList = NULL;
2045
- tree->stateLength = 0;
2046
- tree->state = calloc(stateCaheSize, sizeof(stateEntry));
2047
- tree->maxStateLength = stateCaheSize;
2048
- tree->stateBucketsLength = stateCaheSize / 4;
2049
- tree->stateBuckets = malloc(tree->stateBucketsLength * sizeof(unsigned int));
2050
- memset(tree->stateBuckets, 0xFF, tree->stateBucketsLength * sizeof(unsigned int));
2051
- tree->lruStateOffset = UNDEFINED_HASH_OFFSET;
2052
- tree->mruStateOffset = UNDEFINED_HASH_OFFSET;
2053
-
2054
- unsigned int result = storeString(tree, name, &tree->nameOffset, strlen(name));
2055
- if (result != RULES_OK) {
2056
- return result;
2057
- }
2058
-
2059
- CREATE_HANDLE(tree, handle);
2060
- return RULES_OK;
2061
- }
2062
-
2063
- unsigned int deleteClient(unsigned int handle) {
2064
- ruleset *tree;
2065
- RESOLVE_HANDLE(handle, &tree);
2066
-
2067
- deleteBindingsList(tree);
2068
- free(tree->stringPool);
2069
- free(tree->stateBuckets);
2070
- for (unsigned int i = 0; i < tree->stateLength; ++i) {
2071
- stateEntry *entry = &tree->state[i];
2072
- if (entry->state) {
2073
- free(entry->state);
2074
- }
2075
-
2076
- if (entry->sid) {
2077
- free(entry->sid);
2078
- }
2079
- }
2080
-
1863
+ free(tree->statePool.content);
2081
1864
  free(tree);
2082
1865
  DELETE_HANDLE(handle);
2083
1866
  return RULES_OK;