github.com/scottcagno/storage@v1.8.0/pkg/lsmt/binary/batch.go (about)

     1  package binary
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  )
     7  
     8  type Batch struct {
     9  	Entries []*Entry
    10  }
    11  
    12  func (b *Batch) String() string {
    13  	var ss string
    14  	for i := range b.Entries {
    15  		ss += fmt.Sprintf("b.Entries[%d].key=%q, value=%q\n", i, b.Entries[i].Key, b.Entries[i].Value)
    16  	}
    17  	return ss
    18  }
    19  
    20  func NewBatch() *Batch {
    21  	return &Batch{
    22  		Entries: make([]*Entry, 0),
    23  	}
    24  }
    25  
    26  func (b *Batch) Write(key string, value []byte) {
    27  	b.Entries = append(b.Entries, &Entry{Key: []byte(key), Value: value})
    28  }
    29  
    30  func (b *Batch) WriteEntry(e *Entry) {
    31  	b.Entries = append(b.Entries, e)
    32  }
    33  
    34  // Len [implementing sort interface]
    35  func (b *Batch) Len() int {
    36  	return len(b.Entries)
    37  }
    38  
    39  // Less [implementing sort interface]
    40  func (b *Batch) Less(i, j int) bool {
    41  	return bytes.Compare(b.Entries[i].Key, b.Entries[j].Key) == -1
    42  }
    43  
    44  // Swap [implementing sort interface]
    45  func (b *Batch) Swap(i, j int) {
    46  	b.Entries[i], b.Entries[j] = b.Entries[j], b.Entries[i]
    47  }