github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/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.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) fromDsValue(value []byte) error { 109 var ba []byte 110 if err := json.Unmarshal(value, &ba); err != nil { 111 return fmt.Errorf("failed to decode json: %s", err.Error()) 112 } 113 if err := h.FromByteArray(ba); err != nil { 114 return fmt.Errorf("failed to decode handle: %s", err.Error()) 115 } 116 return nil 117 } 118 119 func (h *Handle) writeToStore() error { 120 h.Lock() 121 store := h.store 122 h.Unlock() 123 if store == nil { 124 return nil 125 } 126 err := store.PutObjectAtomic(h) 127 if err == datastore.ErrKeyModified { 128 return types.RetryErrorf("failed to perform atomic write (%v). Retry might fix the error", err) 129 } 130 return err 131 } 132 133 func (h *Handle) deleteFromStore() error { 134 h.Lock() 135 store := h.store 136 h.Unlock() 137 if store == nil { 138 return nil 139 } 140 return store.DeleteObjectAtomic(h) 141 }