hashicorp / hashicorp/go-memdb
`CompoundMultiIndex` returns duplicate entries with `SingleIndexer`s
- Dominant language
- Go
- Stars
- 3.5k
- Forks
- 230
- PR merge metrics
- No merged PRs in 30d
Description
I'm not 100% sure this (mis)behaviour can be attributed to the combination of `CompoundMultiIndex` and single indexers actually, but I have a repro case which looks simple enough that it's hard to find any other cause there.
```go
&memdb.CompoundMultiIndex{
Indexes: []memdb.Indexer{
&memdb.StringFieldIndex{Field: "Address"},
&memdb.StringFieldIndex{Field: "Version"},
},
AllowMissing: true,
}
```
when a single entry is inserted into a table with such an index:
```go
txn.Insert("providers", &entry{
Address: "aws",
Version: "1.0.0",
})
```
and subsequently looked up (in a separate transaction)
```go
txn.Get("providers", "provider")
```
then the same entry is returned twice:
```
&entry{Address:"aws", Version:"1.0.0"}
&entry{Address:"aws", Version:"1.0.0"}
```
I can confirm it is the _exact_ same entry by comparing the pointer address, which consequently also makes such index impossible to use in `DeleteAll` where each individual entry would be looked up again via `id` index (as part of `Delete`) while iterating over results and (obviously) the second duplicate no longer exists once the first (original) entry is deleted, so `DeleteAll` then returns "not found" error.
https://github.com/hashicorp/go-memdb/blob/542a5804dedaaddf1a0cad8b6a7b0b3fcf438776/txn.go#L313-L329
Here is a full repro case:
- https://gist.github.com/radeksimko/44d205695c70428a7029b7326d917365
- https://play.golang.org/p/STVtJ4M-bcO
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with txn.go at lines 313-329 and reproduce the issue using the linked gist or Go Playground case. Trace the CompoundMultiIndex lookup with the two StringFieldIndex entries and the subsequent id-based Delete lookup. Done means a single inserted entry is returned once and DeleteAll no longer fails with a "not found" error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100