Debian Codegraph @

server/store/inmemory.go

raw
1 /*
2 Copyright 2013-2014 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 package store
18
19 import (
20 "encoding/base64"
21 "encoding/json"
22 "fmt"
23 "strings"
24 "sync"
25 "time"
26
27 "gitlab.com/ubports/development/core/lomiri-push-service/protocol"
28 )
29
30 // one stored channel
31 type channel struct {
32 topLevel int64
33 notifications []protocol.Notification
34 meta []Metadata
35 }
36
37 // InMemoryPendingStore is a basic in-memory pending notification store.
38 type InMemoryPendingStore struct {
39 lock sync.Mutex
40 store map[InternalChannelId]*channel
41 }
42
43 // NewInMemoryPendingStore returns a new InMemoryStore.
44 func NewInMemoryPendingStore() *InMemoryPendingStore {
45 return &InMemoryPendingStore{
46 store: make(map[InternalChannelId]*channel),
47 }
48 }
49
50 func (sto *InMemoryPendingStore) Register(deviceId, appId string) (string, error) {
51 return base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s::%s", appId, deviceId))), nil
52 }
53
54 func (sto *InMemoryPendingStore) Unregister(deviceId, appId string) error {
55 // do nothing, tokens here are computed deterministically and not stored
56 return nil
57 }
58
59 func (sto *InMemoryPendingStore) GetInternalChannelIdFromToken(token, appId, userId, deviceId string) (InternalChannelId, error) {
60 if token != "" && appId != "" {
61 decoded, err := base64.StdEncoding.DecodeString(token)
62 if err != nil {
63 return "", ErrUnknownToken
64 }
65 token = string(decoded)
66 if !strings.HasPrefix(token, appId+"::") {
67 return "", ErrUnauthorized
68 }
69 deviceId := token[len(appId)+2:]
70 return UnicastInternalChannelId(deviceId, deviceId), nil
71 }
72 if userId != "" && deviceId != "" {
73 return UnicastInternalChannelId(userId, deviceId), nil
74 }
75 return "", ErrUnknownToken
76 }
77
78 func (sto *InMemoryPendingStore) GetInternalChannelId(name string) (InternalChannelId, error) {
79 if name == "system" {
80 return SystemInternalChannelId, nil
81 }
82 return InternalChannelId(""), ErrUnknownChannel
83 }
84
85 func (sto *InMemoryPendingStore) appendToChannel(chanId InternalChannelId, newNotification protocol.Notification, inc int64, meta1 Metadata) error {
86 sto.lock.Lock()
87 defer sto.lock.Unlock()
88 prev := sto.store[chanId]
89 if prev == nil {
90 prev = &channel{}
91 }
92 prev.topLevel += inc
93 prev.notifications = append(prev.notifications, newNotification)
94 prev.meta = append(prev.meta, meta1)
95 sto.store[chanId] = prev
96 return nil
97 }
98
99 func (sto *InMemoryPendingStore) AppendToChannel(chanId InternalChannelId, notificationPayload json.RawMessage, expiration time.Time) error {
100 newNotification := protocol.Notification{Payload: notificationPayload}
101 meta1 := Metadata{Expiration: expiration}
102 return sto.appendToChannel(chanId, newNotification, 1, meta1)
103 }
104
105 func (sto *InMemoryPendingStore) AppendToUnicastChannel(chanId InternalChannelId, appId string, notificationPayload json.RawMessage, msgId string, meta Metadata) error {
106 newNotification := protocol.Notification{
107 Payload: notificationPayload,
108 AppId: appId,
109 MsgId: msgId,
110 }
111 return sto.appendToChannel(chanId, newNotification, 0, meta)
112 }
113
114 func (sto *InMemoryPendingStore) getChannelUnfiltered(chanId InternalChannelId) (*channel, []protocol.Notification, []Metadata) {
115 channel, ok := sto.store[chanId]
116 if !ok {
117 return nil, nil, nil
118 }
119 n := len(channel.notifications)
120 res := make([]protocol.Notification, n)
121 meta := make([]Metadata, n)
122 copy(res, channel.notifications)
123 copy(meta, channel.meta)
124 return channel, res, meta
125 }
126
127 func (sto *InMemoryPendingStore) GetChannelUnfiltered(chanId InternalChannelId) (int64, []protocol.Notification, []Metadata, error) {
128 sto.lock.Lock()
129 defer sto.lock.Unlock()
130 channel, res, meta := sto.getChannelUnfiltered(chanId)
131 if channel == nil {
132 return 0, nil, nil, nil
133 }
134 return channel.topLevel, res, meta, nil
135 }
136
137 func (sto *InMemoryPendingStore) GetChannelSnapshot(chanId InternalChannelId) (int64, []protocol.Notification, error) {
138 topLevel, res, meta, _ := sto.GetChannelUnfiltered(chanId)
139 if res == nil {
140 return 0, nil, nil
141 }
142 res = FilterOutObsolete(res, meta)
143 return topLevel, res, nil
144 }
145
146 func (sto *InMemoryPendingStore) Scrub(chanId InternalChannelId, criteria ...string) error {
147 appId := ""
148 replaceTag := ""
149 switch len(criteria) {
150 case 2:
151 replaceTag = criteria[1]
152 fallthrough
153 case 1:
154 appId = criteria[0]
155 case 0:
156 default:
157 panic("Scrub() expects only up to two criterias")
158 }
159 sto.lock.Lock()
160 defer sto.lock.Unlock()
161 channel, res, meta := sto.getChannelUnfiltered(chanId)
162 if channel == nil {
163 return nil
164 }
165 fresh := FilterOutObsolete(res, meta)
166 res = make([]protocol.Notification, 0, len(fresh))
167 resMeta := make([]Metadata, 0, len(fresh))
168 i := 0
169 for j := range meta {
170 if meta[j].Obsolete {
171 continue
172 }
173 notif := fresh[i]
174 i++
175 if replaceTag != "" {
176 if notif.AppId == appId && meta[j].ReplaceTag == replaceTag {
177 continue
178 }
179 } else if notif.AppId == appId {
180 continue
181 }
182 res = append(res, notif)
183 resMeta = append(resMeta, meta[j])
184 }
185 // store as well
186 channel.notifications = res
187 channel.meta = resMeta
188 return nil
189 }
190
191 func (sto *InMemoryPendingStore) Close() {
192 // ignored
193 }
194
195 func (sto *InMemoryPendingStore) DropByMsgId(chanId InternalChannelId, targets []protocol.Notification) error {
196 sto.lock.Lock()
197 defer sto.lock.Unlock()
198 channel, ok := sto.store[chanId]
199 if !ok {
200 return nil
201 }
202 metaById := make(map[string]Metadata, len(channel.notifications))
203 for i, notif := range channel.notifications {
204 metaById[notif.MsgId] = channel.meta[i]
205 }
206 channel.notifications = FilterOutByMsgId(channel.notifications, targets)
207 resMeta := make([]Metadata, len(channel.notifications))
208 for i, notif := range channel.notifications {
209 resMeta[i] = metaById[notif.MsgId]
210 }
211 channel.meta = resMeta
212 return nil
213 }
214
215 // sanity check we implement the interface
216 var _ PendingStore = (*InMemoryPendingStore)(nil)