github.com/adityamillind98/moby@v23.0.0-rc.4+incompatible/libnetwork/bitseq/store.go (about)

     1  package bitseq
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/docker/docker/libnetwork/datastore"
     7  	"github.com/docker/docker/libnetwork/types"
     8  )
     9  
    10  // Key provides the Key to be used in KV Store
    11  func (h *Handle) Key() []string {
    12  	h.Lock()
    13  	defer h.Unlock()
    14  	return []string{h.app, h.id}
    15  }
    16  
    17  // KeyPrefix returns the immediate parent key that can be used for tree walk
    18  func (h *Handle) KeyPrefix() []string {
    19  	h.Lock()
    20  	defer h.Unlock()
    21  	return []string{h.app}
    22  }
    23  
    24  // Value marshals the data to be stored in the KV store
    25  func (h *Handle) Value() []byte {
    26  	b, err := json.Marshal(h)
    27  	if err != nil {
    28  		return nil
    29  	}
    30  	return b
    31  }
    32  
    33  // SetValue unmarshals the data from the KV store
    34  func (h *Handle) SetValue(value []byte) error {
    35  	return json.Unmarshal(value, h)
    36  }
    37  
    38  // Index returns the latest DB Index as seen by this object
    39  func (h *Handle) Index() uint64 {
    40  	h.Lock()
    41  	defer h.Unlock()
    42  	return h.dbIndex
    43  }
    44  
    45  // SetIndex method allows the datastore to store the latest DB Index into this object
    46  func (h *Handle) SetIndex(index uint64) {
    47  	h.Lock()
    48  	h.dbIndex = index
    49  	h.dbExists = true
    50  	h.Unlock()
    51  }
    52  
    53  // Exists method is true if this object has been stored in the DB.
    54  func (h *Handle) Exists() bool {
    55  	h.Lock()
    56  	defer h.Unlock()
    57  	return h.dbExists
    58  }
    59  
    60  // New method returns a handle based on the receiver handle
    61  func (h *Handle) New() datastore.KVObject {
    62  	h.Lock()
    63  	defer h.Unlock()
    64  
    65  	return &Handle{
    66  		app:   h.app,
    67  		store: h.store,
    68  	}
    69  }
    70  
    71  // CopyTo deep copies the handle into the passed destination object
    72  func (h *Handle) CopyTo(o datastore.KVObject) error {
    73  	h.Lock()
    74  	defer h.Unlock()
    75  
    76  	dstH := o.(*Handle)
    77  	if h == dstH {
    78  		return nil
    79  	}
    80  	dstH.Lock()
    81  	dstH.bits = h.bits
    82  	dstH.unselected = h.unselected
    83  	dstH.head = h.head.getCopy()
    84  	dstH.app = h.app
    85  	dstH.id = h.id
    86  	dstH.dbIndex = h.dbIndex
    87  	dstH.dbExists = h.dbExists
    88  	dstH.store = h.store
    89  	dstH.curr = h.curr
    90  	dstH.Unlock()
    91  
    92  	return nil
    93  }
    94  
    95  // Skip provides a way for a KV Object to avoid persisting it in the KV Store
    96  func (h *Handle) Skip() bool {
    97  	return false
    98  }
    99  
   100  // DataScope method returns the storage scope of the datastore
   101  func (h *Handle) DataScope() string {
   102  	h.Lock()
   103  	defer h.Unlock()
   104  
   105  	return h.store.Scope()
   106  }
   107  
   108  func (h *Handle) writeToStore() error {
   109  	h.Lock()
   110  	store := h.store
   111  	h.Unlock()
   112  	if store == nil {
   113  		return nil
   114  	}
   115  	err := store.PutObjectAtomic(h)
   116  	if err == datastore.ErrKeyModified {
   117  		return types.RetryErrorf("failed to perform atomic write (%v). Retry might fix the error", err)
   118  	}
   119  	return err
   120  }
   121  
   122  func (h *Handle) deleteFromStore() error {
   123  	h.Lock()
   124  	store := h.store
   125  	h.Unlock()
   126  	if store == nil {
   127  		return nil
   128  	}
   129  	return store.DeleteObjectAtomic(h)
   130  }