mapping

package module
v0.0.0-...-6d72ccb Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAppendFailed = errors.New("failed to append to timeframe")

Functions

func Keys

func Keys[K comparable, T any](field Field[K, T]) []K

Keys provides access to all keys in a Field, regardless if it's a *Table[K, T] type, or an *Index[K, T] type.

func OrganizeSeq

func OrganizeSeq[M TimeframeType[T, K], T any, K any](seq SeqKV[Interval, T], reducer ReducerFunc[T]) *K

OrganizeSeq consumes a sequence Seq of Interval and data, returning an appropriate timeframe type K with organized / flattened values.

func WithIndex

func WithIndex[T any, K comparable](cmpFunc func(a, b K) int) cfg.Option[Config[K, T]]

WithIndex creates an index of all keys that are accessible via an Index type's keys element. Creating a Field via the New function using this option will always return an *Index[K, T] type, and its Index element can be used if cast to the concrete type.

Specifying a comparison function (like one such as cmp.Compare, to be used in slices.SortFunc) will order the slice of keys in the Index; Otherwise the keys will contain the order as retrieved from the map (random order).

If a Field type is used, the keys slice can also be retrieved using the Keys function.

func WithMutex

func WithMutex[K comparable, T any]() cfg.Option[Config[K, T]]

func WithZero

func WithZero[K comparable, T any](zero T) cfg.Option[Config[K, T]]

WithZero sets a zero value to be returned whenever a certain key isn't found in the mapping Field.

If this option is not used, the default zero value for the given type will be used instead (e.g. "" for strings; 0 for integers; false for booleans; nil for pointers; etc.).

Types

type Config

type Config[K comparable, T any] struct {
	// contains filtered or unexported fields
}

type DataInterval

type DataInterval[T any] struct {
	Data     T
	Interval Interval
}

DataInterval contains the data as Data for a specific Interval, as an isolated data structure used when caching Intervals and data as Seq of Interval and Data are organized.

func Organize

func Organize[T comparable](
	data []DataInterval[T], mergeFunc func(a, b T) T, offset time.Duration,
) []DataInterval[T]

func OrganizeFunc

func OrganizeFunc[T any](
	data []DataInterval[T], cmpFunc func(a, b T) bool, mergeFunc func(a, b T) T, offset time.Duration,
) []DataInterval[T]

type Field

type Field[K comparable, T any] interface {
	// Get fetches the value in a mapping Field for a given key. If the value does not exist, the Field's
	// configured zero value is returned. A boolean value is also returned to highlight whether accessing the key was
	// successful or not.
	Get(key K) (T, bool)
	// Set replaces the value of a certain key in the map, or it adds it if it does not exist. The returned boolean value
	// represents whether the key is new in the mapping Field or not.
	Set(key K, setter Setter[T]) bool
}

Field describes the capabilities of a dynamic field mapping type, that is to Get and Set values of a certain type, using comparable key types.

The Get method fetches the value in a mapping Field for a given key. If the value does not exist, the Field's configured zero value is returned. A boolean value is also returned to highlight whether accessing the key was successful or not.

Set replaces the value of a certain key in the map, or it adds it if it does not exist. The returned boolean value represents whether the key is new in the mapping Field or not.

Implementations of Field include Table (a set of key-value pairs with a configurable zero value), Index (like a Table but with the ability to index ordered or unordered keys) and SyncField -- the latter being a synchronized implementation of either a Table or Index Field.

func New

func New[K comparable, T any](values map[K]T, opts ...cfg.Option[Config[K, T]]) Field[K, T]

New creates a Field type appropriate to the configured options (either a *Table[K, T] type, or an *Index[K, T] type.

Both implementations can be of a SyncField type, if the WithMutex option is used.

type Index

type Index[K comparable, T any] struct {
	Keys []K
	// contains filtered or unexported fields
}

Index is a Field type that stores all the values map's keys in a slice, accessible as a public type within Index.

This Field type can also be an ordered Index, where the keys are ordered with a specific logic, if a comparison function is provided (in WithIndex) when configuring it.

func NewIndex

func NewIndex[K comparable, T any](values map[K]T, opts ...cfg.Option[Config[K, T]]) *Index[K, T]

func (*Index[K, T]) Get

func (i *Index[K, T]) Get(key K) (T, bool)

Get fetches the value in a mapping Field for a given key. If the value does not exist, the Field's configured zero value is returned. A boolean value is also returned to highlight whether accessing the key was successful or not.

func (*Index[K, T]) Set

func (i *Index[K, T]) Set(key K, setter Setter[T]) bool

Set replaces the value of a certain key in the map, or it adds it if it does not exist. The returned boolean value represents whether the key is new in the mapping Field or not.

type Interval

type Interval struct {
	From time.Time
	To   time.Time
}

Interval is a period of time with a From and To time.Time values.

type IntervalSet

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

IntervalSet describes a single period of time after combining two (separate) intervals, where an item could contain the current value, the next value or both, for a specific Interval.

type ReducerFunc

type ReducerFunc[T any] func(SeqKV[Interval, T]) SeqKV[Interval, T]

ReducerFunc describes a strategy to apply to a sequence, that returns a (reduced) sequence and an error.

This type can be used to apply different strategies in a map-reduce scenario, when working with Interval ranges.

It is passed into functions like Organize, and implementations of it include Replace and Flatten.

func Flatten

func Flatten[T comparable](mergeFunc func(a, b T) T, offset time.Duration) ReducerFunc[T]

Flatten consumes the sequence Seq of Interval and data to align all From and To time.Time values for each Interval and coalescing the data in any intersections when required. It returns a similar but new instance of the same type of Seq, but with organized values.

func FlattenFunc

func FlattenFunc[T any](cmpFunc func(a, b T) bool, mergeFunc func(a, b T) T, offset time.Duration) ReducerFunc[T]

FlattenFunc consumes the sequence Seq of Interval and data to align all From and To time.Time values for each Interval and coalescing the data in any intersections when required. It returns a similar but new instance of the same type of Seq, but with organized values.

func Replace

func Replace[T comparable](offset time.Duration) ReducerFunc[T]

Replace consumes the sequence Seq of Interval and data to align all From and To time.Time values for each Interval and coalescing the data in any intersections when required. It returns a similar but new instance of the same type of Seq, but with organized values.

func ReplaceFunc

func ReplaceFunc[T any](cmpFunc func(a, b T) bool, offset time.Duration) ReducerFunc[T]

ReplaceFunc consumes the sequence Seq of Interval and data to align all From and To time.Time values for each Interval and coalescing the data in any intersections when required. It returns a similar but new instance of the same type of Seq, but with organized values.

type Seq

type Seq[T any] func(yield func(T) bool) bool

Seq describes a sequence of iterable items, which takes a yield func which will be used to perform a certain operation on each yielded item throughout the iteration

ref: https://github.com/golang/go/issues/61899

type SeqKV

type SeqKV[T, K any] func(yield func(T, K) bool) bool

SeqKV describes a sequence of iterable items, which takes a yield func which will be used to perform a certain operation on each yielded item throughout the iteration

ref: https://github.com/golang/go/issues/61899

func AsSeq

func AsSeq[T any](data []DataInterval[T]) SeqKV[Interval, T]

type Setter

type Setter[T any] func(old T) (T, bool)

Setter is a generic function type that applies a new value in replacement of a former value of type T. It should return the new (or same) T value and a boolean representing if the item was newly set (from a zero value) or added; where a false value represents a substitution.

type SyncField

type SyncField[K comparable, T any] struct {
	// contains filtered or unexported fields
}

SyncField is a decorator for a Field type (either a *Table or *Index instance), enabling it for use in concurrent operations.

It uses a *sync.RWMutex to lock read and write operations, accordingly.

func (SyncField[K, T]) Get

func (f SyncField[K, T]) Get(key K) (T, bool)

Get fetches the value in a mapping Field for a given key. If the value does not exist, the Field's configured zero value is returned. A boolean value is also returned to highlight whether accessing the key was successful or not.

func (SyncField[K, T]) Set

func (f SyncField[K, T]) Set(key K, setter Setter[T]) bool

Set replaces the value of a certain key in the map, or it adds it if it does not exist. The returned boolean value represents whether the key is new in the mapping Field or not.

type Table

type Table[K comparable, T any] struct {
	// contains filtered or unexported fields
}

Table is a simple Field type, that provides control over the returned zero value when accessing keys in a map.

func NewTable

func NewTable[K comparable, T any](values map[K]T, opts ...cfg.Option[Config[K, T]]) *Table[K, T]

func (*Table[K, T]) Get

func (t *Table[K, T]) Get(key K) (T, bool)

Get fetches the value in a mapping Field for a given key. If the value does not exist, the Field's configured zero value is returned. A boolean value is also returned to highlight whether accessing the key was successful or not.

func (*Table[K, T]) Set

func (t *Table[K, T]) Set(key K, setter Setter[T]) bool

Set replaces the value of a certain key in the map, or it adds it if it does not exist. The returned boolean value represents whether the key is new in the mapping Field or not.

type Timeframe

type Timeframe[T any] struct {
	// contains filtered or unexported fields
}

func NewTimeframe

func NewTimeframe[T any]() *Timeframe[T]

NewTimeframe creates a NewTimeframe of type T, with an 2D-map of Interval's to type T.

func (*Timeframe[T]) Add

func (t *Timeframe[T]) Add(i Interval, value T) bool

Add joins the Interval i and its values to the Timeframe t, while ordering its previously inserted Interval(s) in the process.

func (*Timeframe[T]) All

func (t *Timeframe[T]) All() SeqKV[Interval, T]

All returns an iterator over the values in the Timeframe, through the indexed Interval values ordered by their From time.Time value.

func (*Timeframe[T]) Append

func (t *Timeframe[T]) Append(seq SeqKV[Interval, T]) bool

Append iterates through the input SeqKV and adds all intervals and respective values to the Timeframe t.

func (*Timeframe[T]) Organize

func (t *Timeframe[T]) Organize(reducer ReducerFunc[T]) *Timeframe[T]

Organize returns a new Timeframe with organized Interval(s) and respective values. It is the result of calling the input ReducerFunc (like Flatten or Replace) on Timeframe.All, and appending the resulting sequence to a new instance of Timeframe.

type TimeframeIndex

type TimeframeIndex[T any] struct {
	Index *Index[Interval, T]
}

TimeframeIndex stores values in intervals of time, as an Index of Interval and a map of type T.

func NewTimeframeIndex

func NewTimeframeIndex[T any]() *TimeframeIndex[T]

NewTimeframeIndex creates a TimeframeIndex of type T, with an index on the Interval's From time.Time.

func (*TimeframeIndex[T]) Add

func (t *TimeframeIndex[T]) Add(i Interval, value T) bool

Add joins the Interval i and its values to the TimeframeIndex t, while ordering its previously inserted Interval(s) in the process.

func (*TimeframeIndex[T]) All

func (t *TimeframeIndex[T]) All() SeqKV[Interval, T]

All returns an iterator over the values in the TimeframeIndex, through the indexed Interval values ordered by their From time.Time value.

func (*TimeframeIndex[T]) Append

func (t *TimeframeIndex[T]) Append(seq SeqKV[Interval, T]) (err error)

Append iterates through the input SeqKV and adds all intervals and respective values to the TimeframeIndex t.

func (*TimeframeIndex[T]) Merge

func (t *TimeframeIndex[T]) Merge(tf *TimeframeIndex[T]) (err error)

Merge joins the intervals and respective values of the TimeframeIndex tf into the TimeframeIndex t, by extracting a SeqKV of the same items from tf using TimeframeIndex.All, and adding them into TimeframeIndex t using TimeframeIndex.Append.

func (*TimeframeIndex[T]) Organize

func (t *TimeframeIndex[T]) Organize(reducer ReducerFunc[T]) *TimeframeIndex[T]

Organize returns a new TimeframeIndex with organized Interval(s) and respective values. It is the result of calling Flatten on Timeframe.All, and appending the resulting sequence to a new instance of TimeframeIndex.

type TimeframeMap

type TimeframeMap[T any] struct {
	Keys   []Interval
	Values map[Interval]T
}

func NewTimeframeMap

func NewTimeframeMap[T any]() *TimeframeMap[T]

NewTimeframeMap creates a TimeframeMap of type T, that includes a map of Interval's to type T alongside its (sorted) keys.

func (*TimeframeMap[T]) Add

func (t *TimeframeMap[T]) Add(i Interval, value T) bool

Add joins the Interval i and its values to the TimeframeMap t, while ordering its previously inserted Interval(s) in the process.

func (*TimeframeMap[T]) All

func (t *TimeframeMap[T]) All() SeqKV[Interval, T]

All returns an iterator over the values in the TimeframeMap, through the indexed Interval values ordered by their From time.Time value.

func (*TimeframeMap[T]) Append

func (t *TimeframeMap[T]) Append(seq SeqKV[Interval, T]) error

Append iterates through the input SeqKV and adds all intervals and respective values to the TimeframeMap t.

func (*TimeframeMap[T]) Organize

func (t *TimeframeMap[T]) Organize(reducer ReducerFunc[T]) *TimeframeMap[T]

Organize returns a new TimeframeMap with organized Interval(s) and respective values. It is the result of calling the input ReducerFunc (like Flatten or Replace) on TimeframeMap.All, and appending the resulting sequence to a new instance of TimeframeMap.

type TimeframeStream

type TimeframeStream[T comparable] struct {
	// contains filtered or unexported fields
}

func NewTimeframeStream

func NewTimeframeStream[T comparable](mergeFunc func(a, b T) T, offset time.Duration) *TimeframeStream[T]

NewTimeframeStream creates a TimeframeStream of type T, with an 2D-map of Interval's to type T, that automatically organizes itself each time that an item (or sequence of items) is(/are) added or appended to the TimeframeStream.

func (*TimeframeStream[T]) Add

func (t *TimeframeStream[T]) Add(i Interval, value T) bool

Add joins the Interval i and its values to the Timeframe t, while ordering its previously inserted Interval(s) in the process.

func (*TimeframeStream[T]) All

func (t *TimeframeStream[T]) All() SeqKV[Interval, T]

All returns an iterator over the values in the Timeframe, through the indexed Interval values ordered by their From time.Time value.

func (*TimeframeStream[T]) Append

func (t *TimeframeStream[T]) Append(seq SeqKV[Interval, T]) bool

Append iterates through the input SeqKV and adds all intervals and respective values to the Timeframe t.

func (*TimeframeStream[T]) Organize

func (t *TimeframeStream[T]) Organize() *TimeframeStream[T]

Organize returns a new Timeframe with organized Interval(s) and respective values. It is the result of calling the input ReducerFunc (like Flatten or Replace) on Timeframe.All, and appending the resulting sequence to a new instance of Timeframe.

type TimeframeType

type TimeframeType[T any, M any] interface {
	*M

	Add(i Interval, value T) bool
	// contains filtered or unexported methods
}

TimeframeType is a generic interface that describes a constructor to a timeframe implementation.

The generic interface includes the init method, allowing for a generic function to safely create a pointer to type M as required by the type itself.

Other than initializing type M, the type simply needs to ingest a sequence of Interval and type T, and that is where Append comes in.

Jump to

Keyboard shortcuts

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