Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix only first two iters checked #121

Merged
merged 5 commits into from
Apr 9, 2018
Merged

Conversation

j0hnsmith
Copy link
Contributor

Only the first two iterators are checked, this is a bug that only shows when there are more than 2 partitions.

@db7 db7 requested a review from SamiHiltunen April 5, 2018 16:40
@db7
Copy link
Collaborator

db7 commented Apr 5, 2018

Thanks for your PR! That is a great catch.

However it does not solve the issue completely. Next() should exhaust the current partition before going to the next partition (and only then change the current index). I tried something like: partition 0 with 2 elements, partition 1 with 0, partition 3 with 3 elements. We should see 5 values, but it was still not working. I think this would do the job.

func (m *multiIterator) Next() bool {
	next := m.iters[m.current].Next()
	for ; !next && m.current < len(m.iters); m.current++ {
		next = m.iters[m.current].Next()
	}
	m.current %= len(m.iters)
	return next
}

The modulo just guarantees that we don't panic out-of-bounds if Next() is called again after it has returned false.

@j0hnsmith
Copy link
Contributor Author

I did a slight refactor and forgot to remove a m.current++ so it was doing it twice (essentially skipping partitions).

The change above should fix it, I'm writing a test to confirm but it's failing because it seems that items aren't returned in insert order (for memory storage at least). Don't merge until this test is ready

@j0hnsmith
Copy link
Contributor Author

Test added, ready for review.

Items are returned in correct order, was confused by this not doing what was intended (I assume).

numStorages := 3
numValues := 3
storages := make([]Storage, numStorages)
expected := map[string]string{}
for i := 0; i < numStorages; i++ {
storages[i] = NewMemory()
for j := 0; j < numValues; j++ {
key := fmt.Sprintf("storage-%d", i)
val := fmt.Sprintf("value-%d", j)
expected[key] = val
storages[i].Set(key, []byte(val))
}
}

Only 3 keys in are in the storages, one per storage. I believe the intention was to create 3 keys per storage. On lines 22 & 23 existing keys are overwritten.

@db7
Copy link
Collaborator

db7 commented Apr 6, 2018 via email

if !next && len(m.iters)-1 > m.current {
m.current++
func (m *multiIterator) Next() (next bool) {
for ; m.current < len(m.iters); m.current++ {
next = m.iters[m.current].Next()
Copy link
Contributor Author

@j0hnsmith j0hnsmith Apr 6, 2018

Choose a reason for hiding this comment

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

I think you should first call Next() on the m.current partition.

That's exactly what it does do (m.current only gets incremented at the end of the iteration when next is false). I updated the test to have values only in storages 0 & 2, it still passes.

@db7
Copy link
Collaborator

db7 commented Apr 6, 2018 via email

@db7
Copy link
Collaborator

db7 commented Apr 9, 2018

I'll merge this. The test cases are nondeterministic, however, and fail often. That is because the iteration over maps is nondeterministic in go. I'll create a PR to fix that afterwards.

Thanks for your contribution.

@db7 db7 merged commit ee629ef into lovoo:master Apr 9, 2018
@j0hnsmith j0hnsmith deleted the multi_iter_bug branch April 9, 2018 11:26
This pull request was closed.
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