Documentation ¶
Index ¶
- Variables
- type AllowedReq
- type AllowedResp
- type Clock
- type Event
- type EventType
- type HistoryItem
- type IdentifierShortener
- type InMemoryShortener
- type SetInfo
- type ShortenedMap
- type Shortener
- type ShortenerFactory
- type VClock
- func FromBytes(context context.Context, data []byte, shortenerName string) (vc *VClock, err error)
- func FromBytesWithHistory(context context.Context, data []byte, shortenerName string) (vc *VClock, err error)
- func New(context context.Context, init Clock, shortenerName string) (*VClock, error)
- func NewWithHistory(context context.Context, init Clock, shortenerName string) (*VClock, error)
- func (vc *VClock) AncestorOf(other *VClock) (bool, error)
- func (vc *VClock) Bytes() ([]byte, error)
- func (vc *VClock) Close() error
- func (vc *VClock) Concurrent(other *VClock) (bool, error)
- func (vc *VClock) Copy() (*VClock, error)
- func (vc *VClock) DescendsFrom(other *VClock) (bool, error)
- func (vc *VClock) Equal(other *VClock) (bool, error)
- func (vc *VClock) Get(id string) (uint64, bool)
- func (vc *VClock) GetClock() (Clock, error)
- func (vc *VClock) GetFullHistory() ([]*HistoryItem, error)
- func (vc *VClock) GetHistory() ([]Clock, error)
- func (vc *VClock) LastUpdate() (string, uint64, error)
- func (vc *VClock) Merge(other *VClock) error
- func (vc *VClock) Prune() error
- func (vc *VClock) Set(id string, v uint64) error
- func (vc *VClock) Tick(id string) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrShortenerMustNotBeNil = errors.New("shortener cannot be nil")
Functions ¶
This section is empty.
Types ¶
type AllowedReq ¶ added in v1.1.0
type AllowedResp ¶ added in v1.1.0
type AllowedResp interface { *respClock | *respErr | bool | *respGetter | *respGetterWithStatus | *respHistory | *respHistoryAll }
type Event ¶
Event captures the details of a specific update to the vector clock. Only one of the attributes will contain information.
type HistoryItem ¶
HistoryItem stores details of a state change due to the specified Event, and holds the updated clock after the Event has been applied.
func (*HistoryItem) String ¶
func (h *HistoryItem) String() string
type IdentifierShortener ¶ added in v1.2.0
type IdentifierShortener interface { Name() string // Name of the shortener - msut be unique Shorten(s string) string // Returns the shortened version of the supplied string Recover(s string) (string, error) // Recovers the original string from the shortened version Bytes() ([]byte, error) // The full map of shortened strings to original strings as a serialised ShortenedMap Merge(b []byte) error // Merge the contents of the ShortenedMap into the instance }
IdentifierShortener provides functions to shorten vector clock identifiers to minimise the overall memory footprint of the clock.
type InMemoryShortener ¶ added in v1.2.0
type InMemoryShortener struct {
// contains filtered or unexported fields
}
InMemoryShortener uses a map to store the results of Shorten for a given string, so that it can be easily recovered.
func NewInMemoryShortener ¶ added in v1.3.0
func NewInMemoryShortener(name string, shortener Shortener) (*InMemoryShortener, error)
NewInMemoryShortener creates an instance of InMemoryShortener that will use the specified Shortener
func (*InMemoryShortener) Bytes ¶ added in v1.3.0
func (h *InMemoryShortener) Bytes() ([]byte, error)
func (*InMemoryShortener) Merge ¶ added in v1.3.0
func (h *InMemoryShortener) Merge(b []byte) error
func (*InMemoryShortener) Name ¶ added in v1.3.0
func (h *InMemoryShortener) Name() string
func (*InMemoryShortener) Recover ¶ added in v1.2.0
func (h *InMemoryShortener) Recover(s string) (string, error)
func (*InMemoryShortener) Shorten ¶ added in v1.2.0
func (h *InMemoryShortener) Shorten(s string) string
type SetInfo ¶
SetInfo stores the value to be applied to the vector clock for the specified identifier.
type ShortenedMap ¶ added in v1.3.0
ShortenedMap is the underlying type expected to be serialised by IdentifierShortener implementations
type ShortenerFactory ¶ added in v1.3.0
type ShortenerFactory struct {
// contains filtered or unexported fields
}
ShortenerFactory manages IdentifierShortener instances
func GetShortenerFactory ¶ added in v1.3.0
func GetShortenerFactory() *ShortenerFactory
GetShortenerFactory returns the ShortenerFactory
func (*ShortenerFactory) Get ¶ added in v1.3.0
func (f *ShortenerFactory) Get(name string) (IdentifierShortener, error)
Get returns the IdentifierShortener with the specified name, or an error if not found.
func (*ShortenerFactory) Names ¶ added in v1.3.0
func (f *ShortenerFactory) Names() []string
Names returns the list of shorteners in the factory
func (*ShortenerFactory) Register ¶ added in v1.3.0
func (f *ShortenerFactory) Register(shortener IdentifierShortener) error
Register adds the specified shortener, returns error if the shortener is already registered (i.e. key with the shortener name already exists)
type VClock ¶
type VClock struct {
// contains filtered or unexported fields
}
VClock is an instance of a vector clock that can suppport concurrent use across multiple goroutines
func FromBytes ¶
FromBytes decodes a vector clock. This requires both the serialised clock and also the name of the IdentifierShortener to be used (which may be empty string)
func FromBytesWithHistory ¶ added in v1.3.0
func FromBytesWithHistory(context context.Context, data []byte, shortenerName string) (vc *VClock, err error)
FromBytesWithHistory decodes a vector clock and preserves history from this point forwards. This requires both the serialised clock and also the name of the IdentifierShortener to be used (which may be empty string)
func New ¶
New returns a VClock that is initialised with the specified Clock details, and which will not maintain any history. The specified shortener (which may be empty string) reduces the memory footprint of the vector clock if the identifiers are large strings.
Example ¶
ctx := context.Background() c, _ := New(ctx, Clock{"x": 0, "y": 0}, "") defer c.Close() var wg sync.WaitGroup for i := 0; i < 10; i++ { wg.Add(1) go func(i int) { defer wg.Done() if i%2 == 0 { c.Tick("x") } else { c.Tick("y") } }(i) } wg.Wait() m, _ := c.GetClock() fmt.Println(m)
Output: map[x:5 y:5]
func NewWithHistory ¶ added in v1.1.0
NewWithHistory returns a VClock that is initialised with the specified Clock details, and which will maintain a full history of all updates. The specified shortener (which may be empty string) reduces the memory footprint of the vector clock if the identifiers are large strings.
func (*VClock) AncestorOf ¶
AncestorOf returns true if the contents of this clock instance shows that it can have descended from the other clock instance. This means that the other clock's identifiers must all be present in the this clock, and that the other clock's identifier values must all be the same or less than their value in this clock, with at least one of the other clock's identifier's value being less.
func (*VClock) Concurrent ¶
Concurrent returns true if the contents of the other clock are either completely or partially distinct. Where partially distinct, matching identifiers in the clocks must have the same value.
func (*VClock) Copy ¶
Copy creates a new VClock instance, initialised to the values of this instance
func (*VClock) DescendsFrom ¶
DescendsFrom returns true if the contents of the other clock shows that it can have descended from this clock instance. This means that this clock's identifiers must all be present in the other clock, and that this clock's identifier values must all be the same or less than their value in the other clock, with at least one identifier's value being less.
func (*VClock) Equal ¶
Equal returns true if the contents of the other clock exactly match this instance.
func (*VClock) Get ¶
Get returns the latest clock value for the specified identifier, returning true if the identifier is found, otherwise false
func (*VClock) GetFullHistory ¶
func (vc *VClock) GetFullHistory() ([]*HistoryItem, error)
GetFullHistory returns a copy of each state change of the vectory clock map, including the Event detail of the change as well as new state of the clock
func (*VClock) GetHistory ¶
GetHistory returns a copy of each state change of the vector clock map
func (*VClock) LastUpdate ¶
LastUpdate returns the latest clock time and its associated identifier
func (*VClock) Merge ¶
Merge combines this clock with the other clock. The other clock must not be nil, and neither must be closed