ApplyChanges commits removals and upserts in one lock scope. It returns the keys actually removed from the store.
(removeKeys []string, upsertPeers []*PeerInfo)
| 40 | // ApplyChanges commits removals and upserts in one lock scope. |
| 41 | // It returns the keys actually removed from the store. |
| 42 | func (ps *PeerStore) ApplyChanges(removeKeys []string, upsertPeers []*PeerInfo) []string { |
| 43 | ps.mu.Lock() |
| 44 | defer ps.mu.Unlock() |
| 45 | |
| 46 | removed := make([]string, 0, len(removeKeys)) |
| 47 | |
| 48 | for _, key := range removeKeys { |
| 49 | if peer, exists := ps.peers[key]; exists { |
| 50 | delete(ps.emailToKey, peer.Email) |
| 51 | delete(ps.peers, key) |
| 52 | removed = append(removed, key) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | for _, peer := range upsertPeers { |
| 57 | if peer == nil { |
| 58 | continue |
| 59 | } |
| 60 | |
| 61 | peerCopy := clonePeerInfo(peer) |
| 62 | |
| 63 | if oldKey, exists := ps.emailToKey[peerCopy.Email]; exists && oldKey != peerCopy.PublicKey.String() { |
| 64 | delete(ps.peers, oldKey) |
| 65 | } |
| 66 | |
| 67 | if existingPeer, exists := ps.peers[peerCopy.PublicKey.String()]; exists && existingPeer.Email != peerCopy.Email { |
| 68 | delete(ps.emailToKey, existingPeer.Email) |
| 69 | } |
| 70 | |
| 71 | ps.peers[peerCopy.PublicKey.String()] = peerCopy |
| 72 | ps.emailToKey[peerCopy.Email] = peerCopy.PublicKey.String() |
| 73 | } |
| 74 | |
| 75 | return removed |
| 76 | } |
| 77 | |
| 78 | // ReplaceAll completely replaces the peer store contents with the given peers. |
| 79 | // It returns a list of public keys that were removed in the process. |