github.com/eris-ltd/erisdb@v0.25.0/storage/unique_iterator.go (about)

     1  package storage
     2  
     3  import "bytes"
     4  
     5  type uniqueIterator struct {
     6  	source  KVIterator
     7  	prevKey []byte
     8  }
     9  
    10  func Uniq(source KVIterator) *uniqueIterator {
    11  	return &uniqueIterator{
    12  		source: source,
    13  	}
    14  }
    15  
    16  func (ui *uniqueIterator) Domain() ([]byte, []byte) {
    17  	return ui.source.Domain()
    18  }
    19  
    20  func (ui *uniqueIterator) Valid() bool {
    21  	return ui.source.Valid()
    22  }
    23  
    24  func (ui *uniqueIterator) Next() {
    25  	ui.prevKey = ui.Key()
    26  	ui.source.Next()
    27  	// Skip elements with the same key a previous
    28  	for ui.source.Valid() && bytes.Equal(ui.Key(), ui.prevKey) {
    29  		ui.source.Next()
    30  	}
    31  }
    32  
    33  func (ui *uniqueIterator) Key() []byte {
    34  	return ui.source.Key()
    35  }
    36  
    37  func (ui *uniqueIterator) Value() []byte {
    38  	return ui.source.Value()
    39  }
    40  
    41  func (ui *uniqueIterator) Close() {
    42  	ui.source.Close()
    43  }