watchtower: add GetTower to tower iterator

Add a GetTower method to the tower iterator.
This commit is contained in:
Elle Mouton
2022-10-21 11:24:02 +02:00
parent e432261dab
commit 0ed5c750c8
3 changed files with 46 additions and 4 deletions

View File

@@ -29,6 +29,10 @@ type TowerCandidateIterator interface {
// candidates available as long as they remain in the set.
Reset() error
// GetTower gets the tower with the given ID from the iterator. If no
// such tower is found then ErrTowerNotInIterator is returned.
GetTower(id wtdb.TowerID) (*Tower, error)
// Next returns the next candidate tower. The iterator is not required
// to return results in any particular order. If no more candidates are
// available, ErrTowerCandidatesExhausted is returned.
@@ -76,6 +80,20 @@ func (t *towerListIterator) Reset() error {
return nil
}
// GetTower gets the tower with the given ID from the iterator. If no such tower
// is found then ErrTowerNotInIterator is returned.
func (t *towerListIterator) GetTower(id wtdb.TowerID) (*Tower, error) {
t.mu.Lock()
defer t.mu.Unlock()
tower, ok := t.candidates[id]
if !ok {
return nil, ErrTowerNotInIterator
}
return tower, nil
}
// Next returns the next candidate tower. This iterator will always return
// candidates in the order given when the iterator was instantiated. If no more
// candidates are available, ErrTowerCandidatesExhausted is returned.