forked from ckousik/go-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.go
54 lines (46 loc) · 1.08 KB
/
metadata.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
package pstoremem
import (
"sync"
"github.com/John-LittleBearLabs/go-libp2p/core/peer"
pstore "github.com/John-LittleBearLabs/go-libp2p/core/peerstore"
)
type memoryPeerMetadata struct {
// store other data, like versions
ds map[peer.ID]map[string]interface{}
dslock sync.RWMutex
}
var _ pstore.PeerMetadata = (*memoryPeerMetadata)(nil)
func NewPeerMetadata() *memoryPeerMetadata {
return &memoryPeerMetadata{
ds: make(map[peer.ID]map[string]interface{}),
}
}
func (ps *memoryPeerMetadata) Put(p peer.ID, key string, val interface{}) error {
ps.dslock.Lock()
defer ps.dslock.Unlock()
m, ok := ps.ds[p]
if !ok {
m = make(map[string]interface{})
ps.ds[p] = m
}
m[key] = val
return nil
}
func (ps *memoryPeerMetadata) Get(p peer.ID, key string) (interface{}, error) {
ps.dslock.RLock()
defer ps.dslock.RUnlock()
m, ok := ps.ds[p]
if !ok {
return nil, pstore.ErrNotFound
}
val, ok := m[key]
if !ok {
return nil, pstore.ErrNotFound
}
return val, nil
}
func (ps *memoryPeerMetadata) RemovePeer(p peer.ID) {
ps.dslock.Lock()
delete(ps.ds, p)
ps.dslock.Unlock()
}