2018-09-05 01:07:44 +08:00
|
|
|
package pstoremem
|
2018-08-29 22:12:41 +08:00
|
|
|
|
|
|
|
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"
|
2018-08-29 22:12:41 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type memoryPeerMetadata struct {
|
|
|
|
// store other data, like versions
|
|
|
|
//ds ds.ThreadSafeDatastore
|
|
|
|
ds map[string]interface{}
|
|
|
|
dslock sync.Mutex
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:04:52 +08:00
|
|
|
var _ pstore.PeerMetadata = (*memoryPeerMetadata)(nil)
|
|
|
|
|
2018-08-30 21:13:27 +08:00
|
|
|
func NewPeerMetadata() pstore.PeerMetadata {
|
2018-08-29 22:12:41 +08:00
|
|
|
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 {
|
2018-08-30 21:13:27 +08:00
|
|
|
return nil, pstore.ErrNotFound
|
2018-08-29 22:12:41 +08:00
|
|
|
}
|
|
|
|
return i, nil
|
|
|
|
}
|