ipfs-cluster/map_state.go
Hector Sanjuan 805b867651 Use go-libp2p-rpc. Tests updated.
The former RPC stuff had become a monster, really hard to have an overview
of the RPC api capabilities and with lots of magic.

go-libp2p-rpc allows to have a clearly defined RPC api which
shows which methods every component can use. A component to perform
remote requests, and the convoluted LeaderRPC, BroadcastRPC methods are
no longer necessary.

Things are much simpler now, less goroutines are needed, the central channel
handling bottleneck is gone, RPC requests are very streamlined in form.

In the future, it would be inmediate to have components living on different
libp2p hosts and it is way clearer how to plug into the advanced cluster rpc
api.

License: MIT
Signed-off-by: Hector Sanjuan <hector@protocol.ai>
2016-12-27 18:19:54 +01:00

54 lines
947 B
Go

package ipfscluster
import (
"sync"
cid "github.com/ipfs/go-cid"
)
// MapState is a very simple database to store
// the state of the system.
type MapState struct {
mux sync.RWMutex
PinMap map[string]struct{}
}
func NewMapState() *MapState {
return &MapState{
PinMap: make(map[string]struct{}),
}
}
func (st *MapState) AddPin(c *cid.Cid) error {
st.mux.Lock()
defer st.mux.Unlock()
var a struct{}
st.PinMap[c.String()] = a
return nil
}
func (st *MapState) RmPin(c *cid.Cid) error {
st.mux.Lock()
defer st.mux.Unlock()
delete(st.PinMap, c.String())
return nil
}
func (st *MapState) HasPin(c *cid.Cid) bool {
st.mux.RLock()
defer st.mux.RUnlock()
_, ok := st.PinMap[c.String()]
return ok
}
func (st *MapState) ListPins() []*cid.Cid {
st.mux.RLock()
defer st.mux.RUnlock()
cids := make([]*cid.Cid, 0, len(st.PinMap))
for k, _ := range st.PinMap {
c, _ := cid.Decode(k)
cids = append(cids, c)
}
return cids
}