go-libp2p-peerstore/pstoremem/metadata.go

46 lines
989 B
Go
Raw Normal View History

2018-09-05 01:07:44 +08:00
package pstoremem
import (
"sync"
2018-09-08 01:46:23 +08:00
peer "github.com/libp2p/go-libp2p-peer"
2018-08-30 23:24:09 +08:00
pstore "github.com/libp2p/go-libp2p-peerstore"
)
type memoryPeerMetadata struct {
// store other data, like versions
//ds ds.ThreadSafeDatastore
ds map[string]interface{}
dslock sync.Mutex
}
var _ pstore.PeerMetadata = (*memoryPeerMetadata)(nil)
func NewPeerMetadata() pstore.PeerMetadata {
return &memoryPeerMetadata{
ds: make(map[string]interface{}),
}
}
func (ps *memoryPeerMetadata) Put(p peer.ID, key string, val interface{}) error {
//dsk := ds.NewKey(string(p) + "/" + key)
//return ps.ds.Put(dsk, val)
ps.dslock.Lock()
defer ps.dslock.Unlock()
ps.ds[string(p)+"/"+key] = val
return nil
}
func (ps *memoryPeerMetadata) Get(p peer.ID, key string) (interface{}, error) {
//dsk := ds.NewKey(string(p) + "/" + key)
//return ps.ds.Get(dsk)
ps.dslock.Lock()
defer ps.dslock.Unlock()
i, ok := ps.ds[string(p)+"/"+key]
if !ok {
return nil, pstore.ErrNotFound
}
return i, nil
}