record

package
v0.2.15 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: MIT Imports: 11 Imported by: 10

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CommonValue

func CommonValue(x interface{}, y interface{}) (interface{}, error)

CommonValue returns a (zero-valued) value z that covers the types of x and y, where x, y, z are valid Record types. For example, if x is an int16 and y is an int32, then z will be an int32; if x is a bool and y is a string, then z will be a string.

func Convert

func Convert(r Record, template Record) error

Convert deletes any keys from r not in template, and converts the values in r to types matching those in template.

func ConvertSkipUnknown

func ConvertSkipUnknown(r Record, template Record) error

ConvertSkipUnknown converts the values in r to types matching those in template. Any key in r that is not in template is ignored.

func IsKey

func IsKey(k string) bool

IsKey returns true if and only if the string k has the format required for a key. Namely, k matches [a-zA-Z]+[a-zA-Z0-9_]*.

func IsValue

func IsValue(x interface{}) (ok bool)

IsValue returns true if and only if the type of x is one of those specified in the documentation for Record.

func Scan

func Scan(dest Record, src Record)

Scan copies "src" into "dest". Any previously set keys or values in "dest" will be deleted or overwritten.

Types

type BufferedIterator

type BufferedIterator interface {
	Iterator
	// Buffered returns true iff the next call to Next or NextContext is
	// guaranteed not to block.
	Buffered() bool
}

BufferedIterator described an Iterator with buffer.

func NewBufferedIterator

func NewBufferedIterator(itr Iterator, bufSize int) BufferedIterator

NewBufferedIterator returns an iterator wrapping 'itr' that buffers the records. Calling Close on the returned iterator will Close the wrapped iterator 'itr'.

type ConversionFunc

type ConversionFunc[T any] func(x T) (Record, error)

ConversionFunc returns a record for the given object x.

type CountIterator

type CountIterator struct {
	Iterator
	// contains filtered or unexported fields
}

CountIterator wraps an Iterator, counting the number of entries iterated over.

func NewCountIterator

func NewCountIterator(itr Iterator) *CountIterator

NewCountIterator returns an iterator that will count the number of entries iterated over. Calling Close on the returned iterator will Close the underlying iterator.

func (*CountIterator) Count

func (s *CountIterator) Count() int64

Count returns the number of entries iterated over. This is safe for concurrent access.

func (*CountIterator) Next

func (s *CountIterator) Next() bool

Next advances the iterator. Returns true on successful advance of the iterator; false otherwise. Next or NextContext must be called before the first call to Scan.

func (*CountIterator) NextContext

func (s *CountIterator) NextContext(ctx context.Context) (bool, error)

NextContext advances the iterator. Returns true on successful advance of the iterator; false otherwise. Next or NextContext must be called before the first call to Scan.

type DurationIterator added in v0.2.8

type DurationIterator struct {
	Iterator
	// contains filtered or unexported fields
}

DurationIterator wraps an Iterator, limiting the total duration over which Records are returned.

func NewDurationIterator added in v0.2.8

func NewDurationIterator(itr Iterator, d time.Duration) *DurationIterator

NewDurationIterator returns an iterator that will return records from itr until either itr is exhausted, or duration d has passed since the iterator was created. Calling Close on the returned iterator will Close the underlying iterator.

func (*DurationIterator) Close added in v0.2.8

func (s *DurationIterator) Close() error

Close closes the underlying iterator and prevents future iteration.

func (*DurationIterator) Next added in v0.2.8

func (s *DurationIterator) Next() bool

Next advances the iterator. Returns true on successful advance of the iterator; false otherwise. Next or NextContext must be called before the first call to Scan.

func (*DurationIterator) NextContext added in v0.2.8

func (s *DurationIterator) NextContext(ctx context.Context) (bool, error)

NextContext advances the iterator. Returns true on successful advance of the iterator; false otherwise. Next or NextContext must be called before the first call to Scan.

func (*DurationIterator) Scan added in v0.2.8

func (s *DurationIterator) Scan(dest Record) error

Scan copies the current record into "dest". Any previously set keys or values in "dest" will be deleted or overwritten.

func (*DurationIterator) Stop added in v0.2.8

func (s *DurationIterator) Stop()

Stop releases resources used by the duration iterator and prevents further iteration, but does not close the underlying iterator.

type Iterator

