-
Notifications
You must be signed in to change notification settings - Fork 3
/
keys_storage.go
95 lines (82 loc) · 2.04 KB
/
keys_storage.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
package doubleratchet
// KeysStorage is an interface of an abstract in-memory or persistent keys storage.
type KeysStorage interface {
// Get returns a message key by the given key and message number.
Get(k Key, msgNum uint) (mk Key, ok bool)
// Put saves the given mk under the specified key and msgNum.
Put(k Key, msgNum uint, mk Key)
// DeleteMk ensures there's no message key under the specified key and msgNum.
DeleteMk(k Key, msgNum uint)
// DeletePk ensures there's no message keys under the specified key.
DeletePk(k Key)
// Count returns number of message keys stored under the specified key.
Count(k Key) uint
// All returns all the keys
All() map[Key]map[uint]Key
}
// KeysStorageInMemory is an in-memory message keys storage.
type KeysStorageInMemory struct {
keys map[Key]map[uint]Key
}
// See KeysStorage.
func (s *KeysStorageInMemory) Get(pubKey Key, msgNum uint) (Key, bool) {
if s.keys == nil {
return Key{}, false
}
msgs, ok := s.keys[pubKey]
if !ok {
return Key{}, false
}
mk, ok := msgs[msgNum]
if !ok {
return Key{}, false
}
return mk, true
}
// See KeysStorage.
func (s *KeysStorageInMemory) Put(pubKey Key, msgNum uint, mk Key) {
if s.keys == nil {
s.keys = make(map[Key]map[uint]Key)
}
if _, ok := s.keys[pubKey]; !ok {
s.keys[pubKey] = make(map[uint]Key)
}
s.keys[pubKey][msgNum] = mk
}
// See KeysStorage.
func (s *KeysStorageInMemory) DeleteMk(pubKey Key, msgNum uint) {
if s.keys == nil {
return
}
if _, ok := s.keys[pubKey]; !ok {
return
}
if _, ok := s.keys[pubKey][msgNum]; !ok {
return
}
delete(s.keys[pubKey], msgNum)
if len(s.keys[pubKey]) == 0 {
delete(s.keys, pubKey)
}
}
// See KeysStorage.
func (s *KeysStorageInMemory) DeletePk(pubKey Key) {
if s.keys == nil {
return
}
if _, ok := s.keys[pubKey]; !ok {
return
}
delete(s.keys, pubKey)
}
// See KeysStorage.
func (s *KeysStorageInMemory) Count(pubKey Key) uint {
if s.keys == nil {
return 0
}
return uint(len(s.keys[pubKey]))
}
// See KeysStorage.
func (s *KeysStorageInMemory) All() map[Key]map[uint]Key {
return s.keys
}