memstore.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2021 IBM Corp and others.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0/
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Seth Hoenig
  15. * Allan Stockdill-Mander
  16. * Mike Robertson
  17. */
  18. package mqtt
  19. import (
  20. "sync"
  21. "github.com/eclipse/paho.mqtt.golang/packets"
  22. )
  23. // MemoryStore implements the store interface to provide a "persistence"
  24. // mechanism wholly stored in memory. This is only useful for
  25. // as long as the client instance exists.
  26. type MemoryStore struct {
  27. sync.RWMutex
  28. messages map[string]packets.ControlPacket
  29. opened bool
  30. }
  31. // NewMemoryStore returns a pointer to a new instance of
  32. // MemoryStore, the instance is not initialized and ready to
  33. // use until Open() has been called on it.
  34. func NewMemoryStore() *MemoryStore {
  35. store := &MemoryStore{
  36. messages: make(map[string]packets.ControlPacket),
  37. opened: false,
  38. }
  39. return store
  40. }
  41. // Open initializes a MemoryStore instance.
  42. func (store *MemoryStore) Open() {
  43. store.Lock()
  44. defer store.Unlock()
  45. store.opened = true
  46. DEBUG.Println(STR, "memorystore initialized")
  47. }
  48. // Put takes a key and a pointer to a Message and stores the
  49. // message.
  50. func (store *MemoryStore) Put(key string, message packets.ControlPacket) {
  51. store.Lock()
  52. defer store.Unlock()
  53. if !store.opened {
  54. ERROR.Println(STR, "Trying to use memory store, but not open")
  55. return
  56. }
  57. store.messages[key] = message
  58. }
  59. // Get takes a key and looks in the store for a matching Message
  60. // returning either the Message pointer or nil.
  61. func (store *MemoryStore) Get(key string) packets.ControlPacket {
  62. store.RLock()
  63. defer store.RUnlock()
  64. if !store.opened {
  65. ERROR.Println(STR, "Trying to use memory store, but not open")
  66. return nil
  67. }
  68. mid := mIDFromKey(key)
  69. m := store.messages[key]
  70. if m == nil {
  71. CRITICAL.Println(STR, "memorystore get: message", mid, "not found")
  72. } else {
  73. DEBUG.Println(STR, "memorystore get: message", mid, "found")
  74. }
  75. return m
  76. }
  77. // All returns a slice of strings containing all the keys currently
  78. // in the MemoryStore.
  79. func (store *MemoryStore) All() []string {
  80. store.RLock()
  81. defer store.RUnlock()
  82. if !store.opened {
  83. ERROR.Println(STR, "Trying to use memory store, but not open")
  84. return nil
  85. }
  86. var keys []string
  87. for k := range store.messages {
  88. keys = append(keys, k)
  89. }
  90. return keys
  91. }
  92. // Del takes a key, searches the MemoryStore and if the key is found
  93. // deletes the Message pointer associated with it.
  94. func (store *MemoryStore) Del(key string) {
  95. store.Lock()
  96. defer store.Unlock()
  97. if !store.opened {
  98. ERROR.Println(STR, "Trying to use memory store, but not open")
  99. return
  100. }
  101. mid := mIDFromKey(key)
  102. m := store.messages[key]
  103. if m == nil {
  104. WARN.Println(STR, "memorystore del: message", mid, "not found")
  105. } else {
  106. delete(store.messages, key)
  107. DEBUG.Println(STR, "memorystore del: message", mid, "was deleted")
  108. }
  109. }
  110. // Close will disallow modifications to the state of the store.
  111. func (store *MemoryStore) Close() {
  112. store.Lock()
  113. defer store.Unlock()
  114. if !store.opened {
  115. ERROR.Println(STR, "Trying to close memory store, but not open")
  116. return
  117. }
  118. store.opened = false
  119. DEBUG.Println(STR, "memorystore closed")
  120. }
  121. // Reset eliminates all persisted message data in the store.
  122. func (store *MemoryStore) Reset() {
  123. store.Lock()
  124. defer store.Unlock()
  125. if !store.opened {
  126. ERROR.Println(STR, "Trying to reset memory store, but not open")
  127. }
  128. store.messages = make(map[string]packets.ControlPacket)
  129. WARN.Println(STR, "memorystore wiped")
  130. }