Exemple #1
0
func (rs *s) Providers(c *cid.Cid) []pstore.PeerInfo {
	rs.delayConf.Query.Wait() // before locking

	rs.lock.RLock()
	defer rs.lock.RUnlock()
	k := c.KeyString()

	var ret []pstore.PeerInfo
	records, ok := rs.providers[k]
	if !ok {
		return ret
	}
	for _, r := range records {
		if time.Now().Sub(r.Created) > rs.delayConf.ValueVisibility.Get() {
			ret = append(ret, r.Peer)
		}
	}

	for i := range ret {
		j := rand.Intn(i + 1)
		ret[i], ret[j] = ret[j], ret[i]
	}

	return ret
}
Exemple #2
0
func (w *Wantlist) Remove(c *cid.Cid) bool {
	k := c.KeyString()
	e, ok := w.set[k]
	if !ok {
		return false
	}

	e.RefCnt--
	if e.RefCnt <= 0 {
		delete(w.set, k)
		return true
	}
	return false
}
Exemple #3
0
func (w *Wantlist) Add(c *cid.Cid, priority int) bool {
	k := c.KeyString()
	if e, ok := w.set[k]; ok {
		e.RefCnt++
		return false
	}

	w.set[k] = &Entry{
		Cid:      c,
		Priority: priority,
		RefCnt:   1,
	}

	return true
}
Exemple #4
0
// if ok == false has is inconclusive
// if ok == true then has respons to question: is it contained
func (b *arccache) hasCached(k *cid.Cid) (has bool, ok bool) {
	b.total.Inc()
	if k == nil {
		log.Error("nil cid in arccache")
		// Return cache invalid so the call to blockstore happens
		// in case of invalid key and correct error is created.
		return false, false
	}

	h, ok := b.arc.Get(k.KeyString())
	if ok {
		b.hits.Inc()
		return h.(bool), true
	}
	return false, false
}
Exemple #5
0
func (rs *s) Announce(p pstore.PeerInfo, c *cid.Cid) error {
	rs.lock.Lock()
	defer rs.lock.Unlock()

	k := c.KeyString()

	_, ok := rs.providers[k]
	if !ok {
		rs.providers[k] = make(map[peer.ID]providerRecord)
	}
	rs.providers[k][p.ID] = providerRecord{
		Created: time.Now(),
		Peer:    p,
	}
	return nil
}
Exemple #6
0
func (m *impl) addEntry(c *cid.Cid, priority int, cancel bool) {
	k := c.KeyString()
	e, exists := m.wantlist[k]
	if exists {
		e.Priority = priority
		e.Cancel = cancel
	} else {
		m.wantlist[k] = Entry{
			Entry: &wantlist.Entry{
				Cid:      c,
				Priority: priority,
			},
			Cancel: cancel,
		}
	}
}
Exemple #7
0
func (w *Wantlist) Contains(k *cid.Cid) (*Entry, bool) {
	e, ok := w.set[k.KeyString()]
	return e, ok
}
Exemple #8
0
func CidToDsKey(k *cid.Cid) ds.Key {
	return NewKeyFromBinary(k.KeyString())
}
Exemple #9
0
func (b *arccache) addCache(c *cid.Cid, has bool) {
	b.arc.Add(c.KeyString(), has)
}
Exemple #10
0
func (m *impl) Cancel(k *cid.Cid) {
	delete(m.wantlist, k.KeyString())
	m.addEntry(k, 0, true)
}