ipfs-cluster/state.go

50 lines
858 B
Go
Raw Normal View History

2016-12-02 18:33:39 +00:00
package ipfscluster
import (
"sync"
2016-12-02 18:33:39 +00:00
cid "github.com/ipfs/go-cid"
2016-12-02 18:33:39 +00:00
)
// MapState is a very simple database to store
// the state of the system.
2016-12-02 18:33:39 +00:00
type MapState struct {
mux sync.RWMutex
PinMap map[string]struct{}
rpcCh chan RPC
2016-12-02 18:33:39 +00:00
}
func NewMapState() *MapState {
return &MapState{
PinMap: make(map[string]struct{}),
rpcCh: make(chan RPC),
}
}
func (st *MapState) AddPin(c *cid.Cid) error {
st.mux.Lock()
defer st.mux.Unlock()
var a struct{}
st.PinMap[c.String()] = a
2016-12-02 18:33:39 +00:00
return nil
}
func (st *MapState) RmPin(c *cid.Cid) error {
st.mux.Lock()
defer st.mux.Unlock()
2016-12-02 18:33:39 +00:00
delete(st.PinMap, c.String())
return nil
}
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
}