github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/libnetwork/bitseq/store.go (about)

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