github.com/flower-corp/rosedb@v1.1.2-0.20230117132829-21dc4f7b319a/util/murhash.go (about)

     1  package util
     2  
     3  import (
     4  	"encoding/binary"
     5  	"github.com/spaolacci/murmur3"
     6  	"io"
     7  )
     8  
     9  type Murmur128 struct {
    10  	mur murmur3.Hash128
    11  }
    12  
    13  func NewMurmur128() *Murmur128 {
    14  	return &Murmur128{mur: murmur3.New128()}
    15  }
    16  
    17  func (m *Murmur128) Write(p []byte) error {
    18  	n, err := m.mur.Write(p)
    19  	if n != len(p) {
    20  		return io.ErrShortWrite
    21  	}
    22  	return err
    23  }
    24  
    25  func (m *Murmur128) EncodeSum128() []byte {
    26  	buf := make([]byte, binary.MaxVarintLen64*2)
    27  	s1, s2 := m.mur.Sum128()
    28  	var index int
    29  	index += binary.PutUvarint(buf[index:], s1)
    30  	index += binary.PutUvarint(buf[index:], s2)
    31  	return buf[:index]
    32  }
    33  
    34  func (m *Murmur128) Reset() {
    35  	m.mur.Reset()
    36  }