Skip to content

docs: align README id index key encoding - #191

Merged
pippolo84 merged 1 commit into
cilium:mainfrom
immanuwell:docs/readme-id-index-key-encoding
Aug 28, 2026
Merged

docs: align README id index key encoding#191
pippolo84 merged 1 commit into
cilium:mainfrom
immanuwell:docs/readme-id-index-key-encoding

Conversation

@immanuwell

@immanuwell immanuwell commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

In the first README example ID is uint32, but FromObject encodes with index.Uint64 while FromKey uses index.Uint32.
Stored key is 8 bytes, query key 4 bytes, they never match, so Get always misses and the Modify/Delete steps below it become no-ops.
index.Uint64(obj.ID) doesnt compile either

Fix: use index.Uint32 in FromObject so it matches FromKey and the field type

Repro:
insert &MyObject{ID: 1}, then tbl.Get(txn, IDIndex.Query(1)) returns found=false with index.Uint64, found=true with index.Uint32:

type MyObject struct {
	ID  uint32
	Foo string
}

var IDIndex = statedb.Index[*MyObject, uint32]{
	Name:       "id",
	FromObject: func(o *MyObject) index.KeySet { return index.NewKeySet(index.Uint64(uint64(o.ID))) },
	FromKey:    func(id uint32) index.Key { return index.Uint32(id) },
	Unique:     true,
}

// insert &MyObject{ID: 1}, commit, then:
_, _, found := tbl.Get(db.ReadTxn(), IDIndex.Query(1))
// found == false
// swap FromObject to index.Uint32(o.ID) and found == true

Signed-off-by: immanuwell <pchpr.00@list.ru>
@immanuwell
immanuwell requested a review from a team as a code owner August 28, 2026 06:11
@immanuwell
immanuwell requested review from pippolo84 and removed request for a team August 28, 2026 06:11

@pippolo84 pippolo84 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@pippolo84
pippolo84 merged commit 2b32a3a into cilium:main Aug 28, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants