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

     1  package graph
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	core "github.com/authzed/spicedb/pkg/proto/core/v1"
     8  
     9  	"github.com/authzed/spicedb/pkg/datastore"
    10  	v1 "github.com/authzed/spicedb/pkg/proto/dispatch/v1"
    11  )
    12  
    13  // Ellipsis relation is used to signify a semantic-free relationship.
    14  const Ellipsis = "..."
    15  
    16  // maxDispatchChunkSize is the maximum size for a dispatch chunk. Must be less than or equal
    17  // to the maximum ID count for filters in the datastore.
    18  var maxDispatchChunkSize uint16 = datastore.FilterMaximumIDCount
    19  
    20  // progressiveDispatchChunkSizes are chunk sizes growing over time for dispatching. All entries
    21  // must be less than or equal to the maximum ID count for filters in the datastore.
    22  var progressiveDispatchChunkSizes = []uint16{5, 10, 25, 50, maxDispatchChunkSize}
    23  
    24  // SetDispatchChunkSizesForTesting sets the dispatch chunk sizes for testing.
    25  func SetDispatchChunkSizesForTesting(t *testing.T, sizes []uint16) {
    26  	originalSizes := progressiveDispatchChunkSizes
    27  	maxDispatchChunkSize = sizes[len(sizes)-1]
    28  	progressiveDispatchChunkSizes = sizes
    29  	t.Cleanup(func() {
    30  		progressiveDispatchChunkSizes = originalSizes
    31  		maxDispatchChunkSize = originalSizes[len(sizes)-1]
    32  	})
    33  }
    34  
    35  // CheckResult is the data that is returned by a single check or sub-check.
    36  type CheckResult struct {
    37  	Resp *v1.DispatchCheckResponse
    38  	Err  error
    39  }
    40  
    41  // ExpandResult is the data that is returned by a single expand or sub-expand.
    42  type ExpandResult struct {
    43  	Resp *v1.DispatchExpandResponse
    44  	Err  error
    45  }
    46  
    47  // ReduceableExpandFunc is a function that can be bound to a execution context.
    48  type ReduceableExpandFunc func(ctx context.Context, resultChan chan<- ExpandResult)
    49  
    50  // AlwaysFailExpand is a ReduceableExpandFunc which will always fail when reduced.
    51  func AlwaysFailExpand(_ context.Context, resultChan chan<- ExpandResult) {
    52  	resultChan <- expandResultError(NewAlwaysFailErr(), emptyMetadata)
    53  }
    54  
    55  // ExpandReducer is a type for the functions Any and All which combine check results.
    56  type ExpandReducer func(
    57  	ctx context.Context,
    58  	start *core.ObjectAndRelation,
    59  	requests []ReduceableExpandFunc,
    60  ) ExpandResult
    61  
    62  func decrementDepth(md *v1.ResolverMeta) *v1.ResolverMeta {
    63  	return &v1.ResolverMeta{
    64  		AtRevision:     md.AtRevision,
    65  		DepthRemaining: md.DepthRemaining - 1,
    66  		TraversalBloom: md.TraversalBloom,
    67  	}
    68  }
    69  
    70  var emptyMetadata = &v1.ResponseMeta{}
    71  
    72  func ensureMetadata(subProblemMetadata *v1.ResponseMeta) *v1.ResponseMeta {
    73  	if subProblemMetadata == nil {
    74  		subProblemMetadata = emptyMetadata
    75  	}
    76  
    77  	return &v1.ResponseMeta{
    78  		DispatchCount:       subProblemMetadata.DispatchCount,
    79  		DepthRequired:       subProblemMetadata.DepthRequired,
    80  		CachedDispatchCount: subProblemMetadata.CachedDispatchCount,
    81  		DebugInfo:           subProblemMetadata.DebugInfo,
    82  	}
    83  }
    84  
    85  func addCallToResponseMetadata(metadata *v1.ResponseMeta) *v1.ResponseMeta {
    86  	// + 1 for the current call.
    87  	return &v1.ResponseMeta{
    88  		DispatchCount:       metadata.DispatchCount + 1,
    89  		DepthRequired:       metadata.DepthRequired + 1,
    90  		CachedDispatchCount: metadata.CachedDispatchCount,
    91  		DebugInfo:           metadata.DebugInfo,
    92  	}
    93  }
    94  
    95  func addAdditionalDepthRequired(metadata *v1.ResponseMeta) *v1.ResponseMeta {
    96  	return &v1.ResponseMeta{
    97  		DispatchCount:       metadata.DispatchCount,
    98  		DepthRequired:       metadata.DepthRequired + 1,
    99  		CachedDispatchCount: metadata.CachedDispatchCount,
   100  		DebugInfo:           metadata.DebugInfo,
   101  	}
   102  }