memstore.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git/packets"
  17. "sync"
  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. chkcond(store.opened)
  50. store.messages[key] = message
  51. }
  52. // Get takes a key and looks in the store for a matching Message
  53. // returning either the Message pointer or nil.
  54. func (store *MemoryStore) Get(key string) packets.ControlPacket {
  55. store.RLock()
  56. defer store.RUnlock()
  57. chkcond(store.opened)
  58. mid := mIDFromKey(key)
  59. m := store.messages[key]
  60. if m == nil {
  61. CRITICAL.Println(STR, "memorystore get: message", mid, "not found")
  62. } else {
  63. DEBUG.Println(STR, "memorystore get: message", mid, "found")
  64. }
  65. return m
  66. }
  67. // All returns a slice of strings containing all the keys currently
  68. // in the MemoryStore.
  69. func (store *MemoryStore) All() []string {
  70. store.RLock()
  71. defer store.RUnlock()
  72. chkcond(store.opened)
  73. keys := []string{}
  74. for k := range store.messages {
  75. keys = append(keys, k)
  76. }
  77. return keys
  78. }
  79. // Del takes a key, searches the MemoryStore and if the key is found
  80. // deletes the Message pointer associated with it.
  81. func (store *MemoryStore) Del(key string) {
  82. store.Lock()
  83. defer store.Unlock()
  84. mid := mIDFromKey(key)
  85. m := store.messages[key]
  86. if m == nil {
  87. WARN.Println(STR, "memorystore del: message", mid, "not found")
  88. } else {
  89. store.messages[key] = nil
  90. DEBUG.Println(STR, "memorystore del: message", mid, "was deleted")
  91. }
  92. }
  93. // Close will disallow modifications to the state of the store.
  94. func (store *MemoryStore) Close() {
  95. store.Lock()
  96. defer store.Unlock()
  97. chkcond(store.opened)
  98. store.opened = false
  99. DEBUG.Println(STR, "memorystore closed")
  100. }
  101. // Reset eliminates all persisted message data in the store.
  102. func (store *MemoryStore) Reset() {
  103. store.Lock()
  104. defer store.Unlock()
  105. chkcond(store.opened)
  106. store.messages = make(map[string]packets.ControlPacket)
  107. WARN.Println(STR, "memorystore wiped")
  108. }