@@ -502,11 +502,20 @@ struct flb_mp_accessor *flb_mp_accessor_create(struct mk_list *slist_patterns)
502
502
return mpa ;
503
503
}
504
504
505
- static inline int accessor_key_find_match (struct flb_mp_accessor * mpa ,
506
- msgpack_object * key )
505
+ /**
506
+ * Finds matches for a given key in the list of record accessor patterns.
507
+ * Stores the indexes of the matches in the provided array.
508
+ *
509
+ * @return The number of matches found.
510
+ */
511
+ static inline int accessor_key_find_matches (struct flb_mp_accessor * mpa ,
512
+ msgpack_object * key ,
513
+ int * matched_indexes )
507
514
{
508
515
int i ;
509
516
int count ;
517
+ int match_count = 0 ;
518
+ int out_index = 0 ;
510
519
struct flb_mp_accessor_match * match ;
511
520
512
521
count = mk_list_size (& mpa -> ra_list );
@@ -517,14 +526,17 @@ static inline int accessor_key_find_match(struct flb_mp_accessor *mpa,
517
526
}
518
527
519
528
if (match -> start_key == key ) {
520
- return i ;
529
+ match_count ++ ;
530
+ matched_indexes [out_index ++ ] = i ;
521
531
}
522
532
}
523
533
524
- return -1 ;
534
+ return match_count ;
525
535
}
526
536
527
- static inline int accessor_sub_pack (struct flb_mp_accessor_match * match ,
537
+ static inline int accessor_sub_pack (struct flb_mp_accessor * mpa ,
538
+ int * matched_indexes ,
539
+ int match_count ,
528
540
msgpack_packer * mp_pck ,
529
541
msgpack_object * key ,
530
542
msgpack_object * val )
@@ -534,9 +546,13 @@ static inline int accessor_sub_pack(struct flb_mp_accessor_match *match,
534
546
msgpack_object * k ;
535
547
msgpack_object * v ;
536
548
struct flb_mp_map_header mh ;
549
+ struct flb_mp_accessor_match * match ;
537
550
538
- if (match -> key == key || match -> key == val ) {
539
- return FLB_FALSE ;
551
+ for (i = 0 ; i < match_count ; i ++ ) {
552
+ match = & mpa -> matches [matched_indexes [i ]];
553
+ if (match -> key == key || match -> key == val ) {
554
+ return FLB_FALSE ;
555
+ }
540
556
}
541
557
542
558
if (key ) {
@@ -549,7 +565,7 @@ static inline int accessor_sub_pack(struct flb_mp_accessor_match *match,
549
565
k = & val -> via .map .ptr [i ].key ;
550
566
v = & val -> via .map .ptr [i ].val ;
551
567
552
- ret = accessor_sub_pack (match , mp_pck , k , v );
568
+ ret = accessor_sub_pack (mpa , matched_indexes , match_count , mp_pck , k , v );
553
569
if (ret == FLB_TRUE ) {
554
570
flb_mp_map_header_append (& mh );
555
571
}
@@ -560,7 +576,7 @@ static inline int accessor_sub_pack(struct flb_mp_accessor_match *match,
560
576
flb_mp_array_header_init (& mh , mp_pck );
561
577
for (i = 0 ; i < val -> via .array .size ; i ++ ) {
562
578
v = & val -> via .array .ptr [i ];
563
- ret = accessor_sub_pack (match , mp_pck , NULL , v );
579
+ ret = accessor_sub_pack (mpa , matched_indexes , match_count , mp_pck , NULL , v );
564
580
if (ret == FLB_TRUE ) {
565
581
flb_mp_array_header_append (& mh );
566
582
}
@@ -587,6 +603,7 @@ int flb_mp_accessor_keys_remove(struct flb_mp_accessor *mpa,
587
603
int ret ;
588
604
int rule_id = 0 ;
589
605
int matches = 0 ;
606
+ int * matched_indexes ;
590
607
msgpack_object * key ;
591
608
msgpack_object * val ;
592
609
msgpack_object * s_key ;
@@ -641,6 +658,13 @@ int flb_mp_accessor_keys_remove(struct flb_mp_accessor *mpa,
641
658
/* Initialize map */
642
659
flb_mp_map_header_init (& mh , & mp_pck );
643
660
661
+ /* Initialize array of matching indexes to properly handle sibling keys */
662
+ matched_indexes = flb_malloc (sizeof (int ) * matches );
663
+ if (!matched_indexes ) {
664
+ flb_errno ();
665
+ return -1 ;
666
+ }
667
+
644
668
for (i = 0 ; i < map -> via .map .size ; i ++ ) {
645
669
key = & map -> via .map .ptr [i ].key ;
646
670
val = & map -> via .map .ptr [i ].val ;
@@ -649,30 +673,29 @@ int flb_mp_accessor_keys_remove(struct flb_mp_accessor *mpa,
649
673
* For every entry on the path, check if we should do a step-by-step
650
674
* repackaging or just pack the whole object.
651
675
*
652
- * Just check: does this 'key' exists on any path of the record
653
- * accessor patterns ?
654
- *
655
- * Find if the active key in the map, matches an accessor rule, if
656
- * if match we get the match id as return value, otherwise -1.
676
+ * Find all matching rules that match this 'key'. Return the number of matches or 0
677
+ * if no matches were found. Found matches are stored in the 'matched_indexes' array.
657
678
*/
658
- ret = accessor_key_find_match (mpa , key );
659
- if (ret == -1 ) {
679
+ ret = accessor_key_find_matches (mpa , key , matched_indexes );
680
+ if (ret == 0 ) {
660
681
/* No matches, it's ok to pack the kv pair */
661
682
flb_mp_map_header_append (& mh );
662
683
msgpack_pack_object (& mp_pck , * key );
663
684
msgpack_pack_object (& mp_pck , * val );
664
685
}
665
686
else {
666
687
/* The key has a match. Now we do a step-by-step packaging */
667
- match = & mpa -> matches [ ret ];
668
- ret = accessor_sub_pack (match , & mp_pck , key , val );
688
+
689
+ ret = accessor_sub_pack (mpa , matched_indexes , ret , & mp_pck , key , val );
669
690
if (ret == FLB_TRUE ) {
670
691
flb_mp_map_header_append (& mh );
671
692
}
672
693
}
673
694
}
674
695
flb_mp_map_header_end (& mh );
675
696
697
+ flb_free (matched_indexes );
698
+
676
699
* out_buf = mp_sbuf .data ;
677
700
* out_size = mp_sbuf .size ;
678
701
0 commit comments