mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2025-01-08 01:30:08 +08:00
44 lines
901 B
Go
44 lines
901 B
Go
|
package peerstore
|
||
|
|
||
|
import (
|
||
|
"sync"
|
||
|
|
||
|
"github.com/libp2p/go-libp2p-peer"
|
||
|
)
|
||
|
|
||
|
type memoryPeerMetadata struct {
|
||
|
// store other data, like versions
|
||
|
//ds ds.ThreadSafeDatastore
|
||
|
// TODO: use a datastore for this
|
||
|
ds map[string]interface{}
|
||
|
dslock sync.Mutex
|
||
|
}
|
||
|
|
||
|
func NewPeerMetadata() 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, ErrNotFound
|
||
|
}
|
||
|
return i, nil
|
||
|
}
|