-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstore.go
138 lines (112 loc) · 2.65 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package memory
import (
"context"
"sync"
"github.com/code-payments/code-server/pkg/code/data/push"
"github.com/code-payments/code-server/pkg/code/data/user"
)
type store struct {
mu sync.Mutex
records []*push.Record
last uint64
}
// New returns a new in memory push.Store
func New() push.Store {
return &store{}
}
// Put implements push.Store.Put
func (s *store) Put(_ context.Context, record *push.Record) error {
if err := record.Validate(); err != nil {
return err
}
s.mu.Lock()
defer s.mu.Unlock()
s.last++
if item := s.find(record); item != nil {
return push.ErrTokenExists
} else {
record.Id = s.last
s.records = append(s.records, record.Clone())
}
return nil
}
// MarkAsInvalid implements push.Store.MarkAsInvalid
func (s *store) MarkAsInvalid(ctx context.Context, pushToken string) error {
s.mu.Lock()
defer s.mu.Unlock()
items := s.findByPushToken(pushToken)
for _, item := range items {
item.IsValid = false
}
return nil
}
// Delete implements push.Store.Delete
func (s *store) Delete(ctx context.Context, pushToken string) error {
s.mu.Lock()
defer s.mu.Unlock()
var updated []*push.Record
for _, item := range s.records {
if item.PushToken != pushToken {
updated = append(updated, item)
}
}
s.records = updated
return nil
}
// GetAllValidByDataContainer implements push.Store.GetAllValidByDataContainer
func (s *store) GetAllValidByDataContainer(_ context.Context, id *user.DataContainerID) ([]*push.Record, error) {
s.mu.Lock()
defer s.mu.Unlock()
res := s.findByDataContainer(id)
res = s.filterInvalid(res)
if len(res) == 0 {
return nil, push.ErrTokenNotFound
}
return res, nil
}
func (s *store) find(data *push.Record) *push.Record {
for _, item := range s.records {
if item.Id == data.Id {
return item
}
if item.DataContainerId.String() == data.DataContainerId.String() && item.PushToken == data.PushToken {
return item
}
}
return nil
}
func (s *store) findByPushToken(pushToken string) []*push.Record {
var res []*push.Record
for _, item := range s.records {
if item.PushToken != pushToken {
continue
}
res = append(res, item)
}
return res
}
func (s *store) findByDataContainer(id *user.DataContainerID) []*push.Record {
var res []*push.Record
for _, item := range s.records {
if item.DataContainerId.String() != id.String() {
continue
}
res = append(res, item)
}
return res
}
func (s *store) filterInvalid(items []*push.Record) []*push.Record {
var res []*push.Record
for _, item := range items {
if item.IsValid {
res = append(res, item)
}
}
return res
}
func (s *store) reset() {
s.mu.Lock()
defer s.mu.Unlock()
s.records = nil
s.last = 0
}