2015-06-15 14:43:02 -04:00
|
|
|
package bitseq
|
|
|
|
|
|
|
|
import (
|
2015-06-18 15:06:11 -04:00
|
|
|
"encoding/json"
|
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/datastore"
|
|
|
|
"github.com/docker/docker/libnetwork/types"
|
2015-06-15 14:43:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Key provides the Key to be used in KV Store
|
|
|
|
func (h *Handle) Key() []string {
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
2015-06-16 17:46:51 -04:00
|
|
|
return []string{h.app, h.id}
|
2015-06-15 14:43:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// KeyPrefix returns the immediate parent key that can be used for tree walk
|
|
|
|
func (h *Handle) KeyPrefix() []string {
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
2015-06-16 17:46:51 -04:00
|
|
|
return []string{h.app}
|
2015-06-15 14:43:02 -04:00
|
|
|
}
|
|
|
|
|
2015-06-15 21:28:00 -04:00
|
|
|
// Value marshals the data to be stored in the KV store
|
2015-06-15 14:43:02 -04:00
|
|
|
func (h *Handle) Value() []byte {
|
2015-10-05 17:53:25 -04:00
|
|
|
b, err := json.Marshal(h)
|
2015-06-15 14:43:02 -04:00
|
|
|
if err != nil {
|
2015-10-05 17:53:25 -04:00
|
|
|
return nil
|
2015-06-18 15:06:11 -04:00
|
|
|
}
|
2015-10-05 17:53:25 -04:00
|
|
|
return b
|
2015-06-15 14:43:02 -04:00
|
|
|
}
|
|
|
|
|
2015-06-18 18:13:38 -04:00
|
|
|
// SetValue unmarshals the data from the KV store
|
|
|
|
func (h *Handle) SetValue(value []byte) error {
|
2015-10-05 17:53:25 -04:00
|
|
|
return json.Unmarshal(value, h)
|
2015-06-18 18:13:38 -04:00
|
|
|
}
|
|
|
|
|
2015-06-15 14:43:02 -04:00
|
|
|
// Index returns the latest DB Index as seen by this object
|
|
|
|
func (h *Handle) Index() uint64 {
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
return h.dbIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetIndex method allows the datastore to store the latest DB Index into this object
|
|
|
|
func (h *Handle) SetIndex(index uint64) {
|
|
|
|
h.Lock()
|
|
|
|
h.dbIndex = index
|
2015-06-18 18:13:38 -04:00
|
|
|
h.dbExists = true
|
2015-06-15 14:43:02 -04:00
|
|
|
h.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-06-18 18:13:38 -04:00
|
|
|
// Exists method is true if this object has been stored in the DB.
|
|
|
|
func (h *Handle) Exists() bool {
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
return h.dbExists
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:24:44 -04:00
|
|
|
// New method returns a handle based on the receiver handle
|
|
|
|
func (h *Handle) New() datastore.KVObject {
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
|
|
|
|
return &Handle{
|
|
|
|
app: h.app,
|
|
|
|
store: h.store,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyTo deep copies the handle into the passed destination object
|
|
|
|
func (h *Handle) CopyTo(o datastore.KVObject) error {
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
|
|
|
|
dstH := o.(*Handle)
|
2016-03-29 14:56:39 -04:00
|
|
|
if h == dstH {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
dstH.Lock()
|
2015-10-05 07:24:44 -04:00
|
|
|
dstH.bits = h.bits
|
|
|
|
dstH.unselected = h.unselected
|
|
|
|
dstH.head = h.head.getCopy()
|
|
|
|
dstH.app = h.app
|
|
|
|
dstH.id = h.id
|
|
|
|
dstH.dbIndex = h.dbIndex
|
|
|
|
dstH.dbExists = h.dbExists
|
|
|
|
dstH.store = h.store
|
2017-05-31 16:33:51 -04:00
|
|
|
dstH.curr = h.curr
|
2016-03-29 14:56:39 -04:00
|
|
|
dstH.Unlock()
|
2015-10-05 07:24:44 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-22 10:09:39 -04:00
|
|
|
// Skip provides a way for a KV Object to avoid persisting it in the KV Store
|
|
|
|
func (h *Handle) Skip() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-09-16 07:39:46 -04:00
|
|
|
// DataScope method returns the storage scope of the datastore
|
2015-10-05 07:24:44 -04:00
|
|
|
func (h *Handle) DataScope() string {
|
2015-06-15 14:43:02 -04:00
|
|
|
h.Lock()
|
2015-10-05 07:24:44 -04:00
|
|
|
defer h.Unlock()
|
2015-06-15 14:43:02 -04:00
|
|
|
|
2015-10-05 07:24:44 -04:00
|
|
|
return h.store.Scope()
|
2015-06-15 14:43:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handle) writeToStore() error {
|
|
|
|
h.Lock()
|
|
|
|
store := h.store
|
|
|
|
h.Unlock()
|
|
|
|
if store == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := store.PutObjectAtomic(h)
|
|
|
|
if err == datastore.ErrKeyModified {
|
2015-06-24 15:10:54 -04:00
|
|
|
return types.RetryErrorf("failed to perform atomic write (%v). Retry might fix the error", err)
|
2015-06-15 14:43:02 -04:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handle) deleteFromStore() error {
|
|
|
|
h.Lock()
|
|
|
|
store := h.store
|
|
|
|
h.Unlock()
|
|
|
|
if store == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return store.DeleteObjectAtomic(h)
|
|
|
|
}
|