memstore.go 3.5 KB

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