diff options
| author | mo khan <mo@mokhan.ca> | 2025-07-22 17:35:49 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-07-22 17:35:49 -0600 |
| commit | 20ef0d92694465ac86b550df139e8366a0a2b4fa (patch) | |
| tree | 3f14589e1ce6eb9306a3af31c3a1f9e1af5ed637 /vendor/github.com/authzed/spicedb/pkg/genutil/slicez | |
| parent | 44e0d272c040cdc53a98b9f1dc58ae7da67752e6 (diff) | |
feat: connect to spicedb
Diffstat (limited to 'vendor/github.com/authzed/spicedb/pkg/genutil/slicez')
| -rw-r--r-- | vendor/github.com/authzed/spicedb/pkg/genutil/slicez/chunking.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/vendor/github.com/authzed/spicedb/pkg/genutil/slicez/chunking.go b/vendor/github.com/authzed/spicedb/pkg/genutil/slicez/chunking.go new file mode 100644 index 0000000..bdff41c --- /dev/null +++ b/vendor/github.com/authzed/spicedb/pkg/genutil/slicez/chunking.go @@ -0,0 +1,44 @@ +package slicez + +import ( + "github.com/authzed/spicedb/internal/logging" +) + +// ForEachChunk executes the given handler for each chunk of items in the slice. +func ForEachChunk[T any](data []T, chunkSize uint16, handler func(items []T)) { + _, _ = ForEachChunkUntil(data, chunkSize, func(items []T) (bool, error) { + handler(items) + return true, nil + }) +} + +func ForEachChunkUntil[T any](data []T, chunkSize uint16, handler func(items []T) (bool, error)) (bool, error) { + if chunkSize == 0 { + logging.Warn().Int("invalid-chunk-size", int(chunkSize)).Msg("ForEachChunk got an invalid chunk size; defaulting to 100") + chunkSize = 100 + } + + dataLength := uint64(len(data)) + chunkSize64 := uint64(chunkSize) + chunkCount := (dataLength / chunkSize64) + 1 + for chunkIndex := uint64(0); chunkIndex < chunkCount; chunkIndex++ { + chunkStart := chunkIndex * chunkSize64 + chunkEnd := (chunkIndex + 1) * chunkSize64 + if chunkEnd > dataLength { + chunkEnd = dataLength + } + + chunk := data[chunkStart:chunkEnd] + if len(chunk) > 0 { + ok, err := handler(chunk) + if err != nil { + return false, err + } + if !ok { + return ok, nil + } + } + } + + return true, nil +} |
