github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/services/v1/grouping.go (about)

     1  package v1
     2  
     3  import (
     4  	"context"
     5  
     6  	v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
     7  
     8  	"github.com/authzed/spicedb/internal/graph/computed"
     9  	"github.com/authzed/spicedb/pkg/datastore"
    10  	core "github.com/authzed/spicedb/pkg/proto/core/v1"
    11  )
    12  
    13  var MaxBulkCheckDispatchChunkSize = datastore.FilterMaximumIDCount
    14  
    15  type groupedCheckParameters struct {
    16  	params      *computed.CheckParameters
    17  	resourceIDs []string
    18  }
    19  
    20  type groupingParameters struct {
    21  	atRevision           datastore.Revision
    22  	maximumAPIDepth      uint32
    23  	maxCaveatContextSize int
    24  }
    25  
    26  // groupItems takes a slice of CheckBulkPermissionsRequestItem and groups them based
    27  // on using the same permission, subject type, subject id, and caveat.
    28  func groupItems(ctx context.Context, params groupingParameters, items []*v1.CheckBulkPermissionsRequestItem) (map[string]*groupedCheckParameters, error) {
    29  	res := make(map[string]*groupedCheckParameters)
    30  
    31  	for _, item := range items {
    32  		hash, err := computeCheckBulkPermissionsItemHashWithoutResourceID(item)
    33  		if err != nil {
    34  			return nil, err
    35  		}
    36  
    37  		if _, ok := res[hash]; !ok {
    38  			caveatContext, err := GetCaveatContext(ctx, item.Context, params.maxCaveatContextSize)
    39  			if err != nil {
    40  				return nil, err
    41  			}
    42  
    43  			res[hash] = &groupedCheckParameters{
    44  				params:      checkParametersFromCheckBulkPermissionsRequestItem(item, params, caveatContext),
    45  				resourceIDs: []string{item.Resource.ObjectId},
    46  			}
    47  		} else {
    48  			res[hash].resourceIDs = append(res[hash].resourceIDs, item.Resource.ObjectId)
    49  		}
    50  	}
    51  
    52  	return res, nil
    53  }
    54  
    55  func checkParametersFromCheckBulkPermissionsRequestItem(
    56  	bc *v1.CheckBulkPermissionsRequestItem,
    57  	params groupingParameters,
    58  	caveatContext map[string]any,
    59  ) *computed.CheckParameters {
    60  	return &computed.CheckParameters{
    61  		ResourceType: &core.RelationReference{
    62  			Namespace: bc.Resource.ObjectType,
    63  			Relation:  bc.Permission,
    64  		},
    65  		Subject: &core.ObjectAndRelation{
    66  			Namespace: bc.Subject.Object.ObjectType,
    67  			ObjectId:  bc.Subject.Object.ObjectId,
    68  			Relation:  normalizeSubjectRelation(bc.Subject),
    69  		},
    70  		CaveatContext: caveatContext,
    71  		AtRevision:    params.atRevision,
    72  		MaximumDepth:  params.maximumAPIDepth,
    73  		DebugOption:   computed.NoDebugging,
    74  	}
    75  }