idring

package
v2.0.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2018 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	IDRingOverflowError = common.NewError("No more available IDs")
)

Functions

This section is empty.

Types

type IDRing

type IDRing struct {
	// Ranges is an ordered set of ranges from which we allocate IDs.
	// Initially this should include a single Range.
	// When this is nil, it means that we have exhausted the entire
	// range from OrigMin to OrigMax, and, unless an ID is reclaimed, no more
	// IDs can be given out.
	// TODO this implementation is probably better (memory-wise) for a lot of allocations, with few
	// reclaimed IDs, as is often the case. But if there are intended to be relatively
	// few allocations and lots of reuse, a faster (less memory-efficient, which doesn't matter
	// at small sizes) would be better. Thus it may make sense to extract
	// an interface and split implementation into 2: intended for mostly allocations and few
	// reclamations, and intended for few allocations with a lot of reclamations.
	Ranges []Range

	OrigMin uint64
	OrigMax uint64
	// contains filtered or unexported fields
}

IDRing is responsible for allocating first available ID. Initially the available range is from OrigMin to OrigMax as provided in the creation. As IDs are allocated (see GetID), the range shrinks. IDs can be returned to the Ring (see ReclaimID). An instance of this SHOULD NOT be created directly, use NewIDRing() to create it.

func NewIDRing

func NewIDRing(min uint64, max uint64, locker sync.Locker) *IDRing

NewIDRing constructs a new IDRing instance with a single range for provided min and max

func (*IDRing) Clear

func (ir *IDRing) Clear()

Clear clears out the ring (reverts it to what it was when originally constructed)

func (*IDRing) GetID

func (ir *IDRing) GetID() (uint64, error)

GetID returns the first available ID, starting with OrigMin. It will return an IDRingOverflowError if no more IDs can be returned.

func (*IDRing) GetSpecificID

func (ir *IDRing) GetSpecificID(id uint64) error

GetSpecificID attempts to return a specific ID from the ring. Returns error if impossible.

func (IDRing) Invert

func (ir IDRing) Invert() *IDRing

Invert() returns an IDRing that is the inverse of this IDRing (that is, containing Ranges that are ranges not of available, but of taken IDs).

func (IDRing) IsEmpty

func (ir IDRing) IsEmpty() bool

IsEmpty returns true if there is are allocated IDs.

func (*IDRing) ReclaimID

func (ir *IDRing) ReclaimID(id uint64) error

ReclaimID returns and ID to the pool.

func (*IDRing) ReclaimIDNoLock

func (idRing *IDRing) ReclaimIDNoLock(id uint64) error

ReclaimIDNoLock returns the provided ID into the pool, so it can be returned again upon some future call to GetID.

func (*IDRing) ReclaimIDs

func (ir *IDRing) ReclaimIDs(ids []uint64) (error, []uint64)

ReclaimIDs reclaims a list of IDs. If an error occurs, what additionally a list of IDs that could not be reclaimed is returned.

func (IDRing) String

func (ir IDRing) String() string

String returns a human-readable representation of the ring.

type Range

type Range struct {
	Min uint64
	Max uint64
}

Range represents a range of uint64s that can be used as sequential IDs. The range is inclusive (meaning that the first available ID is Min, and the last is Max).

func Merge

func Merge(ranges []Range) []Range

Merge a slice of ranges combining adjacent ranges together and skipping included ranges.

func (Range) String

func (irr Range) String() string

Jump to

Keyboard shortcuts

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