Commit 0545c4d1 authored by Brian Tiger Chow's avatar Brian Tiger Chow Committed by Juan Batiz-Benet

refactor: *Entry -> Entry

in many places, entries are assigned from one slice to another and in
different goroutines. In one place, entries were modified (in the
queue). To avoid shared mutable state, probably best to handle entries
by value.

License: MIT
Signed-off-by: default avatarBrian Tiger Chow <brian@perfmode.com>
parent 19764880
......@@ -19,7 +19,7 @@ import (
type BitSwapMessage interface {
// Wantlist returns a slice of unique keys that represent data wanted by
// the sender.
Wantlist() []*Entry
Wantlist() []Entry
// Blocks returns a slice of unique blocks
Blocks() []*blocks.Block
......@@ -48,7 +48,7 @@ type Exportable interface {
type impl struct {
full bool
wantlist map[u.Key]*Entry
wantlist map[u.Key]Entry
blocks map[u.Key]*blocks.Block // map to detect duplicates
}
......@@ -59,7 +59,7 @@ func New() BitSwapMessage {
func newMsg() *impl {
return &impl{
blocks: make(map[u.Key]*blocks.Block),
wantlist: make(map[u.Key]*Entry),
wantlist: make(map[u.Key]Entry),
full: true,
}
}
......@@ -90,8 +90,8 @@ func (m *impl) Full() bool {
return m.full
}
func (m *impl) Wantlist() []*Entry {
var out []*Entry
func (m *impl) Wantlist() []Entry {
var out []Entry
for _, e := range m.wantlist {
out = append(out, e)
}
......@@ -120,7 +120,7 @@ func (m *impl) addEntry(k u.Key, priority int, cancel bool) {
e.Priority = priority
e.Cancel = cancel
} else {
m.wantlist[k] = &Entry{
m.wantlist[k] = Entry{
Entry: wantlist.Entry{
Key: k,
Priority: priority,
......
......@@ -13,7 +13,7 @@ type ThreadSafe struct {
// not threadsafe
type Wantlist struct {
set map[u.Key]*Entry
set map[u.Key]Entry
}
type Entry struct {
......@@ -23,7 +23,7 @@ type Entry struct {
Priority int
}
type entrySlice []*Entry
type entrySlice []Entry
func (es entrySlice) Len() int { return len(es) }
func (es entrySlice) Swap(i, j int) { es[i], es[j] = es[j], es[i] }
......@@ -37,7 +37,7 @@ func NewThreadSafe() *ThreadSafe {
func New() *Wantlist {
return &Wantlist{
set: make(map[u.Key]*Entry),
set: make(map[u.Key]Entry),
}
}
......@@ -62,13 +62,13 @@ func (w *ThreadSafe) Contains(k u.Key) bool {
return w.Wantlist.Contains(k)
}
func (w *ThreadSafe) Entries() []*Entry {
func (w *ThreadSafe) Entries() []Entry {
w.lk.RLock()
defer w.lk.RUnlock()
return w.Wantlist.Entries()
}
func (w *ThreadSafe) SortedEntries() []*Entry {
func (w *ThreadSafe) SortedEntries() []Entry {
w.lk.RLock()
defer w.lk.RUnlock()
return w.Wantlist.SortedEntries()
......@@ -78,7 +78,7 @@ func (w *Wantlist) Add(k u.Key, priority int) {
if _, ok := w.set[k]; ok {
return
}
w.set[k] = &Entry{
w.set[k] = Entry{
Key: k,
Priority: priority,
}
......@@ -93,7 +93,7 @@ func (w *Wantlist) Contains(k u.Key) bool {
return ok
}
func (w *Wantlist) Entries() []*Entry {
func (w *Wantlist) Entries() []Entry {
var es entrySlice
for _, e := range w.set {
es = append(es, e)
......@@ -101,7 +101,7 @@ func (w *Wantlist) Entries() []*Entry {
return es
}
func (w *Wantlist) SortedEntries() []*Entry {
func (w *Wantlist) SortedEntries() []Entry {
var es entrySlice
for _, e := range w.set {
es = append(es, e)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment