-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstore.go
121 lines (99 loc) · 2.74 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
package memory
import (
"context"
"encoding/binary"
"errors"
"sync"
"github.com/code-payments/code-server/pkg/code/data/contact"
"github.com/code-payments/code-server/pkg/code/data/user"
)
type store struct {
mu sync.RWMutex
contactsByOwner map[string][]string
}
// New returns a new postgres backed contact.Store
func New() contact.Store {
return &store{
contactsByOwner: make(map[string][]string),
}
}
// Add implements contact.Store.Add
func (s *store) Add(ctx context.Context, owner *user.DataContainerID, contact string) error {
s.mu.Lock()
defer s.mu.Unlock()
contacts := s.contactsByOwner[owner.String()]
for _, existing := range contacts {
if existing == contact {
return nil
}
}
contacts = append(contacts, contact)
s.contactsByOwner[owner.String()] = contacts
return nil
}
// BatchAdd implements contact.Store.BatchAdd
func (s *store) BatchAdd(ctx context.Context, owner *user.DataContainerID, contacts []string) error {
for _, contact := range contacts {
err := s.Add(ctx, owner, contact)
if err != nil {
return err
}
}
return nil
}
// Remove implements contact.Store.Remove
func (s *store) Remove(ctx context.Context, owner *user.DataContainerID, contact string) error {
s.mu.Lock()
defer s.mu.Unlock()
contacts := s.contactsByOwner[owner.String()]
for i, existing := range contacts {
if existing == contact {
s.contactsByOwner[owner.String()] = append(contacts[:i], contacts[i+1:]...)
return nil
}
}
return nil
}
// BatchRemove implements contact.Store.BatchRemove
func (s *store) BatchRemove(ctx context.Context, owner *user.DataContainerID, contacts []string) error {
for _, contact := range contacts {
err := s.Remove(ctx, owner, contact)
if err != nil {
return err
}
}
return nil
}
// Get implements contact.Store.Get
func (s *store) Get(ctx context.Context, owner *user.DataContainerID, limit uint32, pageToken []byte) ([]string, []byte, error) {
s.mu.RLock()
defer s.mu.RUnlock()
var lowerBound uint64
if len(pageToken) == 8 {
lowerBound = binary.LittleEndian.Uint64(pageToken)
} else if len(pageToken) != 0 {
return nil, nil, errors.New("invalid page token bytes")
}
contacts, ok := s.contactsByOwner[owner.String()]
if !ok {
return nil, nil, nil
}
if len(contacts) <= int(lowerBound) {
return nil, nil, nil
}
upperBound := lowerBound + uint64(limit)
if int(upperBound) > len(contacts) {
upperBound = uint64(len(contacts))
}
var nextPageToken []byte
if len(contacts) > int(upperBound) {
nextPageToken = make([]byte, 8)
binary.LittleEndian.PutUint64(nextPageToken, upperBound)
}
return contacts[lowerBound:upperBound], nextPageToken, nil
}
func (s *store) reset() {
s.mu.Lock()
defer s.mu.Unlock()
s.contactsByOwner = make(map[string][]string)
}