1 // Package idm manages reservation/release of numerical ids from a configured set of contiguous ids
8 "github.com/docker/libnetwork/bitseq"
9 "github.com/docker/libnetwork/datastore"
12 // Idm manages the reservation/release of numerical ids from a contiguous set
19 // New returns an instance of id manager for a [start,end] set of numerical ids
20 func New(ds datastore.DataStore, id string, start, end uint64) (*Idm, error) {
22 return nil, errors.New("Invalid id")
25 return nil, fmt.Errorf("Invalid set range: [%d, %d]", start, end)
28 h, err := bitseq.NewHandle("idm", ds, id, 1+end-start)
30 return nil, fmt.Errorf("failed to initialize bit sequence handler: %s", err.Error())
33 return &Idm{start: start, end: end, handle: h}, nil
36 // GetID returns the first available id in the set
37 func (i *Idm) GetID() (uint64, error) {
39 return 0, errors.New("ID set is not initialized")
41 ordinal, err := i.handle.SetAny()
42 return i.start + ordinal, err
45 // GetSpecificID tries to reserve the specified id
46 func (i *Idm) GetSpecificID(id uint64) error {
48 return errors.New("ID set is not initialized")
51 if id < i.start || id > i.end {
52 return errors.New("Requested id does not belong to the set")
55 return i.handle.Set(id - i.start)
58 // GetIDInRange returns the first available id in the set within a [start,end] range
59 func (i *Idm) GetIDInRange(start, end uint64) (uint64, error) {
61 return 0, errors.New("ID set is not initialized")
64 if start < i.start || end > i.end {
65 return 0, errors.New("Requested range does not belong to the set")
68 ordinal, err := i.handle.SetAnyInRange(start-i.start, end-i.start)
70 return i.start + ordinal, err
73 // Release releases the specified id
74 func (i *Idm) Release(id uint64) {
75 i.handle.Unset(id - i.start)