-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from lovoo/view-iterator
view iterator
- Loading branch information
Showing
10 changed files
with
177 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package storage | ||
|
||
type multiIterator struct { | ||
current int | ||
iters []Iterator | ||
} | ||
|
||
// NewMultiIterator returns an iterator that iterates over the given iterators. | ||
func NewMultiIterator(iters []Iterator) Iterator { | ||
if len(iters) == 0 { | ||
return &NullIter{} | ||
} | ||
|
||
return &multiIterator{current: 0, iters: iters} | ||
} | ||
|
||
func (m *multiIterator) Next() bool { | ||
next := m.iters[m.current].Next() | ||
if !next && len(m.iters)-1 > m.current { | ||
m.current++ | ||
next = m.iters[m.current].Next() | ||
} | ||
|
||
return next | ||
} | ||
|
||
func (m *multiIterator) Key() []byte { | ||
return m.iters[m.current].Key() | ||
} | ||
|
||
func (m *multiIterator) Value() (interface{}, error) { | ||
return m.iters[m.current].Value() | ||
} | ||
|
||
func (m *multiIterator) Release() { | ||
for i := range m.iters { | ||
m.iters[i].Release() | ||
} | ||
m.current = 0 | ||
m.iters = []Iterator{&NullIter{}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package storage | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/facebookgo/ensure" | ||
"github.com/lovoo/goka/codec" | ||
) | ||
|
||
func TestMultiIterator(t *testing.T) { | ||
numStorages := 3 | ||
numValues := 3 | ||
|
||
storages := make([]Storage, numStorages) | ||
expected := map[string]string{} | ||
|
||
for i := 0; i < numStorages; i++ { | ||
storages[i] = NewMemory(&codec.String{}) | ||
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, val) | ||
} | ||
} | ||
|
||
iters := make([]Iterator, len(storages)) | ||
for i := range storages { | ||
iter, err := storages[i].Iterator() | ||
ensure.Nil(t, err) | ||
iters[i] = iter | ||
} | ||
|
||
iter := NewMultiIterator(iters) | ||
count := 0 | ||
for iter.Next() { | ||
val, err := iter.Value() | ||
ensure.Nil(t, err) | ||
ensure.DeepEqual(t, expected[string(iter.Key())], val.(string)) | ||
count++ | ||
} | ||
|
||
ensure.DeepEqual(t, count, len(expected)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters