traversal

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: Apache-2.0, BSD-3-Clause, CC-BY-4.0, + 2 more Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package traversal 提供可由调用方重放的通用逻辑分页遍历。

本包只处理 execute 生命周期、分页计划和 opaque cursor,不负责账号选择、 网络策略、过滤语义或任何具体产品的错误映射。

Index

Constants

This section is empty.

Variables

View Source
var ErrExecuteNotConfigured = errors.New("traversal: execute function is not configured")

ErrExecuteNotConfigured 表示调用方没有提供可重放的 execute 函数。 产品 owner 可将它映射为自己的稳定错误类型。

View Source
var ErrStreamFactoryNotConfigured = errors.New("traversal: stream factory is not configured")

ErrStreamFactoryNotConfigured 表示调用方没有提供聚合流构造函数。

Functions

func TraverseWith

func TraverseWith[C any, T any](
	ctx context.Context,
	execute Execute[C],
	plan pagination.PagePlan,
	begin func() error,
	fetch func(context.Context, C, sdk.Cursor) ([]T, sdk.Cursor, error),
	consume func([]T) (committed bool, err error),
) (pagination.PageResult, error)

TraverseWith 在 execute 提供的重放边界内执行一次逻辑分页遍历。

begin 会在每次 execute 尝试开始时调用。consume 返回 committed=true 时, execute owner 可以据此禁止后续重放。分页引擎仍由 shared/pagination 负责 Skip、Limit、OneBatch、cursor 循环检测和 HasMore 语义。返回的 PageResult 携带引擎权威的 Returned 与 HasMore,包括上游批次内被 Limit 截断但上游 cursor 已为空的情形——此时仍有未返回的条目,HasMore 必须为 true。

func TraverseWithFrom added in v1.1.0

func TraverseWithFrom[C any, T any](
	ctx context.Context,
	execute Execute[C],
	plan pagination.PagePlan,
	initial sdk.Cursor,
	begin func() error,
	fetch func(context.Context, C, sdk.Cursor) ([]T, sdk.Cursor, error),
	consume func([]T) (committed bool, err error),
) (pagination.PageResult, error)

TraverseWithFrom 与 TraverseWith 相同,但从调用方已有的 opaque cursor 开始。 这保持 cursor 的具体语义在 shared pagination 内,不让调用方解码或重建它。

Types

type Execute

type Execute[C any] func(context.Context, func(context.Context, C) (committed bool, err error)) error

Execute 在调用方提供的可重放边界内使用一次资源。 committed=true 表示本次尝试已经越过交付或副作用边界。

type PagedReadResult

type PagedReadResult[T any] struct {
	Items   []T
	HasMore bool
}

PagedReadResult 是逻辑分页结果;opaque cursor 不离开 traversal。

func CollectWith

func CollectWith[C any, T any](
	ctx context.Context,
	execute Execute[C],
	plan pagination.PagePlan,
	fetch func(context.Context, C, sdk.Cursor) ([]T, sdk.Cursor, error),
) (PagedReadResult[T], error)

CollectWith 在 execute 提供的重放边界内收集逻辑分页结果。 每次新尝试开始时都会清空前一次未提交的结果,避免重放时混入不同资源 的数据;失败时返回当前尝试的结果和原始错误。

func CollectWithFrom added in v1.1.0

func CollectWithFrom[C any, T any](
	ctx context.Context,
	execute Execute[C],
	plan pagination.PagePlan,
	initial sdk.Cursor,
	fetch func(context.Context, C, sdk.Cursor) ([]T, sdk.Cursor, error),
) (PagedReadResult[T], error)

CollectWithFrom 与 CollectWith 相同,但从调用方已有的 opaque cursor 开始。

type StreamReadResult added in v1.1.0

type StreamReadResult[T any, C pagination.Cursor] struct {
	Items   []T
	State   pagination.StreamState[C]
	HasMore bool
}

StreamReadResult 是可重放聚合流的逻辑分页结果。State 由产品 owner 进一步编码为自己的 opaque cursor;traversal 不解释其中的 cursor。

func CollectStreamsWith added in v1.1.0

func CollectStreamsWith[E any, T any, C pagination.Cursor](ctx context.Context, execute Execute[E], plan pagination.PagePlan, streams func(E) []pagination.Stream[T, C]) (StreamReadResult[T, C], error)

CollectStreamsWith 从所有流的零 cursor 开始,在 execute 的可重放边界内 收集一个统一逻辑页。

func CollectStreamsWithFrom added in v1.1.0

func CollectStreamsWithFrom[E any, T any, C pagination.Cursor](ctx context.Context, execute Execute[E], plan pagination.PagePlan, initial pagination.StreamState[C], streams func(E) []pagination.Stream[T, C]) (StreamReadResult[T, C], error)

CollectStreamsWithFrom 与 CollectStreamsWith 相同,但从调用方已有的聚合 位置开始。每次 execute attempt 都从同一个 initial state 重新收集,失败 attempt 的部分结果不会混入安全重放的成功结果。

Jump to

Keyboard shortcuts

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