state

package
v0.0.0-...-522126a Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2019 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package state provides functionality related to saving and loading object graphs. For most types, it provides a set of default saving / loading logic that will be invoked automatically if custom logic is not defined.

Kind             Support
----             -------
Bool             default
Int              default
Int8             default
Int16            default
Int32            default
Int64            default
Uint             default
Uint8            default
Uint16           default
Uint32           default
Uint64           default
Float32          default
Float64          default
Complex64        custom
Complex128       custom
Array            default
Chan             custom
Func             custom
Interface        custom
Map              default (*)
Ptr              default
Slice            default
String           default
Struct           custom
UnsafePointer    custom

(*) Maps are treated as value types by this package, even if they are pointers internally. If you want to save two independent references to the same map value, you must explicitly use a pointer to a map.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsZeroValue

func IsZeroValue(val interface{}) bool

IsZeroValue checks if the given value is the zero value.

This function is used by the stateify tool.

func Load

func Load(ctx context.Context, r io.Reader, rootPtr interface{}, stats *Stats) error

Load loads a checkpoint.

func PrettyPrint

func PrettyPrint(w io.Writer, r io.Reader, html bool) error

PrettyPrint reads the state stream from r, and pretty prints to w.

func ReadHeader

func ReadHeader(r io.Reader) (length uint64, object bool, err error)

ReadHeader reads an object header.

Each object written to the statefile is prefixed with a header. See WriteHeader for more information; these functions are exported to allow non-state writes to the file to play nice with debugging tools.

func Register

func Register(name string, instance interface{}, fns Fns)

Register must be called for any interface implementation types that implements Loader.

Register should be called either immediately after startup or via init() methods. Double registration of either names or types will result in a panic.

No synchronization is provided; this should only be called in init.

Example usage:

state.Register("Foo", (*Foo)(nil), state.Fns{
	Save: (*Foo).Save,
	Load: (*Foo).Load,
})

func Save

func Save(ctx context.Context, w io.Writer, rootPtr interface{}, stats *Stats) error

Save saves the given object state.

func UnwrapErrState

func UnwrapErrState(err error) error

UnwrapErrState returns the underlying error in ErrState.

If err is not *ErrState, err is returned directly.

func WriteHeader

func WriteHeader(w io.Writer, length uint64, object bool) error

WriteHeader writes a header.

Each object written to the statefile should be prefixed with a header. In order to generate statefiles that play nicely with debugging tools, raw writes should be prefixed with a header with object set to false and the appropriate length. This will allow tools to skip these regions.

Types

type ErrState

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

ErrState is returned when an error is encountered during encode/decode.

func (*ErrState) Error

func (e *ErrState) Error() string

Error returns a sensible description of the state error.

type Fns

type Fns struct {
	// Save is a function like Save(concreteType, Map).
	Save interface{}

	// Load is a function like Load(concreteType, Map).
	Load interface{}
}

Fns are the state dispatch functions.

func (*Fns) Validate

func (fns *Fns) Validate(typ reflect.Type) bool

Validate validates all state functions.

type Map

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

Map is a generic state container.

This is the object passed to Save and Load in order to store their state.

Detailed documentation is available in individual methods.

func (Map) AfterLoad

func (m Map) AfterLoad(fn func())

AfterLoad schedules a function execution when all objects have been allocated and their automated loading and customized load logic have been executed. fn will not be executed until all of current object's dependencies' AfterLoad() logic, if exist, have been executed.

func (Map) Context

func (m Map) Context() context.Context

Context returns the current context object.

func (Map) Failf

func (m Map) Failf(format string, args ...interface{})

Failf fails the save or restore with the provided message. Processing will stop after calling Failf, as the state package uses a panic & recover mechanism for state errors. You should defer any cleanup required.

func (Map) Load

func (m Map) Load(name string, objPtr interface{})

Load loads the given object from the map.

See Save for an example.

func (Map) LoadValue

func (m Map) LoadValue(name string, objPtr interface{}, fn func(interface{}))

LoadValue loads the given object value from the map.

See SaveValue for an example.

func (Map) LoadWait

func (m Map) LoadWait(name string, objPtr interface{})

LoadWait loads the given objects from the map, and marks it as requiring all AfterLoad executions to complete prior to running this object's AfterLoad.

See Save for an example.

func (Map) Save

func (m Map) Save(name string, objPtr interface{})

Save adds the given object to the map.

You should pass always pointers to the object you are saving. For example:

type X struct {
	A int
	B *int
}
func (x *X) Save(m Map) {
	m.Save("A", &x.A)
	m.Save("B", &x.B)
}
func (x *X) Load(m Map) {
	m.Load("A", &x.A)
	m.Load("B", &x.B)
}

func (Map) SaveValue

func (m Map) SaveValue(name string, obj interface{})

SaveValue adds the given object value to the map.

This should be used for values where pointers are not available, or casts are required during Save/Load.

For example, if we want to cast external package type P.Foo to int64:

type X struct {
	A P.Foo
}
func (x *X) Save(m Map) {
	m.SaveValue("A", int64(x.A))
}
func (x *X) Load(m Map) {
	m.LoadValue("A", new(int64), func(x interface{}) {
		x.A = P.Foo(x.(int64))
	})
}

type Stats

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

Stats tracks encode / decode timing.

This currently provides a meaningful String function and no other way to extract stats about individual types.

All exported receivers accept nil.

func (*Stats) Add

func (s *Stats) Add(obj reflect.Value)

Add adds a sample count.

func (*Stats) Done

func (s *Stats) Done()

Done finishes the current sample.

func (*Stats) Remove

func (s *Stats) Remove(obj reflect.Value)

Remove removes a sample count. It should only be called after a previous Add().

func (*Stats) Start

func (s *Stats) Start(obj reflect.Value)

Start starts a sample.

func (*Stats) String

func (s *Stats) String() string

String returns a table representation of the stats.

Directories

Path Synopsis
Package statefile defines the state file data stream.
Package statefile defines the state file data stream.

Jump to

Keyboard shortcuts

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