common

package
v0.0.0-...-3e52749 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AOrB

func AOrB(flag bool, a string, b string) string

AOrB is a simple helper function that returns either the 'a' string if the supplied flag is true, or the 'b' string. This simplifies trace format calls by removing inline if-then sequences.

func CompleteWithin

func CompleteWithin(ch <-chan bool, delay time.Duration) bool

CompleteWithin ensures that a channel either produces a result within the specified time, or it times out.

func CompleteWithinInterface

func CompleteWithinInterface(ch <-chan interface{}, delay time.Duration) interface{}

CompleteWithinInterface ensures that a channel either produces a result within the specified time, or it times out.

func ContextHasTick

func ContextHasTick(ctx context.Context) bool

ContextHasTick verifies whether the supplied context has a simulated time tick in its set of values.

func ContextWithTick

func ContextWithTick(ctx context.Context, tick int64) context.Context

ContextWithTick returns a new context with the current simulated time added.

func Decode

func Decode(keywords []string, source string) ([]bool, error)

Decode is a function that finds all occurences of the source string in the keyword array.

  • It returns an error if no occurence of the source string is found in the keyword array.
  • When there is no error, the boolean array describes which keywords were matched . An element in that array is true if the element with the same index in the keywords array was matched.

func DoNotCompleteWithin

func DoNotCompleteWithin(ch <-chan bool, delay time.Duration) bool

DoNotCompleteWithin ensures that the channel does not produce a result before the specified time. When this function returns with success no value will have been read from the supplied channel.

func DoNotCompleteWithinInterface

func DoNotCompleteWithinInterface(ch <-chan interface{}, delay time.Duration) interface{}

DoNotCompleteWithinInterface ensures that the channel does not produce a result before the specified time. When this function returns with success no value will have been read from the supplied channel.

func MaxInt

func MaxInt(a int, b int) int

MaxInt is a helper function to return the maximum of two int values

func MaxInt64

func MaxInt64(a int64, b int64) int64

MaxInt64 is a helper function to return the maximum of two int64 values

func TickFromContext

func TickFromContext(ctx context.Context) int64

TickFromContext extracts the simulated time from the supplied context.

func URLPrefix

func URLPrefix(r *http.Request) string

URLPrefix extracts the URL used by the request, and returns it after ensuring that it has a trailing '/'

Types

type Guarded

type Guarded struct {
	// Guard is the last sequence number used
	Guard int64
}

Guarded provides basic support for access gated by last sequence value. It maintains the last sequence number used, and has functions that can be used to refuse access based on out of order sequencing, as well as the maintenance of the guard value itself.

func (*Guarded) AdvanceGuard

func (g *Guarded) AdvanceGuard(at int64)

AdvanceGuard updates the guard value if the new value is greater than the value the object already holds.

func (*Guarded) Pass

func (g *Guarded) Pass(check int64, at int64) bool

Pass checks that the provided check value is not earlier than the guard. If it passes, the guard is updated with supplied current sequence value from the at parameter. This allows for checks based on values the caller last saw, and for updates to provide something close to a linear sequence across guarded objects. Pass returns true if the check passes, and false if it didn't.

type MultiMap

type MultiMap interface {
	// Get returns the instance associated with the supplied primary key, or
	// nil, if it was not found.  The second return value is true, if it was
	// found, or false, if it was not.
	Get(key PrimaryKey) (MultiMapEntry, bool)

	// GetPrimaryKeysFromSecondary returns the primary keys for entries that
	// match the supplied value for the secondary key at the index.  The second
	// return value is true, if it was found, or false, if it was not.
	GetPrimaryKeysFromSecondary(index int, key SecondaryKey) ([]PrimaryKey, bool)

	// Add attempts to add the supplied entry to the MultiMap instance.  It
	// returns true if the new entry was inserted; false, if it was not.  The
	// reason for insertion failing would be due to the primary key value for
	// this new entry matching one already in the MultiMap instance.
	Add(entry MultiMapEntry) bool

	// Remove attempts to remove the entry identified by the supplied primary
	// key.  It returns the current entry, if it was found, and true, if the
	// entry was found and removed, or false, if no such entry was found.
	Remove(key PrimaryKey) (MultiMapEntry, bool)

	// ForEach calls the supplied action function with each entry held in the
	// MultiMap instance, in random order.
	ForEach(action func(item MultiMapEntry))

	// ForEachSecondary processes each entry for the specified secondary key.
	// It calls the supplied action function for each key value, passing that
	// value and the set of primary keys associated with it.
	ForEachSecondary(index int, action func(key SecondaryKey, items []PrimaryKey))

	// Count returns the number of entries held in this MultiMap instance.
	Count() int

	// SecondaryCount returns the number of unique secondary keys held in this
	// MultiMap instance for the specified secondary key index.
	SecondaryCount(index int) int

	// Clear removes all existing entries and resets all secondary keys indices.
	Clear()
}

MultiMap is the interface callers use for accessing a MultiMap instance.

func NewMultiMap

func NewMultiMap(count int) MultiMap

NewMultiMap creates a new MultiMap instance. It requires that the caller supply the number of secondary key indices.

type MultiMapEntry

type MultiMapEntry interface {
	// Primary returns the primary key value for this entry.
	Primary() PrimaryKey

	// Secondary returns the key value for the secondary key specified by the
	// index parameter.  If the entry does not support that secondary key, it
	// must return nil.
	Secondary(index int) SecondaryKey
}

MultiMapEntry is the interface that an entry instance is required to support in order to be placed in a MultiMap.

type MultiMapImpl

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

MultiMapImpl is an implementation of the MultiMap interface. This implementation uses multiple maps and slice for the indices.

func (*MultiMapImpl) Add

func (m *MultiMapImpl) Add(entry MultiMapEntry) bool

func (*MultiMapImpl) Clear

func (m *MultiMapImpl) Clear()

func (*MultiMapImpl) Count

func (m *MultiMapImpl) Count() int

func (*MultiMapImpl) ForEach

func (m *MultiMapImpl) ForEach(action func(item MultiMapEntry))

func (*MultiMapImpl) ForEachSecondary

func (m *MultiMapImpl) ForEachSecondary(
	index int,
	action func(key SecondaryKey, items []PrimaryKey))

func (*MultiMapImpl) Get

func (m *MultiMapImpl) Get(key PrimaryKey) (MultiMapEntry, bool)

func (*MultiMapImpl) GetPrimaryKeysFromSecondary

func (m *MultiMapImpl) GetPrimaryKeysFromSecondary(
	index int,
	key SecondaryKey) ([]PrimaryKey, bool)

func (*MultiMapImpl) Remove

func (m *MultiMapImpl) Remove(key PrimaryKey) (MultiMapEntry, bool)

func (*MultiMapImpl) SecondaryCount

func (m *MultiMapImpl) SecondaryCount(index int) int

type PrimaryKey

type PrimaryKey int

PrimaryKey is the abstract type for an entry's primary key

type Range

type Range struct {
	Low  int64
	High int64
}

Range contains a pair of values that form a range of acceptable int64 values, low to high, inclusive.

func (Range) Pick

func (dr Range) Pick() int64

Pick returns a value in the acceptable range.

func (Range) String

func (dr Range) String() string

String returns a formatted description of the range of values as '[low:high]'

func (Range) Validate

func (dr Range) Validate(name string, minLow int64, maxHigh int64) error

Validate checks whether or not the values are within an acceptable range, and that Low is not greater than High. It returns an error if the validation fails, or nil, if the range is valid.

type SecondaryKey

type SecondaryKey interface{}

SecondaryKey is the abstract type for each of the secondary keys for an entry

Jump to

Keyboard shortcuts

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