8 "github.com/docker/libnetwork/datastore"
11 type endpointCnt struct {
19 const epCntKeyPrefix = "endpoint_count"
21 func (ec *endpointCnt) Key() []string {
25 return []string{epCntKeyPrefix, ec.n.id}
28 func (ec *endpointCnt) KeyPrefix() []string {
32 return []string{epCntKeyPrefix, ec.n.id}
35 func (ec *endpointCnt) Value() []byte {
39 b, err := json.Marshal(ec)
46 func (ec *endpointCnt) SetValue(value []byte) error {
50 return json.Unmarshal(value, &ec)
53 func (ec *endpointCnt) Index() uint64 {
59 func (ec *endpointCnt) SetIndex(index uint64) {
66 func (ec *endpointCnt) Exists() bool {
72 func (ec *endpointCnt) Skip() bool {
78 func (ec *endpointCnt) New() datastore.KVObject {
87 func (ec *endpointCnt) CopyTo(o datastore.KVObject) error {
91 dstEc := o.(*endpointCnt)
93 dstEc.Count = ec.Count
94 dstEc.dbExists = ec.dbExists
95 dstEc.dbIndex = ec.dbIndex
100 func (ec *endpointCnt) DataScope() string {
101 return ec.n.DataScope()
104 func (ec *endpointCnt) EndpointCnt() uint64 {
111 func (ec *endpointCnt) updateStore() error {
112 store := ec.n.getController().getStore(ec.DataScope())
114 return fmt.Errorf("store not found for scope %s on endpoint count update", ec.DataScope())
116 // make a copy of count and n to avoid being overwritten by store.GetObject
117 count := ec.EndpointCnt()
120 if err := ec.n.getController().updateToStore(ec); err == nil || err != datastore.ErrKeyModified {
123 if err := store.GetObject(datastore.Key(ec.Key()...), ec); err != nil {
124 return fmt.Errorf("could not update the kvobject to latest on endpoint count update: %v", err)
133 func (ec *endpointCnt) setCnt(cnt uint64) error {
137 return ec.updateStore()
140 func (ec *endpointCnt) atomicIncDecEpCnt(inc bool) error {
152 store := ec.n.getController().getStore(ec.DataScope())
154 return fmt.Errorf("store not found for scope %s", ec.DataScope())
157 if err := ec.n.getController().updateToStore(ec); err != nil {
158 if err == datastore.ErrKeyModified {
159 if err := store.GetObject(datastore.Key(ec.Key()...), ec); err != nil {
160 return fmt.Errorf("could not update the kvobject to latest when trying to atomic add endpoint count: %v", err)
172 func (ec *endpointCnt) IncEndpointCnt() error {
173 return ec.atomicIncDecEpCnt(true)
176 func (ec *endpointCnt) DecEndpointCnt() error {
177 return ec.atomicIncDecEpCnt(false)