state

package
v0.0.0-...-d1ab4a3 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2015 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrOpenTx error = errors.New("tx: transaction is already open")
	ErrNoTx   error = errors.New("tx: no open transaction")
)
View Source
var (
	ErrNoSuchKey error = errors.New("state: no such key")
)

Functions

This section is empty.

Types

type Dict

type Dict interface {
	// Name returns the name the dictionary.
	Name() string

	// Get the value associated with k.
	Get(key string) (val interface{}, err error)
	// Associate value with the key.
	Put(key string, val interface{}) error
	// Del deletes key from dictionary.
	Del(key string) error
	// ForEach iterates over all entries in the dictionary, and invokes f for
	// each entry.
	ForEach(f IterFn)
}

Dict is a simple key-value store.

type InMem

type InMem struct {
	InMemDicts map[string]*inMemDict
}

InMem is a simple dictionary that uses in memory maps.

func NewInMem

func NewInMem() *InMem

NewInMem creates a new InMem state.

func (*InMem) Dict

func (s *InMem) Dict(name string) Dict

func (*InMem) Dicts

func (s *InMem) Dicts() []Dict

func (*InMem) Restore

func (s *InMem) Restore(b []byte) error

func (*InMem) Save

func (s *InMem) Save() ([]byte, error)

type IterFn

type IterFn func(key string, val interface{}) (next bool)

IterFn is the function used to iterate the entries of a dictionary. If it returns false the foreach loop will stop.

type Op

type Op struct {
	T OpType
	D string      // Dictionary.
	K string      // Key.
	V interface{} // Value.
}

Op is a state operation in a transaction.

type OpType

type OpType int

OpType is the type of an operation in a transaction.

const (
	Unknown OpType = iota
	Put
	Del
)

Valid values for OpType.

type State

type State interface {
	// Returns a dictionary for this state. Creates one if it does not exist.
	Dict(name string) Dict
	// Dicts returns all the dictionaries created in the state.
	Dicts() []Dict
	// Save save the state into bytes.
	Save() ([]byte, error)
	// Restore restores the state from b.
	Restore(b []byte) error
}

State is a collection of dictionaries.

type Transactional

type Transactional struct {
	State
	// contains filtered or unexported fields
}

Transactional wraps any state dictionary and makes it transactional.

func NewTransactional

func NewTransactional(s State) *Transactional

NewTransactional wraps s and writtens a transactional state.

func (*Transactional) AbortTx

func (t *Transactional) AbortTx() error

func (*Transactional) Apply

func (t *Transactional) Apply(ops []Op) error

func (*Transactional) BeginTx

func (t *Transactional) BeginTx() error

func (*Transactional) CommitTx

func (t *Transactional) CommitTx() error

func (*Transactional) Dict

func (t *Transactional) Dict(name string) Dict

func (*Transactional) HasEmptyTx

func (t *Transactional) HasEmptyTx() bool

func (*Transactional) Reset

func (t *Transactional) Reset()

func (*Transactional) Restore

func (t *Transactional) Restore(b []byte) error

func (*Transactional) Save

func (t *Transactional) Save() ([]byte, error)

func (*Transactional) Tx

func (t *Transactional) Tx() Tx

func (*Transactional) TxOps

func (t *Transactional) TxOps() []Op

func (*Transactional) TxStatus

func (t *Transactional) TxStatus() TxStatus

type Tx

type Tx struct {
	Ops    []Op     // State operations in this tx.
	Status TxStatus // Status of this tx.
}

Tx represents the side effects of an operation: messages emitted during the transaction as well as state operations.

func (*Tx) AddOp

func (t *Tx) AddOp(op Op)

AddOp adds a state operation to this transaction.

func (*Tx) IsEmpty

func (t *Tx) IsEmpty() bool

IsEmpty returns true if there is no operations in the tx.

func (*Tx) IsOpen

func (t *Tx) IsOpen() bool

IsOpen returns true if the transaction is open.

func (*Tx) Reset

func (t *Tx) Reset()

Reset simply clears operations of the transaction.

func (Tx) String

func (t Tx) String() string

type TxDict

type TxDict struct {
	Dict   Dict
	Status TxStatus
	Ops    map[string]Op
}

TxDict implements the Dict interface, and wraps any dictionary and make it transactional. All modifications will fail if there is no open tx.

func (*TxDict) AbortTx

func (d *TxDict) AbortTx() error

func (*TxDict) BeginTx

func (d *TxDict) BeginTx() error

func (*TxDict) CommitTx

func (d *TxDict) CommitTx() error

func (*TxDict) Del

func (d *TxDict) Del(k string) error

func (*TxDict) ForEach

func (d *TxDict) ForEach(f IterFn)

func (*TxDict) Get

func (d *TxDict) Get(k string) (interface{}, error)

func (*TxDict) Name

func (d *TxDict) Name() string

func (*TxDict) Put

func (d *TxDict) Put(k string, v interface{}) error

type TxStatus

type TxStatus uint8

TxStatus represents the status of a transaction.

const (
	TxNone TxStatus = iota
	TxOpen          = iota
)

Valid values for TxStatus

Jump to

Keyboard shortcuts

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