Skip to content

Add range over func iterators to Bucket and Tx - #1302

Open
raju-mechatronics wants to merge 1 commit into
etcd-io:mainfrom
raju-mechatronics:iterators
Open

raju-mechatronics wants to merge 1 commit into
etcd-io:mainfrom
raju-mechatronics:iterators

Conversation

@raju-mechatronics

Copy link
Copy Markdown

This PR adds Go range-over-func iterators (iter.Seq2) as counterparts to the existing callback-based iteration APIs:

New method Yields Equivalent to
Bucket.All() k, v []byte Bucket.ForEach()
Bucket.Buckets() name []byte, *Bucket Bucket.ForEachBucket()
Tx.Buckets() name []byte, *Bucket Tx.ForEach()
db.View(func(tx *bolt.Tx) error {
	for name, b := range tx.Buckets() {
		for k, v := range b.All() {
			if bytes.Equal(k, stop) {
				break
			}
			fmt.Printf("%s: %s=%s\n", name, k, v)
		}
	}
	return nil
})

Motivation

  • Stopping early is a plain break or return instead of returning a sentinel error from the callback and filtering it out afterwards.
  • return, continue and labeled break work naturally inside the loop.
  • The module already requires Go 1.26, so the iter package is available.

The existing APIs are unchanged.

Testing

  • TestBucket_All: ordering and nil values for nested buckets, before and after commit.
  • TestBucket_Iterators_Empty, TestBucket_Iterators_Break: empty buckets and early break.
  • TestBucket_Buckets: only nested buckets are yielded, the yielded bucket is the cached instance, and writes through it persist after commit.
  • TestTx_Buckets: matches Tx.ForEach().
  • Runnable examples: ExampleBucket_All, ExampleBucket_Buckets, ExampleTx_Buckets.
  • Benchmark over 10k keys shows no regression, with the same allocations:
BenchmarkBucket_ForEach-24    65062 ns/op    480 B/op    8 allocs/op
BenchmarkBucket_All-24        51755 ns/op    480 B/op    8 allocs/op

README updated with a "Range over iterators" section and a Tx.Buckets() example

Signed-off-by: Raju Ahmed <rajuahmed1708@gmail.com>
@kubernetes-prow

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: raju-mechatronics
Once this PR has been reviewed and has the lgtm label, please assign serathius for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@raju-mechatronics

Copy link
Copy Markdown
Author

@ahrtr @Elbehery can you please take a look at this when you get a chance? I am ready to do changes if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

1 participant

⌂ 换网址