-
Notifications
You must be signed in to change notification settings - Fork 246
/
chunking.go
42 lines (37 loc) · 1.09 KB
/
chunking.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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 1")
chunkSize = 1
}
dataLength := uint16(len(data))
chunkCount := (dataLength / chunkSize) + 1
for chunkIndex := uint16(0); chunkIndex < chunkCount; chunkIndex++ {
chunkStart := chunkIndex * chunkSize
chunkEnd := (chunkIndex + 1) * chunkSize
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
}