wantlist.go 2.6 KB
Newer Older
Jeromy's avatar
Jeromy committed
1 2
// package wantlist implements an object for bitswap that contains the keys
// that a given peer wants.
Jeromy's avatar
Jeromy committed
3 4 5 6
package wantlist

import (
	"sort"
7
	"sync"
8 9 10 11

	key "github.com/ipfs/go-ipfs/blocks/key"

	"gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
Jeromy's avatar
Jeromy committed
12 13
)

14
type ThreadSafe struct {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
15 16
	lk       sync.RWMutex
	Wantlist Wantlist
17 18 19
}

// not threadsafe
Jeromy's avatar
Jeromy committed
20
type Wantlist struct {
21
	set map[key.Key]Entry
Jeromy's avatar
Jeromy committed
22 23
}

24
type Entry struct {
25
	Key      key.Key
26
	Priority int
27 28 29 30

	Ctx    context.Context
	cancel func()
	RefCnt int
31 32
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
33
type entrySlice []Entry
34 35 36 37 38 39 40 41 42 43 44

func (es entrySlice) Len() int           { return len(es) }
func (es entrySlice) Swap(i, j int)      { es[i], es[j] = es[j], es[i] }
func (es entrySlice) Less(i, j int) bool { return es[i].Priority > es[j].Priority }

func NewThreadSafe() *ThreadSafe {
	return &ThreadSafe{
		Wantlist: *New(),
	}
}

45
func New() *Wantlist {
Jeromy's avatar
Jeromy committed
46
	return &Wantlist{
47
		set: make(map[key.Key]Entry),
Jeromy's avatar
Jeromy committed
48 49 50
	}
}

51
func (w *ThreadSafe) Add(k key.Key, priority int) {
52 53
	w.lk.Lock()
	defer w.lk.Unlock()
54
	w.Wantlist.Add(k, priority)
55 56 57
}

func (w *ThreadSafe) AddEntry(e Entry) {
58 59
	w.lk.Lock()
	defer w.lk.Unlock()
60
	w.Wantlist.AddEntry(e)
Jeromy's avatar
Jeromy committed
61 62
}

63
func (w *ThreadSafe) Remove(k key.Key) {
64 65
	w.lk.Lock()
	defer w.lk.Unlock()
66 67 68
	w.Wantlist.Remove(k)
}

69
func (w *ThreadSafe) Contains(k key.Key) (Entry, bool) {
70 71 72 73 74
	w.lk.RLock()
	defer w.lk.RUnlock()
	return w.Wantlist.Contains(k)
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
75
func (w *ThreadSafe) Entries() []Entry {
76 77
	w.lk.RLock()
	defer w.lk.RUnlock()
78
	return w.Wantlist.Entries()
79 80
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
81
func (w *ThreadSafe) SortedEntries() []Entry {
82 83
	w.lk.RLock()
	defer w.lk.RUnlock()
84
	return w.Wantlist.SortedEntries()
85 86
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
87 88 89 90 91 92 93 94 95 96
func (w *ThreadSafe) Len() int {
	w.lk.RLock()
	defer w.lk.RUnlock()
	return w.Wantlist.Len()
}

func (w *Wantlist) Len() int {
	return len(w.set)
}

97 98 99
func (w *Wantlist) Add(k key.Key, priority int) {
	if e, ok := w.set[k]; ok {
		e.RefCnt++
Jeromy's avatar
Jeromy committed
100 101
		return
	}
102 103

	ctx, cancel := context.WithCancel(context.Background())
Brian Tiger Chow's avatar
Brian Tiger Chow committed
104
	w.set[k] = Entry{
105
		Key:      k,
Jeromy's avatar
Jeromy committed
106
		Priority: priority,
107
		Ctx:      ctx,
108 109
		cancel:   cancel,
		RefCnt:   1,
110 111 112 113 114 115
	}
}

func (w *Wantlist) AddEntry(e Entry) {
	if _, ok := w.set[e.Key]; ok {
		return
Jeromy's avatar
Jeromy committed
116
	}
117
	w.set[e.Key] = e
Jeromy's avatar
Jeromy committed
118 119
}

120
func (w *Wantlist) Remove(k key.Key) {
121 122 123 124 125 126 127 128 129 130 131 132
	e, ok := w.set[k]
	if !ok {
		return
	}

	e.RefCnt--
	if e.RefCnt <= 0 {
		delete(w.set, k)
		if e.cancel != nil {
			e.cancel()
		}
	}
Jeromy's avatar
Jeromy committed
133 134
}

135
func (w *Wantlist) Contains(k key.Key) (Entry, bool) {
136 137
	e, ok := w.set[k]
	return e, ok
Jeromy's avatar
Jeromy committed
138 139
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
140
func (w *Wantlist) Entries() []Entry {
Jeromy's avatar
Jeromy committed
141 142 143 144 145 146 147
	var es entrySlice
	for _, e := range w.set {
		es = append(es, e)
	}
	return es
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
148
func (w *Wantlist) SortedEntries() []Entry {
Jeromy's avatar
Jeromy committed
149 150 151 152 153 154 155
	var es entrySlice
	for _, e := range w.set {
		es = append(es, e)
	}
	sort.Sort(es)
	return es
}