2019-05-17 17:33:33 +08:00
|
|
|
package pstoremem
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
|
|
|
|
|
|
|
pstore "github.com/libp2p/go-libp2p-peerstore"
|
|
|
|
)
|
|
|
|
|
|
|
|
type protoSegment struct {
|
2019-05-17 17:37:46 +08:00
|
|
|
sync.RWMutex
|
2019-05-17 17:33:33 +08:00
|
|
|
protocols map[peer.ID]map[string]struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type protoSegments [256]*protoSegment
|
|
|
|
|
2019-05-17 18:14:39 +08:00
|
|
|
func (s *protoSegments) get(p peer.ID) *protoSegment {
|
2019-05-18 04:36:46 +08:00
|
|
|
return s[byte(p[len(p)-1])]
|
2019-05-17 17:33:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type memoryProtoBook struct {
|
|
|
|
segments protoSegments
|
2019-05-17 17:47:07 +08:00
|
|
|
|
|
|
|
lk sync.RWMutex
|
|
|
|
interned map[string]string
|
2019-05-17 17:33:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ pstore.ProtoBook = (*memoryProtoBook)(nil)
|
|
|
|
|
|
|
|
func NewProtoBook() pstore.ProtoBook {
|
|
|
|
return &memoryProtoBook{
|
2019-05-28 05:35:05 +08:00
|
|
|
interned: make(map[string]string, 256),
|
2019-05-17 17:33:33 +08:00
|
|
|
segments: func() (ret protoSegments) {
|
|
|
|
for i := range ret {
|
|
|
|
ret[i] = &protoSegment{
|
|
|
|
protocols: make(map[peer.ID]map[string]struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 17:47:07 +08:00
|
|
|
func (pb *memoryProtoBook) internProtocol(proto string) string {
|
|
|
|
// check if it is interned with the read lock
|
|
|
|
pb.lk.RLock()
|
|
|
|
interned, ok := pb.interned[proto]
|
|
|
|
pb.lk.RUnlock()
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
return interned
|
|
|
|
}
|
|
|
|
|
|
|
|
// intern with the write lock
|
|
|
|
pb.lk.Lock()
|
|
|
|
defer pb.lk.Unlock()
|
|
|
|
|
|
|
|
// check again in case it got interned in between locks
|
|
|
|
interned, ok = pb.interned[proto]
|
|
|
|
if ok {
|
|
|
|
return interned
|
|
|
|
}
|
|
|
|
|
|
|
|
pb.interned[proto] = proto
|
|
|
|
return proto
|
|
|
|
}
|
|
|
|
|
2019-05-17 17:33:33 +08:00
|
|
|
func (pb *memoryProtoBook) SetProtocols(p peer.ID, protos ...string) error {
|
|
|
|
s := pb.segments.get(p)
|
2019-05-17 17:37:46 +08:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
2019-05-17 17:33:33 +08:00
|
|
|
|
|
|
|
newprotos := make(map[string]struct{}, len(protos))
|
|
|
|
for _, proto := range protos {
|
2019-05-17 17:47:07 +08:00
|
|
|
newprotos[pb.internProtocol(proto)] = struct{}{}
|
2019-05-17 17:33:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
s.protocols[p] = newprotos
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pb *memoryProtoBook) AddProtocols(p peer.ID, protos ...string) error {
|
|
|
|
s := pb.segments.get(p)
|
2019-05-17 17:37:46 +08:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
2019-05-17 17:33:33 +08:00
|
|
|
|
|
|
|
protomap, ok := s.protocols[p]
|
|
|
|
if !ok {
|
|
|
|
protomap = make(map[string]struct{})
|
|
|
|
s.protocols[p] = protomap
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, proto := range protos {
|
2019-05-17 17:47:07 +08:00
|
|
|
protomap[pb.internProtocol(proto)] = struct{}{}
|
2019-05-17 17:33:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pb *memoryProtoBook) GetProtocols(p peer.ID) ([]string, error) {
|
|
|
|
s := pb.segments.get(p)
|
2019-05-17 17:37:46 +08:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
2019-05-17 17:33:33 +08:00
|
|
|
|
|
|
|
out := make([]string, 0, len(s.protocols))
|
|
|
|
for k := range s.protocols[p] {
|
|
|
|
out = append(out, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pb *memoryProtoBook) SupportsProtocols(p peer.ID, protos ...string) ([]string, error) {
|
|
|
|
s := pb.segments.get(p)
|
2019-05-17 17:37:46 +08:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
2019-05-17 17:33:33 +08:00
|
|
|
|
|
|
|
out := make([]string, 0, len(protos))
|
|
|
|
for _, proto := range protos {
|
|
|
|
if _, ok := s.protocols[p][proto]; ok {
|
|
|
|
out = append(out, proto)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|