batchers

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2023 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Batch

type Batch[T TypeConstraint] []T

Batch[T TypeConstraint] is a slice of elements used to define a processing batch. A Batch knows how to produce a clone of its elements.

func (Batch[T]) Clone

func (b Batch[T]) Clone() Batch[T]

Clone the elements of the batch.

func (Batch[T]) Empty

func (b Batch[T]) Empty() Batch[T]

Empty returns an empty batch.

func (Batch[T]) Len

func (b Batch[T]) Len() int

Len yields the number of elements in the batch.

type Executor

type Executor[T TypeConstraint] struct {
	// contains filtered or unexported fields
}

Executor runs an executor function over a batch of T.

The Executor handles the slicing of Push-ed input into batches of fixed size.

When the stream of input is complete, a call to Flush() executes the last (possibly incomplete) batch.

Example
// nolint: forbidigo
package main

import (
	"fmt"

	"github.com/fredbi/go-patterns/batchers"
)

type testItem struct {
	A int
}

func makeTestItems(n int) []testItem {
	fixtures := make([]testItem, n)

	for i := 0; i < n; i++ {
		fixtures[i] = testItem{
			A: i,
		}
	}

	return fixtures
}

func main() {
	// This example pushes a few test items into 2 batch executors.

	const n = 42

	batchExecutor := batchers.NewExecutor[testItem](10, func(in batchers.Batch[testItem]) {
		if len(in) == 0 {
			return
		}

		fmt.Printf("processing batch [%d items]: [%d-%d]\n", len(in), in[0].A, in[len(in)-1].A)
	})

	batchExecutorWithPointers := batchers.NewPointerExecutor[testItem](10, func(in batchers.Batch[*testItem]) {
		if len(in) == 0 {
			return
		}

		fmt.Printf("processing batch [%d pointer items]: [%d-%d]\n", len(in), in[0].A, in[len(in)-1].A)
	})

	for _, item := range makeTestItems(n) {
		batchExecutor.Push(item)
		// we actually clone immediately the value that is pointed to, so we may safely pass the iterated (constant) pointer here
		batchExecutorWithPointers.Push(&item) //nolint:gosec
	}

	batchExecutor.Flush()
	batchExecutorWithPointers.Flush()

}
Output:

processing batch [10 items]: [0-9]
processing batch [10 pointer items]: [0-9]
processing batch [10 items]: [10-19]
processing batch [10 pointer items]: [10-19]
processing batch [10 items]: [20-29]
processing batch [10 pointer items]: [20-29]
processing batch [10 items]: [30-39]
processing batch [10 pointer items]: [30-39]
processing batch [2 items]: [40-41]
processing batch [2 pointer items]: [40-41]

func NewExecutor

func NewExecutor[T TypeConstraint](batchSize int, executor func(Batch[T]), opts ...Option) *Executor[T]

func (Executor) Executed

func (e Executor) Executed() uint64

Executed yields the count of batch executions.

func (*Executor[T]) Flush

func (e *Executor[T]) Flush()

func (*Executor[T]) Push

func (e *Executor[T]) Push(in T)

type Option

type Option func(*options)

Option for an executor.

At this moment, no options are provided.

type PointerExecutor

type PointerExecutor[T TypeConstraint] struct {
	// contains filtered or unexported fields
}

PointerExecutor runs an executor function over a batch of pointers *T.

A shallow copy of *T is performed when assembling into a new batch.

Apart from the pointer logic, the PointerExecutor behaves like the Executor.

func NewPointerExecutor

func NewPointerExecutor[T TypeConstraint](batchSize int, executor func(Batch[*T]), opts ...Option) *PointerExecutor[T]

func (PointerExecutor) Executed

func (e PointerExecutor) Executed() uint64

Executed yields the count of batch executions.

func (*PointerExecutor[T]) Flush

func (e *PointerExecutor[T]) Flush()

func (*PointerExecutor[T]) Push

func (e *PointerExecutor[T]) Push(in *T)

type TypeConstraint

type TypeConstraint interface {
	any
}

TypeConstraint is any type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL