cgl

package module
v0.0.0-...-46acba0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2011 License: BSD-3-Clause Imports: 17 Imported by: 0

Documentation

Overview

The Tideland Common Go Library contains a lot of helpful types and functions for many kinds of software. Those are a map/reduce, sorting, caching, time handling, UUIDs, lazy evaluation, chronological jobs, state machines, monitoring, suervision, a simple markup language and much more.

Index

Constants

View Source
const (
	SIG_STOP    = true
	SIG_RESTART = false
)

Signals to stop or restart a cached value.

View Source
const RELEASE = "Tideland Common Go Library Release 2011-09-22"

Variables

View Source
var QuickSortParallelThreshold int = 4095

Threshold for switching from parallel to sequential quick sort.

View Source
var QuickSortSequentialThreshold int = 63

Threshold for switching from sequential quick sort to insertion sort.

Functions

func BuildLazyEvaluator

func BuildLazyEvaluator(evalFunc EvalFunc, initState interface{}) func() interface{}

Generic builder for lazy evaluators.

func BuildLazyIntEvaluator

func BuildLazyIntEvaluator(evalFunc EvalFunc, initState interface{}) func() int

Builder for lazy evaluators with ints as result.

func DayInList

func DayInList(time *time.Time, days []int) bool

Test if the day of a time is in a given list.

func DayInRange

func DayInRange(time *time.Time, minDay, maxDay int) bool

Test if a day of a time is in a given range.

func Debug

func Debug(format string, args ...interface{})

Debug prints a debug information to the log with file and line.

func Dispatch

func Dispatch(variable interface{}, name string, args ...interface{}) ([]interface{}, bool)

Dispatch a string to a method of a type.

func HelpIfNeeded

func HelpIfNeeded(r Recoverable, err interface{})

Tell the supervisor to help if the passed error is not nil.

func HourInList

func HourInList(time *time.Time, hours []int) bool

Test if the hour of a time is in a given list.

func HourInRange

func HourInRange(time *time.Time, minHour, maxHour int) bool

Test if a hour of a time is in a given range.

func Identifier

func Identifier(parts ...interface{}) string

Identifier works like SepIdentifier but the seperator is set to be a colon.

func ImAlive

func ImAlive(h *Heartbeat)

Send a heartbeat.

func KeyLessFunc

func KeyLessFunc(a *KeyValue, b *KeyValue) bool

KeyLessFunc compares the keys of two key/value pairs. It returns true if the key of a is less the key of b.

func LimitedSepIdentifier

func LimitedSepIdentifier(sep string, limit bool, parts ...interface{}) string

LimitedSepIdentifier builds an identifier out of multiple parts, all as lowercase strings and concatenated with the separator Non letters and digits are exchanged with dashes and reduced to a maximum of one each. If limit is true only 'a' to 'z' and '0' to '9' are allowed.

func MinuteInList

func MinuteInList(time *time.Time, minutes []int) bool

Test if the minute of a time is in a given list.

func MinuteInRange

func MinuteInRange(time *time.Time, minMinute, maxMinute int) bool

Test if a minute of a time is in a given range.

func MonthInList

func MonthInList(time *time.Time, months []int) bool

Test if the month of a time is in a given list.

func MonthInRange

func MonthInRange(time *time.Time, minMonth, maxMonth int) bool

Test if a month of a time is in a given range.

func NsDays

func NsDays(count int64) int64

Calc nanoseconds from days.

func NsHours

func NsHours(count int64) int64

Calc nanoseconds from hours.

func NsMicroseconds

func NsMicroseconds(count int64) int64

Calc nanoseconds from microseconds.

func NsMilliseconds

func NsMilliseconds(count int64) int64

Calc nanoseconds from milliseconds.

func NsMinutes

func NsMinutes(count int64) int64

Calc nanoseconds from minutes.

func NsSeconds

func NsSeconds(count int64) int64

Calc nanoseconds from seconds.

func NsWeeks

func NsWeeks(count int64) int64

Calc nanoseconds from weeks.

func SecondInList

func SecondInList(time *time.Time, seconds []int) bool

Test if the second of a time is in a given list.

func SecondInRange

func SecondInRange(time *time.Time, minSecond, maxSecond int) bool

Test if a second of a time is in a given range.

func SepIdentifier

func SepIdentifier(sep string, parts ...interface{}) string

SepIdentifier builds an identifier out of multiple parts, all as lowercase strings and concatenated with the separator Non letters and digits are exchanged with dashes and reduced to a maximum of one each.

func Sort

func Sort(data sort.Interface)

func TypeAsIdentifierPart

func TypeAsIdentifierPart(i interface{}) string

TypeAsIdentifierPart transforms the name of the arguments type into a part for identifiers. It's splitted at each uppercase char, concatenated with dashes and transferred to lowercase.

func WeekdayInList

func WeekdayInList(time *time.Time, weekdays []int) bool

Test if the weekday of a time is in a given list.

func WeekdayInRange

func WeekdayInRange(time *time.Time, minWeekday, maxWeekday int) bool

Test if a weekday of a time is in a given range.

func YearInList

func YearInList(time *time.Time, years []int64) bool

Test if the year of a time is in a given list.

func YearInRange

func YearInRange(time *time.Time, minYear, maxYear int64) bool

Test if a year of a time is in a given range.

Types

type CachedValue

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

CachedValue retrieves and caches a value for a defined time. After that time the cache is cleared and set again automatically when accessed.

func NewCachedValue

func NewCachedValue(r RetrievalFunc, ttl int64) *CachedValue

NewCachedValue creates a new cache.

func (*CachedValue) Clear

func (cv *CachedValue) Clear()

Clear removes the value from the cache.

func (*CachedValue) Stop

func (cv *CachedValue) Stop()

Stop removes everything and sends a stop signal to the backend.

func (*CachedValue) Value

func (cv *CachedValue) Value() (v interface{}, err os.Error)

Value returns the cached value. If an error occurred during retrieval that will be returned too.

type CheckFunc

type CheckFunc func(*time.Time) (bool, bool)

Check function type.

type Condition

type Condition struct {
	Now     int64
	Payload interface{}
}

Condition type.

type Crontab

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

Crontab.

func NewCrontab

func NewCrontab() *Crontab

Start a crontab server.

func (*Crontab) AddJob

func (ctb *Crontab) AddJob(job *Job)

Add a job to the server.

func (*Crontab) DeleteJob

func (ctb *Crontab) DeleteJob(id string)

Delete a job from the server.

func (*Crontab) Recover

func (ctb *Crontab) Recover(recoverable Recoverable, err interface{})

Recover after an error.

func (*Crontab) Stop

func (ctb *Crontab) Stop()

Stop the server.

func (*Crontab) Supervisor

func (src *Crontab) Supervisor() *Supervisor

Return the supervisor.

type DynamicStatusRetriever

type DynamicStatusRetriever func() string

DynamicStatusRetriever is called by the server and returns a current status as string.

type EvalFunc

type EvalFunc func(interface{}) (interface{}, interface{})

Function to evaluate.

type FSM

type FSM struct {
	Handler Handler
	// contains filtered or unexported fields
}

State machine type.

func NewFSM

func NewFSM(h Handler, timeout int64) *FSM

Create a new finite state machine.

func (*FSM) Recover

func (fsm *FSM) Recover(recoverable Recoverable, err interface{})

Recover after an error.

func (*FSM) Send

func (fsm *FSM) Send(payload interface{})

Send a payload with no result.

func (*FSM) SendAfter

func (fsm *FSM) SendAfter(payload interface{}, ns int64)

Send a payload with no result after a given time.

func (*FSM) SendWithResult

func (fsm *FSM) SendWithResult(payload interface{}) interface{}

Send a payload to handle and return the result.

func (*FSM) State

func (fsm *FSM) State() string

Return the current state.

func (*FSM) Supervisor

func (fsm *FSM) Supervisor() *Supervisor

Return the supervisor.

type Handler

type Handler interface {
	Init() string
	Terminate(string, interface{}) string
}

Handler interface.

type Hash

type Hash map[string]interface{}

Hash is a map of string keys to any value.

func NewHash

func NewHash() Hash

NewHash creates a new hash.

func (Hash) KeyValues

func (h Hash) KeyValues() *KeyValues

KeyValues returns the hash content as key/value slice.

func (Hash) Merge

func (h Hash) Merge(mh Hash)

Merge merges another hash into the hash.

type Hashable

type Hashable interface {
	GetHash() Hash
	SetHash(h Hash)
}

Hashable is interface for types representable as a hash.

type Heartbeat

type Heartbeat struct {
	HeartbeatChan chan *Heartbeat
	ImAliveChan   chan bool
	// contains filtered or unexported fields
}

Heartbeat for one recoverable.

func NewHeartbeat

func NewHeartbeat(r Recoverable, ns int64) *Heartbeat

Create a new heartbeat.

type Heartbeatable

type Heartbeatable interface {
	Recoverable
	SetHearbeat(*Heartbeat)
}

The interface for heartbeatable types.

type Job

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

Job type.

func NewJob

func NewJob(id string, check CheckFunc, task TaskFunc) *Job

Create a new job.

type KeyValue

type KeyValue struct {
	Key   string
	Value interface{}
}

KeyValue is a pair of a string key and any data as value.

type KeyValueChan

type KeyValueChan chan *KeyValue

Channel for the transfer of key/value pairs.

func MapReduce

func MapReduce(inChan KeyValueChan, mapFunc MapFunc, mapSize int, reduceFunc ReduceFunc, reduceSize int) KeyValueChan

MapReduce applies a map and a reduce function to keys and values in parallel.

func SortedMapReduce

func SortedMapReduce(inChan KeyValueChan, mapFunc MapFunc, mapSize int, reduceFunc ReduceFunc, reduceSize int, lessFunc KeyValueLessFunc) KeyValueChan

SortedMapReduce performes a map/reduce and sorts the result.

type KeyValueChans

type KeyValueChans []KeyValueChan

Slice of key/value channels.

type KeyValueLessFunc

type KeyValueLessFunc func(*KeyValue, *KeyValue) bool

Less function for sorting.

type KeyValues

type KeyValues struct {
	Data     []*KeyValue
	LessFunc KeyValueLessFunc
}

KeyValues is an ordered slice of keys and values, keys can occur multiple times.

func NewKeyValues

func NewKeyValues(c int, kvlf KeyValueLessFunc) *KeyValues

NewKeyValues create a new empty key/value slice with size 0, capacity c and a less func for sorting.

func (*KeyValues) Add

func (kv *KeyValues) Add(key string, value interface{})

Add adds a key/value to the slice.

func (*KeyValues) Append

func (kv *KeyValues) Append(akv KeyValues)

Append appends the data of other key/values to this key/values.

func (*KeyValues) AppendChan

func (kv *KeyValues) AppendChan(kvChan KeyValueChan)

AppendChan appends the key/values of a chan to this key/values.

func (*KeyValues) KeyValueChan

func (kv *KeyValues) KeyValueChan() KeyValueChan

KeyValueChan returns a key/value channel and feeds it with the data.

func (*KeyValues) Len

func (kv *KeyValues) Len() int

Len returns the number of keys and values.

func (*KeyValues) Less

func (kv *KeyValues) Less(i, j int) bool

Less returns wether the key with index i should sort before the key with index j.

func (*KeyValues) Sort

func (kv *KeyValues) Sort()

Sort sorts the key/values based on the less func.

func (*KeyValues) Swap

func (kv *KeyValues) Swap(i, j int)

Swap swaps the key/value pairs with indexes i and j.

type MapFunc

type MapFunc func(*KeyValue, KeyValueChan)

Map a key/value pair, emit to the channel.

type Measuring

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

Measuring contains one measuring.

func (*Measuring) EndMeasuring

func (m *Measuring) EndMeasuring() int64

EndMEasuring ends a measuring and passes it to the measuring server in the background.

type MeasuringPoint

type MeasuringPoint struct {
	Id      string
	Count   int64
	MinTime int64
	MaxTime int64
	TtlTime int64
	AvgTime int64
}

MeasuringPoint contains the cumulated measuring data of one measuring point.

type Node

type Node interface {
	Len() int
	ProcessWith(p Processor)
}

The node.

type Processor

type Processor interface {
	OpenTag(tag []string)
	CloseTag(tag []string)
	Text(text string)
}

Processor interface.

type Recoverable

type Recoverable interface {
	Supervisor() *Supervisor
	Recover(Recoverable, interface{})
}

The interface for recoverable types.

type ReduceFunc

type ReduceFunc func(KeyValueChan, KeyValueChan)

Reduce the key/values of the first channel, emit to the second channel.

type RetrievalFunc

type RetrievalFunc func() (interface{}, os.Error)

RetrievalFunc is the signature of a function responsible for the retrieval of the cached value from somewhere else in the system, e.g. a database.

type SigChan

type SigChan chan bool

Channel for closing signals.

type SmlReader

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

Reader for SML.

func NewSmlReader

func NewSmlReader(reader io.Reader) *SmlReader

Create the reader.

func (*SmlReader) RootTagNode

func (sr *SmlReader) RootTagNode() (*TagNode, os.Error)

Return the root tag node.

type SmlWriterProcessor

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

Processor for writing SML.

func NewSmlWriterProcessor

func NewSmlWriterProcessor(writer io.Writer, prettyPrint bool) *SmlWriterProcessor

Create a new SML writer processor.

func (*SmlWriterProcessor) CloseTag

func (swp *SmlWriterProcessor) CloseTag(tag []string)

Close a tag.

func (*SmlWriterProcessor) OpenTag

func (swp *SmlWriterProcessor) OpenTag(tag []string)

Open a tag.

func (*SmlWriterProcessor) Text

func (swp *SmlWriterProcessor) Text(text string)

Write a text.

type StaySetVariable

type StaySetVariable struct {
	Id       string
	Count    int64
	ActValue int64
	MinValue int64
	MaxValue int64
	AvgValue int64
	// contains filtered or unexported fields
}

StaySetVariable contains the cumulated values for one stay-set variable.

type Supervisor

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

The supervisor itself.

func GlobalSupervisor

func GlobalSupervisor() *Supervisor

Return the global supervisor.

func NewSupervisor

func NewSupervisor(parent *Supervisor) *Supervisor

Create a new supervisor.

func (*Supervisor) AddRecoverable

func (s *Supervisor) AddRecoverable(id string, r Recoverable)

Add a recoverable for joint restart in case of an error.

func (*Supervisor) Help

func (s *Supervisor) Help(r Recoverable, err interface{})

Let a recoverable cry for help at its supervisor.

func (*Supervisor) Recover

func (s *Supervisor) Recover(r Recoverable, err interface{})

Implement Recover() of the recoverable interface for the supervisor itself.

func (*Supervisor) Supervisor

func (s *Supervisor) Supervisor() *Supervisor

Implement Supervisor() of the recoverable interface for the supervisor itself.

type SystemMonitor

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

The system monitor type.

func Monitor

func Monitor() *SystemMonitor

Monitor returns the system monitor if it exists. Otherwise it creates it first.

func (*SystemMonitor) BeginMeasuring

func (sm *SystemMonitor) BeginMeasuring(id string) *Measuring

BeginMeasuring starts a new measuring with a given id. All measurings with the same id will be aggregated.

func (*SystemMonitor) DynamicStatusValuesDo

func (sm *SystemMonitor) DynamicStatusValuesDo(f func(string, string))

DynamicStatusValuesDo performs the function f for all status values.

func (*SystemMonitor) DynamicStatusValuesMap

func (sm *SystemMonitor) DynamicStatusValuesMap(f func(string, string) interface{}) []interface{}

DynamicStatusValuesMap performs the function f for all status values and returns a slice with the return values of the function that are not nil.

func (*SystemMonitor) DynamicStatusValuesPrintAll

func (sm *SystemMonitor) DynamicStatusValuesPrintAll()

DynamicStatusValuesPrintAll prints all status values to STDOUT.

func (*SystemMonitor) DynamicStatusValuesWrite

func (sm *SystemMonitor) DynamicStatusValuesWrite(w io.Writer, ff func(string, string) bool)

DynamicStatusValuesWrite prints the status values for which the passed function returns true to the passed writer.

func (*SystemMonitor) Measure

func (sm *SystemMonitor) Measure(id string, f func())

Measure the execution of a function.

func (*SystemMonitor) MeasuringPointsDo

func (sm *SystemMonitor) MeasuringPointsDo(f func(*MeasuringPoint))

MeasuringPointsDo performs the function f for all measuring points.

func (*SystemMonitor) MeasuringPointsMap

func (sm *SystemMonitor) MeasuringPointsMap(f func(*MeasuringPoint) interface{}) []interface{}

MeasuringPointsMap performs the function f for all measuring points and returns a slice with the return values of the function that are not nil.

func (*SystemMonitor) MeasuringPointsPrintAll

func (sm *SystemMonitor) MeasuringPointsPrintAll()

MeasuringPointsPrintAll prints all measuring points to STDOUT.

func (*SystemMonitor) MeasuringPointsWrite

func (sm *SystemMonitor) MeasuringPointsWrite(w io.Writer, ff func(*MeasuringPoint) bool)

MeasuringPointsWrite prints the measuring points for which the passed function returns true to the passed writer.

func (*SystemMonitor) Recover

func (sm *SystemMonitor) Recover(recoverable Recoverable, err interface{})

Recover after an error.

func (*SystemMonitor) Register

func (sm *SystemMonitor) Register(id string, rf DynamicStatusRetriever)

Register registers a new dynamic status retriever function.

func (*SystemMonitor) SetValue

func (sm *SystemMonitor) SetValue(id string, v int64)

SetValue sets a value of a stay-set variable.

func (*SystemMonitor) StaySetVariablesDo

func (sm *SystemMonitor) StaySetVariablesDo(f func(*StaySetVariable))

StaySetVariablesDo performs the function f for all variables.

func (*SystemMonitor) StaySetVariablesMap

func (sm *SystemMonitor) StaySetVariablesMap(f func(*StaySetVariable) interface{}) []interface{}

StaySetVariablesMap performs the function f for all variables and returns a slice with the return values of the function that are not nil.

func (*SystemMonitor) StaySetVariablesPrintAll

func (sm *SystemMonitor) StaySetVariablesPrintAll()

StaySetVariablesPrintAll prints all stay-set variables to STDOUT.

func (*SystemMonitor) StaySetVariablesWrite

func (sm *SystemMonitor) StaySetVariablesWrite(w io.Writer, ff func(*StaySetVariable) bool)

StaySetVariablesWrite prints the stay-set variables for which the passed function returns true to the passed writer.

func (*SystemMonitor) Supervisor

func (sm *SystemMonitor) Supervisor() *Supervisor

Return the supervisor.

func (*SystemMonitor) Unregister

func (sm *SystemMonitor) Unregister(id string)

Unregister unregisters a dynamic status retriever function.

type TagNode

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

The tag node.

func NewTagNode

func NewTagNode(tag string) *TagNode

Create a new tag node.

func (*TagNode) AppendTag

func (tn *TagNode) AppendTag(tag string) *TagNode

Append a new tag.

func (*TagNode) AppendTagNode

func (tn *TagNode) AppendTagNode(n *TagNode) *TagNode

Append a tag node.

func (*TagNode) AppendTaggedText

func (tn *TagNode) AppendTaggedText(tag, text string) *TagNode

Append a tagged text node.

func (*TagNode) AppendText

func (tn *TagNode) AppendText(text string) *TextNode

Append a text node.

func (*TagNode) AppendTextNode

func (tn *TagNode) AppendTextNode(n *TextNode) *TextNode

Append a text node.

func (*TagNode) Len

func (tn *TagNode) Len() int

Return the len of the tag node (aka number of children).

func (*TagNode) ProcessWith

func (tn *TagNode) ProcessWith(p Processor)

Process the node.

func (*TagNode) String

func (tn *TagNode) String() string

Return the node as a string.

type TaskFunc

type TaskFunc func(string)

Tast function type.

type TextNode

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

The text node.

func NewTextNode

func NewTextNode(text string) *TextNode

Create a new text node.

func (*TextNode) Len

func (tn *TextNode) Len() int

Return the len of the text node.

func (*TextNode) ProcessWith

func (tn *TextNode) ProcessWith(p Processor)

Process the node.

func (*TextNode) String

func (tn *TextNode) String() string

Return the node as a string.

type Timeout

type Timeout int64

Timeout type.

type UUID

type UUID []byte

UUID represent a universal identifier with 16 bytes.

func NewUUID

func NewUUID() UUID

NewUUID generates a new UUID based on version 4.

func (UUID) Raw

func (uuid UUID) Raw() []byte

Raw returns a copy of the UUID bytes.

func (UUID) String

func (uuid UUID) String() string

String returns a hexadecimal string representation with standardized separators.

Jump to

Keyboard shortcuts

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