digest

package
v0.10.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2019 License: Apache-2.0 Imports: 15 Imported by: 0

README

digest

This package consolidates all our digest algorithms used for data integrity into a single place. Adler32 is used and dependendent on the use case we rely on the standard library or a modified rolling hash version that can be stack allocated.

For highly concurrent callsites that require digests, they use the stack based adler32 library to avoid having to pool digest structs. The stack based adler32 library is a few percent slower than the standard library adler32 algorithm but heap allocation free.

For less concurrent callsites or callsites already under a mutex a cached digest struct can be kept and reused, these callsites use the standard library methods.

For callsites that do not need to incremental checksumming, meaning they only need to checksum a single byte slice, the static checksum method from the standard library is used.

Documentation

Overview

Package digest is a generated GoMock package.

Index

Constants

View Source
const (
	// DigestLenBytes is the length of generated digests in bytes.
	DigestLenBytes = 4
)

Variables

This section is empty.

Functions

func Checksum

func Checksum(buf []byte) uint32

Checksum returns the checksum for a buffer.

func NewDigest

func NewDigest() stackadler32.Digest

NewDigest creates a new digest. The default 32-bit hashing algorithm is adler32.

func SegmentChecksum

func SegmentChecksum(segment ts.Segment) uint32

SegmentChecksum returns the 32-bit checksum for a segment avoiding any allocations.

Types

type Buffer

type Buffer []byte

Buffer is a byte slice that facilitates digest reading and writing.

func NewBuffer

func NewBuffer() Buffer

NewBuffer creates a new digest buffer.

func ToBuffer

func ToBuffer(buf []byte) Buffer

ToBuffer converts a byte slice to a digest buffer.

func (Buffer) ReadDigest

func (b Buffer) ReadDigest() uint32

ReadDigest reads the digest from the buffer.

func (Buffer) ReadDigestFromFile

func (b Buffer) ReadDigestFromFile(fd *os.File) (uint32, error)

ReadDigestFromFile reads the digest from the file.

func (Buffer) WriteDigest

func (b Buffer) WriteDigest(digest uint32)

WriteDigest writes a digest to the buffer.

func (Buffer) WriteDigestToFile

func (b Buffer) WriteDigestToFile(fd *os.File, digest uint32) error

WriteDigestToFile writes a digest to the file.

type FdWithDigest

type FdWithDigest interface {
	xclose.Closer

	// Fd returns the file descriptor.
	Fd() *os.File

	// Digest returns the digest.
	Digest() hash.Hash32

	// Reset resets the file descriptor and the digest.
	Reset(fd *os.File)
}

FdWithDigest is a container for a file descriptor and the digest for the file contents.

type FdWithDigestContentsReader

type FdWithDigestContentsReader interface {
	FdWithDigestReader

	// ReadDigest reads a digest from the underlying file.
	ReadDigest() (uint32, error)
}

FdWithDigestContentsReader provides additional functionality of reading a digest from the underlying file.

func NewFdWithDigestContentsReader

func NewFdWithDigestContentsReader(bufferSize int) FdWithDigestContentsReader

NewFdWithDigestContentsReader creates a new FdWithDigestContentsReader.

type FdWithDigestContentsWriter

type FdWithDigestContentsWriter interface {
	FdWithDigestWriter

	// WriteDigests writes a list of digests to the underlying file.
	WriteDigests(digests ...uint32) error
}

FdWithDigestContentsWriter provides additional functionality of writing a digest to the underlying file.

func NewFdWithDigestContentsWriter

func NewFdWithDigestContentsWriter(bufferSize int) FdWithDigestContentsWriter

NewFdWithDigestContentsWriter creates a new FdWithDigestContentsWriter.

type FdWithDigestReader

type FdWithDigestReader interface {
	FdWithDigest
	io.Reader

	// ReadAllAndValidate reads everything in the underlying file and validates
	// it against the expected digest, returning an error if they don't match.
	// Note: the buffer "b" must be an exact match for how long the contents being
	// read is, the signature is structured this way to allow for buffer reuse.
	ReadAllAndValidate(b []byte, expectedDigest uint32) (int, error)

	// Validate compares the current digest against the expected digest and returns
	// an error if they don't match.
	Validate(expectedDigest uint32) error
}

FdWithDigestReader provides a buffered reader for reading from the underlying file.

func NewFdWithDigestReader

func NewFdWithDigestReader(bufferSize int) FdWithDigestReader

NewFdWithDigestReader creates a new FdWithDigestReader.

type FdWithDigestWriter

type FdWithDigestWriter interface {
	FdWithDigest
	io.Writer

	Flush() error
}

FdWithDigestWriter provides a buffered writer for writing to the underlying file.

func NewFdWithDigestWriter

func NewFdWithDigestWriter(bufferSize int) FdWithDigestWriter

NewFdWithDigestWriter creates a new FdWithDigestWriter.

type MockReaderWithDigest

type MockReaderWithDigest struct {
	// contains filtered or unexported fields
}

MockReaderWithDigest is a mock of ReaderWithDigest interface

func NewMockReaderWithDigest

func NewMockReaderWithDigest(ctrl *gomock.Controller) *MockReaderWithDigest

NewMockReaderWithDigest creates a new mock instance

func (*MockReaderWithDigest) Digest

func (m *MockReaderWithDigest) Digest() hash.Hash32

Digest mocks base method

func (*MockReaderWithDigest) EXPECT

EXPECT returns an object that allows the caller to indicate expected use

func (*MockReaderWithDigest) Read

func (m *MockReaderWithDigest) Read(arg0 []byte) (int, error)

Read mocks base method

func (*MockReaderWithDigest) Reset

func (m *MockReaderWithDigest) Reset(arg0 io.Reader)

Reset mocks base method

func (*MockReaderWithDigest) Validate

func (m *MockReaderWithDigest) Validate(arg0 uint32) error

Validate mocks base method

type MockReaderWithDigestMockRecorder

type MockReaderWithDigestMockRecorder struct {
	// contains filtered or unexported fields
}

MockReaderWithDigestMockRecorder is the mock recorder for MockReaderWithDigest

func (*MockReaderWithDigestMockRecorder) Digest

Digest indicates an expected call of Digest

func (*MockReaderWithDigestMockRecorder) Read

func (mr *MockReaderWithDigestMockRecorder) Read(arg0 interface{}) *gomock.Call

Read indicates an expected call of Read

func (*MockReaderWithDigestMockRecorder) Reset

func (mr *MockReaderWithDigestMockRecorder) Reset(arg0 interface{}) *gomock.Call

Reset indicates an expected call of Reset

func (*MockReaderWithDigestMockRecorder) Validate

func (mr *MockReaderWithDigestMockRecorder) Validate(arg0 interface{}) *gomock.Call

Validate indicates an expected call of Validate

type ReaderWithDigest

type ReaderWithDigest interface {
	io.Reader

	// Reset resets the reader for use with a new reader.
	Reset(reader io.Reader)

	// Digest returns the digest.
	Digest() hash.Hash32

	// Validate compares the current digest against the expected digest and returns
	// an error if they don't match.
	Validate(expectedDigest uint32) error
}

ReaderWithDigest is a reader that that calculates a digest as it is read.

func NewReaderWithDigest

func NewReaderWithDigest(reader io.Reader) ReaderWithDigest

NewReaderWithDigest creates a new reader that calculates a digest as it reads an input.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL