Documentation ¶
Overview ¶
Package lookup defines feed lookup algorithms and provides tools to place updates so they can be found
Index ¶
Constants ¶
const DefaultLevel = HighestLevel
DefaultLevel sets what level will be chosen to search when there is no hint
const EpochLength = 8
EpochLength stores the serialized binary length of an Epoch
const HighestLevel = 25 // default is 25 (~1 year)
HighestLevel sets the lowest frequency the algorithm will operate at, as a power of 2. 25 -> 2^25 equals to roughly one year.
const LowestLevel uint8 = 0 // default is 0 (1 second)
LowestLevel establishes the frequency resolution of the lookup algorithm as a power of 2.
const MaxTime uint64 = (1 << 56) - 1
MaxTime contains the highest possible time value an Epoch can handle
Variables ¶
var NoClue = Epoch{}
NoClue is a hint that can be provided when the Lookup caller does not have a clue about where the last update may be
Functions ¶
func FluzCapacitorAlgorithm ¶
func FluzCapacitorAlgorithm(ctx context.Context, now uint64, hint Epoch, read ReadFunc) (value interface{}, err error)
FluzCapacitorAlgorithm works by narrowing the epoch search area if an update is found going back and forth in time First, it will attempt to find an update where it should be now if the hint was really the last update. If that lookup fails, then the last update must be either the hint itself or the epochs right below. If however, that lookup succeeds, then the update must be that one or within the epochs right below. see the guide for a more graphical representation
func GetNextLevel ¶
GetNextLevel returns the frequency level a next update should be placed at, provided where the last update was and what time it is now. This is the first nonzero bit of the XOR of 'last' and 'now', counting from the highest significant bit but limited to not return a level that is smaller than the last-1
Types ¶
type Algorithm ¶
type Algorithm func(ctx context.Context, now uint64, hint Epoch, read ReadFunc) (value interface{}, err error)
Algorithm is the function signature of a lookup algorithm
var Lookup Algorithm = FluzCapacitorAlgorithm
Lookup finds the update with the highest timestamp that is smaller or equal than 'now' It takes a hint which should be the epoch where the last known update was If you don't know in what epoch the last update happened, simply submit lookup.NoClue read() will be called on each lookup attempt Returns an error only if read() returns an error Returns nil if an update was not found
type Epoch ¶
type Epoch struct { Time uint64 `json:"time"` // Time stores the time at which the update or lookup takes place Level uint8 `json:"level"` // Level indicates the frequency level as the exponent of a power of 2 }
Epoch represents a time slot at a particular frequency level
func GetFirstEpoch ¶
GetFirstEpoch returns the epoch where the first update should be located based on what time it is now.
func GetNextEpoch ¶
GetNextEpoch returns the epoch where the next update should be located according to where the previous update was and what time it is now.
func (*Epoch) Equals ¶
Equals compares two epochs and returns true if they refer to the same time period.
func (*Epoch) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaller interface
func (*Epoch) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaller interface
type EpochID ¶
type EpochID [8]byte
EpochID is a unique identifier for an Epoch, based on its level and base time.
type ReadFunc ¶
ReadFunc is a handler called by Lookup each time it attempts to find a value It should return <nil> if a value is not found It should return <nil> if a value is found, but its timestamp is higher than "now" It should only return an error in case the handler wants to stop the lookup process entirely.