Debian Codegraph @

paho/store/memory/store.go

raw
1 /*
2 * Copyright (c) 2024 Contributors to the Eclipse Foundation
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 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
14 */
15
16 package memory
17
18 import (
19 "bytes"
20 "errors"
21 "fmt"
22 "io"
23 "sync"
24
25 "github.com/eclipse/paho.golang/packets"
26 )
27
28 var (
29 ErrNotInStore = errors.New("the requested ID was not found in the store") // Returned when requested ID not found
30 )
31
32 // memoryPacket is an element in the memory store
33 type memoryPacket struct {
34 c int // message count (used for ordering; as this is 32 bit min chance of rolling over seems remote)
35 p []byte // the packet we are storing
36 }
37
38 // New creates a Store
39 func New() *Store {
40 return &Store{
41 data: make(map[uint16]memoryPacket),
42 }
43
44 }
45
46 // Store is an implementation of a Store that stores the data in memory
47 type Store struct {
48 // server store - holds packets where the message ID was generated on the server
49 sync.Mutex
50 data map[uint16]memoryPacket // Holds messages initiated by the server (i.e. we will receive the PUBLISH)
51 c int // sequence counter used to maintain message order
52 }
53
54 // Put stores the packet
55 func (m *Store) Put(packetID uint16, packetType byte, w io.WriterTo) error {
56 m.Lock()
57 defer m.Unlock()
58 var buff bytes.Buffer
59
60 _, err := w.WriteTo(&buff)
61 if err != nil {
62 panic(err)
63 }
64
65 m.data[packetID] = memoryPacket{
66 c: m.c,
67 p: buff.Bytes(),
68 }
69 m.c++
70 return nil
71 }
72
73 func (m *Store) Get(packetID uint16) (io.ReadCloser, error) {
74 m.Lock()
75 defer m.Unlock()
76 d, ok := m.data[packetID]
77 if !ok {
78 return nil, ErrNotInStore
79 }
80 return io.NopCloser(bytes.NewReader(d.p)), nil
81 }
82
83 // Delete removes the message with the specified store ID
84 func (m *Store) Delete(id uint16) error {
85 m.Lock()
86 defer m.Unlock()
87 if _, ok := m.data[id]; !ok {
88 // This could be ignored, but reporting it may help reveal other issues
89 return fmt.Errorf("request to delete packet %d; packet not found", id)
90 }
91 delete(m.data, id)
92 return nil
93 }
94
95 // Quarantine is called if a corrupt packet is detected.
96 // There is little we can do other than deleting the packet.
97 func (m *Store) Quarantine(id uint16) error {
98 return m.Delete(id)
99 }
100
101 // List returns packet IDs in the order they were Put
102 func (m *Store) List() ([]uint16, error) {
103 m.Lock()
104 defer m.Unlock()
105
106 ids := make([]uint16, 0, len(m.data))
107 seq := make([]int, 0, len(m.data))
108
109 // Basic insert sort from map ordered by time
110 // As the map is relatively small, this should be quick enough (data is retrieved infrequently)
111 itemNo := 0
112 var pos int
113 for i, v := range m.data {
114 for pos = 0; pos < itemNo; pos++ {
115 if seq[pos] > v.c {
116 break
117 }
118 }
119 ids = append(ids[:pos], append([]uint16{i}, ids[pos:]...)...)
120 seq = append(seq[:pos], append([]int{v.c}, seq[pos:]...)...)
121 itemNo++
122 }
123 return ids, nil
124 }
125
126 // Reset clears the store (deleting all messages)
127 func (m *Store) Reset() error {
128 m.Lock()
129 defer m.Unlock()
130 m.data = make(map[uint16]memoryPacket)
131 return nil
132 }
133
134 // String is for debugging purposes; it dumps the content of the store in a readable format
135 func (m *Store) String() string {
136 var b bytes.Buffer
137 for i, c := range m.data {
138 p, err := packets.ReadPacket(bytes.NewReader(c.p))
139 if err != nil {
140 b.WriteString(fmt.Sprintf("packet %d could not be read: %s\n", i, err))
141 continue
142 }
143
144 b.WriteString(fmt.Sprintf("packet %d is %s\n", i, p))
145 }
146 return b.String()
147 }