1package memdb
2
3// FilterFunc is a function that takes the results of an iterator and returns
4// whether the result should be filtered out.
5type FilterFunc func(interface{}) bool
6
7// FilterIterator is used to wrap a ResultIterator and apply a filter over it.
8type FilterIterator struct {
9	// filter is the filter function applied over the base iterator.
10	filter FilterFunc
11
12	// iter is the iterator that is being wrapped.
13	iter ResultIterator
14}
15
16func NewFilterIterator(wrap ResultIterator, filter FilterFunc) *FilterIterator {
17	return &FilterIterator{
18		filter: filter,
19		iter:   wrap,
20	}
21}
22
23// WatchCh returns the watch channel of the wrapped iterator.
24func (f *FilterIterator) WatchCh() <-chan struct{} { return f.iter.WatchCh() }
25
26// Next returns the next non-filtered result from the wrapped iterator
27func (f *FilterIterator) Next() interface{} {
28	for {
29		if value := f.iter.Next(); value == nil || !f.filter(value) {
30			return value
31		}
32	}
33}
34