/usr/share/gocode/src/github.com/docker/libnetwork/idm/idm.go is in golang-github-docker-libnetwork-dev 0.8.0-dev.2+git20170202.599.45b4086-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | // Package idm manages reservation/release of numerical ids from a configured set of contiguous ids
package idm
import (
"fmt"
"github.com/docker/libnetwork/bitseq"
"github.com/docker/libnetwork/datastore"
)
// Idm manages the reservation/release of numerical ids from a contiguous set
type Idm struct {
start uint64
end uint64
handle *bitseq.Handle
}
// New returns an instance of id manager for a [start,end] set of numerical ids
func New(ds datastore.DataStore, id string, start, end uint64) (*Idm, error) {
if id == "" {
return nil, fmt.Errorf("Invalid id")
}
if end <= start {
return nil, fmt.Errorf("Invalid set range: [%d, %d]", start, end)
}
h, err := bitseq.NewHandle("idm", ds, id, 1+end-start)
if err != nil {
return nil, fmt.Errorf("failed to initialize bit sequence handler: %s", err.Error())
}
return &Idm{start: start, end: end, handle: h}, nil
}
// GetID returns the first available id in the set
func (i *Idm) GetID() (uint64, error) {
if i.handle == nil {
return 0, fmt.Errorf("ID set is not initialized")
}
ordinal, err := i.handle.SetAny()
return i.start + ordinal, err
}
// GetSpecificID tries to reserve the specified id
func (i *Idm) GetSpecificID(id uint64) error {
if i.handle == nil {
return fmt.Errorf("ID set is not initialized")
}
if id < i.start || id > i.end {
return fmt.Errorf("Requested id does not belong to the set")
}
return i.handle.Set(id - i.start)
}
// GetIDInRange returns the first available id in the set within a [start,end] range
func (i *Idm) GetIDInRange(start, end uint64) (uint64, error) {
if i.handle == nil {
return 0, fmt.Errorf("ID set is not initialized")
}
if start < i.start || end > i.end {
return 0, fmt.Errorf("Requested range does not belong to the set")
}
ordinal, err := i.handle.SetAnyInRange(start-i.start, end-i.start)
return i.start + ordinal, err
}
// Release releases the specified id
func (i *Idm) Release(id uint64) {
i.handle.Unset(id - i.start)
}
|