MCPcopy Create free account
hub / github.com/Permify/permify / subjectFilterIntersection

Function subjectFilterIntersection

internal/engines/subject_filter.go:735–811  ·  view source on GitHub ↗

subjectFilterIntersection function is used to find the intersection of subjects returned by executing multiple lookup subject functions concurrently.

(ctx context.Context, functions []SubjectFilterFunction, limit int)

Source from the content-addressed store, hash-verified

733// subjectFilterIntersection function is used to find the intersection of subjects
734// returned by executing multiple lookup subject functions concurrently.
735func subjectFilterIntersection(ctx context.Context, functions []SubjectFilterFunction, limit int) ([]string, error) {
736 // If there are no functions to be executed, return an empty response
737 if len(functions) == 0 {
738 return subjectFilterEmpty(), nil
739 }
740
741 // Create channels to handle asynchronous responses from lookup functions.
742 decisionChan := make(chan SubjectFilterResponse, len(functions))
743
744 // Create a cancellable context
745 cancelCtx, cancel := context.WithCancel(ctx)
746
747 // Run the functions concurrently
748 clean := subjectFiltersRun(cancelCtx, functions, decisionChan, limit)
749
750 // Ensure resources are cleaned up correctly after functions are done executing
751 defer func() {
752 cancel()
753 clean()
754 close(decisionChan)
755 }()
756
757 var commonIds []string
758 initialized := false
759 encounteredWildcard := false
760 var excludedIds []string
761
762 // For each function, collect results
763 for i := 0; i < len(functions); i++ {
764 select {
765 case d := <-decisionChan:
766 // If an error occurred in the function, return the error
767 if d.err != nil {
768 return subjectFilterEmpty(), d.err
769 }
770 // If "<>" is encountered, handle any exclusions that come with it
771 if containsWildcard(d.resp) {
772 encounteredWildcard = true
773 for _, id := range d.resp {
774 if id != ALL && !slices.Contains(excludedIds, id) {
775 excludedIds = append(excludedIds, id)
776 }
777 }
778 continue
779 }
780 // If it's the first function, initialize the common IDs list
781 if !initialized {
782 commonIds = append(commonIds, d.resp...)
783 initialized = true
784 } else {
785 // Intersect the current common IDs with the new set of IDs
786 commonIds = intersect(commonIds, d.resp)
787 }
788 case <-ctx.Done():
789 return subjectFilterEmpty(), errors.New(base.ErrorCode_ERROR_CODE_CANCELLED.String())
790 }
791 }
792

Callers

nothing calls this directly

Calls 6

subjectFilterEmptyFunction · 0.85
subjectFiltersRunFunction · 0.85
closeFunction · 0.85
containsWildcardFunction · 0.85
intersectFunction · 0.85
StringMethod · 0.65

Tested by

no test coverage detected