github.com/rawahars/moby@v24.0.4+incompatible/libnetwork/bitseq/store.go (about) 1 package bitseq 2 3 import ( 4 "encoding/json" 5 6 "github.com/docker/docker/libnetwork/bitmap" 7 "github.com/docker/docker/libnetwork/datastore" 8 "github.com/docker/docker/libnetwork/types" 9 ) 10 11 // Key provides the Key to be used in KV Store 12 func (h *Handle) Key() []string { 13 h.mu.Lock() 14 defer h.mu.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.mu.Lock() 21 defer h.mu.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.mu.Lock() 42 defer h.mu.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.mu.Lock() 49 h.dbIndex = index 50 h.dbExists = true 51 h.mu.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.mu.Lock() 57 defer h.mu.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.mu.Lock() 64 defer h.mu.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.mu.Lock() 75 defer h.mu.Unlock() 76 77 dstH := o.(*Handle) 78 if h == dstH { 79 return nil 80 } 81 dstH.mu.Lock() 82 defer dstH.mu.Unlock() 83 dstH.bm = bitmap.Copy(h.bm) 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 90 return nil 91 } 92 93 // Skip provides a way for a KV Object to avoid persisting it in the KV Store 94 func (h *Handle) Skip() bool { 95 return false 96 } 97 98 // DataScope method returns the storage scope of the datastore 99 func (h *Handle) DataScope() string { 100 h.mu.Lock() 101 defer h.mu.Unlock() 102 103 return h.store.Scope() 104 } 105 106 func (h *Handle) writeToStore() error { 107 h.mu.Lock() 108 store := h.store 109 h.mu.Unlock() 110 if store == nil { 111 return nil 112 } 113 err := store.PutObjectAtomic(h) 114 if err == datastore.ErrKeyModified { 115 return types.RetryErrorf("failed to perform atomic write (%v). Retry might fix the error", err) 116 } 117 return err 118 } 119 120 func (h *Handle) deleteFromStore() error { 121 h.mu.Lock() 122 store := h.store 123 h.mu.Unlock() 124 if store == nil { 125 return nil 126 } 127 return store.DeleteObjectAtomic(h) 128 }