type Iterator interface {
	// Close closes the iterator, preventing further iteration.
	Close() error
	// Err returns the last error, if any, encountered during iteration. Err
	// may be called after Close.
	Err() error
	// Next advances the iterator. Returns true on successful advance of the
	// iterator; false otherwise. Next or NextContext must be called
	// before the first call to Scan.
	Next() bool
	// NextContext advances the iterator. Returns true on successful
	// advance of the iterator; false otherwise. Next or NextContext
	// must be called before the first call to Scan.
	NextContext(ctx context.Context) (bool, error)
	// Scan copies the current record into "dest". Any previously set keys
	// or values in "dest" will be deleted or overwritten.
	Scan(dest Record) error
}

Iterator defines the interface satisfied by a Record iterator.

func EmptyIterator

func EmptyIterator() Iterator

EmptyIterator returns an empty iterator. Err and Close will always return nil.

func LimitIterator

func LimitIterator(itr Iterator, N int64) Iterator

LimitIterator returns an iterator that will return at most N records. Calling Close on the returned iterator will Close the underlying iterator.

func LimitSizeIterator added in v0.2.8

func LimitSizeIterator(itr Iterator, N int) Iterator

LimitSizeIterator returns an iterator that will return records from itr until either itr is exhausted, or the total size of the returned records exceeds N bytes. Calling Close on the returned iterator will Close the underlying iterator.

func NewIterator

func NewIterator(resC <-chan Record, errC <-chan error, doneC chan<- struct{}) Iterator

NewIterator returns an iterator that reads from the channel resC. Iteration will halt when resC closes. Any error in errC will be returned to the user. Iteration should stop if doneC is closed. A typical implementation looks like:

// Create the communication channels
resC := make(chan record.Record) // Buffer this channel if required
errC := make(chan error)
doneC := make(chan struct{})
// Start the background worker running
go func() {
	// Feed resC with records
	var err error
	for {
		r := record.Record{}
		// ...
		// Handle any errors
		if err != nil {
			break
		}
		// Pass the record down the channel
		select {
		case <-doneC:
			break // We've been asked to exit
		case resC <- r:
		}
	}
	// Iteration has finished, so tidy up
	close(resC)
	if err != nil {
		errC <- err
	}
	close(errC)
}()
// Create the iterator
itr := record.NewIterator(resC, errC, doneC)

func SliceIterator

func SliceIterator(S []Record) Iterator

SliceIterator returns an iterator for the given slice. There is no need to call Close on the returned iterator. Err and Close will always return nil.

func SliceIteratorWithConversionFunc

func SliceIteratorWithConversionFunc[T any](S []T, f ConversionFunc[T]) Iterator

SliceIteratorWithConversionFunc returns an iterator for the slice S, where the function f is used to convert the elements in S to records.

type Record

type Record map[string]interface{}

Record is the type defining a record in a table. The keys of the map are the keys of the record, and the values of the map are the values of the record. The values must take one of the following types:

int, int8, int16, int32, int64
uint, uint8, uint16, uint32, uint64
float64
bool
string
[]byte

func IteratorToSliceMaxLen

func IteratorToSliceMaxLen(ctx context.Context, itr Iterator, n int) ([]Record, error)

IteratorToSliceMaxLen reads from itr attempting to populate a slice of maximum length n. Returns the slice created, along with any errors.

func (Record) Copy

func (r Record) Copy() Record

Copy returns a copy of the record.

func (*Record) GobDecode

func (r *Record) GobDecode(b []byte) error

GobDecode overwrites the receiver, which must be a pointer, with the value represented by the byte slice, which was written by GobEncode.

func (Record) GobEncode

func (r Record) GobEncode() ([]byte, error)

GobEncode returns a byte slice representing the encoding of the record.

func (Record) IsValid

func (r Record) IsValid() (bool, error)

IsValid returns true if and only if the keys in the record satisfy IsKey, and the values in the record satisfy IsValue.

func (Record) Keys

func (r Record) Keys() []string

Keys returns a slice of keys in this record. The keys are returned sorted using sort.Strings.

func (Record) Size added in v0.2.8

func (r Record) Size() int

Size returns the size, in bytes, of this record.

The size is defined to be the sum of the number of bytes for each key, plus the sum of the number of bytes of storage used by each value. The size is only defined if the record is valid. We define an int and uint as requiring 8 bytes of storage, regardless of whether this is a 32- or 64-bit system.

func (Record) String

func (r Record) String() string

String returns a string representation of the record.

Jump to

Keyboard shortcuts

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