github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/forensics/storage/unique_iterator.go (about) 1 package storage 2 3 import ( 4 "bytes" 5 6 "github.com/hyperledger/burrow/storage" 7 ) 8 9 type uniqueIterator struct { 10 source storage.KVIterator 11 prevKey []byte 12 } 13 14 func Uniq(source storage.KVIterator) *uniqueIterator { 15 return &uniqueIterator{ 16 source: source, 17 } 18 } 19 20 func (ui *uniqueIterator) Domain() ([]byte, []byte) { 21 return ui.source.Domain() 22 } 23 24 func (ui *uniqueIterator) Valid() bool { 25 return ui.source.Valid() 26 } 27 28 func (ui *uniqueIterator) Next() { 29 ui.prevKey = ui.Key() 30 ui.source.Next() 31 // Skip elements with the same key a previous 32 for ui.source.Valid() && bytes.Equal(ui.Key(), ui.prevKey) { 33 ui.source.Next() 34 } 35 } 36 37 func (ui *uniqueIterator) Key() []byte { 38 return ui.source.Key() 39 } 40 41 func (ui *uniqueIterator) Value() []byte { 42 return ui.source.Value() 43 } 44 45 func (ui *uniqueIterator) Close() error { 46 return ui.source.Close() 47 } 48 49 func (ui *uniqueIterator) Error() error { 50 return ui.source.Error() 51 }