store.go 2.6 KB

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